插件窝 干货文章 如何在 RHEL 9/8 上设置高可用性 Apache(HTTP)集群

如何在 RHEL 9/8 上设置高可用性 Apache(HTTP)集群

sudo bash 节点 步骤 832    来源:    2025-04-20

在 RHEL 9/8 上设置高可用性 Apache (HTTP) 集群

概述

在 RHEL 9/8 上设置高可用性 Apache 集群需要使用 Pacemaker 和 Corosync 集群管理工具。以下是详细的配置步骤:

前提条件

  • 两台或更多运行 RHEL 9/8 的服务器
  • 具有 root 或 sudo 权限
  • 网络连接正常,服务器之间可以互相通信
  • 共享存储 (如 NFS, iSCSI 或 GFS2) 用于 Web 内容

步骤 1: 安装必要软件包

在所有节点上安装所需软件包:

sudo dnf install -y pcs pacemaker fence-agents-all httpd

步骤 2: 配置防火墙

允许集群通信所需的端口:

sudo firewall-cmd --permanent --add-service=high-availability
sudo firewall-cmd --reload

步骤 3: 设置 hacluster 用户密码

在所有节点上设置 hacluster 用户密码:

sudo passwd hacluster

步骤 4: 启用并启动 pcsd 服务

在所有节点上:

sudo systemctl enable --now pcsd

步骤 5: 认证集群节点

在一个节点上运行以下命令(将 node1 和 node2 替换为您的实际节点名称):

sudo pcs host auth node1 node2 -u hacluster -p your_password

步骤 6: 创建集群

sudo pcs cluster setup my_http_cluster node1 node2 --start --enable

步骤 7: 配置集群属性

sudo pcs property set stonith-enabled=false
sudo pcs property set no-quorum-policy=ignore

步骤 8: 配置 Apache 资源

  1. 首先在所有节点上配置 Apache 但不启用服务:
sudo systemctl disable --now httpd
  1. 创建 Apache 资源:
sudo pcs resource create WebSite ocf:heartbeat:apache \
  configfile=/etc/httpd/conf/httpd.conf \
  statusurl="http://127.0.0.1/server-status" \
  op monitor interval=1min

步骤 9: 配置虚拟 IP 资源

sudo pcs resource create Virtual_IP ocf:heartbeat:IPaddr2 \
  ip=192.168.1.100 cidr_netmask=24 \
  op monitor interval=30s

(将 192.168.1.100 替换为您要使用的虚拟 IP)

步骤 10: 创建资源组

sudo pcs resource group add WebGroup Virtual_IP WebSite

步骤 11: 测试故障转移

  1. 查看资源运行在哪个节点:
sudo pcs status
  1. 手动将资源迁移到另一个节点:
sudo pcs resource move WebGroup node2
  1. 清除迁移约束:
sudo pcs resource clear WebGroup

步骤 12: 配置共享存储 (可选)

如果使用共享存储来存放网站内容,可以添加文件系统资源:

sudo pcs resource create WebData Filesystem \
  device="/dev/shared/web" \
  directory="/var/www/html" \
  fstype="ext4" \
  --group WebGroup

高级配置选项

配置监控间隔

sudo pcs resource op defaults timeout=240s
sudo pcs resource op defaults interval=30s

设置资源粘性

sudo pcs resource meta WebGroup resource-stickiness=100

启用 STONITH (生产环境推荐)

sudo pcs property set stonith-enabled=true
# 然后配置适当的 STONITH 设备

验证集群状态

sudo pcs status
sudo pcs cluster status
sudo pcs resource show

故障排除

  1. 如果资源无法启动,检查日志:

    journalctl -xe
    
  2. 验证 Apache 配置:

    apachectl configtest
    
  3. 检查集群通信:

    sudo corosync-cfgtool -s
    

通过以上步骤,您已经在 RHEL 9/8 上成功设置了高可用性 Apache 集群。这个配置可以确保在一个节点故障时,Web 服务会自动切换到另一个节点,保证服务的持续可用性。