693579551 发表于 2018-12-29 06:03:06

Keepalived中Master和Backup主备切换机制浅析

  在keepalived的VRRP实例配置中会一般会设置Master和Backup来指定初始状态,但是这并不意味着此节点一直就是Master角色。控制节点角色的是Keepalived配置文件中的“priority”值和vrrp_script模块中设置的“weight”值。下面分别分情况对主备机切换机制作详细说明。
  配置简介:
主机
IP
操作系统
软件
VIP
备注
nginx01
172.27.9.91
Centos7.3.1611keepalived nginx
172.27.9.200
关闭防火墙和selinux,nginx端口为82nginx02
172.27.9.92
Centos7.3.1611keepalived nginx
172.27.9.200
关闭防火墙和selinux,nginx端口为82  初始nginx01中keepalived配置:
# more keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   acassen@firewall.loc
   failover@firewall.loc
   sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id proxy1
}
vrrp_script chk_nginx {
script "/etc/keepalived/test.sh"
interval 2      #健康检查周期
weight 20         #优先级变化幅度
fall 3             #判定服务异常的检查次数
rise 2             #判定服务正常的检查次数
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      172.27.9.200
    }
    track_script {
      chk_nginx
    }
}  初始nginx02中keepalived配置:
# view keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   acassen@firewall.loc
   failover@firewall.loc
   sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id proxy2
}
vrrp_script chk_nginx {
script "/etc/keepalived/test.sh"
interval 2
weight 20
fall 3
rise 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      172.27.9.200
    }
    track_script {
      chk_nginx
    }
}  vrrp_script中的脚本:
# more test.sh
#!/bin/bash
count=`ps -ef|grep nginx|grep -v grep|wc -l`
if [ $count -gt 0 ];then
    exit 0
else
    exit 1
fi  该脚本用于检测nginx进程是否存在。
  场景一:
主机
state指定值
  priority值
vrrp_script中的weight值
nginx状态
nginx01
MASTER
100
20
正常启动
nginx02
BACKUP
90
20
正常启动
  此种方式为keepalived常规配置方式,也是本次测试的初始状态,下面的场景都是基于该初始状态。查看ip和页面访问:
http://s1.运维网.com/images/20180412/1523516097915422.pnghttp://s1.运维网.com/images/20180412/1523516110619246.png
http://s1.运维网.com/images/20180412/1523516224230391.png
  发现vip在nginx01上,访问的页面也是nginx01。
  场景二:
主机
state指定值
priority值vrrp_script中的weight值
nginx状态
nginx01
BACKUP100
20
正常启动
nginx02
MASTER90
20
正常启动
  修改nginx两台服务器state的值,重启keepalived服务,查看vip是否切换。
http://s1.运维网.com/images/20180412/1523516536614287.png
  发现vip还是在nginx01上,页面访问也是nginx01。
  结论:keepalived主备和state值无关。
  场景三:
主机
state指定值
priority值vrrp_script中的weight值
nginx状态
nginx01
MASTER
100
20
挂起
nginx02
BACKUP
90
20
正常启动
  将初始状态nginx01上的nginx进程kill掉,查看是否发生切换。
http://s1.运维网.com/images/20180412/1523516859529571.png
http://s1.运维网.com/images/20180412/1523516889909253.png
  nginx01日志:
http://s1.运维网.com/images/20180412/1523517481934187.png
  页面访问:

http://s1.运维网.com/images/20180412/1523516979751445.png
  此时nginx01的nginx进程检验脚本检测失败,vip切换至nginx02,页面访问为nginx02。
  结论:若nginx01中的priority值小于nginx02中的priority值+vrrp_script中的weight值,则发生主备切换。
  场景四:
主机
state指定值
priority值vrrp_script中的weight值
nginx状态
nginx01
MASTER
100
5
正常启动
nginx02
BACKUP
90
20
正常启动
  将初始状态nginx01中vrrp_script中的weight值为5,重启nginx01的keepalived服务,查看是否发生切换。
http://s1.运维网.com/images/20180412/1523517956845322.png
  nginx01日志:
http://s1.运维网.com/images/20180412/1523518552427474.png
  发现vip由初始状态的nginx01上漂移至nginx02,页面访问为nginx02。
  结论:若nginx01中的priority值+vrrp_script中的weight值小于nginx02中的priority值+vrrp_script中的weight值,则发生主备切换。
  场景五:
主机
state指定值
priority值vrrp_script中的weight值
nginx状态
nginx01
MASTER
100
20
挂起
nginx02
BACKUP
90
5
正常启动
  将初始状态nginx01中nginx进程kill掉,修改nginx02的vrrp_script中的weight值为5,重启nginx02的keepalived服务,查看是否发生切换。
http://s1.运维网.com/images/20180412/1523519050325412.png

  nginx01日志:
http://s1.运维网.com/images/20180412/1523519016600249.png
  nginx02日志:
http://s1.运维网.com/images/20180412/1523519033306148.png

  此时nginx01的nginx进程检测脚本检测失败,发现vip并未发生漂移,keepalived为切换,页面访问为nginx01。
  结论:若nginx01中的priority值大于nginx02中的priority值+vrrp_script中的weight值,则不发生主备切换。
  综上所述,通过实践可以得出结论:
  1.keepalived的主备状态与state值设置无关;

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



页: [1]
查看完整版本: Keepalived中Master和Backup主备切换机制浅析