本文示範如何把 Keepalived 與 Nginx 結合,做出一組會自動偵測服務狀態並切換 VIP 的高可用反向代理。
[root@node1 keepalived]# systemctl stop keepalived.service [root@node2 keepalived]# systemctl stop keepalived.service [root@rs1 ~]# bash setka.sh stop [root@rs2 ~]# bash setka.sh stop
[root@node1 ~]# yum install nginx [root@node2 ~]# yum install nginx [root@node1 ~]# cd /etc/nginx/ [root@node1 nginx]# vim nginx.conf
在 include /etc/nginx/conf.d/*.conf; 與 server { 之間(約第 33 行後)加入:
upstream websrvs {
server 192.168.1.62:80 weight=1;
server 192.168.1.72:80 weight=1;
}
並把 location / { } 改為:
location / {
proxy_pass https://websrvs/;
}
[root@node1 nginx]# systemctl start nginx.service [root@node1 nginx]# scp nginx.conf 192.168.1.74:/etc/nginx/ [root@node2 nginx]# systemctl start nginx.service
兩個節點做相同設定。先在 vrrp_instance VI_1(約第 19 行)之前加入健康檢查腳本:
vrrp_script chk_nginx {
script "killall -0 nginx > /dev/null"
interval 1
weight -10
}
再把 track_script { chk_mt } 改成 track_script { chk_nginx },並刪除 virtual_server 段落,然後啟動:
[root@node1 keepalived]# systemctl start keepalived.service [root@node2 keepalived]# systemctl start keepalived.service
關掉 node1 的 nginx:
[root@node1 keepalived]# systemctl stop nginx
node1 日誌結果:
9月 04 08:40:32 node1 Keepalived_vrrp[49560]: VRRP_Script(chk_nginx) failed 9月 04 08:40:34 node1 Keepalived_vrrp[49560]: VRRP_Instance(VI_1) Received higher prio advert 9月 04 08:40:34 node1 Keepalived_vrrp[49560]: VRRP_Instance(VI_1) Entering BACKUP STATE 9月 04 08:40:34 node1 Keepalived_vrrp[49560]: VRRP_Instance(VI_1) removing protocol VIPs. 9月 04 08:40:34 node1 Keepalived_healthcheckers[49559]: Netlink reflector reports IP 192.168.1.80 removed
也就是說:腳本偵測失敗 → node1 由 MASTER 降為 BACKUP → VIP 被移除,切換成功。
node1 keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email { root@localhost }
notification_email_from kaadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_mcast_group4 224.0.0.18
}
vrrp_script chk_mt {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -2
}
vrrp_script chk_nginx {
script "killall -0 nginx > /dev/null"
interval 1
weight -10
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass aaf0d876679a
}
virtual_ipaddress {
192.168.1.80/16 dev eno16777736 label eno16777736:1
}
track_script { chk_nginx }
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state BACKUP
interface eno16777736
virtual_router_id 133
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 5a806846279a
}
virtual_ipaddress {
192.168.1.180/16 dev eno16777736 label eno16777736:2
}
track_script { chk_nginx }
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
notify.sh
#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
vip=192.168.1.80
contact='root@localhost'
notify() {
mailsubject="`hostname` to be $1: $vip floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
master) notify master; exit 0 ;;
backup) notify backup; exit 0 ;;
fault) notify fault; exit 0 ;;
*) echo 'Usage: `basename $0` {master|backup|fault}'; exit 1 ;;
esac
node2 keepalived.conf 與 node1 幾乎一致,差別在 VI_1 為 BACKUP/priority 99,VI_2 為 MASTER/priority 100,且 global_defs 中沒有 vrrp_mcast_group4 那行。notify.sh 內容相同。
本文出自「追夢」博客,請務必保留此出處 https://sihua.blog.51cto.com/377227/1846065