使用KeepAlived实现主备服务

使用KeepAlived实现主备服务

使用两台机器 + 一个虚拟IP

两台机器上分别启动着自己的HTTP服务,端口8999。

安装KeepAlived

在两台机器上安装keepalived

yum install keepalived -y

配置keepAlived

在master那台,配置文件位于/etc/keepalived/keepalived.conf

写入内容

global_defs {
   router_id ljktest 
}

vrrp_script chk_http_port {
        script "

在Backup那台写入

global_defs {
   router_id ljktest 
}

vrrp_script chk_http_port {
        script "

两台的配置,只有priority的值不一样。且要配置成MASTER的那台必须大于BACKUP那台。

启动keepAlived

systemctl start keepalived

在Master那台能够看到,网卡上面多了一条这样的信息。证明生效了。访问服务192.168.0.131:8999下的服务,会定向到Master那台上面。

inet 192.168.0.131/32 scope global ens32
valid_lft forever preferred_lft forever

测试

关闭KeepAlived,主备切换

关闭Master这台的keepalived,发现BACKUP这台变成主。

重启Master这台的keepalived,发现Master这台又会成为主。

关闭应用程序,主备切换

目前这样的配置,关闭应用程序无法进行主备切换。

不明白为什么 不会生效。

自己写个脚本来检测试试

xypt_logs_check.sh

#!/bin/bash
count=`ps -ef|grep xypt_logs_producer|grep -v grep|wc -l`
if [ $count -gt 0 ];then
    exit 0
else
    exit 1
fi

脚本作用就是用来查看下这个应用进程是否存在,不存在异常退出,存在则正常退出。

配置脚本部分修改成

vrrp_script chk_http_port {
        script "/etc/keepalived/xypt_logs_check.sh"
        interval 2
        weight 2
        fall 3
        rise 2
}

接下来我们关闭Master这台上的应用,VIP地址会漂移到Backup那台。再启动回来,VIP地址又会再漂移到Master这台。

查看Master这台/var/log/messages日志,可以看到它的迁移过程。

Aug  7 12:57:17 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1
Aug  7 12:57:19 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1
Aug  7 12:57:21 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1
Aug  7 12:57:21 nn1 Keepalived_vrrp[4088]: VRRP_Script(chk_http_port) failed
Aug  7 12:57:22 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Changing effective priority from 102 to 100
Aug  7 12:57:23 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Received advert with higher priority 101, ours 100
Aug  7 12:57:23 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Entering BACKUP STATE
Aug  7 12:57:23 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) removing protocol VIPs.


Aug  7 12:57:49 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1
Aug  7 12:57:51 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1
Aug  7 12:57:55 nn1 Keepalived_vrrp[4088]: VRRP_Script(chk_http_port) succeeded
Aug  7 12:57:56 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Changing effective priority from 100 to 102
Aug  7 12:57:57 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) forcing a new MASTER election
Aug  7 12:57:58 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Transition to MASTER STATE
Aug  7 12:57:59 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Entering MASTER STATE
Aug  7 12:57:59 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) setting protocol VIPs.
Aug  7 12:57:59 nn1 Keepalived_vrrp[4088]: Sending gratuitous ARP on ens32 for 192.168.0.131
Aug  7 12:57:59 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens32 for 192.168.0.131
Aug  7 12:57:59 nn1 Keepalived_vrrp[4088]: Sending gratuitous ARP on ens32 for 192.168.0.131

总结

  1. keepalived的主备状态与state值设置无关;
  2. 主备机由priority值和vrrp_script中的weight值之和决定,大的为主;
  3. 主备比较权值=priority值+weight值*标志位,当vrrp_script检测脚本为true时标志位为1,反之为0;
  4. 为保证正常的主备切换,weight值应大于主备priority值之差。

附录

你可能感兴趣的:(keepalived)