KeepAlived搭建高可用的HAproxy负载均衡集群系统

服务器规划:

serverd(haproxy1,keepalived):192.168.233.141

serverb(haproxy2,keepalived):192.168.233.144

servera(web1):192.168.233.132

serverc(web2):192.168.233.140

域名映射:(所有端均配置):

复制代码
vim /etc/hosts
192.168.233.141 haproxy1
192.168.233.144 haproxy2
192.168.233.140 web2
192.168.233.132 web1

serverd(141):

复制代码
haproxy配置:

yum install haproxy -y
vim /etc/haproxy/haproxy.cfg(配置文件) 
# 设置日志,记录在本地3号设备上,记录级别为info
global
    log 127.0.0.1 local3 info
    # 最大连接数为4096
    maxconn 4096
    # 运行haproxy的用户和组
    user nobody
    group nobody
    # 以守护进程方式运行
    daemon
    # 进程数为1
    nbproc 1
    # PID文件路径
    pidfile /run/haproxy.pid
 
# 默认配置
defaults
    # 全局日志记录
    log global
    # 模式为HTTP
    mode http
    # 最大连接数为2048
    maxconn 2048
    # 重试次数为3
    retries 3
    # 开启转发
    option redispatch
    # 连接超时时间为5秒
    timeout connect 5000
    # 客户端超时时间为50秒
    timeout client 50000
    # 服务器超时时间为50秒
    timeout server 50000
    # 关闭连接时强制关闭
    option abortonclose
    # 配置统计页面
    stats uri /admin?stats
    stats realm Private lands
    stats auth admin:password
    stats hide-version
 
# 前端配置
frontend http-in
    # 监听所有IP的80端口
    bind 0.0.0.0:80
    # 使用HTTP模式
    mode http
    # 记录日志
    log global
    # 开启httplog选项
    option httplog
    # 关闭HTTP长连接
    option httpclose
    # 定义acl规则
    acl html url_reg -i \.html$
    # 如果是HTML文件,则使用html-server后端
    use_backend html-server if html
    # 默认使用html-server后端
    default_backend html-server
 
# 后端配置
backend html-server
    # 使用HTTP模式
    mode http
    # 负载均衡算法为轮询
    balance roundrobin
    # 健康检查请求为GET /index.html
    option httpchk GET /index.html
    # 插入SERVERID COOKIE,并且间接设置,不缓存
    cookie SERVERID insert indirect nocache
    # 配置服务器A
    server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
    # 配置服务器B
    server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
    systemctl start haproxy


keepalived配置:
yum install keepalived -y
cd /etc/keepalived/
! Configuration File for keepalived

# 定义全局参数
global_defs {
   router_id LVS_141  # 路由器ID
}

# 定义一个用于检查HAProxy的脚本
vrrp_script chk_haproxy {
        script "/etc/keepalived/chk_haproxy.sh"
}

# 定义一个VRRP实例为nginx
vrrp_instance nginx {
    state MASTER  # 设置实例状态为MASTER
    interface ens160  # 指定接口
    virtual_router_id 51  # 设置虚拟路由器ID
    priority 100  # 设置优先级
    advert_int 1  # 设置通告间隔
    authentication {
        auth_type PASS  # 设置认证类型为PASS
        auth_pass 1111  # 设置认证密码
    }
    virtual_ipaddress {
        192.168.233.50  # 设置虚拟IP地址
    }
    track_script {
        chk_haproxy  # 跟踪chk_haproxy脚本
     }
}


健康检查:
vim chk_haproxy.sh
#!/bin/bash

# 检查 haproxy 服务的状态
ST=$(systemctl is-active haproxy)

# 如果 haproxy 服务状态不是 "active",则执行以下操作
if [ "$ST" != "active" ]; then
    # 重启 haproxy 服务并将输出重定向到 /dev/null
    systemctl restart haproxy &> /dev/null
    sleep 1
    
    # 重新获取 haproxy 服务的状态
    ST=$(systemctl is-active haproxy)
    
    # 如果重启后 haproxy 服务状态仍不是 "active"
    if [ "$ST" != "active" ]; then
        # 停止 keepalived 服务
        systemctl stop keepalived
    else
        # 如果重启后 haproxy 服务状态是 "active",则退出脚本
        exit
    fi
fi

systemctl start keepalived

serverb(144):

复制代码
haproxy配置:
yum install haproxy -y
vim /etc/haproxy/haproxy.cfg(配置文件) 
# 设置日志,记录在本地3号设备上,记录级别为info
global
    log 127.0.0.1 local3 info
    # 最大连接数为4096
    maxconn 4096
    # 运行haproxy的用户和组
    user nobody
    group nobody
    # 以守护进程方式运行
    daemon
    # 进程数为1
    nbproc 1
    # PID文件路径
    pidfile /run/haproxy.pid
 
# 默认配置
defaults
    # 全局日志记录
    log global
    # 模式为HTTP
    mode http
    # 最大连接数为2048
    maxconn 2048
    # 重试次数为3
    retries 3
    # 开启转发
    option redispatch
    # 连接超时时间为5秒
    timeout connect 5000
    # 客户端超时时间为50秒
    timeout client 50000
    # 服务器超时时间为50秒
    timeout server 50000
    # 关闭连接时强制关闭
    option abortonclose
    # 配置统计页面
    stats uri /admin?stats
    stats realm Private lands
    stats auth admin:password
    stats hide-version
 
