Linux,  NGINX

如何通过keepalived来配置NGINX高可用

0 背景和步骤说明

NGINX作为反向代理服务器,通常用于应用系统的访问入口地址。如果NGINX出现问题的话,那么相当于无法访问应用系统了。因此,我们得保证NGINX始终是可用的,尽管NGINX自身已经非常稳定。

实验环境:2台Linux服务器上分别运行着NGINX,NGINX的配置也相同,即:我们通过任意一台机器的IP和端口(其实是NGINX的反向代理)都可以正常访问到后端的应用系统。问题是,我们不可能在其它应用系统里,同时配置成通过访问这2台机器的IP来访问应用。于是,我们引入一个虚拟IP|浮动IP,正常情况下,该VIP运行在其中一台机器上,我们就可以通过这个虚拟|浮动IP来访问NGINX服务。一旦,该机器出现故障,或者拥有VIP机器上的NGINX异常,失去响应了,那么该VIP会自动切换到另外一台正常的NGINX机器上。对于应用来讲,始终是通过VIP来访问的NGINX服务,而且受到的影响也只是在VIP切换到新服务器上的很短一段儿时间。

VIP的漂移和接管通过keepalived软件来实现。

首先,在两台CentOS 7服务器10.0.9.216和10.0.9.217机器上通过编译源码来安装配置NGINX 1.24;

然后,再在两台机器上安装配置keepalived软件;

接着,通过keepalived软件和虚拟IP 10.0.9.218来实现NGINX的高可用。

最后,模拟当前VIP所在机器上的NGINX出现故障,来实现VIP被keepalived接管并自动漂移到新服务器,我们通过该VIP依然可以正常访问后端应用的场景。

1 在2台机器上编译源码来安装NGINX1.24

参考:如何在CentOS 7上通过编译源码来安装配置NGINX

2 修改NGINX默认页

216机器:

[root@primary1 conf]#  echo "<h1>This is NGINX Web Server from Node 216</h1>" | sudo tee /etc/nginx/html/index.html
<h1>This is NGINX Web Server from Node 216</h1>
[root@primary1 conf]# 

217机器:

[root@primary2 nginx]# echo "<h1>This is NGINX Web Server from Node 217</h1>" | sudo tee /etc/nginx/html/index.html
<h1>This is NGINX Web Server from Node 217</h1>
[root@primary2 nginx]# 

3 分别重新加载NGINX服务并验证

分别在2台服务器上执行/etc/nginx/sbin/nginx -s reload来热加载NGINX配置,然后在client端上,通过执行curl http://10.0.9.216和curl http://10.0.9.217来验证NGINX服务是正常的。

#/etc/nginx/sbin/nginx -s reload
​
$ curl http://10.0.9.216
<h1>This is NGINX Web Server from Node 216</h1>
​
asher at MacBook-Air-3 in ~
$ curl http://10.0.9.217
<h1>This is NGINX Web Server from Node 217</h1>
​
asher at MacBook-Air-3 in ~
$ 

4 2台机器分别安装keepalived

10.0.9.216机器:

[root@primary1 ~]# yum install keepalived
Loaded plugins: fastestmirror, langpacks, product-id, search-disabled-repos, subscription-manager
​
This system is not registered with an entitlement server. You can use subscription-manager to register.
​
base                                                                                                                                                                                        | 3.6 kB  00:00:00     
extras                                                                                                                                                                                      | 2.9 kB  00:00:00     
updates                                                                                                                                                                                     | 2.9 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                                           | 250 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                                                                          |  22 MB  00:00:19     
Determining fastest mirrors
 * base: ftp.sjtu.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 0:1.3.5-19.el7 will be installed
--> Finished Dependency Resolution
​
Dependencies Resolved
​
===================================================================================================================================================================================================================
 Package                                              Arch                                             Version                                                Repository                                      Size
===================================================================================================================================================================================================================
Installing:
 keepalived                                           x86_64                                           1.3.5-19.el7                                           base                                           332 k
