LVS实现负载均衡
ip地址规划
____________
| |
| client |
|____________|
CIP=192.168.0.253 (eth0)
|
|
VIP=192.168.194.130 (eth1)
____________
| |
| director |
|____________|
DIP=192.168.201.129 (eth0)
|
(switch)------------------------
| |
RIP=192.168.201.200 (eth1) RIP=192.168.201.201 (eth1)
_____________ _____________
| | | |
| realserver1 | | realserver2 |
|_____________| |_____________|
配置ip地址:
修改配置文件:/etc/sysconfig/network-script/下的ifcfg-eth0 或 ifcfg-eth1
director 上有两块网卡 :
/etc/sysconfig/network-script/ifcfg-eth1 修改为
DEVICE=eth1
BOOTPROTO=static
HWADDR=00:0C:29:21:6D:9F
IPADDR=192.168.194.130
NETMASK=255.255.255.0
GATEWAY=192.168.194.2
IPV6INIT=no
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
UUid="0ecd1937-cd5f-4282-b0a7-ad5cb48a4cb6"
USERCTL=no
DNS1=192.168.1.1
PEERDNS=yes
/etc/sysconfig/network-script/ifcfg-eth0 修改为
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0c:29:21:6d:95
IPADDR=192.168.201.129
NETMASK=255.255.255.0
IPV6INIT=no
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
UUid="0ecd1937-cd5f-4282-b0a7-ad5cb48a4cb6"
USERCTL=no
DNS1=192.168.1.1
PEERDNS=yes
realserver1
/etc/sysconfig/network-script/ifcfg-eth0 修改为
DEVICE="eth1"
BOOTPROTO="static"
IPADDR=192.168.201.200
NETMASK=255.255.255.0
GATEWAY=192.168.201.129
HWADDR="00:0C:29:13:5C:B0"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
UUid="a2e0838f-cd7d-45bd-92bc-9146f4f2aeb5"
网关需指定为DIP
DEVICE="eth1"
BOOTPROTO="static"
HWADDR="00:0C:29:58:D7:F3"
IPADDR=192.168.201.201
NETMASK=255.255.255.0
GATEWAY=192.168.201.129
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
UUid="a2e0838f-cd7d-45bd-92bc-9146f4f2aeb5"
配置好网络后,分别重启各个主机的网络服务
service network restart
关闭各主机的iptables服务
service iptables stop
chkconfig iptables off
在 realserver1和realserver2上安装httpd
yum -y install httpd
安装完成后再 /var/www/html路径下新建index.html文件,并将servername 写入作为负载均衡后区分是否正确分发请求。
echo realserver1 > index.html
分别启动realserver1和realserver2 上的httpd服务
service httpd start
chkconfig httpd on
分别访问两台主机的web服务,查看服务是否已经可以访问。
http://192.168.201.200
http://192.168.201.201
在director上开启路由转发功能
echo "1" > /proc/sys/net/ipv4/ip_forward
在director建立服务
ipvsadm -A -t 192.168.194.130:80 -s rr
ipvsadm -a -t 192.168.194.130:80 -r 192.168.201.200 -m -w 1
ipvsadm -a -t 192.168.194.130:80 -r 192.168.201.201 -m -w 1
通过director的VIP进行访问
http://192.168.194.130
负载均衡的落在两台realserver上
讲建立的规则保存下来
service ipvsadm save