Pacemaker+Corosync+PostgreSQL集群故障测试和解决方案

本文详细介绍了在Pacemaker+Corosync环境下,PostgreSQL集群遇到的各种故障,包括单节点和两节点故障,以及Pacemaker和Corosync自身的故障。针对每种故障,提供了详细的解决步骤,确保集群在故障发生时仍能保持服务的可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

150555_H4rD_2426299.jpg

· 2015年度PG大象会报名地址http://postgres2015.eventdove.com/

· PostgreSQL中国社区 http://postgres.cn/index.php/home 

· PostgreSQL专业1群: 3336901(已满)

· PostgreSQL专业2群: 100910388

· PostgreSQL专业3群: 150657323


· PostgresChina微信公众号     PostgreSQL用户会微博二维码

150623_gb0K_2426299.png150645_vlYZ_2426299.png

一、环境
$ cat /etc/redhat-release 
CentOS Linux release 7.0.1406 (Core) 
$ uname -a
Linux zhaopin-5-90 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ sudo pcs status
Cluster name: pgcluster
WARNING: corosync and pacemaker node names do not match (IPs used in setup?)
Last updated: Tue Oct 20 11:02:08 2015Last change: Tue Oct 20 10:57:37 2015 by root via crm_attribute on zhaopin-5-92
Stack: corosync
Current DC: zhaopin-5-92 (version 1.1.13-a14efad) - partition with quorum
3 nodes and 5 resources configured
Online: [ zhaopin-5-90 zhaopin-5-91 zhaopin-5-92 ]
Full list of resources:
 Master/Slave Set: pgsql-master [pgsql]
     Masters: [ zhaopin-5-92 ]
     Slaves: [ zhaopin-5-90 zhaopin-5-91 ]
 Resource Group: master-group
     vip-master(ocf::heartbeat:IPaddr2):Started zhaopin-5-92
 Resource Group: slave-group
     vip-slave(ocf::heartbeat:IPaddr2):Started zhaopin-5-90
PCSD Status:
  zhaopin-5-90 (172.17.5.90): Online
  zhaopin-5-91 (172.17.5.91): Online
  zhaopin-5-92 (172.17.5.92): Online
Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled
node1: 172.17.5.90
node2: 172.17.5.91
node3: 172.17.5.92
node4: 172.17.5.93 应急

vip-master: 172.17.5.99
vip-slave:  172.17.5.98


二、PostgreSQL故障
1. 单节点故障

1)master故障

在node3上执行:

$ ps aux | grep postgres | grep -v grep
postgres  29482  0.0  0.2 103216  7900 ?        S    10:57   0:00 /usr/bin/postgres -D /data/postgresql/data -c config_file=/data/postgresql/data/postgresql.conf
postgres  29524  0.0  0.0 103216  1980 ?        Ss   10:57   0:00 postgres: checkpointer process   
postgres  29525  0.0  0.0 103216  1988 ?        Ss   10:57   0:00 postgres: writer process   
postgres  29526  0.0  0.0 103216  1896 ?        Ss   10:57   0:00 postgres: wal writer process   
postgres  29527  0.0  0.0 104044  3008 ?        Ss   10:57   0:00 postgres: autovacuum launcher process   
postgres  29528  0.0  0.0  88912  1792 ?        Ss   10:57   0:00 postgres: archiver process   last was 00000001000000000000000E
postgres  29529  0.0  0.0  89024  1920 ?        Ss   10:57   0:00 postgres: stats collector process   
postgres  29790  0.0  0.0 104060  3208 ?        Ss   10:57   0:00 postgres: wal sender process postgres 172.17.5.90(35734) streaming 0/F0000E0
postgres  29791  0.0  0.0 104192  3392 ?        Ss   10:57   0:00 postgres: wal sender process postgres 172.17.5.91(60722) streaming 0/F0000E0
$ psql -U postgres
psql (9.2.13)
Type "help" for help.
postgres=# select * from pg_stat_replication ;
  pid  | usesysid | usename  | application_name | client_addr | client_hostname | client_port |         backend_start         |   state   | sent_location | write_location | flush_location | replay_location | sync_priority | sync_state 
-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-----------+---------------+----------------+----------------+-----------------+---------------+------------
 29790 |       10 | postgres | zhaopin-5-90     | 172.17.5.90 |                 |       35734 | 2015-10-20 02:57:25.718047+00 | streaming | 0/F0000E0     | 0/F0000E0      | 0/F0000E0      | 0/F0000E0       |             1 | sync
 29791 |       10 | postgres | zhaopin-5-91     | 172.17.5.91 |                 |       60722 | 2015-10-20 02:57:25.718566+00 | streaming | 0/F0000E0     | 0/F0000E0      | 0/F0000E0      | 0/F0000E0       |             2 | potential
