1. 程式人生 > >配置Keepalived雙實例高可用Nginx

配置Keepalived雙實例高可用Nginx

keepalived 雙實例 高可用nginx服務

我們知道Keepalived原生設計目的是為了高可用LVS集群的,但Keepalived除了可以高可用LVS服務之外,還可以基於vrrp_script和track_script高可用其它服務,如Nginx等。本篇主要演示如何使用Keepalived高可用Nginx服務(雙實例),關於vrrp_script、track_script的更多介紹可以見上一篇博客《Keepalived學習總結》。


實驗要求 ==> 實現Keepalived基於vrrp_script、track_script高可用Nginx服務,要求配置為雙實例,兩個vrrp實例的VIP分別為192.168.10.77、192.168.10.78,可以分別將其配置在網卡別名上。

實驗環境 ==> CentOS 7.x

實驗目的 ==> 實現Keepalived雙實例高可用Nginx服務

實驗主機 ==> 192.168.10.6(主機名:node1)、192.168.10.8(主機名:node2)

實驗前提 ==> 高可用對之間時間同步(可通過周期性任務來實現)

實驗操作如下。


1、編輯Keepalived配置文件

技術分享


2、在兩個節點上分別使用yum安裝Nginx,並啟動Web服務

(1) 在node1(192.168.10.6)上

[[email protected] ~]# yum -y install nginx
[[email protected] ~]# systemctl start nginx.service
[[email protected] ~]# ss -tnl | grep :80
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128         :::80                      :::*


(2) 在node2(192.168.10.8)上

[[email protected] ~]# yum -y install nginx
[[email protected] ~]# systemctl start nginx.service
[[email protected] ~]# ss -tnl | grep :80
LISTEN     0      128          *:80                       *:*


3、在兩個節點上分別啟動Keepalived服務

(1) 在node1(192.168.10.6)上

