解决nginx负载均衡高可用keepalived只针对物理机的问题

在高可用keepalived软件,在默认的情况下仅仅在对方机器宕机或keepalived停掉的时候才会接管业务。

但是在实际工作过程中,例如在nginx负载均衡工作实例中,nginx服务已停止,而keepalived服务还在

工作的情况,这会导致用户访问VIP,而找不到对应的服务。

例如:

主机IP:10.0.0.5/24

备机IP:10.0.0.6/24

VIP:10.0.0.100/24

方法一:直接在主机上写守护脚本,来对nginx服务进行监控

1、脚本内容如下:

 1 [root@lb01 scripts]# cat check_nginx.sh 
 2 #!/bin/bash
 3 
 4 #-------------CopyRight-------------  
 5 #   Name:check nginx server  
 6 #   Version Number:1.00  
 7 #   Type:sh  
 8 #   Language:bash shell  
 9 #   Date:2018-06-020  
10 #   Author:sandy
11 #   QQ:442656067
12 #   Email:eeexu123@163.com  
13 #   Blog:https://www.cnblogs.com/eeexu123/
14 
15 while true
16 do
17     if [ `netstat -tulnp|grep nginx|wc -l` -ne 1 ];then
18         /etc/init.d/keepalived stop
19     fi
20     sleep 5
21 done

2、然后,直接运行此脚本,每五秒进行一次检测nginx服务是否在线。

1 [root@lb01 scripts]# sh check_nginx.sh &
2 [1] 15259
3 [root@lb01 scripts]# ps -ef |grep check_nginx
4 root      15259  14594  0 10:03 pts/0    00:00:00 sh check_nginx.sh
5 root      15266  14594  0 10:03 pts/0    00:00:00 grep check_nginx

3、测试

1)查看主机VIP是否在线

 1 [root@lb01 ~]# ifconfig
 2 eth0      Link encap:Ethernet  HWaddr 00:0C:29:2C:00:BB  
 3           inet addr:10.0.0.5  Bcast:10.0.0.255  Mask:255.255.255.0
 4           inet6 addr: fe80::20c:29ff:fe2c:bb/64 Scope:Link
 5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 6           RX packets:41469 errors:0 dropped:0 overruns:0 frame:0
 7           TX packets:26310 errors:0 dropped:0 overruns:0 carrier:0
 8           collisions:0 txqueuelen:1000 
 9           RX bytes:33793651 (32.2 MiB)  TX bytes:5162495 (4.9 MiB)
10 
11 eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:2C:00:BB  
12           inet addr:10.0.0.100  Bcast:0.0.0.0  Mask:255.255.255.0
13           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
14 
15 eth1      Link encap:Ethernet  HWaddr 00:0C:29:2C:00:C5  
16           inet addr:172.16.1.5  Bcast:172.16.1.255  Mask:255.255.255.0
17           inet6 addr: fe80::20c:29ff:fe2c:c5/64 Scope:Link
18           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
19           RX packets:8830 errors:0 dropped:0 overruns:0 frame:0
20           TX packets:311 errors:0 dropped:0 overruns:0 carrier:0
21           collisions:0 txqueuelen:1000 
22           RX bytes:809854 (790.8 KiB)  TX bytes:38876 (37.9 KiB)
23 
24 lo        Link encap:Local Loopback  
25           inet addr:127.0.0.1  Mask:255.0.0.0
26           inet6 addr: ::1/128 Scope:Host
27           UP LOOPBACK RUNNING  MTU:65536  Metric:1
28           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
29           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
30           collisions:0 txqueuelen:0 
31           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

2)停止nginx服务

1 [root@lb01 ~]# /application/nginx/sbin/nginx -s stop
2 [root@lb01 ~]# netstat -tulnp
3 Active Internet connections (only servers)
4 Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
5 tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1613/sshd           
6 tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1692/master         
7 tcp        0      0 :::22                       :::*                        LISTEN      1613/sshd           
8 tcp        0      0 ::1:25                      :::*                        LISTEN      1692/master   

