在高可用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、脚本内容如下:

 [root@lb01 scripts]# cat check_nginx.sh
#!/bin/bash #-------------CopyRight-------------
# Name:check nginx server
# Version Number:1.00
# Type:sh
# Language:bash shell
# Date:--
# Author:sandy
# QQ:
# Email:eeexu123@.com
# Blog:https://www.cnblogs.com/eeexu123/ while true
do
if [ `netstat -tulnp|grep nginx|wc -l` -ne ];then
/etc/init.d/keepalived stop
fi
sleep
done

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

 [root@lb01 scripts]# sh check_nginx.sh &
[]
[root@lb01 scripts]# ps -ef |grep check_nginx
root : pts/ :: sh check_nginx.sh
root : pts/ :: grep check_nginx

3、测试

1)查看主机VIP是否在线

  [root@lb01 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr :0C::2C::BB
inet addr:10.0.0.5 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2c:bb/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (32.2 MiB) TX bytes: (4.9 MiB) eth0: Link encap:Ethernet HWaddr :0C::2C::BB
inet addr:10.0.0.100 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU: Metric: eth1 Link encap:Ethernet HWaddr :0C::2C::C5
inet addr:172.16.1.5 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2c:c5/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (790.8 KiB) TX bytes: (37.9 KiB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 b) TX bytes: (0.0 b)

2)停止nginx服务

 [root@lb01 ~]# /application/nginx/sbin/nginx -s stop
[root@lb01 ~]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0.0.0.0: 0.0.0.0:* LISTEN /sshd
tcp 127.0.0.1: 0.0.0.0:* LISTEN /master
tcp ::: :::* LISTEN /sshd
tcp ::: :::* LISTEN /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、脚本内容如下:

 [root@lb01 scripts]# cat keep_nginx.sh
#!/bin/bash #-------------CopyRight-------------
# Name:check nginx server
# Version Number:1.00
# Type:sh
# Language:bash shell
# Date:--
# Author:sandy
# QQ:
# Email:eeexu123@.com
# Blog:https://www.cnblogs.com/eeexu123/ if [ `netstat -tulnp|grep nginx|wc -l` -ne ];then
/etc/init.d/keepalived stop
fi

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

[root@lb01 scripts]# chmod +x keep_nginx.sh 
 [root@lb01 scripts]# ll
总用量
-rw-r--r--. root root 7月 : check_nginx.sh
-rwxr-xr-x. root root 7月 : keep_nginx.sh

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

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

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

 ! Configuration File for keepalived

 global_defs {
notification_email {
eeexu123@.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout
router_id lb01
} vrrp_script chk_nginx_proxy {
script "/server/scripts/keep_nginx.sh" interval
weight
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
10.0.0.100/ dev eth0 label eth0:
}
32 track_script {
chk_nginx_proxy
}
}

4、说明:

vrrp_script chk_nginx_proxy {                    #定义vrrp脚本
script "/server/scripts/keep_nginx.sh" #执行keep_nginx.sh脚本,当nginx服务有问题时,就停止keepalived服务 interval 2 #间隔两秒检查一次
weight
}

track_script {                                     #触发检查
chk_nginx_proxy
}

5、测试

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

 [root@lb01 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr :0C::2C::BB
inet addr:10.0.0.5 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2c:bb/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (32.2 MiB) TX bytes: (4.9 MiB) eth0: Link encap:Ethernet HWaddr :0C::2C::BB
inet addr:10.0.0.100 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU: Metric: eth1 Link encap:Ethernet HWaddr :0C::2C::C5
inet addr:172.16.1.5 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2c:c5/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (790.8 KiB) TX bytes: (37.9 KiB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 b) TX bytes: (0.0 b)

2)停止nginx服务

 [root@lb01 ~]# /application/nginx/sbin/nginx -s stop
[root@lb01 ~]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0.0.0.0: 0.0.0.0:* LISTEN /sshd
tcp 127.0.0.1: 0.0.0.0:* LISTEN /master
tcp ::: :::* LISTEN /sshd
tcp ::: :::* LISTEN /master

3)查看VIP是否漂移

