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

Several situations of Linux automatically reboot without reboot logs in the /var/log/messages

Situation One

This Linux server is a virtual server. If we reboot it though its virtual software, there is no relevant logs in the /var/log/messages.

Situation Two

This Linux server is a member of a pacemaker cluster. If the pacemaker cluster software fences this server for protecting the whole cluster, there is no relate logs in the /var/log/messages.

Situation Three

This Linux server has critical problems in its system or hardware. Core panic of Linux and hardware problem both can reboot the system automatically without any reboot logs in the /var/log/messages.

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

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

步骤一:拓扑图

1.1 服务器列表

client enp1s0: 172.16.1.99

proxy1 enp1s0: 172.16.0.101
enp7s0: 172.16.1.101
virtual IP: 172.16.1.100

proxy2 enp1s0: 172.16.0.102
enp7s0: 172.16.1.102

web1 enp1s0: 172.16.0.11

web2 enp1s0: 172.16.0.12

1.2 拓扑图

                      proxy1                                       web1
                      enp7s0:172.16.1.101 enp1s0:172.16.0.101      enp1s0:172.16.0.11
                      virtual IP:172.16.1.100
client
enp1s0:172.16.1.99
                      proxy2                                       web2
                      enp7s0:172.16.1.102 enp1s0:172.16.0.102      enp1s0:172.16.0.12

1.3 拓扑图简介

1) web1 安装 Nginx,web2 安装 Apache 实现网站服务
2) proxy1 和 proxy2 安装 Nginx 实现网站代理,轮询代理 web1、web2 上的网站服务实现负载均衡
3) 虚拟 IP 172.16.1.90 通过 Keepalived 默认放在 proxy1 的 enp7s0 网卡上,如果 proxy1 宕机或者检测到自己 Nginx 代理进程死掉,则虚拟 IP 172.16.1.90 则挂在 proxy2 的 enp7s0 网卡上实现高可用
4) 如果 web1 和 web2 中有一台服务器宕机,则 proxy1 和 proxy2 会自动不再向这台服务器请求网站服务,直到它恢复正常
5) 最终达到的效果是 client 向虚拟 IP 请求网站服务,此时如果 proxy1 正常就代表虚拟 IP 轮询调度 web1 和 web2 上的网站服务,再返回给 client。如果 proxy1 宕机则由 proxy2 代表虚拟 IP 完成次操作

步骤二: 系统环境要求

1) 所有服务器的系统都需要是 CentOS 8 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器都要关闭 SELinux
4) 所有服务器系统都要配置好可用的软件源
5) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
6) client 的 enp1s0 网卡、proxy1 的 enp7s0 网卡和 proxy2 的 enp7s0 网卡要可以相互 ping 通自己和对方的 IP
7) proxy1 的 enp1s0 网卡、proxy2 的 enp1s0 网卡、web1 的 enp1s0 网卡和 web2 的 enp1s0 网卡要可以相互 ping 通自己和对方的 IP 地址

步骤三:搭建网站服务

3.1 在 web1 上搭建网站服务

3.1.1 在 web1 上安装 Nginx

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

# yum -y install nginx
3.1.2 给 web1 制定网页

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

# echo web1 > /usr/share/nginx/html/index.html
3.1.3 启动 Nginx 并将它设置为开机自启

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

# systemctl enable --now nginx

3.2 在 web2 上搭建网站服务

3.2.1 在 web2 上安装 Apache

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

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

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

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

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

# systemctl enable --now httpd

步骤四:搭建代理服务

4.1 安装 Nginx

(分别在 proxy1 和 proxy2 上执行以下步骤)

# yum -y install nginx

4.2 修改 Nginx 配置文件

(分别在 proxy1 和 proxy2 上执行以下步骤)

# vi /etc/nginx/nginx.conf

将部分内容修改如下:

......
http {
    upstream webserver {
        server 172.16.0.11:80;
        server 172.16.0.12:80;
    }
......
    server {
        listen       80;

        location / {
        proxy_pass http://webserver;
        }
    }
......
}

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

(分别在 proxy1 和 proxy2 上执行以下步骤)

# systemctl enable --now nginx

步骤五:搭建高可用服务

5.1 安装 Keepalived

(分别在 proxy1 和 proxy2 上执行以下步骤)

# yum -y install keepalived

5.2 创建 Keepalived 检查脚本

5.2.1 创建 Keepalived 检查脚本

(分别在 proxy1 和 proxy2 上执行以下步骤)

# vi /etc/keepalived/nginx_check.sh

创建以下内容:

#!/bin/bash

if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
    systemctl stop nginx
    sleep 5
    if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

(补充:这里以检测 Nginx 没启动就启动 Nginx,5 秒后 Nginx 要是还没有启动就关闭 keepalived 为例)

5.2.2 给 Keepalived 检查脚本执行权限

(分别在 proxy1 和 proxy2 上执行以下步骤)

# chmod u+x /etc/keepalived/nginx_check.sh

5.3 修改 proxy1 上的 Keepalived 配置文件

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

# 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 proxy1
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight 20
}

vrrp_instance VI_1 {
    state MASTER
    interface enp7s0
    virtual_router_id 90
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        172.16.1.100
    }
}


补充:
1) script “/etc/keepalived/nginx_check.sh” 代表使用的检测脚本是 /etc/keepalived/nginx_check.sh
2) interface enp7s0 代表虚拟 IP 将挂载在 enp7s0 网卡上
3) priority 代表修建级是 101,数字越大优先级越高
4) 172.16.1.100 代表虚拟 IP 是 172.16.1.100

5.4 修改 proxy2 上的 Keepalived 配置文件

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