# 前端配置
frontend http-in
    # 监听所有IP的80端口
    bind 0.0.0.0:80
    # 使用HTTP模式
    mode http
    # 记录日志
    log global
    # 开启httplog选项
    option httplog
    # 关闭HTTP长连接
    option httpclose
    # 定义acl规则
    acl html url_reg -i \.html$
    # 如果是HTML文件,则使用html-server后端
    use_backend html-server if html
    # 默认使用html-server后端
    default_backend html-server
 
# 后端配置
backend html-server
    # 使用HTTP模式
    mode http
    # 负载均衡算法为轮询
    balance roundrobin
    # 健康检查请求为GET /index.html
    option httpchk GET /index.html
    # 插入SERVERID COOKIE,并且间接设置,不缓存
    cookie SERVERID insert indirect nocache
    # 配置服务器A
    server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
    # 配置服务器B
    server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
systemctl start haproxy



keepalived配置:
yum install keepalived -y
cd /etc/keepalived/
! Configuration File for keepalived

global_defs {
   router_id LVS_144  # 设置路由器ID
}

vrrp_script chk_haproxy {
        script "/etc/keepalived/chk_haproxy.sh"  # 指定用于检查HAProxy运行状态的脚本路径
}

vrrp_instance nginx {
    state BACKUP  # 设置实例状态为备份
    interface ens160  # 指定网络接口
    virtual_router_id 51  # 设置虚拟路由器ID
    priority 80  # 设置优先级
    advert_int 1  # 设置广播间隔
    authentication {
        auth_type PASS  # 设置认证类型为密码
        auth_pass 1111  # 设置认证密码
    }
    virtual_ipaddress {
        192.168.233.50  # 设置虚拟IP地址
    }
    track_script {
        chk_haproxy  # 设置要跟踪的脚本
     }
}


健康检查:
vim chk_haproxy.sh
#!/bin/bash

# 检查 haproxy 服务的状态
ST=$(systemctl is-active haproxy)

# 如果 haproxy 服务状态不是 "active",则执行以下操作
if [ "$ST" != "active" ]; then
    # 重启 haproxy 服务并将输出重定向到 /dev/null
    systemctl restart haproxy &> /dev/null
    sleep 1
    
    # 重新获取 haproxy 服务的状态
    ST=$(systemctl is-active haproxy)
    
    # 如果重启后 haproxy 服务状态仍不是 "active"
    if [ "$ST" != "active" ]; then
        # 停止 keepalived 服务
        systemctl stop keepalived
    else
        # 如果重启后 haproxy 服务状态是 "active",则退出脚本
        exit
    fi
fi

systemctl start keepalived

servera(132):web1

复制代码
# 关闭SELinux
setenforce 0

# 停止firewalld防火墙
systemctl stop firewalld

# 安装Apache HTTP服务器
yum install httpd -y

# 在网站根目录下创建一个名为index.html的文件,并写入内容"web2"
echo web1 > /var/www/html/index.html

# 设置HTTP服务器开机自启动
systemctl enable httpd

# 启动HTTP服务器
systemctl start httpd

serverb(140):web2

复制代码
# 关闭SELinux
setenforce 0

# 停止firewalld防火墙
systemctl stop firewalld

# 安装Apache HTTP服务器
yum install httpd -y

# 在网站根目录下创建一个名为index.html的文件,并写入内容"web2"
echo web2 > /var/www/html/index.html

# 设置HTTP服务器开机自启动
systemctl enable httpd

# 启动HTTP服务器
systemctl start httpd

任意端通过vip访问:

复制代码
curl 192.168.233.50

通过haproxy访问:

复制代码
curl http://haproxy1
相关推荐
snow@li1 分钟前
Java:Java 服务器(Web 容器 / Servlet 容器)完整工作清单
java·服务器·前端
胖兔纸214 分钟前
OpenStack 1.7.2 & Ceph 9.2.1 运维命令
运维·ceph·openstack
JL1530 分钟前
Agent工程-为什么Agent必须有观测和Tracing
服务器·网络·人工智能·安全
不会C语言的男孩44 分钟前
Docker 在嵌入式设备中的常用玩法
运维·docker·容器
神州世通44 分钟前
IP Office内置外显卡兼容性说明
服务器·网络·tcp/ip
ylscode1 小时前
CVE-2026-31694 漏洞深度分析:Linux 内核 FUSE 组件存在本地提权风险,普通用户可直取 root 权限
linux·运维·服务器
frank00600712 小时前
Rocky 9.8设置本地yum源
服务器
智脑API平台2 小时前
Codex CLI 怎么用 .env 配置 API Key?本地项目和自动化脚本接入教程
运维·自动化
土星云SaturnCloud2 小时前
边缘计算在储气库调峰智能管控中的应用:架构设计与技术解析
服务器·人工智能·ai·边缘计算
weixin_307779132 小时前
Linux下Docker Compose里运行的MySQL数据库故障诊断Shell脚本
linux·运维·mysql·docker·自动化