在现代IT基础设施中,确保服务的高可用性(High Availability, HA)至关重要。对于运行在 Debian 系统上的关键业务应用,使用 Pacemaker 构建集群是一种成熟且可靠的解决方案。本教程将手把手教你如何在 Debian 系统上部署和管理 Pacemaker 集群,即使你是初学者也能轻松上手。
Pacemaker 是一个开源的高可用性集群资源管理器,常与 Corosync 或 Heartbeat 搭配使用。它能自动检测节点或服务故障,并在健康节点上重新启动服务,从而实现“零停机”目标。在 Debian Pacemaker集群 中,Pacemaker 负责资源调度,而 Corosync 负责节点间通信。

本教程假设你有两台运行 Debian 11(Bullseye) 的服务器,分别命名为:
node1.example.com(IP: 192.168.1.10)node2.example.com(IP: 192.168.1.11)确保以下条件满足:
在两台节点上执行以下命令安装所需软件包:
sudo apt updatesudo apt install -y pacemaker corosync crmshCorosync 是集群通信层。我们需要生成配置文件。在 node1 上执行:
sudo corosync-keygen 注意:corosync-keygen 会生成认证密钥,过程中可能需要输入随机字符以增加熵值。编辑 Corosync 配置文件 /etc/corosync/corosync.conf:
totem { version: 2 cluster_name: debian-ha-cluster transport: udpu}nodelist { node { ring0_addr: node1.example.com nodeid: 1 } node { ring0_addr: node2.example.com nodeid: 2 }}quorum { provider: corosync_votequorum expected_votes: 2 two_node: 1}logging { to_logfile: yes logfile: /var/log/corosync/corosync.log to_syslog: yes}将该配置文件复制到 node2:
scp /etc/corosync/corosync.conf root@node2:/etc/corosync/scp /etc/corosync/authkey root@node2:/etc/corosync/在两台节点上启动并启用 Corosync 和 Pacemaker:
sudo systemctl enable --now corosyncsudo systemctl enable --now pacemaker验证集群状态:
sudo pcs status# 或使用 crm 命令sudo crm status如果看到两个节点都在线,说明 Debian HA集群 已成功组建!
我们以 Apache Web 服务器为例,演示如何将其作为集群资源管理。
首先在两台节点上安装 Apache:
sudo apt install -y apache2sudo systemctl stop apache2sudo systemctl disable apache2注意:必须停止并禁用 Apache 的系统服务,否则 Pacemaker 无法接管其控制权。
创建一个虚拟 IP 地址资源(VIP),用于客户端访问:
sudo pcs resource create virtual_ip ocf:heartbeat:IPaddr2 \ ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s创建 Apache 资源:
sudo pcs resource create web_service ocf:heartbeat:apache \ configfile=/etc/apache2/apache2.conf \ statusurl="http://localhost/server-status" \ op monitor interval=1min将两个资源绑定为一个资源组(确保它们在同一节点运行):
sudo pcs resource group add web_group virtual_ip web_service现在,访问 http://192.168.1.100 即可看到 Apache 默认页面。如果当前节点宕机,Pacemaker 会自动将 VIP 和 Apache 切换到另一节点,实现无缝故障转移。
/var/log/pacemaker.log 和 /var/log/corosync/corosync.logpcs resource move web_group node2pcs resource clear web_group通过本教程,你已经掌握了在 Debian 系统上搭建和管理 Pacemaker 高可用集群 的基本技能。无论是 Web 服务、数据库还是自定义应用,都可以通过类似方式实现高可用。希望这篇 Pacemaker教程 能帮助你构建更稳定、可靠的 IT 环境。
关键词回顾:本文重点介绍了 Debian Pacemaker集群、高可用集群配置、Pacemaker教程 和 Debian HA集群 的实践方法。
本文由主机测评网于2025-12-08发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025124797.html