keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
基于LVS(NAT)+ Keepalived实现高性能高可用负载均衡服务器。
软件下载:
keepalived:http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
ipvsadm: http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz 注意:(for kernel 2.6.28-rc3 or later)
popt-static:http://mirror.centos.org/centos/6/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm
实验拓扑图
1、在2台Director Server上分别配置LVS+Keepalived
[root@host1 ~]# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
#看我前一篇文章http://crazylinux.blog.51cto.com/259244/811591
[root@host1 ~]# yum -y install kernel-devel make gcc openssl-devel lftp libnl* popt*
[root@host1 ~]# ln -s /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux
[root@host1 ~]# tar -zxvf ipvsadm-1.26.tar.gz -C /usr/src/
[root@host1 ~]# cd /usr/src/ipvsadm-1.26/
[root@host1 ipvsadm-1.26]# make
[root@host1 ipvsadm-1.26]# make install
[root@host1 ~]# tar -zxvf keepalived-1.2.2.tar.gz -C /usr/src/
[root@host1 ~]# cd /usr/src/keepalived-1.2.2/
[root@host1 keepalived-1.2.2]# ./configure
[root@host1 keepalived-1.2.2]# make
[root@host1 keepalived-1.2.2]# make install
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@host1 keepalived-1.2.2]# mkdir /etc/keepalived
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@host1 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
2 、开启路由转发
[root@host1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@host1 ~]# sysctl -p
3、配置Keepalived
[root@host1 ~]# less /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- router_id LVS_MASTER #备上修改LVS_BACKUP
- }
- vrrp_instance VI_1 {
- state MASTER #备上修改BACKUP
- interface eth0
- virtual_router_id 51
- priority 100 #备上修改80
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 10.0.0.10
- }
- }
- vrrp_instance LAN_GATEWAY {
- state MASTER #备上修改BACKUP
- interface eth1
- virtual_router_id 52
- priority 100 #备上修改80
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.10.10
- }
- }
- virtual_server 10.0.0.10 80 {
- delay_loop 6
- lb_algo rr
- lb_kind NAT
- # nat_mask 255.255.255.0
- # persistence_timeout 50
- protocol TCP
- real_server 192.168.10.3 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- connect_port 80
- }
- }
- real_server 192.168.10.4 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- connect_port 80
- }
- }
- }
BACKUP服务器同上配置,先安装lvs再按装keepalived,然后配置/etc/keepalived/keepalived.conf,只需将批注部分改一下即可。
4、分别在2台Real Server上面设置网关
把网关都设置成:192.168.10.10
5、在2台RealServer中分别配置HTTP
[root@host3 ~]# yum -y install httpd
[root@host3 ~]# cd /var/www/html/
[root@host3 html]# cat index.html
<h1>RealServer for 192.168.10.3</h1>
[root@host3 html]# /etc/init.d/httpd start
另一台机器配置一样,过程略、、、
6、测试:
分别启动Keepalived
[root@host1 ~]# chkconfig keepalived on
[root@host1 ~]# /etc/init.d/keepalived restart
[root@host1 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.10:80 rr
-> 192.168.10.3:80 Masq 1 0 0
-> 192.168.10.4:80 Masq 1 0 0
先关闭LVS_Master的Keepalived,观察LVS_Backup的日志
[root@host2 ~]# tailf /var/log/messages
Mar 22 22:39:16 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Transition to MASTER STATE
Mar 22 22:39:16 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Entering MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) setting protocol VIPs.
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Sending gratuitous ARPs on eth1 for 192.168.10.10
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.0.0.10
Mar 22 22:39:17 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.10 added
Mar 22 22:39:17 host2 Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.10 added
Mar 22 22:39:22 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Sending gratuitous ARPs on eth1 for 192.168.10.10
Mar 22 22:39:22 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.0.0.10
在测试机上访问:
在2台RealServer中分别配置HTTP
[root@host3 ~]# yum -y install httpd
[root@host3 ~]# cd /var/www/html/
[root@host3 html]# cat index.html
<h1>RealServer for 192.168.10.3</h1>
[root@host3 html]# /etc/init.d/httpd start
另一台机器配置一样,过程略、、、
7、测试:
分别启动Keepalived
[root@host1 ~]# chkconfig keepalived on
[root@host1 ~]# /etc/init.d/keepalived restart
[root@host1 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.10:80 rr
-> 192.168.10.3:80 Masq 1 0 0
-> 192.168.10.4:80 Masq 1 0 0
先关闭LVS_Master的Keepalived,观察LVS_Backup的日志
[root@host2 ~]# tailf /var/log/messages
Mar 22 22:39:16 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Transition to MASTER STATE
Mar 22 22:39:16 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Entering MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) setting protocol VIPs.
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Sending gratuitous ARPs on eth1 for 192.168.10.10
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 22 22:39:17 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.0.0.10
Mar 22 22:39:17 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.10 added
Mar 22 22:39:17 host2 Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.10 added
Mar 22 22:39:22 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Sending gratuitous ARPs on eth1 for 192.168.10.10
Mar 22 22:39:22 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.0.0.10
在测试机上访问:
启动LVS_Master,在查看LVS_Backup日志
[root@host2 ~]# tailf /var/log/messages
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Received higher prio advert
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) Entering BACKUP STATE
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(LAN_GATEWAY) removing protocol VIPs.
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Mar 22 22:51:50 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.10 removed
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 22 22:51:50 host2 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 22 22:51:50 host2 Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.10 removed
一切访问正常~!