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
相关推荐
咖丨喱1 小时前
【Action帧简要分析】
服务器·数据库·asp.net
三体世界1 小时前
TCP传输控制层协议深入理解
linux·服务器·开发语言·网络·c++·网络协议·tcp/ip
LuDvei1 小时前
CH9121T电路及配置详解
服务器·嵌入式硬件·物联网·网络协议·tcp/ip·网络安全·信号处理
zkmall1 小时前
企业电商平台搭建:ZKmall开源商城服务器部署与容灾方案
运维·服务器·开源
泷羽Sec-静安1 小时前
OSCP官方靶场-Solstice WP
服务器·网络·数据库
华不完1 小时前
下一代防火墙混合模式部署
运维·服务器·网络
tomcsdn311 小时前
SMTPman,smtp的端口号是多少全面解析配置
服务器·开发语言·php·smtp·邮件营销·域名邮箱·邮件服务器
x县豆瓣酱1 小时前
ubuntu server配置静态IP
linux·运维·ubuntu
工藤新一¹2 小时前
Linux
linux·运维·服务器