主机IP:

 [root@lb01 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr :0C::2C::BB
inet addr:10.0.0.5 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2c:bb/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (32.2 MiB) TX bytes: (4.9 MiB) eth1 Link encap:Ethernet HWaddr :0C::2C::C5
inet addr:172.16.1.5 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2c:c5/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (792.4 KiB) TX bytes: (37.9 KiB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 b) TX bytes: (0.0 b)

备机IP

 [root@lb02 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr :0C::::8D
inet addr:10.0.0.6 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe93:728d/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (31.8 MiB) TX bytes: (3.9 MiB) eth0: Link encap:Ethernet HWaddr :0C::::8D
inet addr:10.0.0.100 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU: Metric: eth1 Link encap:Ethernet HWaddr :0C::::
inet addr:172.16.1.6 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe93:/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (762.8 KiB) TX bytes: (6.4 KiB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 b) TX bytes: (0.0 b)

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

解决nginx负载均衡高可用keepalived只针对物理机的问题的更多相关文章

  1. JAVAEE——宜立方商城03:Nginx负载均衡高可用、Keepalived+Nginx实现主备

    1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务, ...

  2. Nginx负载均衡高可用

    1.   Nginx负载均衡高可用 首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实 ...

  3. Nginx负载均衡高可用---架构

    1. Nginx负载均衡高可用 首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实现w ...

  4. nginx负载均衡高可用部署和代理配置

    nginx负载均衡高可用配置 服务器A:172.16.100.2 服务器B:172.16.100.3 首先先在两台服务器上分别安装好nginx和keepalived后,再进行下面的操作 配置keepa ...

  5. Keepalived实现Nginx负载均衡高可用

    第一章:keepalived介绍 VRRP协议 目的就是为了解决静态路由单点故障问题的 第二章: keepalived工作原理 2.1 作为系统网络服务的高可用功能(failover) keepali ...

  6. Nginx+Keepalived负载均衡高可用

    Nginx+Keepalived负载均衡高可用方案: Nginx 使用平台:unix.linux.windows. 功能: A.www web服务  http 80 b.负载均衡(方向代理proxy) ...

  7. Nginx+keepalived实现负载均衡高可用配置

    1. 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了屏蔽负载均衡服务 ...

  8. Keepalived+Nginx实现负载均衡高可用

    一.负载均衡高可用 Nginx作为负载均衡器,所有请求都到了Nginx,可见Nginx处于非常重点的位置,如果Nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了避免负载均衡服务器的宕机 ...

  9. 23.Nginx+keepalived负载均衡高可用

    Nginx+keepalived负载均衡高可用 结构图 环境: 主 服务器:192.168.239.10 备 服务器:192.168.239.20 Web 服务器1:192.168.239.40 We ...

随机推荐

  1. left outer join preserving unmatched rows from the first table

    https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj18922.html INNER JOIN operation Specifies a join ...

  2. Memory usage of a Java process java Xms Xmx Xmn

    http://www.oracle.com/technetwork/java/javase/memleaks-137499.html 3.1 Meaning of OutOfMemoryError O ...

  3. Jquery AJAX如何使用Promise/Deferred实现顺序执行?

    有的时候有我有N个AJAX请求,第下个请求可能要依赖上个请求的返回值, 可以用 $.ajax("test1.php").then(function(data) { // data ...

  4. ArcGIS服务器的feature图层限制

    今天遇到了esri.layers.FeatureLayer发布一个宗地图层,里面有些数据未显示,导致数据显示不全,原来是服务中数据返回参数限制. ArcGIS的feature图层(在JavaScrip ...

  5. html5--5-13 渐变色

    html5--5-13 渐变色 学习要点 掌握渐变色的绘制方法 渐变色绘制方法 createLinearGradient() 创建线性渐变 createLinearGradient(x1,y1,x2, ...

  6. UVA-11892(组合游戏)

    题意: 给n堆石子,每堆有ai个,每次可以取每堆中任意数目的石子;但是上一次操作的人没有将一堆全部取走,那么下一个人还要在那一堆取; 思路: 每次取到这堆就剩一个的策略; AC代码: #include ...

  7. 【servlet学习1】使用eclipse+tomcat开发servlet示例

    零.开发环境 1.eclipse+tomcat(tomcat插件已安装到eclipse中). 一.开发servlet步骤 1.在eclipse中新建工程 File —> New —> Ot ...

  8. C++之输入输出流和文件传输流

    1.流的控制 iomanip          在使用格式化I/O时应包含此头文件.    stdiostream   用于混合使用C和C + +的I/O机制时,例如想将C程序转变为C++程序 2.类 ...

  9. 用Merge存储引擎中间件实现MySQL分表

    觉得一个用Merge存储引擎中间件来实现MySQL分表的方法不错. 可以看下这个博客写的很清楚--> http://www.cnblogs.com/xbq8080/p/6628034.html ...

  10. 如何在ubuntu下使用windows下的程序(eg: .exe)

    为了在ubutu下安装百度云管家,上网查了下如何在ubuntu 下安装.exe文件,其中遇到一些问题记录如下: 使用的命令: 开始时直接使用的sudo apt-get install wine 在运行 ...