CentOS Keepalived配置

借助 Keepalived 完成VIP配置

  • 在网关节点安装 Keepalived
  1. $ yum install -y keepalived
  • 编辑配置文件

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

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

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

  1. $ vi /etc/keepalived/check_gateway_status.sh
  2. #!/bin/bash
  3. /usr/bin/curl -I http://localhost:10254/healthz
  4. if [ $? -ne 0 ];then
  5. cat /var/run/keepalived.pid | xargs kill
  6. fi

添加执行权限

  1. $ chmod +x /etc/keepalived/check_gateway_status.sh
  • 更改 Keepalived systemd 配置文件,添加两项配置

    添加两项参数

  1. $ vi /lib/systemd/system/keepalived.service
  2. # /lib/systemd/system/keepalived.service
  3. [Unit]
  4. Description=Keepalive Daemon (LVS and VRRP)
  5. After=network-online.target
  6. Wants=network-online.target
  7. # Only start if there is a configuration file
  8. ConditionFileNotEmpty=/etc/keepalived/keepalived.conf
  9. [Service]
  10. Type=forking
  11. KillMode=process
  12. PIDFile=/var/run/keepalived.pid
  13. # Read configuration variable file if it is present
  14. EnvironmentFile=-/etc/default/keepalived
  15. ExecStart=/usr/sbin/keepalived $DAEMON_ARGS
  16. ExecReload=/bin/kill -HUP $MAINPID
  17. # 总是重启该服务
  18. Restart=always
  19. # 重启间隔时间
  20. RestartSec=10
  21. [Install]
  22. WantedBy=multi-user.target
  • 启动服务

    启动服务并设置开机自启动

  1. systemctl start keepalived
  2. systemctl enable keepalived
  3. systemctl status keepalived
  • 检测 VIP 是否启动
  1. ip a |grep <VIP>

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