keepalived部署说明

2020-07-16  本文已影响0人  CaiGuangyin

流程规范

  1. route_id分配原则
    网络分配
    vip 10.XXX.XXX.2-10.XXX.XXX.9 对应route_id 132-139
    vip 10.XXX.XXX.90-10.XXX.XXX.99 对应route_id 140-149
  2. 密码要求
    每套集群独立配置一套密码,避免配置重复导致脑裂

部署流程

1. 安装keepalived

yum install -y keepalived

2. 配置文件

配置文件参考:
下载地址

链接:https://pan.baidu.com/s/1QyfUsGrMbUhsa3W0VOLzLg 
提取码:wpia
check.sh_example          检查脚本样例
KaToding                  报警样例
keepalived.conf_example   配置文件样例

替换变量:

{{ vip }}             网络组分配vip
{{ route_id }}        参见route_id分配原则
{{ devname }}         默认为bond0

主配置文件
/etc/keepalived/keepalived.conf 需要替换{{ vip }}、{{ route_id }}、{{ devname }}
配置参考:

global_defs {
  # Keepalived process identifier
  #lvs_id proxy_HA
}
# Script used to check if Proxy is running
vrrp_script check_proxy {
  script "/etc/keepalived/check.sh"
  interval 1
  weight 2
}
# Virtual interface
# The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_{{ route_id }} {
  state BACKUP
  nopreempt     #作为master开启,slave注释掉本行
  interface {{ devname }}
  virtual_router_id {{ route_id }}
  priority 100
  authentication {
      auth_type PASS
      auth_pass XXXXXXXXX      # Set this to some secret phrase
  }
  # The virtual ip address shared between the two loadbalancers
  virtual_ipaddress {
    {{ vip }} dev {{ devname }}
  }
  track_script {
    check_proxy
  }
notify_master /etc/keepalived/KaToding/notify_master.py
notify_fault /etc/keepalived/KaToding/notify_fault.py
notify_stop /etc/keepalived/KaToding/notify_stop.py
}

报警配置
/etc/keepalived/KaToding/func/config 仅需替换{{ vip }}

[info]
vip = {{ vip }}
chatid = chata0146991ba090ff538bde22c56b71ad1

健康检查脚本

#!/bin/bash
exit 0
USERNAME=check_mysql_health
PASSWORD=1Z8CL7e6UyVLFvW6
SOCKET=/var/run/mysqld/mysqld.sock
MYSQL_CMD="/usr/local/mysql/bin/mysql --connect-timeout=3  -S $SOCKET -u${USERNAME} -p${PASSWORD}  "
declare rs=0
$MYSQL_CMD -Ns -e "select 1"
if [ $? -eq 0 ];then
  echo OK,rs=$?
  exit 14
else
  declare -i num=0
  while [ $num -lt 4 ];do
    $MYSQL_CMD -Ns -e "select 1"
    if [ $? -gt 0 ];then
     ((rs++))
    fi
    ((num++));
  done

  if [ $rs -ge 3 ];then
     service keepalived stop
  fi
fi

3. 启动keepalived

systemctl  enable keepalived
systemctl  start  keepalived

4. 漂移测试

systemctl  restart  keepalived

附录:

双slave配置参考

global_defs {
  # Keepalived process identifier
  #lvs_id proxy_HA
}
# Script used to check if Proxy is running
vrrp_script check_proxy {
  script "/etc/keepalived/check.sh"
  interval 1
  weight 2
}
# Virtual interface
# The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_135 {
  state BACKUP
  interface bond0
  virtual_router_id 135
  priority 50
  authentication {
      auth_type PASS
      auth_pass mcDBhapwd135      # Set this to some secret phrase
  }

  # The virtual ip address shared between the two loadbalancers
  virtual_ipaddress {
    10.12.35.5 dev bond0
  }
  track_script {
    check_proxy
  }
notify_master /etc/keepalived/KaToding/notify_master.py
notify_fault /etc/keepalived/KaToding/notify_fault.py
notify_stop /etc/keepalived/KaToding/notify_stop.py
}
vrrp_script check_proxy_4 {
  script "/etc/keepalived/check_4.sh"
  interval 1
  weight 2
}
# Virtual interface
# The priority specifies the order in which the assigned interface to take over in a failover
vrrp_instance VI_134 {
  state BACKUP
  interface bond0
  virtual_router_id 134
  priority 50
  authentication {
      auth_type PASS
      auth_pass mcDBhapwd134      # Set this to some secret phrase
  }

  # The virtual ip address shared between the two loadbalancers
  virtual_ipaddress {
    10.12.35.4 dev bond0
  }
  track_script {
    check_proxy_4
  }
notify_master /etc/keepalived/KaToding_4/notify_master.py
notify_fault /etc/keepalived/KaToding_4/notify_fault.py
notify_stop /etc/keepalived/KaToding_4/notify_stop.py
}
上一篇 下一篇

猜你喜欢

热点阅读