[实验] HAProxy + Keepalived 网站服务负载均衡加高可用的实现

纪念:站主于 2025 年 8 月完成了此开源实验,并将过程中的所有命令经过整理和主是以后,形成以下教程

步骤一:拓扑规划

1.1 服务器列表

httpd1 eth0: 192.168.100.21
httpd2 eth0: 192.168.100.22
Httpd3 eth0: 192.168.100.23
tool1 eth0: 192.168.100.11
tool2 eth0: 192.168.100.12

1.2 拓扑图

client           VIP               tool1                   httpd1
192.168.100.1    192.168.100.10    eth0: 192.168.100.11    eth0: 192.168.100.21

                                   tool2                   httpd2
                                   eth0: 192.168.100.12    eth0: 192.168.100.22

                                   httpd3
                                   eth0: 192.168.100.23

1.3 拓扑图简介

1) tool1 和 tool2 通过 keepalived 服务实现冗余,虚拟 IP 地址默认会挂在 tool1 上,当 tool1 出现故障或者 HAProxy 进程出现问题,虚拟 IP 地址会自动挂在 tool2 上
2) client 向 VIP 的虚拟 IP 192.168.100.10 发送访问网页的请求
3) tool1 或 tool2 收到访问网页的请求后将请求发往 httpd1、httpd2 或 httpd3
4) httpd1、httpd2 或 httpd3 回应访问网页的请求,并通过 tool1 或 tool2 向 client 返回网页
5) 最终实现双代理,三网站热备份

步骤二:系统环境要求

1) 所有服务器的系统都需要是 RockyLinux 8 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器都要关闭 SELinux
4) 所有服务器系统都要配置好可用的软件源
5) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机名

步骤三:搭建网站服务

3.1 在 httpd1 上搭建网站服务

3.1.1 在 httpd1 上安装 httpd

(只在 httpd1 上执行以下步骤)

# yum -y install httpd
3.1.2 给 httpd1 制定网页

(只在 httpd1 上执行以下步骤)

# echo httpd1 > /var/www/html/index.html
3.1.3 启动 httpd 并将它设置为开机自启

(只在 httpd1 上执行以下步骤)

# systemctl enable --now httpd

3.2 在 httpd2 上搭建网站服务

3.2.1 在 httpd2 上安装 httpd

(只在 httpd2 上执行以下步骤)

# yum -y install httpd
3.2.2 给 httpd2 制定网页

(只在 httpd2 上执行以下步骤)

# echo httpd2 > /var/www/html/index.html
3.2.3 启动 HTTPd 并将它设置为开机自启

(只在 httpd2 上执行以下步骤)

# systemctl enable --now httpd

3.3 在 httpd3 上搭建网站服务

3.3.1 在 httpd3 上安装 httpd

(只在 httpd3 上执行以下步骤)

# yum -y install httpd
3.3.2 给 httpd3 制定网页

(只在 httpd3 上执行以下步骤)

# echo httpd3 > /var/www/html/index.html
3.3.3 启动 httpd 并将它设置为开机自启

(只在 httpd3 上执行以下步骤)

# systemctl enable --now httpd

步骤四:搭建代理服务

4.1 安装 HAProxy

(分别在 tool1 和 tool2 上执行以下步骤)

# yum -y install haproxy

4.2 修改 HAProxy 配置文件

(分别在 tool1 和 tool2 上执行以下步骤)

# vi /etc/haproxy/haproxy.cfg

添加以下内容:

......

listen ingress-router-80 
  bind *:80
  mode tcp
  balance source
  server 192.168.100.21 192.168.100.21:80 check inter 1s
  server 192.168.100.22 192.168.100.22:80 check inter 1s
  server 192.168.100.23 192.168.100.23:80 check inter 1s

4.3 启动 HAProxy 并将它设置为开机自启

(分别在 tool1 和 tool2 上执行以下步骤)

# systemctl enable --now haproxy.service


注意:如果 tool1 和 tool2 开启了 SELinux 则在执行次步骤前需要执行以下命令:

# setsebool -P haproxy_connect_any=1

步骤五:搭建高可用服务

5.1 安装 Keepalived

(分别在 tool1 和 tool2 上执行以下步骤)

# yum -y install keepalived

5.2 修改 tool1 上的 Keepalived 配置文件

(只在 tool1 上执行以下步骤)

# vim /etc/keepalived/keepalived.conf

将全部内容修改如下:

! 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_track_process track_haproxy {
  process haproxy
  weight 50
}

vrrp_instance haproxy {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.10/24
    }
    track_process {
      track_haproxy
    }
}

5.3 修改 tool2 上的 Keepalived 配置文件

(只在 tool2 上执行以下步骤)

# vim /etc/keepalived/keepalived.conf

将全部内容修改如下:

! 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_track_process track_haproxy {
  process haproxy
  weight 50
}

vrrp_instance haproxy {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.10/24
    }
    track_process {
      track_haproxy
    }
}

5.4 启动 Keepalived 并将它设置为开机自启

(分别在 tool1 和 tool2 上执行以下步骤)

# systemctl enable --now keepalived.service

步骤六:测试网站负载均衡加高可用

6.1 正常情况下测试网站服务

(只在 client 上执行以下步骤)

# curl 192.168.100.10

(补充:执行以上命令会发现显示 httpd1)

6.2 在单节点故障的情况下测试网站服务

6.2.1 关闭 tool1、tool2、httpd1、httpd2、httpd3 中的任意一台服务器

(只在 tool1、tool2、httpd1、httpd2、httpd3 中的任意一台服务器上执行以下步骤)

# poweroff
6.2.2 测试网站服务

(只在 client 上执行以下步骤)

# curl 172.16.1.100

(补充:如果在上面的步骤中关闭的是 httpd1,则执行以上命令会发现显示的是 httpd2 或 httpd3)