[root@node1 ~]# /etc/init.d/keepalived start (主LVS)
Oct 15 13:59:36 vmtest Keepalived: Starting Keepalived v1.1.15 (10/15,2009)
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Using MII-BMSR NIC polling thread...
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.253 added
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.253 added
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Registering Kernel netlink reflector
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Registering Kernel netlink command channel
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Configuration is using : 11891 Bytes
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.253:80]
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.254:80]
Oct 15 13:59:36 vmtest Keepalived: Starting Healthcheck child process, pid=28419
Oct 15 13:59:36 vmtest Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Oct 15 13:59:36 vmtest Keepalived_vrrp: Netlink reflector reports IP 192.168.1.253 added
Oct 15 13:59:36 vmtest Keepalived: Starting VRRP child process, pid=28420
Oct 15 13:59:36 vmtest Keepalived_vrrp: Netlink reflector reports IP 10.0.0.253 added
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering Kernel netlink reflector
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering Kernel netlink command channel
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering gratutious ARP shared channel
Oct 15 13:59:36 vmtest Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Oct 15 13:59:36 vmtest Keepalived_vrrp: Configuration is using : 36278 Bytes
Oct 15 13:59:36 vmtest Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]
Oct 15 13:59:37 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
Oct 15 13:59:38 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.250 added
Oct 15 13:59:38 vmtest avahi-daemon[2580]: Registering new address record for 192.168.1.250 on eth0.
Oct 15 13:59:38 vmtest Keepalived_vrrp: Netlink reflector reports IP 192.168.1.250 added
Oct 15 13:59:43 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
[root@node1 ~]# /etc/init.d/keepalived start (从LVS)
Oct 15 15:40:26 node1 Keepalived: Starting Keepalived v1.1.15 (10/12,2009)
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Using MII-BMSR NIC polling thread...
Oct 15 15:40:26 node1 Keepalived: Starting Healthcheck child process, pid=11157
Oct 15 15:40:26 node1 Keepalived: Starting VRRP child process, pid=11159
Oct 15 15:40:26 node1 Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering Kernel netlink reflector
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering Kernel netlink command channel
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering gratutious ARP shared channel
Oct 15 15:40:26 node1 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Oct 15 15:40:26 node1 Keepalived_vrrp: Configuration is using : 36712 Bytes
Oct 15 15:40:26 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Oct 15 15:40:26 node1 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Registering Kernel netlink reflector
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Registering Kernel netlink command channel
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Configuration is using : 12417 Bytes
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.253:80]
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.254:80]
绿色字体表示启动成功
6、测试,我现在停掉主lvs上面的keepalived,从lvs会出现以下日志,表示从lvs能正常接管主的lvs,从而实现lvs主从互备功能,当主lvs的keepalived启动后。会自动切换回来
[root@node1 ~]# tali -f /var/log/messages
Oct 15 15:43:18 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
Oct 15 15:43:19 node1 avahi-daemon[3087]: Registering new address record for 192.168.1.250 on eth0.
Oct 15 15:43:24 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
7、测试down机,比如我这机跑的是web服务,我现在停止apache,出现以下现像
绿线画的是lvs正常,当我停止httpd的时候,再看lvs时,192.168.1.253这个IP已不在lvs表中(画红线处),因为我停掉了httpd服务。
我再启动httpd服务时,再查看lvs,192.168.1.253又自动加进表中(画蓝线处),此时已实现了负载均衡、故障剔除功能。
8、心得
这个配置只需要两个软件就可以了,ipvsadm、keepalived,再加上一个脚本,这个脚本只在
web机上执行
还有keepalived的配置文件,在一台机上配置好了之后,直接拷到另一台机去,拷到另一台
机只需要改动两个地方
vrrp_instanceVI_1{
stateMASTER ##主lvs设置成master
interfaceeth0
virtual_router_id51
priority102 ##从lvs设置成 99
stateMASTER 如果是从lvs,则改成BACKUP,priority102 改为小于100.