Centos5 下构建NAT模式ipvs轮叫服务
在linux中可以做基于NAT的负载均衡,这个也可以再路由器上实现,之前有在 cisco路由器上成功实践过,不过这种负载均衡的方式似乎不太健壮,不太适合大型的器群集,下面来简单介绍下如何在centos5上配置linux 群集的轮叫,所谓轮叫也就是说器1响应第一个请求,2响应第二个,如果只有两台,那第三个请求又是服务器1来响应!从而实现比较简单的负 载均衡。
在本例中需要有一台双网卡的计算机来担任ipvs服务器,另外两台web服务器的 IP同ipvs服务器的中的一个IP在同一个网络,并将网关指向ipvs服务器。下面就来设置下ipvs服务器:
[root@centos5 ~]# ifconfig eth0 (本张网卡模拟外网卡,用来连接公网)
eth0 Link encap:Ethernet HWaddr 00:0C:29:FD:CA:A0
inet addr:192.168.0.200 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fefd:caa0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:832 errors:0 dropped:0 overruns:0 frame:0
TX packets:543 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:72365 (70.6 KiB) TX bytes:55642 (54.3 KiB)
Interrupt:185 Base address:0x1400
[root@centos5 ~]# ifconfig eth1 (本张网卡模拟内网,用来连接内部web服务器)
eth1 Link encap:Ethernet HWaddr 00:0C:29:FD:CA:AA
inet addr:192.168.1.254 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fefd:caaa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:15523 (15.1 KiB)
Interrupt:169 Base address:0x1480
[root@centos5 ~]# yum search ipvsadm (通过yum服务搜索和安装ipvs软件包)
ipvsadm.i386 : Utility to administer the Linux Virtual Server
[root@centos5 ~]# yum -y install ipvsadm
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package ipvsadm.i386 0:1.24-8.1 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
ipvsadm i386 1.24-8.1 base 31 k
Transaction Summary
=============================================================================
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 31 k
Downloading Packages:
(1/1): ipvsadm-1.24-8.1.i 100% |=========================| 31 kB 00:03
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: ipvsadm ######################### [1/1]
Installed: ipvsadm.i386 0:1.24-8.1
Complete!
[root@centos5 ~]# ipvsadm --version (查看下ipvs的版本信息)
ipvsadm v1.24 2003/06/07 (compiled with popt and IPVS v1.2.0)
[root@centos5 ~]# vi /etc/rc.d/ipvs.sh (通过编辑一个脚本来配置ipvs服务)
#!/bin/sh
echo "1" > /proc/sys/net/ipv4/ip_forward 启动本机路由功能
ipvsadm -C 清除所有ipvs规则
ipvsadm -A -t 192.168.0.200:80 -s rr 指定ipvs工作方式为轮叫,其中rr代表为轮叫
ipvsadm -a -t 192.168.0.200:80 -r 192.168.1.55:80 -m -w 1 指定web1的ip和端口
ipvsadm -a -t 192.168.0.200:80 -r 192.168.1.66:80 -m -w 1 指定web2的ip和端口
[root@centos5 ~]# chmod 755 /etc/rc.d/ipvs.sh 执行此脚本
[root@centos5 ~]# /etc/rc.d/ipvs.sh
[root@centos5 ~]# ssh (配置其他的web服务器,下面以web2为例)
The authenticity of host '192.168.1.66 (192.168.1.66)' can't be established.
RSA key fingerprint is 27:1a:b2:d8:52:30:1e:63:7a:df:9b:de:d6:92:f3:33.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.66' (RSA) to the list of known hosts.
password:
Last login: Sat Apr 25 04:07:57 2009
[root@bogon ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 (具体的IP配置信息)
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
HWADDR=00:0c:29:7a:e1:59
IPADDR=192.168.1.66
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
[root@bogon ~]# netstat -ntpl (启动apache服务)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
0 0 0.0.0.0:111 0.0.0.0:* LISTEN 2510/portmap
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2803/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2827/sendmail: acce
tcp 0 0 0.0.0.0:601 0.0.0.0:* LISTEN 2539/rpc.statd
tcp 0 0 :::80 :::* LISTEN 3261/httpd
tcp 0 0 :::22 :::* LISTEN 2788/sshd
回到IPVS服务器,确定本机并没有开启80端口
[root@centos5 ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:745 0.0.0.0:* LISTEN 1835/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1810/portmap
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2124/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2148/sendmail: acce
tcp 0 0 :::22 :::* LISTEN 2101/sshd
下面我们来测试下!
刷新下,可以看到跳转到web2了
- 最新评论