keepalived 自生存活監測
vrrp script
keepalived調用外部的輔助腳本進行資源監控,並根據監控的結果狀態能實現優先動態調整。也就是keepalived監測自身,當自身掛了之後主動讓出VIP。
需要對
vrrp_script:自定義資源監控腳本,vrrp實例根據腳本返回值進行下一步操作,腳本可被多個實例調用。
track_script:調用vrrp_script定義的腳本去監控資源,定義在實例之內,調用事先定義的vrrp_script
vrrp_script
vrrp_script <SCRIPT_NAME> { #定義 名稱
script <STRING>|<QUOTED-STRING> #定義腳本所在的位置
interval <INTEGER> #間隔多久執行一次腳本
timeout <INTEGER> #多久麼有返回值就失敗
weight <INTEGER:-254..254> #權重-254到254,如果監測失敗則當前優先權減去次權重,如果
rise <INTEGER> #服務器下線了開始監測多少測成功則上線
fall <INTEGER> #服務器連續檢測多少測都失敗,則標記爲失敗
user USERNAME [GROUPNAME] #一般爲root
init_fail #在未進行監測時,默認爲失敗。
}
keepalived+lvs實現自生存活監測
由於lvs沒有進程,所以只能使用腳本去訪問第三方的設備來探測自己是否存活,比如本機的端口,或者網關。
配置方法1
1.創建出一個ping腳本
[root@s1 ~]# vim /etc/keepalived/ping.sh
#!/bin/bash
ping -c 2 172.20.0.1 &> /dev/null
if [ $? -eq 0 ];then
exit 0
else
exit 2
fi
2.修改keepalived配置文件
vrrp_script check { #定義腳本
script /etc/keepalived/ping.sh
interval 2
weight -50
fall 3
rise 5
timeout 2
}
vrrp_instance VI_1 {
state Master
interface ens33
virtual_router_id 27
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.10
unicast_peer {
172.20.27.11
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
}
track_script { #調用腳本
check
}
}
#在另一臺主機上也執行相同的配置
3.重啓服務後查看vip
[root@s1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.10 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::20c:29ff:fec5:123c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
RX packets 540749 bytes 43766835 (41.7 MiB)
RX errors 0 dropped 12 overruns 0 frame 0
TX packets 78080 bytes 11718371 (11.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.100 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
#vip在當前的主機上
4.測試
更改ping.sh腳本中的地址到一個不存在的地址,並對keepalived日誌進行跟蹤
[root@s1 ~]# tail -f /var/log/messages
Jun 8 15:48:37 s1 Keepalived_healthcheckers[10792]: SMTP alert successfully sent.
Jun 8 15:50:05 s1 Keepalived_vrrp[10793]: /etc/keepalived/ping.sh exited due to signal 15 #腳本監測失敗
Jun 8 15:50:07 s1 Keepalived_vrrp[10793]: /etc/keepalived/ping.sh exited due to signal 15 #腳本監測失敗
Jun 8 15:50:09 s1 Keepalived_vrrp[10793]: VRRP_Script(check) timed out #連續三次次超時
Jun 8 15:50:09 s1 Keepalived_vrrp[10793]: VRRP_Instance(VI_1) Changing effective priority from 100 to 50 #優先級從100降低到50
Jun 8 15:50:09 s1 Keepalived_vrrp[10793]: /etc/keepalived/ping.sh exited due to signal 15 #腳本監測失敗
Jun 8 15:50:11 s1 Keepalived_vrrp[10793]: VRRP_Instance(VI_1) Received advert with higher priority 80, ours 50 #發現備節點的優先級比本機高,主動讓出vip
查看vip是否在s2節點上
[root@s2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.11 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::20c:29ff:fe4d:1ce3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
RX packets 535679 bytes 43641678 (41.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 36428 bytes 3457323 (3.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.100 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
#VIP在備節點上
配置方法2
在腳本中判斷lvs後端的服務器是否存在,如果存不存在,則創建一個文件
在vrrp_script中判斷文件是否存在如果文件存在,則表示自己掛了,將自己的優先級減低讓出vip
script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 7 || exit 0"
HAProxy+keepalived的檢測機制
HAProxy+keepalived的檢測方式可以使用curl HAProxy的狀態頁面,或者使用killall -0對HAProxy發送一個信號,如果進程存在則返回值爲0,如果進程不存在則返回值爲非0
配置方法1
1.創建檢測腳本
[root@s1 ~]# vim /etc/keepalived/curl.sh
#!/bin/bash
curl -I http://172.20.27.10:9000/haproxy-status &> /dev/null
if [ $? -eq 0 ];then
exit 0
else
exit 2
fi
2.修改keepalived配置文件定義vrrp_script和調用
root@s1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id s1.mylinuxops.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check { #定義腳本名
script /etc/keepalived/curl.sh #定義腳本路徑
interval 2
weight -50
fall 3
rise 5
timeout 2
}
vrrp_instance VI_1 {
state Master
interface ens33
virtual_router_id 27
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.10
unicast_peer {
172.20.27.11
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
}
track_script {
check #調用腳本
}
}
#另一臺服務器也執行相同的操作
3.重啓服務後查看vip是否存在
[root@s1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.10 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::20c:29ff:fec5:123c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
RX packets 639634 bytes 52435377 (50.0 MiB)
RX errors 0 dropped 12 overruns 0 frame 0
TX packets 103375 bytes 13944325 (13.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.100 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 4511 bytes 317479 (310.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4511 bytes 317479 (310.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4.停止haproxy並追蹤日誌
[root@s1 ~]# tail -f /var/log/messages
Jun 8 17:15:16 s1 Keepalived_vrrp[16954]: /etc/keepalived/curl.sh exited with status 2
Jun 8 17:15:18 s1 Keepalived_vrrp[16954]: /etc/keepalived/curl.sh exited with status 2
Jun 8 17:15:20 s1 Keepalived_vrrp[16954]: /etc/keepalived/curl.sh exited with status 2 #連續3次無法curl到頁面
Jun 8 17:15:20 s1 Keepalived_vrrp[16954]: VRRP_Script(check) failed
Jun 8 17:15:20 s1 Keepalived_vrrp[16954]: VRRP_Instance(VI_1) Changing effective priority from 100 to 50 #自動將優先級降低50
Jun 8 17:15:22 s1 Keepalived_vrrp[16954]: VRRP_Instance(VI_1) Received advert with higher priority 80, ours 50 #發現備的優先級比自己高
Jun 8 17:15:22 s1 Keepalived_vrrp[16954]: VRRP_Instance(VI_1) Entering BACKUP STATE #自己轉爲備
Jun 8 17:15:22 s1 Keepalived_vrrp[16954]: VRRP_Instance(VI_1) removing protocol VIPs.
Jun 8 17:15:22 s1 Keepalived_vrrp[16954]: /etc/keepalived/curl.sh exited with status 2
Jun 8 17:15:24 s1 Keepalived_vrrp[16954]: /etc/keepalived/curl.sh exited with status 2
配置方法2
使用killall -0 haproxy對進程發起信號
1.安裝killall
[root@s1 ~]# yum install psmisc -y
2.修改配置文件
[root@s1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id s1.mylinuxops.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check { #定義腳本名稱
script "killall -0 haproxy" #由於腳本中只有一條命令,直接寫在這裏就行,無需再寫腳本
interval 2
weight -50
fall 3
rise 5
timeout 2
}
vrrp_instance VI_1 {
state Master
interface ens33
virtual_router_id 27
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.10
unicast_peer {
172.20.27.11
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
}
track_script {
check #調用腳本
}
}
3.重啓服務後查看vip是否存在
[root@s1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.10 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::20c:29ff:fec5:123c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
RX packets 639634 bytes 52435377 (50.0 MiB)
RX errors 0 dropped 12 overruns 0 frame 0
TX packets 103375 bytes 13944325 (13.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.100 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 4511 bytes 317479 (310.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4511 bytes 317479 (310.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4.停止haproxy並追蹤日誌
[root@s1 ~]# tail -f /var/log/messages
Jun 8 17:30:53 s1 Keepalived_vrrp[18639]: /usr/bin/killall -0 haproxy exited with status 1
Jun 8 17:30:55 s1 Keepalived_vrrp[18639]: /usr/bin/killall -0 haproxy exited with status 1
Jun 8 17:30:57 s1 Keepalived_vrrp[18639]: /usr/bin/killall -0 haproxy exited with status 1
Jun 8 17:30:57 s1 Keepalived_vrrp[18639]: VRRP_Script(check) failed #連續監測3次失敗
Jun 8 17:30:57 s1 Keepalived_vrrp[18639]: VRRP_Instance(VI_1) Changing effective priority from 100 to 50 #優先級降低爲50
Jun 8 17:30:59 s1 Keepalived_vrrp[18639]: VRRP_Instance(VI_1) Received advert with higher priority 80, ours 50 #發現有優先級比當前高的主機
Jun 8 17:30:59 s1 Keepalived_vrrp[18639]: VRRP_Instance(VI_1) Entering BACKUP STATE #自動將爲備
Jun 8 17:30:59 s1 Keepalived_vrrp[18639]: VRRP_Instance(VI_1) removing protocol VIPs. #移除vip
Jun 8 17:30:59 s1 Keepalived_vrrp[18639]: /usr/bin/killall -0 haproxy exited with status 1
Jun 8 17:31:01 s1 Keepalived_vrrp[18639]: /usr/bin/killall -0 haproxy exited with status 1
Jun 8 17:31:03 s1 Keepalived_vrrp[18639]: /usr/bin/killall -0 haproxy exited with status 1