[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.6  netmask 255.255.255.0  broadcast 192.168.10.255
        ether 00:0c:29:f7:b3:4e  txqueuelen 1000  (Ethernet)
        RX packets 46064  bytes 21104876 (20.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 43548  bytes 3735943 (3.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.77  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:f7:b3:4e  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 0  (Local Loopback)
        RX packets 195  bytes 12522 (12.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 195  bytes 12522 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


(2) 在node2(192.168.10.8)上

[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# ifconfig 
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.8  netmask 255.255.255.0  broadcast 192.168.10.255
        ether 00:0c:29:ef:52:87  txqueuelen 1000  (Ethernet)
        RX packets 60646  bytes 17433464 (16.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 29539  bytes 2636829 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens34:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.78  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:ef:52:87  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 0  (Local Loopback)
        RX packets 319  bytes 20447 (19.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 319  bytes 20447 (19.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以發現,因為在第一個vrrp實例(VIP為192.168.10.77)中,node1節點為主節點,因此該VIP配置在node1上;而在第二個vrrp實例(VIP為192.168.10.78)中,node2節點為主節點,因此該VIP配置在node2上。


4、人為制造故障進行測試

(1) 在node2上殺死nginx進程,查看第二個vrrp實例的VIP(192.168.10.78)是否會漂移至node1。

[[email protected] ~]# killall nginx


#查看日誌

[[email protected] ~]# tail /var/log/messages
...(其他省略)...
Aug  8 16:29:28 node2 Keepalived_vrrp[6832]: VRRP_Script(chk_nginx) failed
Aug  8 16:29:30 node2 Keepalived_vrrp[6832]: VRRP_Instance(VI_2) Received higher prio advert
Aug  8 16:29:30 node2 Keepalived_vrrp[6832]: VRRP_Instance(VI_2) Entering BACKUP STATE
Aug  8 16:29:30 node2 Keepalived_vrrp[6832]: VRRP_Instance(VI_2) removing protocol VIPs.
Aug  8 16:29:30 node2 Keepalived_healthcheckers[6831]: Netlink reflector reports IP 192.168.10.78 removed


#查看node2上的IP地址

[[email protected] ~]# ifconfig 
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.8  netmask 255.255.255.0  broadcast 192.168.10.255
        ether 00:0c:29:ef:52:87  txqueuelen 1000  (Ethernet)
        RX packets 61483  bytes 17487867 (16.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 29994  bytes 2672261 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

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 0  (Local Loopback)
        RX packets 323  bytes 20647 (20.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 323  bytes 20647 (20.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以發現第二個vrrp實例的VIP(192.168.10.78)已經從node2上移除。


#查看node1上的IP地址

[[email protected] ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.6  netmask 255.255.255.0  broadcast 192.168.10.255
        ether 00:0c:29:f7:b3:4e  txqueuelen 1000  (Ethernet)
        RX packets 46700  bytes 21146944 (20.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 44417  bytes 3794925 (3.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.77  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:f7:b3:4e  txqueuelen 1000  (Ethernet)

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.78  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:f7:b3:4e  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 0  (Local Loopback)
        RX packets 199  bytes 12722 (12.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 199  bytes 12722 (12.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

此時node1不僅是第一個vrrp實例(VIP為192.168.10.77)的主節點,還是第二個vrrp實例(VIP為192.168.10.78)的主節點。


(2) 在node1上殺死nginx進程,在node2上重新啟動nginx進程,查看第一個vrrp實例的VIP(192.168.10.77)和第二個vrrp實例的VIP(192.168.10.78)是否會漂移至node2。

#先在node2上重啟nginx進程,此時第二個vrrp實例的VIP(192.168.10.78)應該會重新漂移至node2上

[[email protected] ~]# systemctl restart nginx.service
[[email protected] ~]# ss -tnl | grep :80
LISTEN     0      128          *:80                       *:*                  
[[email protected] ~]# ifconfig 
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.8  netmask 255.255.255.0  broadcast 192.168.10.255
        ether 00:0c:29:ef:52:87  txqueuelen 1000  (Ethernet)
        RX packets 62438  bytes 17551143 (16.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30145  bytes 2688809 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens34:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.78  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:ef:52:87  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 0  (Local Loopback)
        RX packets 331  bytes 21047 (20.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 331  bytes 21047 (20.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

果然在意料之中!


#在node1上殺死nginx進程,查看第一個vrrp實例的VIP(192.168.10.77)是否會重新漂移至node2上

[[email protected] ~]# killall nginx        # 殺死nginx進程
[[email protected] ~]# tail /var/log/messages    # 查看日誌
Aug  8 16:36:41 node1 Keepalived_vrrp[7026]: VRRP_Instance(VI_2) Sending gratuitous ARPs on ens33 for 192.168.10.78
Aug  8 16:37:10 node1 Keepalived_vrrp[7026]: VRRP_Instance(VI_2) Received higher prio advert
Aug  8 16:37:10 node1 Keepalived_vrrp[7026]: VRRP_Instance(VI_2) Entering BACKUP STATE
Aug  8 16:37:10 node1 Keepalived_vrrp[7026]: VRRP_Instance(VI_2) removing protocol VIPs.
Aug  8 16:37:10 node1 Keepalived_healthcheckers[7025]: Netlink reflector reports IP 192.168.10.78 removed
Aug  8 16:38:44 node1 Keepalived_vrrp[7026]: VRRP_Script(chk_nginx) failed
Aug  8 16:38:45 node1 Keepalived_vrrp[7026]: VRRP_Instance(VI_1) Received higher prio advert
Aug  8 16:38:45 node1 Keepalived_vrrp[7026]: VRRP_Instance(VI_1) Entering BACKUP STATE
Aug  8 16:38:45 node1 Keepalived_vrrp[7026]: VRRP_Instance(VI_1) removing protocol VIPs.
Aug  8 16:38:45 node1 Keepalived_healthcheckers[7025]: Netlink reflector reports IP 192.168.10.77 removed
[[email protected] ~]# ifconfig     # 查看IP地址
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.6  netmask 255.255.255.0  broadcast 192.168.10.255
        ether 00:0c:29:f7:b3:4e  txqueuelen 1000  (Ethernet)
        RX packets 47288  bytes 21191119 (20.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 45193  bytes 3854623 (3.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

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 0  (Local Loopback)
        RX packets 215  bytes 13522 (13.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 215  bytes 13522 (13.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

兩個vrrp實例的VIP都不見了。


#重新查看node2上的IP地址

[[email protected] ~]# ifconfig 
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.8  netmask 255.255.255.0  broadcast 192.168.10.255
        ether 00:0c:29:ef:52:87  txqueuelen 1000  (Ethernet)
        RX packets 62612  bytes 17562546 (16.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30521  bytes 2712799 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens34:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.77  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:ef:52:87  txqueuelen 1000  (Ethernet)

ens34:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.78  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:ef:52:87  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 0  (Local Loopback)
        RX packets 335  bytes 21247 (20.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 335  bytes 21247 (20.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

兩個vrrp實例的VIP都漂移到node2去了。


當然,本配置示例中自定義的vrrp_script過於簡單,此處提供一個vrrp_script監控腳本,僅供參考。

[[email protected] ~]# vim /etc/keepalived/vrrp_script.sh 
#!/bin/bash
#
server=nginx

restart_server() {
    if ! systemctl restart $server &> /dev/null; then
        sleep 1
        return 0
    else
        exit 0
    fi
}

if ! killall -0 $server &> /dev/null; then
    restart_server
    restart_server
    restart_server
    [ $? -ne 0 ] && exit 1 || exit 0
fi

在keepalived.conf配置文件中需要指明腳本路徑。

[[email protected] ~]# vim /etc/keepalived/keepalived.conf
...(其他省略)...
vrrp_script chk_nginx {
    script "/etc/keepalived/vrrp_script.sh"
    interval 2
    weight -5
}
...(其他省略)...


實驗完成!



本文出自 “Tab” 博客,請務必保留此出處http://xuweitao.blog.51cto.com/11761672/1954608

配置Keepalived雙實例高可用Nginx