[实验] 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)

[步骤] dnsmasq 的搭建 (DNS 服务)

步骤一:安装 dnsmasq

# yum install dnsmasq

步骤二:添加 DNS 配置

# vi /etc/dnsmasq.d/address.conf

创建以下内容:

all-servers
server=1.1.1.1
server=8.8.8.8

server=/cn/114.114.114.114

address=/test-gateway.com/192.168.100.1

host-record=gateway.com,192.168.100.1


补充:
1) 这里的 all-servers 代表同时解析下面所有的 DNS 记录
2) 这里的 server=1.1.1.1 和 server=8.8.8.8 代表上游 DNS 服务器使用 1.1.1.1 和 8.8.8.8
3) 这里的 server=/cn/114.114.114.114 代表以 cn 结尾的域名上游 DNS 使用 114.114.114.114
4) 这里的 address=/test-gateway.com/192.168.100.1 代表所有以 test-gateway.com 结尾的域名 (例如 one.test-gateway.com) 指向 IP 地址 192.168.100.1
5) 这里的 host-record=gateway.com,192.168.100.1 代表 gateway.com 域名指向 IP 地址 192.168.100.1

步骤三:重启 dnsmasq 服务

3.1 重启 dnsmasq 服务

# systemctl restart dnsmasq.service 

3.2 查看 dnsmasq 服务状态

# systemctl status dnsmasq.service
● dnsmasq.service - DNS caching server.
   Loaded: loaded (/usr/lib/systemd/system/dnsmasq.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2025-08-14 00:00:35 CST; 7s ago
 Main PID: 10412 (dnsmasq)
    Tasks: 1 (limit: 5781)
   Memory: 704.0K
   CGroup: /system.slice/dnsmasq.service
           └─10412 /usr/sbin/dnsmasq -k

Aug 14 00:00:35 tool1 dnsmasq[10412]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN2 DHCP>
Aug 14 00:00:35 tool1 dnsmasq[10412]: using nameserver 114.114.114.114#53 for domain cn
Aug 14 00:00:35 tool1 dnsmasq[10412]: using nameserver 8.8.8.8#53
Aug 14 00:00:35 tool1 dnsmasq[10412]: using nameserver 1.1.1.1#53
Aug 14 00:00:35 tool1 dnsmasq[10412]: reading /etc/resolv.conf
Aug 14 00:00:35 tool1 dnsmasq[10412]: using nameserver 114.114.114.114#53 for domain cn
Aug 14 00:00:35 tool1 dnsmasq[10412]: using nameserver 8.8.8.8#53
Aug 14 00:00:35 tool1 dnsmasq[10412]: using nameserver 1.1.1.1#53
Aug 14 00:00:35 tool1 dnsmasq[10412]: ignoring nameserver 192.168.100.11 - local interface
Aug 14 00:00:35 tool1 dnsmasq[10412]: read /etc/hosts - 2 addresses 

步骤四:使用配置好的 dnsmasq 服务

# vi /etc/resolv.conf

将全部内容修改如下:

nameserver 192.168.100.11

(补充:这里的 192.168.100.11 指的是刚刚搭建 dnsmasq 服务的服务器的 IP 地址)

步骤五:测试 dnsmasq 服务

5.1 测试 DNS 正向解析

# nslookup test-gateway.com
Server:		192.168.100.11
Address:	192.168.100.11#53

Name:	test-gateway.com
Address: 192.168.100.1

(补充:这里以正向解析刚刚配置的域名 test-gateway.com 为例)

5.2 测试 DNS 反向解析

# arp 192.168.100.1
Address                  HWtype  HWaddress           Flags Mask            Iface
_gateway                 ether   52:54:00:ef:3a:4d   C                     eth0

(补充:这里以反向解析刚刚配置的 IP 地址 192.168.100.1 为例)

[排错] 解决 Ubuntu dpkg 数据库损坏 (apt 更新到一半时意外中断)

解决方法:

步骤一:重新配置 dpkg 数据库

# sudo dpkg --configure -a

步骤二:修复 apt 的中断

# sudo apt install --fix-broken

步骤三:删除报错的软件包

# sudo apt remove --purge postfix

(补充:这里以删除报错的 postfix 软件包为例)

步骤四:删除不用的软件包

4.1 清理 apt 缓存

# sudo apt clean

4.2 删除不用的软件包

# sudo apt autoremove

步骤五:更新所有软件包

5.1 下载所有需要更新的软件包

# sudo apt update

5.2 升级所有软件包

# sudo apt upgrade

步骤六:重新安装前面报错并删除的软件包

# sudo apt-get install postfix

(补充:这里以重新安装前面报错并删除的 postfix 软件包为例)

[内容] 软件源的设置 (Ubuntu 版)

内容一:添加 Ubuntu 软件源

# cat /etc/apt/sources.list
# Ubuntu sources have moved to /etc/apt/sources.list.d/ubuntu.sources
# vim /etc/apt/sources.list.d/ubuntu.sources

创建以下内容:

Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

内容二:添加第 3 方软件源

# cat /etc/apt/sources.list
# Ubuntu sources have moved to /etc/apt/sources.list.d/ubuntu.sources
# vim /etc/apt/sources.list.d/ubuntu.sources

创建以下内容:

Types: deb
URIs: http://mirrors.aliyun.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

(补充:这里以添加阿里云的软件源为例)

[步骤] Linux 重启后网卡上没有 IP 地址的处理方法

步骤一:在硬件管理页面检查物理网卡是否联通

记录需要使用的网络端口的网卡机器码 (MAC) 地址

(步骤略)


注意:
1) 只有物理机才需要完成这步
2) 当物理机 1 个网络模块上有 2 个网络端口时,要特别注意 2 个网络端口各自的网卡机器码 (MAC) 地址,例如:戴尔服务器网络端口的网卡机器码 (MAC) 地址在页面的最下方

步骤二:在系统中查看对应的网卡是否联通

2.1 使用 IP 命令查看对应的网卡是否联通

# ip a s

或者:

# ip address show

(补充:当出现 ”UP“ 字样时代表网卡已经启动)

(注意:nmcli device show 命令不能确认此时网卡是否联通)

2.2 使用 ethtool 命令查看对应的网卡是否联通

# ethtool eth0

(注意:nmcli device show 命令不能确认此时网卡是否联通)

步骤三:尝试配置临时 IP 地址和临时网关 (只有当确认网卡是联通的时候才进行以下操作)

3.1 尝试配置临时 IP 地址

# ifconfig eth0 192.168.0.2/24

或者:

# ip a add 192.168.0.2/24 dev eth0

(补充:这里以给 eth0 网卡添加临时 IP 地址 192.168.0.2/24 为例)

(注意:只有当确认网卡是联通的时候才进行此操作)

3.2 尝试配置临时网关

# route add default gw 192.168.0.1

或者:

# ip route add default via 192.168.0.1

(注意:只有当确认网卡是联通的时候才进行此操作)

3.3 查看配置的临时 IP 地址和临时网关

3.3.1 查看配置的临时 IP 地址
# ip a s
3.3.2 查看配置的临时网关
# route -n