3)查看VIP是否漂移

主机IP:

 1 [root@lb01 ~]# ifconfig
 2 eth0      Link encap:Ethernet  HWaddr 00:0C:29:2C:00:BB  
 3           inet addr:10.0.0.5  Bcast:10.0.0.255  Mask:255.255.255.0
 4           inet6 addr: fe80::20c:29ff:fe2c:bb/64 Scope:Link
 5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 6           RX packets:41555 errors:0 dropped:0 overruns:0 frame:0
 7           TX packets:26526 errors:0 dropped:0 overruns:0 carrier:0
 8           collisions:0 txqueuelen:1000 
 9           RX bytes:33803135 (32.2 MiB)  TX bytes:5182305 (4.9 MiB)
10 
11 eth1      Link encap:Ethernet  HWaddr 00:0C:29:2C:00:C5  
12           inet addr:172.16.1.5  Bcast:172.16.1.255  Mask:255.255.255.0
13           inet6 addr: fe80::20c:29ff:fe2c:c5/64 Scope:Link
14           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
15           RX packets:8854 errors:0 dropped:0 overruns:0 frame:0
16           TX packets:311 errors:0 dropped:0 overruns:0 carrier:0
17           collisions:0 txqueuelen:1000 
18           RX bytes:811478 (792.4 KiB)  TX bytes:38876 (37.9 KiB)
19 
20 lo        Link encap:Local Loopback  
21           inet addr:127.0.0.1  Mask:255.0.0.0
22           inet6 addr: ::1/128 Scope:Host
23           UP LOOPBACK RUNNING  MTU:65536  Metric:1
24           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
25           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
26           collisions:0 txqueuelen:0 
27           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

备机IP

 1 [root@lb02 ~]# ifconfig
 2 eth0      Link encap:Ethernet  HWaddr 00:0C:29:93:72:8D  
 3           inet addr:10.0.0.6  Bcast:10.0.0.255  Mask:255.255.255.0
 4           inet6 addr: fe80::20c:29ff:fe93:728d/64 Scope:Link
 5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 6           RX packets:40535 errors:0 dropped:0 overruns:0 frame:0
 7           TX packets:23552 errors:0 dropped:0 overruns:0 carrier:0
 8           collisions:0 txqueuelen:1000 
 9           RX bytes:33425626 (31.8 MiB)  TX bytes:4173830 (3.9 MiB)
10 
11 eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:93:72:8D  
12           inet addr:10.0.0.100  Bcast:0.0.0.0  Mask:255.255.255.0
13           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
14 
15 eth1      Link encap:Ethernet  HWaddr 00:0C:29:93:72:97  
16           inet addr:172.16.1.6  Bcast:172.16.1.255  Mask:255.255.255.0
17           inet6 addr: fe80::20c:29ff:fe93:7297/64 Scope:Link
18           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
19           RX packets:8608 errors:0 dropped:0 overruns:0 frame:0
20           TX packets:56 errors:0 dropped:0 overruns:0 carrier:0
21           collisions:0 txqueuelen:1000 
22           RX bytes:781127 (762.8 KiB)  TX bytes:6646 (6.4 KiB)
23 
24 lo        Link encap:Local Loopback  
25           inet addr:127.0.0.1  Mask:255.0.0.0
26           inet6 addr: ::1/128 Scope:Host
27           UP LOOPBACK RUNNING  MTU:65536  Metric:1
28           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
29           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
30           collisions:0 txqueuelen:0 
31           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

由上述可知,VIP发生漂移。keepalived触发脚本执行成功。

方法二:可以使用keepalived的配置文件来触发已写好的脚本

1、脚本内容如下:

 1 [root@lb01 scripts]# cat keep_nginx.sh 
 2 #!/bin/bash
 3 
 4 #-------------CopyRight-------------  
 5 #   Name:check nginx server  
 6 #   Version Number:1.00  
 7 #   Type:sh  
 8 #   Language:bash shell  
 9 #   Date:2018-06-020  
