------client----------主LVS----------从LVS------------WEB1-------------WEB2---------
2.2.2.250 2.2.2.10 2.2.2.20 2.2.2.30 2.2.2.40
lo:2.2.2.2 lo:2.2.2.2
一、配置LVS服务器(主-从两台)
1、配置IP
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@localhost ~]#service network restart
2、配置相应参数(主--从配置一样)
[root@localhost ~]#vim /etc/sysctl.conf
添加:
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
[root@localhost ~]#sysctl -p
3、安装并配置ipvsadm (主--从配置一样)
[root@localhost ~]#rpm -ihv /mnt/Packages/ipvsadm-1.26-2.el6.x86_64.rpm
[root@localhost ~]#service ipvsadm start
[root@localhost ~]#chkconfig --add ipvsadm
[root@localhost ~]#chkconfig ipvsadm on
二、配置keepalive服务器
4、安装keepalive软件(主--从配置一样)
[root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel
[root@localhost ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/keepalived-1.2.13/
[root@localhost keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64
[root@localhost keepalived-1.2.13]# make && make install
[root@localhost ~]# /etc/init.d/keepalived start
[root@localhost ~]# netstat -anp | grep keepalived
[root@localhost ~]# chkconfig --add keepalived
[root@localhost ~]# chkconfig keepalived on
5、主服务器配置
[root@localhost ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
修改:
global_defs {
router_id LVS_DEVEL_R1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
2.2.2.2
}
}
virtual_server 2.2.2.2 80 {
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 2.2.2.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 2.2.2.40 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0
6、从服务器配置
[root@localhost ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
修改:
global_defs {
router_id LVS_DEVEL_R2
}
vrrp_instance VI_1 {
state BACKUP
priority 99
......
---其他参数与主服务器保持一致----
}
说明:这里主LVS-DR-MASTER和LVS-DR-BACKUP之间的配置的差别就只有红色部分:HA的角色(MASTER,BACKUP)和优先级不同,还有router_id。
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0
三、配置WEB节点服务器
1、配置IP地址
1)设置IP
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
2)设置VIP
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo
修改:
DEVICE=lo:0
IPADDR=2.2.2.2
NETMASK=255.255.255.255
ONBOOT=yes
3)添加路由
[root@localhost ~]# route add -host 2.2.2.2 dev lo
2、调整响应参数
[root@localhost ~]# vim /etc/sysctl.conf
添加:
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost ~]# sysctl -p
3、配置http服务
WEB-1:
[root@localhost ~]# echo "welcome to 2.2.2.30 web server" > /var/www/html/index.html
[root@localhost ~]# service httpd restart
[root@localhost ~]# chkconfig --add httpd
[root@localhost ~]# chkconfig httpd on
WEB-2:
[root@localhost ~]# echo "welcome to 2.2.2.40 web server" > /var/www/html/index.html
[root@localhost ~]# service httpd restart
[root@localhost ~]# chkconfig --add httpd
[root@localhost ~]# chkconfig httpd on
五、负载均衡配置(主--从配置一样)
接下来就是配置LVS,就是在负载均衡器(2.2.2.10)上建一个虚拟ip,
然后用ipvsadm建立转发规则:
ipvsadm -Cipvsadm -A -t 2.2.2.2:80 -s rripvsadm -a -t 2.2.2.2:80 -r 2.2.2.30 -g -w 1
ipvsadm -a -t 2.2.2.2:80 -r 2.2.2.40 -g -w 1
注意:2.2.2.30和2.2.2.40分别运行了一个Web服务器,端口都是80,
在此之前一定要确保这两个Web服务器通过浏览器可以正常访问到。
如上命令执行完毕之后,可以再次运行命令:ipvsadm如果看到如下信息,则说明配置成功:IP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConnTCP eric-app-server.local:8888 rr -> ubuntu-2.local:8888 Route 1 0 0 -> ubuntu.local:8888 Route 1 0 0
三、验证:
1)使用ping命令客户端:ping -t 2.2.2.2
断开主LVS的网卡,查看客户端的ping情况。重启主LVS的网卡,查看客户端的ping情况。
2)客户端
1、访问:http://2.2.2.2/
在主LVS上查看:[root@localhost ~]# ipvsadm -Lnc
2、断开主LVS的网卡
客户端访问:http://2.2.2.2/