部署 Keepalived

部署 Keepalived - 图1warning

局域网内交换机需要支持 VRRP 协议,否则 Keepalived 无法正常工作。

部署 Keepalived

在所有网关节点安装 Keepalived

CentOS

  1. yum install -y keepalived

Ubuntu

  1. apt-get -y install libssl-dev
  2. apt-get -y install openssl
  3. apt-get -y install libpopt-dev
  4. apt-get -y install keepalived

修改 Keepalived 配置文件

部署 Keepalived - 图2caution

注意!当前调用健康监测脚本内容为注释状态,原因是在安装 Rainbond 前 需要确保 VIP 已经存在;在 Rainbond 安装完成之后需将注释取消,才能实现健康监测,确保 网关高可用。

主节点配置文件

vim /etc/keepalived/keepalived.conf

  1. ! Configuration File for keepalived
  2. global_defs {
  3. router_id LVS_DEVEL
  4. }
  5. #vrrp_script check_gateway {
  6. # 检测脚本
  7. # script "/etc/keepalived/check_gateway_status.sh"
  8. # 执行间隔时间
  9. # interval 5
  10. #}
  11. vrrp_instance VI_1 {
  12. #因使用非抢占模式,这里都为backup
  13. state BACKUP
  14. #网卡设备名,通过 ifconfig 命令确定
  15. interface ens6f0
  16. virtual_router_id 51
  17. #优先级,主节点大于备节点
  18. priority 100
  19. advert_int 1
  20. #非抢占模式
  21. nopreempt
  22. authentication {
  23. auth_type PASS
  24. auth_pass 1111
  25. }
  26. virtual_ipaddress {
  27. <VIP>
  28. }
  29. # track_script {
  30. # check_gateway
  31. # }
  32. }
  33. ``` 从节点配置文件
  34. vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs { router_id LVS_DEVEL }

vrrp_script check_gateway {

检测脚本

script “/etc/keepalived/check_gateway_status.sh”

执行间隔时间

interval 5

}

  1. vrrp_instance VI_1 {
  2. #因使用非抢占模式,这里都为backup
  3. state BACKUP
  4. #网卡设备名,通过 ifconfig 命令确定
  5. interface ens6f0
  6. virtual_router_id 51
  7. #优先级,主节点大于备节点
  8. priority 50
  9. advert_int 1
  10. #非抢占模式
  11. nopreempt
  12. authentication {
  13. auth_type PASS
  14. auth_pass 1111
  15. }
  16. virtual_ipaddress {
  17. <VIP>
  18. }

track_script {

check_gateway

}

}

  1. ### 健康监测脚本
  2. 扩展对网关节点健康检查的脚本,脚本的功能是当 rbd-gateway 组件停止服务,则关闭本机的 Keepalived,切换 VIP 。(主从都需操作)

$ vim /etc/keepalived/check_gateway_status.sh

!/bin/bash

/usr/bin/curl -I http://localhost:10254/healthz

if [ $? -ne 0 ]; then cat /var/run/keepalived.pid | xargs kill fi

添加执行权限

$ chmod +x /etc/keepalived/check_gateway_status.sh

  1. ### 修改 Keepalived Systemd 配置文件
  2. 更改 Keepalived systemd 配置文件,添加两项配置 `Restart` `RestartSec` ,保证 Keepalived 服务异常退出后自动重启。
  3. vi /lib/systemd/system/keepalived.service

/lib/systemd/system/keepalived.service

[Unit] Description=Keepalive Daemon (LVS and VRRP) After=network-online.target Wants=network-online.target

Only start if there is a configuration file

ConditionFileNotEmpty=/etc/keepalived/keepalived.conf [Service] Type=forking KillMode=process PIDFile=/var/run/keepalived.pid

Read configuration variable file if it is present

EnvironmentFile=-/etc/default/keepalived ExecStart=/usr/sbin/keepalived $DAEMON_ARGS ExecReload=/bin/kill -HUP $MAINPID

总是重启该服务

Restart=always

重启间隔时间

RestartSec=10

[Install] WantedBy=multi-user.target

  1. ### 启动 Keepalived 服务
  2. 启动服务并设置开机自启动

systemctl start keepalived systemctl enable keepalived systemctl status keepalived

  1. ### 检测 VIP 是否启动

ip a |grep ```

部署 Keepalived - 图3warning

在进行下一步操作前请确保 VIP 已经存在,如果 VIP 不存在,请重新审查本节操作;在 Rainbond 安装完成之后请将配置文件中注释取消并重启 Keepalived 服务,实现健康监测,以此确保 网关高可用。