使用 Corosync + Pacemaker 实现秒级切换?

解读

面试官问“秒级切换”,并不是让你背命令,而是考察:

  1. 对 Pacemaker 资源模型与故障检测机制的理解深度;
  2. 能否把 Puppet 的“声明式”思路落地到高可用集群,做到“代码即架构”;
  3. 在国内机房常见的“云主机+物理机”混合、网络抖动、STONITH 设备受限场景下,如何既保证秒级切换,又避免脑裂。

一句话:给你 3 台 CentOS 7/8 节点,用 Puppet 把 Corosync+Pacemaker 搭成可秒级切换的 Web 集群,你怎么写 manifests、怎么调参数、怎么验收?

知识点

  1. 资源代理(OCF/systemd)与 op monitor 间隔:monitor 间隔 ≤5 s 才能实现“秒级”发现故障。
  2. 仲裁策略:国内云环境常关 multicast,需改用 knet + udpu,两节点时必须加 qdevice(阿里云可用轻量版 qnetd,物理机可复用交换机仲裁)。
  3. 资源粘性(resource-stickiness)与迁移阈值(migration-threshold):防止“乒乓”回切,确保一次故障只切一次。
  4. STONITH 拓扑:公有云无 IPMI,用阿里“云助手重启”或腾讯“CVM 重启 API”作为 fence_aliyun/fence_tencent;物理机用 IPMI 或智能 PDU。
  5. Puppet 高可用模块生态:
    • puppetlabs-corosync:官方,支持 pcs provider,但 2.x 以后才支持 EL8;
    • herculesteam/ha_cluster:社区版,pcs+crmsh 双后端,国内用得多;
    • 自封装:用 augeas+pcs 命令,写 pure Puppet DSL,方便审计。
  6. 秒级验收指标:kill -9 主节点上的 httpd,客户端 302 重定向到备节点,业务中断时间 ≤1 s(用 curl -w "%{time_total}" 连续探测 100 次,99 分位 ≤1.2 s)。

答案

给出一套可直接落地的 Puppet 方案,分为“集群层”“资源层”“验收层”三部分,全部用 Puppet DSL 描述,版本锁定 CentOS 8 Stream + Puppet 7。

  1. 集群层(site/profile/manifests/corosync.pp)
class profile::corosync {
  package { ['corosync', 'pacemaker', 'pcs', 'fence-agents-all']: ensure => installed }

  service { 'pcsd':
    ensure => running,
    enable => true,
  }

  # 一次性认证,只在 bootstrap 节点执行
  exec { 'pcs cluster auth':
    command => "/usr/sbin/pcs cluster auth node1 node2 node3 -u hacluster -p ${lookup('corosync::password')} --force",
    unless  => '/usr/sbin/pcs cluster status',
    require => Service['pcsd'],
  }

  # 生成并下发 corosync.conf,使用 knet + udpu,rrp_mode=passive 抗抖动
  file { '/etc/corosync/corosync.conf':
    ensure  => file,
    content => epp('profile/corosync.conf.epp', {
      bindnetaddr => $facts['networking']['interfaces']['eth0']['network'],
      nodes       => ['node1','node2','node3'],
    }),
    notify  => Service['corosync'],
  }

  service { ['corosync', 'pacemaker']:
    ensure    => running,
    enable    => true,
    subscribe => File['/etc/corosync/corosync.conf'],
  }

  # 关闭 quorum 时自动自杀,国内两节点场景必关
  exec { 'pcs property set no-quorum-policy=ignore':
    path    => '/usr/sbin',
    unless  => 'pcs property show no-quorum-policy | grep ignore',
    require => Service['pacemaker'],
  }

  # STONITH 云重启,以阿里云为例
  pcs_resource { 'fence_ali':
    ensure   => present,
    resource => 'stonith:fence_aliyun',
    options  => {
      'pcmk_host_map'      => 'node1:i-xxx1;node2:i-xxx2;node3:i-xxx3',
      'access_key'         => lookup('aliyun::access_key'),
      'access_secret'      => lookup('aliyun::secret'),
      'region'             => 'cn-shanghai',
      'action'             => 'reboot',
      'monitor'            => '60s',
    },
    require  => Service['pacemaker'],
  }
}
  1. 资源层(site/profile/manifests/cluster_vip.pp)
class profile::cluster_vip {
  # 虚拟 IP
  pcs_resource { 'cluster_vip':
    ensure   => present,
    resource => 'ocf:heartbeat:IPaddr2',
    options  => {
      'ip'            => '192.168.1.200',
      'cidr_netmask'  => '24',
      'nic'           => 'eth0',
      'monitor'       => '5s',      # 秒级探测
    },
  }

  # Web 资源,systemd 托管
  pcs_resource { 'webserver':
    ensure   => present,
    resource => 'systemd:httpd',
    options  => {
      'monitor' => '5s',
    },
  }

  # 组资源,保证 VIP 与 Web 同节点
  pcs_group { 'web_group':
    ensure    => present,
    resources => ['cluster_vip', 'webserver'],
    options   => {
      'meta' => [
        'resource-stickiness=100',
        'migration-threshold=1',
      ],
    },
  }
}
  1. 验收层(site/profile/manifests/ha_test.pp)
class profile::ha_test {
  package { 'httpd': ensure => installed }

  file { '/var/www/html/index.html':
    ensure  => file,
    content => "<h1>Response from ${facts['networking']['hostname']}</h1>",
    notify  => Service['httpd'],
  }

  service { 'httpd':
    ensure => running,
    enable => false, # 由 pacemaker 托管
  }

  # 本地持续探测脚本,供 CI 调用
  file { '/usr/local/bin/ha_probe.sh':
    mode   => '0755',
    source => 'puppet:///modules/profile/ha_probe.sh',
  }
}

验收步骤(手工或 Jenkins 流水线):

  1. 在客户端 while true; do curl -o /dev/null -s -w "%{time_total}\n" http://192.168.1.200; done
  2. 登录主节点 killall -9 corosync 或 echo c > /proc/sysrq-trigger
  3. 观察 curl 输出,中断时间 ≤1 s 即为通过。

拓展思考

  1. 如果业务要求“零丢包”而非“秒级”,可把 VIP 换成 Keepalived+ECMP 或 BGP Anycast,但 Puppet 侧只需把资源代理换成 ocf:heartbeat:bird 即可,思路一致。
  2. 多云场景下,STONITH 需要链式调用:先调用云 API,再调用物理 PDU;Puppet 用同一个 stonith 资源,通过 params 区分即可。
  3. 大规模集群(>16 节点)时,Corosync 的 knet 压缩线程会成为瓶颈,可在 Puppet 里根据 $facts['processors']['count'] 动态调整 vsft_threads,实现“弹性调参”。
  4. 把上述 manifests 封装成 control-repo,配合 r10k 与 GitLab CI,可实现“Pull Request 即演练”:每提交一次,自动在测试环境触发一次 kill 主节点,秒级切换报告自动回写到 MR,真正做到“高可用即代码”。