原创

安装 keepalive

温馨提示:
本文最后更新于 2025年09月26日,已超过 259 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

centos 7.6 一键安装  install_keepalived.sh

#!/bin/bash
# CentOS 7.6 完整版 keepalived 安装脚本

# 检查是否为 root 用户
if [ $(id -u) -ne 0 ]; then
    echo "请使用 root 用户或 sudo 权限执行此脚本"
    exit 1
fi

# 检查系统版本
if ! grep -q "CentOS Linux release 7" /etc/redhat-release; then
    echo "警告: 此脚本专为 CentOS 7.6 设计"
fi

echo "开始安装 keepalived..."

# 安装 EPEL 源
echo "正在安装 epel-release..."
yum install -y epel-release

# 安装 keepalived
echo "正在安装 keepalived..."
yum install -y keepalived

# 检查安装是否成功
if ! command -v keepalived &> /dev/null; then
    echo "安装失败,请检查网络连接和 yum 源配置"
    exit 1
fi

# 设置开机自启
systemctl enable keepalived

# 创建基本配置文件(如果不存在)
if [ ! -f /etc/keepalived/keepalived.conf ]; then
    echo "创建默认配置文件..."
    cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100/24
    }
}
EOF
fi

echo "keepalived 安装完成!"
echo "配置文件路径: /etc/keepalived/keepalived.conf"
echo "启动命令: systemctl start keepalived"
echo "查看状态: systemctl status keepalived"


配置      负载 docker 启动的项目

a注意点:在 keepalived 配置中使用了 lb_kind NAT,但在实际环境中存在复杂的 Docker 网络 NAT 规则,可能导致冲突。

! 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 LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}

virtual_server 192.168.200.16 8443 {
   delay_loop 15
   lb_algo rr
   lb_kind DR  # 改为 Direct Routing 模式
   persistence_timeout 50
   protocol TCP

  real_server 172.17.0.6 8443 {
      weight 1
      TCP_CHECK {
          connect_timeout 3
          nb_get_retry 2
          delay_before_retry 3
      }
  }
}



}

使用方法

将脚本保存为 install_keepalived.sh
添加执行权限:chmod +x install_keepalived.sh
执行脚本:sudo ./install_keepalived.sh

安装完成后,可以使用以下命令验证:

# 检查版本
keepalived --version

# 检查服务状态
systemctl status keepalived

# 查看配置文件
cat /etc/keepalived/keepalived.conf

单独的配置解析;

普通负载均衡web容器docker容器的负载均衡
virtual_server 192.168.200.16 8443 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 172.17.0.6 8443 { weight 1 HTTP_GET { url { path / digest 72ba14b38df375dd0c5c4109dc7c14e4 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }virtual_server 192.168.200.16 8443 { delay_loop 15 lb_algo rr lb_kind DR # 改为 Direct Routing 模式 persistence_timeout 50 protocol TCP real_server 172.17.0.6 8443 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 2 delay_before_retry 3 } } }



 两者的区别
lb_kind:模式不同。
real_server:指向docker容器的真实ip地址。即使已经映射到物理机端口,
依然填docker容器的真实ip地址



正文到此结束