​
Transaction Summary
===================================================================================================================================================================================================================
Install  1 Package
​
Total download size: 332 k
Installed size: 1.0 M
Is this ok [y/d/N]: y
Downloading packages:
keepalived-1.3.5-19.el7.x86_64.rpm                                                                                                                                                          | 332 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 1 pre-existing rpmdb problem(s), 'yum check' output follows:
libkkc-0.3.1-9.el7.x86_64 has missing requires of libmarisa.so.0()(64bit)
  Installing : keepalived-1.3.5-19.el7.x86_64                                                                                                                                                                  1/1 
  Verifying  : keepalived-1.3.5-19.el7.x86_64                                                                                                                                                                  1/1 
​
Installed:
  keepalived.x86_64 0:1.3.5-19.el7                                                                                                                                                                                 
​
Complete!
[root@primary1 ~]# 

10.0.9.217机器:

[root@primary2 ~]# yum install keepalived
Loaded plugins: fastestmirror, langpacks
base                                                                                                                                                                                        | 3.6 kB  00:00:00     
extras                                                                                                                                                                                      | 2.9 kB  00:00:00     
updates                                                                                                                                                                                     | 2.9 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                                           | 250 kB  00:00:01     
(2/2): updates/7/x86_64/primary_db                                                                                                                                                          |  22 MB  00:00:18     
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 0:1.3.5-19.el7 will be installed
--> Finished Dependency Resolution
​
Dependencies Resolved
​
===================================================================================================================================================================================================================
 Package                                              Arch                                             Version                                                Repository                                      Size
===================================================================================================================================================================================================================
Installing:
 keepalived                                           x86_64                                           1.3.5-19.el7                                           base                                           332 k
​
Transaction Summary
===================================================================================================================================================================================================================
Install  1 Package
​
Total download size: 332 k
Installed size: 1.0 M
Is this ok [y/d/N]: y
Downloading packages:
keepalived-1.3.5-19.el7.x86_64.rpm                                                                                                                                                          | 332 kB  00:00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : keepalived-1.3.5-19.el7.x86_64                                                                                                                                                                  1/1 
  Verifying  : keepalived-1.3.5-19.el7.x86_64                                                                                                                                                                  1/1 
​
Installed:
  keepalived.x86_64 0:1.3.5-19.el7                                                                                                                                                                                 
​
Complete!
[root@primary2 ~]# 

5 10.0.9.216机器(master)配置keepalived

#先备份/etc/keepalived/keepalived.conf文件
[root@primary1 keepalived]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.raw
​
#修改/etc/keepalived/keepalived.conf文件为下述内容:
cat <<EOF> /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
 script "/etc/nginx/check_nginx.sh"
 fall 2                           # failover after 2 times the nginx reports a non-zero exit code
 interval 2                       # every 2 seconds
 weight 2
 }
​
 vrrp_instance VI_1 {
 interface ens192                 # the network interface to which the virtual address is assigned
 state MASTER                     # MASTER for master server
 virtual_router_id 50
 unicast_src_ip 10.0.9.216        # IP address of Master Server
 unicast_peer {
 10.0.9.217                       # IP address of Slave Server
 }
 priority 102                     # master server should always have a higher priority number than the slave
 track_script {
      chk_nginx
 }
 virtual_ipaddress {
 10.0.9.218/24 dev ens192         # Virtual IP address
 }             
 }
 EOF

对于该配置文件的说明:

通过/etc/nginx/check_nginx.sh来校验NGINX是否健康,间隔2秒执行1次该脚本,若连续2次发现脚本的执行结果都是非0,则认为NGINX服务已经出现问题,会把虚拟IP地址漂移到另外一个节点;

当前节点配置为MASTER节点,这里只是个逻辑概念,你也可以名为为A,B等任何其它字符串来取代MASTER;

虚拟IP地址配置在当前机器的名为ens192的网卡上,即正常情况下该网卡上,会看到2个IP地址:本机IP地址10.0.9.216和虚拟IP 10.0.9.218;

