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

文章详细描述了如何通过Haproxy进行服务器负载均衡,包括配置多个服务器节点、域名映射、健康检查以及使用Keepalived实现高可用性。同时介绍了ApacheHTTP服务器的安装与配置以响应来自VIP的请求。
摘要由CSDN通过智能技术生成

服务器规划:

                            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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值