# 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 proxy1
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
     script "/etc/keepalived/nginx_check.sh"
     interval 2
     weight 20
}

vrrp_instance VI_1 {
    state BACKUP
    interface enp7s0
    virtual_router_id 90
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        172.16.1.100
    }
}


补充:
1) script “/etc/keepalived/nginx_check.sh” 代表使用的检测脚本是 /etc/keepalived/nginx_check.sh
2) interface enp7s0 代表虚拟 IP 将挂载在 enp7s0 网卡上
3) priority 代表修建级是 99,数字越大优先级越高
4) 172.16.1.100 代表虚拟 IP 是 172.16.1.100

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

(分别在 proxy1 和 proxy2 上执行以下步骤)

# systemctl enable --now keepalived.service

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

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

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

# curl 172.16.1.100

(补充:重复以上命令会发现重复显示 web1 和 web2)

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

6.2.1 关闭 proxy1、proxy2、web1、web2 中的任意一台服务器

(只在 proxy1、proxy2、web1、web2 中的任意一台服务器上执行以下步骤)

# poweroff
6.2.2 测试网站服务

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

# curl 172.16.1.100

(补充:重复以上命令会发现重复显示 web1 和 web2)

[实验] FTP + Pacemaker 存储服务高可用的实现

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

注意:

在实现 FTP + Pacemaker 存储服务高可用之前要先安装 Pacemaker 集群 ,并且需要 root 权限

正文:

步骤一:Pacemaker 高可用 FTP 服务的解析

1.1 集群本身需要的服务

需要额外一台服务器提供 Iscasi 远程目录服务

1.2 本 Pacemaker 高可用 FTP 服务的特点

1) 使用其他服务器提供的 Iscasi 服务器作为 FTP 的共享目录
2) 提供 FTP 服务
4) 提供虚拟 IP 服务
5) 以上三项服务器都实现高可用
6) 唯一的单点故障在于额外的那台服务器提供的 Iscasi 远程目录服务器

步骤二:前期准备所有集群主机上都安装 FTP 服务

2.1 在所有集群主机上安装 FTP

(在所有集群服务器上执行以下步骤)

# yum -y install vsftpd

2.2 确保 vsftpd 服务没有启动

(在所有集群服务器上执行以下步骤)

# systemctl stop vsftpd
# systemctl disable vsftpd

步骤三:部署 Pacemaker 的 FTP 高可用服务

3.1 在 ftp 资源组中创建名为 ftpip 的虚拟 ip 资源

(只在一台集群里的服务器上执行以下步骤)

# pcs resource create ftpip IPaddr2 ip=192.168.0.21 cidr_netmask=24 --group ftp

3.2 在 ftp 资源组中创建名为 ftpfiles 挂载其他服务器的 Iscasi 服务的资源

(只在 1 台集群里的服务器上执行以下步骤)

# pcs resource create ftpfiles Filesystem device=192.168.8.21:/content/ftp directory=/var/ftp fstype=nfs options=ro --group ftp

(注意:这里的 Filesystem 指的是其他服务器搭建的 Iscasi 服务,这个服务需要提前搭建好)

3.3 在 ftp 资源组中创建名为 vsftpd 的 ftp 资源

(只在一台集群里的服务器上执行以下步骤)

# pcs resource create vsftpd systemd:vsftpd --group ftp

[实验] Apache + Pacemaker 网站服务高可用的实现

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

注意:

在实现 Apache + Pacemaker 网站服务高可用之前要先安装 Pacemaker 集群

正文:

步骤一:Pacemaker 高可用网站服务的解析

1.1 集群本身需要的服务

需要额外一台服务器提供 NFS 远程目录服务

1.2 本 Pacemaker 高可用网站服务的特点

1) 使用其他服务器提供的 NFS 服务器作为网站的网页目录
2) 提供网站 服务
3) 提供虚拟 IP 地址服务
4) 以上三项服务器都实现高可用
5) 唯一的单点故障在于额外的那台服务器提供的 NFS 远程目录服务器

步骤二:前期准备

2.1 在所有集群服务器上安装 httpd

(在所有集群服务器上执行以下步骤)

# yum -y install httpd

2.2 确保 httpd 不会被 SELinux 限制

(在所有集群服务器上执行以下步骤)

# setsebool -P httpd_use_nfs 1

(补充:这里是要求 SELinux 的布尔值让 httpd 也可以使用 NFS 服务)

2.3 确保 httpd 没有启动

(在所有集群服务器上执行以下步骤)

# systemctl stop httpd
# systemctl disable httpd

步骤三:部署 Pacemaker 的网站高可用服务

3.1 在 网站资源组中创建名为 webip 的虚拟 IP 地址资源

(只在一台集群里的服务器上执行以下步骤)

# pcs resource create webip IPaddr2 ip=192.168.0.20 cidr_netmask=24 --group=web

3.2 在 网站资源组中创建名为 webnfs 挂载其他服务器的 NFS 服务的资源

(只在一台集群里的服务器上执行以下步骤)

# pcs resource create webnfs Filesystem device=192.168.8.21:/content directory=/var/www/html fstype=nfs options=ro --group web

(注意:这里的 Filesystem 指的是其他服务器搭建的 NFS 服务,这个服务需要提前搭建好,可以参考 https://eternalcenter.com/nfs/ 里的内容)

3.3 在网站资源组中创建名为 webserver 的网站资源

(只在 1 台集群里的服务器上执行以下步骤)

# pcs resource create webserver apache configfile="/etc/httpd/conf/httpd.conf" statusurl="http://127.0.0.1/server-status" --group web