keepalived高可用nginx
node1 10.211.55.48
node2 10.211.55.49
vip 10.211.55.33
node1
1
~]# yum install -y nginx keepalived
2
~}# echo 'node1' > /usr/share/nginx/html/index.html
3
~]# systemctl start nginx
4
~]# vim /etc/keepalived/keepalived.conf
5
global_defs {
6
notification_email {
7
root@node1
8
}
9
notification_email_from keepalived@node1
10
smtp_server 127.0.0.1
11
smtp_connect_timeout 30
12
router_id node1
13
vrrp_mcast_group4 224.0.100.18
14
}
15
vrrp_script chk_down {
16
script "[ -f /etc/keepalived/down ] && exit 1 || exit 0"
17
interval 2
18
weight -10
19
20
}
21
vrrp_script chk_nginx {
22
script "killall -0 nginx"
23
interval 2
24
weight -10
25
26
}
27
28
vrrp_instance VI_1 {
29
state MASTER
30
interface eth0
31
virtual_router_id 100
32
priority 100
33
advert_int 1
34
authentication {
35
auth_type PASS
36
auth_pass 1111
37
}
38
virtual_ipaddress {
39
10.211.55.25/24 dev eth0 label eth0:0
40
}
41
track_script {
42
chk_nginx
43
44
}
45
#notify_master "notify.sh master"
46
#notify_backup "notify.sh backup"
47
#notify_default "notify.sh default"
48
}
49
~]# scp /etc/keepalived/{keepalived.conf,notify.sh} node2:/etc/keepalived/
50
~]# cat /etc/keepalived/notify.sh
51
#!/bin/bash
52
53
case $1 in
54
master)
55
echo "`hostname` change to master in `date`" | mail -s "`hostname` change master" root
56
systemctl start nginx
57
;;
58
backup)
59
echo "`hostname` change to backup in `date`" | mail -s "`hostname` change backup" root
60
systemctl restart nginx
61
;;
62
default)
63
echo "`hostname` error change to default in `date`" | mail -s "`hostname` change default" root
64
;;
65
*)
66
echo "Usage:$0 [ master | backup | default ]"
67
;;
68
esac
node2
1
~]# yum install -y nginx keepalived
2
~}# echo 'node2' > /usr/share/nginx/html/index.html
3
~]# systemctl start nginx
4
~]# vim /etc/keepalived/keepalived.conf
5
global_defs {
6
notification_email {
7
root@node2
8
}
9
notification_email_from keepalived@node2
10
smtp_server 127.0.0.1
11
smtp_connect_timeout 30
12
router_id node2
13
vrrp_mcast_group4 224.0.100.18
14
}
15
vrrp_script chk_down {
16
script "[ -f /etc/keepalived/down ] && exit 1 || exit 0"
17
interval 2
18
weight -10
19
20
}
21
vrrp_script chk_nginx {
22
script "killall -0 nginx"
23
interval 2
24
weight -10
25
26
}
27
28
vrrp_instance VI_1 {
29
state BACKUP
30
interface eth0
31
virtual_router_id 100
32
priority 98
33
advert_int 1
34
authentication {
35
auth_type PASS
36
auth_pass 1111
37
}
38
virtual_ipaddress {
39
10.211.55.25/24 dev eth0 label eth0:0
40
}
41
track_script {
42
chk_nginx
43
44
}
45
#notify_master "notify.sh master"
46
#notify_backup "notify.sh backup"
47
#notify_default "notify.sh default"
48
}
测试
node2
1
~]# systemctl start keepalived
2
~]# tail -f /var/log/messages
3
May 19 19:55:00 node2 Keepalived[2003]: Starting Keepalived v1.2.13 (03/19,2015)
4
May 19 19:55:00 node2 Keepalived[2004]: Starting Healthcheck child process, pid=2005
5
May 19 19:55:00 node2 Keepalived[2004]: Starting VRRP child process, pid=2006
6
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Netlink reflector reports IP 10.211.55.49 added
7
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Netlink reflector reports IP fdb2:2c26:f4e4:0:21c:42ff:fee9:5318 added
8
May 19 19:55:00 node2 Keepalived_healthcheckers[2005]: Netlink reflector reports IP 10.211.55.49 added
9
May 19 19:55:00 node2 Keepalived_healthcheckers[2005]: Netlink reflector reports IP fdb2:2c26:f4e4:0:21c:42ff:fee9:5318 added
10
May 19 19:55:00 node2 Keepalived_healthcheckers[2005]: Netlink reflector reports IP fe80::21c:42ff:fee9:5318 added
11
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Netlink reflector reports IP fe80::21c:42ff:fee9:5318 added
12
May 19 19:55:00 node2 Keepalived_healthcheckers[2005]: Registering Kernel netlink reflector
13
May 19 19:55:00 node2 Keepalived_healthcheckers[2005]: Registering Kernel netlink command channel
14
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Registering Kernel netlink reflector
15
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Registering Kernel netlink command channel
16
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Registering gratuitous ARP shared channel
17
May 19 19:55:00 node2 Keepalived_healthcheckers[2005]: Opening file '/etc/keepalived/keepalived.conf'.
18
May 19 19:55:00 node2 Keepalived_healthcheckers[2005]: Configuration is using : 7396 Bytes
19
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Opening file '/etc/keepalived/keepalived.conf'.
20
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Configuration is using : 65071 Bytes
21
May 19 19:55:00 node2 Keepalived_vrrp[2006]: Using LinkWatch kernel netlink reflector...
22
May 19 19:55:00 node2 Keepalived_vrrp[2006]: VRRP_Instance(VI_1) Entering BACKUP STATE
23
May 19 19:55:00 node2 Keepalived_vrrp[2006]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
24
May 19 19:55:00 node2 Keepalived_healthcheckers[2005]: Using LinkWatch kernel netlink reflector...
25
May 19 19:55:00 node2 Keepalived_vrrp[2006]: VRRP_Script(chk_nginx) succeeded
26
May 19 19:55:03 node2 Keepalived_vrrp[2006]: VRRP_Instance(VI_1) Transition to MASTER STATE
27
May 19 19:55:04 node2 Keepalived_vrrp[2006]: VRRP_Instance(VI_1) Entering MASTER STATE
28
May 19 19:55:04 node2 Keepalived_vrrp[2006]: VRRP_Instance(VI_1) setting protocol VIPs.
29
May 19 19:55:04 node2 Keepalived_vrrp[2006]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.211.55.33
30
May 19 19:55:04 node2 Keepalived_healthcheckers[2005]: Netlink reflector reports IP 10.211.55.33 added
31
May 19 19:55:09 node2 Keepalived_vrrp[2006]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.211.55.33
32
33
~]# curl 10.211.55.33
34
node2
node1
1
~]# systemctl start keepalived
2
~]# tail -f /var/log/messages
3
May 19 19:58:46 node1 Keepalived[24595]: Starting Keepalived v1.2.13 (03/19,2015)
4
May 19 19:58:46 node1 Keepalived[24596]: Starting Healthcheck child process, pid=24597
5
May 19 19:58:46 node1 Keepalived[24596]: Starting VRRP child process, pid=24598
6
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Netlink reflector reports IP 10.211.55.48 added
7
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Netlink reflector reports IP fdb2:2c26:f4e4:0:21c:42ff:fe83:186f added
8
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Netlink reflector reports IP fe80::21c:42ff:fe83:186f added
9
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Registering Kernel netlink reflector
10
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Registering Kernel netlink command channel
11
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Registering gratuitous ARP shared channel
12
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Opening file '/etc/keepalived/keepalived.conf'.
13
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Configuration is using : 65073 Bytes
14
May 19 19:58:46 node1 Keepalived_vrrp[24598]: Using LinkWatch kernel netlink reflector...
15
May 19 19:58:46 node1 Keepalived_vrrp[24598]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
16
May 19 19:58:46 node1 Keepalived_healthcheckers[24597]: Netlink reflector reports IP 10.211.55.48 added
17
May 19 19:58:46 node1 Keepalived_healthcheckers[24597]: Netlink reflector reports IP fdb2:2c26:f4e4:0:21c:42ff:fe83:186f added
18
May 19 19:58:46 node1 Keepalived_healthcheckers[24597]: Netlink reflector reports IP fe80::21c:42ff:fe83:186f added
19
May 19 19:58:46 node1 Keepalived_healthcheckers[24597]: Registering Kernel netlink reflector
20
May 19 19:58:46 node1 Keepalived_healthcheckers[24597]: Registering Kernel netlink command channel
21
May 19 19:58:46 node1 Keepalived_healthcheckers[24597]: Opening file '/etc/keepalived/keepalived.conf'.
22
May 19 19:58:46 node1 Keepalived_healthcheckers[24597]: Configuration is using : 7398 Bytes
23
May 19 19:58:46 node1 Keepalived_healthcheckers[24597]: Using LinkWatch kernel netlink reflector...
24
May 19 19:58:46 node1 Keepalived_vrrp[24598]: VRRP_Script(chk_nginx) succeeded
25
May 19 19:58:46 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Transition to MASTER STATE
26
May 19 19:58:46 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
27
May 19 19:58:47 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Entering MASTER STATE
28
May 19 19:58:47 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) setting protocol VIPs.
29
May 19 19:58:47 node1 Keepalived_healthcheckers[24597]: Netlink reflector reports IP 10.211.55.33 added
30
May 19 19:58:47 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.211.55.33
31
32
~]# curl 10.211.55.33
33
node1
我们关闭nginx,来看看服务会不会自动转移:
node1
1
~]# systemctl stop nginx
2
~]# tail -f /var/log/messages
3
May 19 20:00:50 node1 Keepalived_vrrp[24598]: VRRP_Script(chk_nginx) failed
4
May 19 20:00:51 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Received higher prio advert
5
May 19 20:00:51 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Entering BACKUP STATE
6
May 19 20:00:51 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) removing protocol VIPs.
7
May 19 20:00:51 node1 Keepalived_healthcheckers[24597]: Netlink reflector reports IP 10.211.55.33 removed
8
9
~]# curl 10.211.55.33
10
node2
node1
1
~]# systemctl start nginx
2
~]# tail -f /var/log/messages
3
May 19 20:03:52 node1 Keepalived_vrrp[24598]: VRRP_Script(chk_nginx) succeeded
4
May 19 20:03:52 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) forcing a new MASTER election
5
May 19 20:03:52 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) forcing a new MASTER election
6
May 19 20:03:53 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Transition to MASTER STATE
7
May 19 20:03:54 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Entering MASTER STATE
8
May 19 20:03:54 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) setting protocol VIPs.
9
May 19 20:03:54 node1 Keepalived_healthcheckers[24597]: Netlink reflector reports IP 10.211.55.33 added
10
May 19 20:03:54 node1 Keepalived_vrrp[24598]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.211.55.33
11
12
~]# curl 10.211.55.33
13
node1