(2 rows)
postgres=# \q

杀掉PostgreSQL的进程

$ sudo killall postgres
$ ps aux | grep postgres | grep -v grep

PostgreSQL的进程已经没有了

$ sudo pcs status
Cluster name: pgcluster
WARNING: corosync and pacemaker node names do not match (IPs used in setup?)
Last updated: Tue Oct 20 11:11:54 2015Last change: Tue Oct 20 11:10:31 2015 by root via crm_attribute on zhaopin-5-90
Stack: corosync
Current DC: zhaopin-5-92 (version 1.1.13-a14efad) - partition with quorum
3 nodes and 5 resources configured
Online: [ zhaopin-5-90 zhaopin-5-91 zhaopin-5-92 ]
Full list of resources:
 Master/Slave Set: pgsql-master [pgsql]
     Masters: [ zhaopin-5-90 ]
     Slaves: [ zhaopin-5-91 ]
     Stopped: [ zhaopin-5-92 ]
 Resource Group: master-group
     vip-master(ocf::heartbeat:IPaddr2):Started zhaopin-5-90
 Resource Group: slave-group
     vip-slave(ocf::heartbeat:IPaddr2):Started zhaopin-5-91
Failed Actions:
* pgsql_start_0 on zhaopin-5-92 'unknown error' (1): call=33, status=complete, exitreason='My data may be inconsistent. You have to remove /var/lib/pgsql/tmp/PGSQL.lock file to force start.',
    last-rc-change='Tue Oct 20 11:10:14 2015', queued=0ms, exec=389ms
PCSD Status:
  zhaopin-5-90 (172.17.5.90): Online
  zhaopin-5-91 (172.17.5.91): Online
  zhaopin-5-92 (172.17.5.92): Online
Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled

查看集群状态可见,master和vip-master已经切换到node1上了


在node1上执行:

$ psql -U postgres
psql (9.2.13)
Type "help" for help.
postgres=# select * from pg_stat_replication ;
  pid  
### 配置CorosyncPacemaker实现PostgreSQL高可用性集群 为了建立一个基于CorosyncPacemakerPostgreSQL高可用性(HA)集群,需遵循一系列特定配置步骤来确保数据库服务能够在节点间自动切换并保持数据一致性。 #### 安装必要的软件包 安装过程中涉及的主要组件包括`corosync`, `pacemaker` `postgresql`。这些工具通常可以通过Linux发行版的标准仓库获取[^1]。 #### 初始化Corosync通信层 Corosync作为底层消息传递库负责各节点间的通讯协调工作。其配置文件一般位于 `/etc/corosync/corosync.conf` 中,主要参数设置如下: - **totem**: 定义环回地址、接口以及心跳超时时间等网络属性; - **nodelist**: 列举参与集群的所有成员机器信息; ```bash # Example of totem section in corosync.conf totem { version: 2 secauth: off interface { ringnumber: 0 bindnetaddr: 192.168.1.0 mcastport: 5405 ttl: 1 } } ``` #### 设置Pacemaker资源管理器 一旦Corosync启动成功,则可以继续配置Pacemaker来进行更高级别的HA策略定义。通过命令行工具如`pcs`(ClusterLabs),能够简化此过程中的许多操作,比如创建、修改或删除各种类型的资源服务约束条件。 对于PostgreSQL而言,在Pacemaker环境中应将其视为一种可迁移的服务单元——即所谓的克隆集(clone set)[^1]。这意味着即使某个实例发生故障,另一个备用副本也会立即接管业务请求而不会造成任何中断现象。 具体来说,需要执行以下几项任务: - 创建虚拟IP(Virtual IP, VIP)用于对外提供统一访问入口; - 将PostgreSQL进程注册成受控对象以便于监控状态变化情况; - 设定主备模式下的同步复制机制以保障事务持久性读写分离特性。 ```bash # Add PostgreSQL as a resource under Pacemaker control using pcs command line tool. pcs resource create pgsql ocf:heartbeat:pgsql \ params config="/var/lib/pgsql/data/postgresql.conf" \ op monitor interval="30s" ``` #### 数据库层面调整 最后一步是在PostgreSQL内部做出相应改动支持上述架构设计需求。这主要包括但不限于开启流式复制(streaming replication)功能,并指定合适的恢复目标(recovery target)位置路径。 此外,建议定期备份整个环境的相关配置文档资料,便于日后维护升级或者排查问题之用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值