virtual_router_id 50;非必须配置项,可以不配置该项;

unicast_src_ip:当前节点的IP地址;

unicast_peer:备用节点IP地址;

priority: 102,该值一定要配置为备用节点的值大,意味着,总是倾向于把虚拟IP地址漂移到该节点上;

6 10.0.9.216机器(master)编辑/etc/nginx/check_nginx.sh检查文件

该文件用于校验 NGINX服务进程是否存在,如果不存在,则该脚本返回值是1。

[root@primary1 keepalived]# cat /etc/nginx/check_nginx.sh 
#!/bin/bash
if [ -z "`pidof nginx`" ]; then
  exit 1
fi
[root@primary1 keepalived]# 

然后,给脚本加上执行权限。

[root@primary1 keepalived]# chmod 755 /etc/nginx/check_nginx.sh 
[root@primary1 keepalived]# 

7 10.0.9.217机器(slave)上配置

将步骤5,6中的配置文件scp到10.0.9.217 slave机器上,然后修改/etc/keepalived/keepalived.conf的内容如下:

[root@primary2 ~]# cat /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
 script "/etc/nginx/check_nginx.sh"
 fall 2                           # failover after 2 times the nginx reports a non-zero exit code
 interval 2                       # every 2 seconds
 weight 2
 }
​
 vrrp_instance VI_1 {
 interface ens192                 # the network interface to which the virtual address is assigned
 state SLAVE                      # MASTER for master server
 virtual_router_id 50
 unicast_src_ip 10.0.9.217        # IP address of Master Server
 unicast_peer {
 10.0.9.216                       # IP address of Slave Server
 }
 priority 101                     # master server should always have a higher priority number than the slave
 track_script {
      chk_nginx
 }
 virtual_ipaddress {
 10.0.9.218/24 dev ens192         # Virtual IP address
 }             
 }
[root@primary2 ~]# 

注意📢:此时,配置文件里的IP地址正好和10.0.9.216机器里的颠倒过来;另外,此时的priority 101降为101了。其它保持不变。

/etc/nginx/check_nginx.sh脚本文件内容和权限也保持不变。

8 测试验证

8.1NGINX配合keepalived正常工作

10.0.9.216机器:可以看到该机器上同时运行这NGINX和keepalived进程,同时,虚拟IP 10.0.9.218驻留在该机器上。

