##主机haproxy-1(由于优先级的问题,可以看到vip1在haproxy-1上)
[root@haproxy-1 ~]# systemctl restart keepalived
[root@haproxy-1 ~]# systemctl enable keepalived
[root@haproxy-1 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2017-10-28 12:30:32 CST; 1s ago
Process: 2445 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 2446 (keepalived)
CGroup: /system.slice/keepalived.service
├─2446 /usr/sbin/keepalived -D
├─2447 /usr/sbin/keepalived -D
└─2448 /usr/sbin/keepalived -D
Oct 28 12:30:33 haproxy-1 Keepalived_vrrp[2448]: VRRP_Instance(VI_1) Changing effective priority from 150 to 152
Oct 28 12:30:33 haproxy-1 Keepalived_vrrp[2448]: VRRP_Instance(VI_2) Changing effective priority from 100 to 102
Oct 28 12:30:34 haproxy-1 Keepalived_vrrp[2448]: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 28 12:30:34 haproxy-1 Keepalived_vrrp[2448]: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 28 12:30:34 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:30:34 haproxy-1 Keepalived_vrrp[2448]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 10.0.0.100
Oct 28 12:30:34 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:30:34 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:30:34 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:30:34 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
[root@haproxy-1 ~]# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:1d:7a:63 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.11/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::8ec5:50ac:d71:20d7/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::f87c:449f:eb4a:ba03/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
##主机haproxy-2(由于优先级的问题,可以看到vip2在haproxy-2上)
[root@haproxy-2 ~]# systemctl start keepalived
[root@haproxy-2 ~]# systemctl enable keepalived
[root@haproxy-2 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2017-10-28 12:30:09 CST; 5s ago
Process: 9158 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 9159 (keepalived)
CGroup: /system.slice/keepalived.service
├─9159 /usr/sbin/keepalived -D
├─9160 /usr/sbin/keepalived -D
└─9161 /usr/sbin/keepalived -D
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: VRRP_Instance(VI_1) Changing effective priority from 100 to 102
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: VRRP_Instance(VI_2) Changing effective priority from 150 to 152
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: VRRP_Instance(VI_2) Entering MASTER STATE
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: VRRP_Instance(VI_2) setting protocol VIPs.
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.200
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: VRRP_Instance(VI_2) Sending/queueing gratuitous ARPs on ens33 for 10.0.0.200
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.200
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.200
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.200
Oct 28 12:30:10 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.200
[root@haproxy-2 ~]# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:76:bf:48 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.12/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.200/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::f87c:449f:eb4a:ba03/64 scope link
valid_lft forever preferred_lft forever
访问web服务:
[root@localhost ~]# curl 10.0.0.100
It is web1
[root@localhost ~]# curl 10.0.0.100
It is web2
[root@localhost ~]# curl 10.0.0.100
It is web1
[root@localhost ~]# curl 10.0.0.100
It is web2
[root@localhost ~]#
[root@localhost ~]# curl 10.0.0.200
It is web1
[root@localhost ~]# curl 10.0.0.200
It is web2
[root@localhost ~]# curl 10.0.0.200
It is web1
[root@localhost ~]# curl 10.0.0.200
It is web2
##在haproxy-1上关闭haproxy
[root@haproxy-1 ~]# systemctl stop haproxy
[root@haproxy-1 ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Oct 28 11:30:32 haproxy-1 haproxy-systemd-wrapper[2321]: haproxy-systemd-wrapper: exit, haproxy RC=1
Oct 28 11:30:32 haproxy-1 systemd[1]: Unit haproxy.service entered failed state.
Oct 28 11:30:32 haproxy-1 systemd[1]: haproxy.service failed.
Oct 28 12:18:47 haproxy-1 systemd[1]: Started HAProxy Load Balancer.
Oct 28 12:18:47 haproxy-1 systemd[1]: Starting HAProxy Load Balancer...
Oct 28 12:18:47 haproxy-1 haproxy-systemd-wrapper[2369]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Oct 28 12:45:34 haproxy-1 systemd[1]: Stopping HAProxy Load Balancer...
Oct 28 12:45:34 haproxy-1 haproxy-systemd-wrapper[2369]: haproxy-systemd-wrapper: SIGTERM -> 2371.
Oct 28 12:45:34 haproxy-1 systemd[1]: Stopped HAProxy Load Balancer.
Oct 28 12:45:34 haproxy-1 haproxy-systemd-wrapper[2369]: haproxy-systemd-wrapper: exit, haproxy RC=0
##查看haproxy-1的keepalive服务是否已经停掉了
[root@haproxy-1 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Oct 28 12:30:39 haproxy-1 Keepalived_vrrp[2448]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 10.0.0.100
Oct 28 12:30:39 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:30:39 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:30:39 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:30:39 haproxy-1 Keepalived_vrrp[2448]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:06 haproxy-1 Keepalived[2446]: Stopping
Oct 28 12:51:06 haproxy-1 systemd[1]: Stopping LVS and VRRP High Availability Monitor...
Oct 28 12:51:06 haproxy-1 Keepalived_vrrp[2448]: VRRP_Instance(VI_1) sent 0 priority
Oct 28 12:51:06 haproxy-1 Keepalived_vrrp[2448]: VRRP_Instance(VI_1) removing protocol VIPs.
Oct 28 12:51:07 haproxy-1 systemd[1]: Stopped LVS and VRRP High Availability Monitor.
[root@haproxy-1 ~]# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:1d:7a:63 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.11/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::8ec5:50ac:d71:20d7/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::f87c:449f:eb4a:ba03/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
##查看haproxy-2的keepalive服务状态和ip,可以看到vip1已经成功飘到haproxy-2上了
[root@haproxy-2 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2017-10-28 12:30:09 CST; 21min ago
Process: 9158 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 9159 (keepalived)
CGroup: /system.slice/keepalived.service
├─9159 /usr/sbin/keepalived -D
├─9160 /usr/sbin/keepalived -D
└─9161 /usr/sbin/keepalived -D
Oct 28 12:51:07 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:07 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:07 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:07 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:12 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:12 haproxy-2 Keepalived_vrrp[9161]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 10.0.0.100
Oct 28 12:51:12 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:12 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:12 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
Oct 28 12:51:12 haproxy-2 Keepalived_vrrp[9161]: Sending gratuitous ARP on ens33 for 10.0.0.100
[root@haproxy-2 ~]# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:76:bf:48 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.12/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.200/32 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::f87c:449f:eb4a:ba03/64 scope link
valid_lft forever preferred_lft forever
##访问web网页(可以看到web页面还是可以正常访问的)
[root@localhost ~]# curl 10.0.0.100
It is web1
[root@localhost ~]# curl 10.0.0.100
It is web2
[root@localhost ~]# curl 10.0.0.100
It is web1
[root@localhost ~]# curl 10.0.0.100
It is web2
[root@localhost ~]# curl 10.0.0.200
It is web1
[root@localhost ~]# curl 10.0.0.200
It is web2
[root@localhost ~]# curl 10.0.0.200
It is web1
[root@localhost ~]# curl 10.0.0.200
It is web2