10 #   Author:sandy
11 #   QQ:442656067
12 #   Email:eeexu123@163.com  
13 #   Blog:https://www.cnblogs.com/eeexu123/
14 
15 if [ `netstat -tulnp|grep nginx|wc -l` -ne 1 ];then
16     /etc/init.d/keepalived stop
17 fi

2、将脚本加上可执行权限

[root@lb01 scripts]# chmod +x keep_nginx.sh 
1 [root@lb01 scripts]# ll
2 总用量 8
3 -rw-r--r--. 1 root root 401 7月   4 10:11 check_nginx.sh
4 -rwxr-xr-x. 1 root root 365 7月   4 10:11 keep_nginx.sh

3、在主机的keepalived的配置文件上,做如下修改

  /etc/keepalived/keepalived.conf配置文件如下:

  绿色部分内容是新添加触发脚本

 1 ! Configuration File for keepalived
 2 
 3 global_defs {
 4    notification_email {
 5      eeexu123@163.com
 6    }
 7    notification_email_from Alexandre.Cassen@firewall.loc
 8    smtp_server 127.0.0.1
 9    smtp_connect_timeout 30
10    router_id lb01
11 }
12 
13 vrrp_script chk_nginx_proxy {
14     script "/server/scripts/keep_nginx.sh"
15 
16     interval 2
17     weight 2
18 }
19 vrrp_instance VI_1 {
20     state MASTER
21     interface eth0
22     virtual_router_id 51
23     priority 150
24     advert_int 1
25     authentication {
26         auth_type PASS
27         auth_pass 1111
28     }
29     virtual_ipaddress {
30         10.0.0.100/24 dev eth0 label eth0:1
31     }
32     track_script {
33         chk_nginx_proxy
34     }
35 }

4、说明:

vrrp_script chk_nginx_proxy {                    #定义vrrp脚本
    script "/server/scripts/keep_nginx.sh"       #执行keep_nginx.sh脚本,当nginx服务有问题时,就停止keepalived服务

    interval 2                                   #间隔两秒检查一次
    weight 2
}

track_script {                                     #触发检查
chk_nginx_proxy
}

5、测试

1)查看主机中的VIP是否在线

 1 [root@lb01 ~]# ifconfig
 2 eth0      Link encap:Ethernet  HWaddr 00:0C:29:2C:00:BB  
 3           inet addr:10.0.0.5  Bcast:10.0.0.255  Mask:255.255.255.0
 4           inet6 addr: fe80::20c:29ff:fe2c:bb/64 Scope:Link
 5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 6           RX packets:41469 errors:0 dropped:0 overruns:0 frame:0
 7           TX packets:26310 errors:0 dropped:0 overruns:0 carrier:0
 8           collisions:0 txqueuelen:1000 
 9           RX bytes:33793651 (32.2 MiB)  TX bytes:5162495 (4.9 MiB)
10 
11 eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:2C:00:BB  
12           inet addr:10.0.0.100  Bcast:0.0.0.0  Mask:255.255.255.0
13           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
14 
15 eth1      Link encap:Ethernet  HWaddr 00:0C:29:2C:00:C5  
16           inet addr:172.16.1.5  Bcast:172.16.1.255  Mask:255.255.255.0
17           inet6 addr: fe80::20c:29ff:fe2c:c5/64 Scope:Link
18           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
19           RX packets:8830 errors:0 dropped:0 overruns:0 frame:0
20           TX packets:311 errors:0 dropped:0 overruns:0 carrier:0
21           collisions:0 txqueuelen:1000 
22           RX bytes:809854 (790.8 KiB)  TX bytes:38876 (37.9 KiB)
23 
24 lo        Link encap:Local Loopback  
25           inet addr:127.0.0.1  Mask:255.0.0.0
26           inet6 addr: ::1/128 Scope:Host
27           UP LOOPBACK RUNNING  MTU:65536  Metric:1
28           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
29           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
30           collisions:0 txqueuelen:0 
31           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

2)停止nginx服务