[root@primary1 ~]# ps -ef|grep nginx
root      1380     1  0 Jul26 ?        00:00:00 nginx: master process /etc/nginx/sbin/nginx
root      1381  1380  0 Jul26 ?        00:00:00 nginx: worker process
root      1382  1380  0 Jul26 ?        00:00:00 nginx: worker process
root      1384  1380  0 Jul26 ?        00:00:00 nginx: worker process
root      1385  1380  0 Jul26 ?        00:00:00 nginx: worker process
root     29012 26557  0 16:56 pts/0    00:00:00 grep --color=auto nginx
[root@primary1 ~]# ps -ef|grep keepalived
root      4794     1  0 16:43 ?        00:00:00 /usr/sbin/keepalived -D
root      4795  4794  0 16:43 ?        00:00:00 /usr/sbin/keepalived -D
root      4796  4794  0 16:43 ?        00:00:00 /usr/sbin/keepalived -D
root     29122 26557  0 16:56 pts/0    00:00:00 grep --color=auto keepalived
[root@primary1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:06:8c:0d brd ff:ff:ff:ff:ff:ff
    inet 10.0.9.216/24 brd 10.0.9.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet 10.0.9.218/24 scope global secondary ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::9af1:b03:b50a:a8f7/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:8b:01:c8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:8b:01:c8 brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ea:23:61:36 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:eaff:fe23:6136/64 scope link 
       valid_lft forever preferred_lft forever
7: veth263c38e@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether d2:da:fa:15:3e:a2 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::d0da:faff:fe15:3ea2/64 scope link 
       valid_lft forever preferred_lft forever
[root@primary1 ~]# 

10.0.9.217机器:可以看到该机器上同时运行这NGINX和keepalived进程。但是,虚拟IP 10.0.9.218并不在该机器上。

[root@primary2 ~]# ps -ef|grep nginx
root      1879     1  0 16:45 ?        00:00:00 nginx: master process /etc/nginx/sbin/nginx
nobody    1881  1879  0 16:45 ?        00:00:00 nginx: worker process
root      2362  1748  0 17:02 pts/0    00:00:00 grep --color=auto nginx
[root@primary2 ~]# ps -ef|grep keepalived
root      2174     1  0 17:02 ?        00:00:00 /usr/sbin/keepalived -D
root      2175  2174  0 17:02 ?        00:00:00 /usr/sbin/keepalived -D
root      2176  2174  0 17:02 ?        00:00:00 /usr/sbin/keepalived -D
root      2476  1748  0 17:02 pts/0    00:00:00 grep --color=auto keepalived
[root@primary2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5b:c8:a5 brd ff:ff:ff:ff:ff:ff
    inet 10.0.9.217/24 brd 10.0.9.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::47b7:291b:d1ed:a1c3/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:f7:97:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:f7:97:a0 brd ff:ff:ff:ff:ff:ff
[root@primary2 ~]# 

客户端执行验证:

$ curl http://10.0.9.216
<h1>This is NGINX Web Server from Node 1</h1>
​
asher at MacBook-Air-3 in ~
$ curl http://10.0.9.217
<h1>This is NGINX Web Server from Node 2</h1>
​
asher at MacBook-Air-3 in ~
$ curl http://10.0.9.218
<h1>This is NGINX Web Server from Node 1</h1>
​
asher at MacBook-Air-3 in ~
$ 

在客户端上分别执行curl http://10.0.9.216、curl http://10.0.9.217和curl http://10.0.9.218。从返回结果可以验证,VIP驻留在10.0.9.216机器上。

8.2 手工停止10.0.9.216机器上的NGINX

我们通过手工停止10.0.9.216机器上的NGINX来模拟故障,用于验证keepalived是否自动接管VIP 10.0.9.218,并把该VIP自动漂移到10.0.9.217机器上。

10.0.9.216机器执行:
#先停止10.0.9.216机器上的NGINX
[root@primary1 ~]# systemctl stop nginx
[root@primary1 ~]# 
#查看NGINX进程已不在,keepalived进程依然存在
[root@primary1 ~]# ps -ef|grep nginx
root     13102 26557  0 17:09 pts/0    00:00:00 grep --color=auto nginx
[root@primary1 ~]# ps -ef|grep keepalived
root      4794     1  0 16:43 ?        00:00:00 /usr/sbin/keepalived -D
root      4795  4794  0 16:43 ?        00:00:00 /usr/sbin/keepalived -D
root      4796  4794  0 16:43 ?        00:00:00 /usr/sbin/keepalived -D
root     13159 26557  0 17:09 pts/0    00:00:00 grep --color=auto keepalived
[root@primary1 ~]# 
#查看IP地址,已经没有了VIP 10.0.9.218
[root@primary1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:06:8c:0d brd ff:ff:ff:ff:ff:ff
    inet 10.0.9.216/24 brd 10.0.9.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::9af1:b03:b50a:a8f7/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:8b:01:c8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:8b:01:c8 brd ff:ff:ff:ff:ff:ff
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:ea:23:61:36 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:eaff:fe23:6136/64 scope link 
       valid_lft forever preferred_lft forever
[root@primary1 ~]# 
10.0.9.217机器执行:
[root@primary2 ~]# ps -ef|grep nginx
root      1879     1  0 Aug01 ?        00:00:00 nginx: master process /etc/nginx/sbin/nginx
nobody    1881  1879  0 Aug01 ?        00:00:00 nginx: worker process
root     18379 18032  0 07:39 pts/0    00:00:00 grep --color=auto nginx
[root@primary2 ~]# ps -ef|grep keepalived
root      2174     1  0 Aug01 ?        00:00:02 /usr/sbin/keepalived -D
root      2175  2174  0 Aug01 ?        00:00:02 /usr/sbin/keepalived -D
root      2176  2174  0 Aug01 ?        00:00:25 /usr/sbin/keepalived -D
root     18423 18032  0 07:39 pts/0    00:00:00 grep --color=auto keepalived
[root@primary2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5b:c8:a5 brd ff:ff:ff:ff:ff:ff
    inet 10.0.9.217/24 brd 10.0.9.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet 10.0.9.218/24 scope global secondary ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::47b7:291b:d1ed:a1c3/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:f7:97:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:f7:97:a0 brd ff:ff:ff:ff:ff:ff
[root@primary2 ~]# 

从上看到,VIP已经从10.0.9.216机器自动漂移到10.0.9.217机器上了。

客户端重新执行curl 请求:
$ curl http://10.0.9.216
curl: (7) Failed to connect to 10.0.9.216 port 80 after 11 ms: Couldn't connect to server
​
asher at MacBook-Air-3 in ~
$ curl http://10.0.9.217
<h1>This is NGINX Web Server from Node 2</h1>
​
asher at MacBook-Air-3 in ~
$ curl http://10.0.9.218
<h1>This is NGINX Web Server from Node 2</h1>
​
asher at MacBook-Air-3 in ~
$ 

正如我们所预期的那样,curl http://10.0.9.216无法访问,因为NGINX已经出现故障。反而curl http://10.0.9.217 和curl http://10.0.9.218 请求返回结果相同,说明访问的是同一个NGINX。

9 补充说明和注意事项

当我们修复|启动10.0.9.216机器上的NGINX之后,VIP会自动被keepalived接管,并且重新漂移到10.0.9.216机器上。

10.0.9.216机器:
[root@primary1 ~]# systemctl start nginx.service 
[root@primary1 ~]# ps -ef|grep nginx
root     32691     1  0 07:57 ?        00:00:00 nginx: master process /etc/nginx/sbin/nginx
root     32692 32691  0 07:57 ?        00:00:00 nginx: worker process
root     32693 32691  0 07:57 ?        00:00:00 nginx: worker process
root     32694 32691  0 07:57 ?        00:00:00 nginx: worker process
root     32695 32691  0 07:57 ?        00:00:00 nginx: worker process
root     32726 32588  0 07:57 pts/0    00:00:00 grep --color=auto nginx
[root@primary1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:06:8c:0d brd ff:ff:ff:ff:ff:ff
    inet 10.0.9.216/24 brd 10.0.9.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet 10.0.9.218/24 scope global secondary ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::9af1:b03:b50a:a8f7/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:8b:01:c8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:8b:01:c8 brd ff:ff:ff:ff:ff:ff
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:ea:23:61:36 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:eaff:fe23:6136/64 scope link 
       valid_lft forever preferred_lft forever
[root@primary1 ~]# 
客户端请求:
$ curl http://10.0.9.218
<h1>This is NGINX Web Server from Node 1</h1>
​
asher at MacBook-Air-3 in ~
$ curl http://10.0.9.217
<h1>This is NGINX Web Server from Node 2</h1>
​
asher at MacBook-Air-3 in ~
$ curl http://10.0.9.216
<h1>This is NGINX Web Server from Node 1</h1>
​
asher at MacBook-Air-3 in ~
$ 
注意事项:

如果机器上有通过docker方式来启动并且运行有其它NGINX容器的话,/etc/nginx/check_nginx.sh 脚本检查NGINX进程时,会出现问题,即使主机上的NGINX被停掉,该脚本依然会认为NGINX进程还在,它会把container中的NGINX统计在内。

10 参考链接:

https://www.cyberciti.biz/faq/bash-check-if-process-is-running-or-notonlinuxunix/

https://www.chakray.com/nginx-high-availability-through-keepalived-virtual-ip/

留言