解决高可用服务器只针对物理服务器的问题

Posted 我的城市没有海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决高可用服务器只针对物理服务器的问题相关的知识,希望对你有一定的参考价值。

默认情况下 Keepalived软件仅仅在对方机器宕机或 Keepalived停掉的时候才会接管业务。但在实际工作中,有业务服务停止而Keepalived服务还在工作的情况,这就会导致用户访问的VIP无法找到对应的服务,那么,如何解决业务服务宕机可以将IP漂移到备节点使之接管提供服务呢?

第一个方法:可以写守护进程脚本来处理。当 nginx业务有问题时,就停掉本地的Keepalived服务,实现P漂移到对端继续提供服务。实际工作中部署及开发的示例脚本如下:

[[email protected] shell]# cat check_nginx.sh 
#!/bin/bash
#Author:Mr.Ding
#Created Time:2018-10-05 15:51:54
#Name:check_nginx.sh
#Description:
while true
do
	if [ `netstat -lntup|grep nginx|wc -l` -ne 1 ];then
		systemctl stop keepalived.service
	fi
		sleep 5
done

 此脚本的思路是若没有80端口存在,就停掉keepalived服务实现释放本地VIP。在后台执行上述脚本检查,也可加入计划任务中。

[[email protected] shell]# sh check_nginx.sh &
[1] 1428
[[email protected] shell]# ps -ef |grep check |grep -v grep
root       1428   1320  0 15:56 pts/0    00:00:00 sh check_nginx.sh

 停掉nginx服务,看IP是否发生切换

[[email protected] shell]# systemctl stop nginx.service 
[[email protected] shell]# netstat -lntup|grep nginx
[[email protected] shell]# ip add|grep 192.168.200.16

查看备节点是否已接管
[[email protected] ~]# ip add|grep 192.168.200.16
    inet 192.168.200.16/24 scope global secondary ens33

 第二个方法:可以使用keepalived的配置文件参数触发写好的服务检测脚本。

[[email protected] shell]# cat chk_nginx_proxy.sh 
#!/bin/bash
#Author:Mr.Ding
#Created Time:2018-10-05 16:09:27
#Name:chk_nginx_proxy.sh
#Description:
if [ `netstat -lntup|grep nginx|wc -l` -ne 1 ];then
	systemctl stop keepalived.service
fi
[[email protected] shell]# chmod +x chk_nginx_proxy.sh 
[[email protected] shell]# ls -l chk_nginx_proxy.sh 
-rwxr-xr-x 1 root root 192 10月  5 16:10 chk_nginx_proxy.sh

 在keepalived.conf文件中配置如下:

[[email protected] shell]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id lb01
}

vrrp_script chk_nginx_proxy {              #定义vrrp脚本,检测http端口
script "/server/scripts/shell/chk_nginx_proxy.sh"   #执行脚本,当nginx服务有问题,就停掉keepalived服务

interval 2                          #间隔2秒
weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 55
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.16/24 dev ens33 lable ens33:1
    }
	track_script {
	chk_nginx_proxy    #触发检查
	}
}

 测试接管结果

[[email protected] shell]# systemctl stop nginx
[[email protected] shell]# ip add|grep 192.168.200.16

在备服务器上查看接管结果
[[email protected] ~]# ip add|grep 192.168.200.16
    inet 192.168.200.16/24 scope global secondary ens33

 当停掉nginx的时候,keepalived会在2秒钟内自动停掉,VIP被释放,由备用端接管,这样就实现了即服务宕机也会进行IP漂移,业务切换。

 



以上是关于解决高可用服务器只针对物理服务器的问题的主要内容,如果未能解决你的问题,请参考以下文章

一文聊聊高可用的“异地多活”架构设计

运维“冷”思考:一文聊聊高可用的“异地多活”架构设计

高可用服务的报错异常处理

SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端Eureka 服务信息Eureka 发现管理Eureka 安全配置Eureka-HA(高可用) 机制Eur(代码片段

一文彻底揭秘高可用「异地多活」架构设计!

与传统的物理服务器对比,云服务器有哪些优势?