1 [root@lb01 ~]# /application/nginx/sbin/nginx -s stop
2 [root@lb01 ~]# netstat -tulnp
3 Active Internet connections (only servers)
4 Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
5 tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1613/sshd           
6 tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1692/master         
7 tcp        0      0 :::22                       :::*                        LISTEN      1613/sshd           
8 tcp        0      0 ::1:25                      :::*                        LISTEN      1692/master   

3)查看VIP是否漂移

主机IP:

 1 [root@lb01 ~]# ifconfig
 2 eth0      Link encap:Ethernet  HWaddr 00:0C:29:2C:00:BB  
 3           inet addr:10.0.0.5  Bcast:10.0.0.255  Mask:255.255.255.0
 4           inet6 addr: fe80::20c:29ff:fe2c:bb/64 Scope:Link
 5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 6           RX packets:41555 errors:0 dropped:0 overruns:0 frame:0
 7           TX packets:26526 errors:0 dropped:0 overruns:0 carrier:0
 8           collisions:0 txqueuelen:1000 
 9           RX bytes:33803135 (32.2 MiB)  TX bytes:5182305 (4.9 MiB)
10 
11 eth1      Link encap:Ethernet  HWaddr 00:0C:29:2C:00:C5  
12           inet addr:172.16.1.5  Bcast:172.16.1.255  Mask:255.255.255.0
13           inet6 addr: fe80::20c:29ff:fe2c:c5/64 Scope:Link
14           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
15           RX packets:8854 errors:0 dropped:0 overruns:0 frame:0
16           TX packets:311 errors:0 dropped:0 overruns:0 carrier:0
17           collisions:0 txqueuelen:1000 
18           RX bytes:811478 (792.4 KiB)  TX bytes:38876 (37.9 KiB)
19 
20 lo        Link encap:Local Loopback  
21           inet addr:127.0.0.1  Mask:255.0.0.0
22           inet6 addr: ::1/128 Scope:Host
23           UP LOOPBACK RUNNING  MTU:65536  Metric:1
24           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
25           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
26           collisions:0 txqueuelen:0 
27           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

备机IP

 1 [root@lb02 ~]# ifconfig
 2 eth0      Link encap:Ethernet  HWaddr 00:0C:29:93:72:8D  
 3           inet addr:10.0.0.6  Bcast:10.0.0.255  Mask:255.255.255.0
 4           inet6 addr: fe80::20c:29ff:fe93:728d/64 Scope:Link
 5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 6           RX packets:40535 errors:0 dropped:0 overruns:0 frame:0
 7           TX packets:23552 errors:0 dropped:0 overruns:0 carrier:0
 8           collisions:0 txqueuelen:1000 
 9           RX bytes:33425626 (31.8 MiB)  TX bytes:4173830 (3.9 MiB)
10 
11 eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:93:72:8D  
12           inet addr:10.0.0.100  Bcast:0.0.0.0  Mask:255.255.255.0
13           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
14 
15 eth1      Link encap:Ethernet  HWaddr 00:0C:29:93:72:97  
16           inet addr:172.16.1.6  Bcast:172.16.1.255  Mask:255.255.255.0
17           inet6 addr: fe80::20c:29ff:fe93:7297/64 Scope:Link
18           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
19           RX packets:8608 errors:0 dropped:0 overruns:0 frame:0
20           TX packets:56 errors:0 dropped:0 overruns:0 carrier:0
21           collisions:0 txqueuelen:1000 
22           RX bytes:781127 (762.8 KiB)  TX bytes:6646 (6.4 KiB)
23 
24 lo        Link encap:Local Loopback  
25           inet addr:127.0.0.1  Mask:255.0.0.0
26           inet6 addr: ::1/128 Scope:Host
27           UP LOOPBACK RUNNING  MTU:65536  Metric:1
28           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
29           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
30           collisions:0 txqueuelen:0 
31           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

由上述可知,VIP发生漂移。keepalived触发脚本执行成功。

posted @ 2018-07-04 11:00  启云  阅读(534)  评论(0编辑  收藏  举报