PostgreSQL高可用
需求描述
我们有两台centos7的数据库主机A、B。要对A、B实现双机热备,A作为数据库master主机,对外提供读写服务,B作为slave主机能实时同步A的数据。当A发生故障时,B从slave状态切换到master状态,以保证pg数据库能正常对外提供服务。
经过调研,最终我们给出的解决方案是采用pacemaker和corosync组件来实现postgresql的高可用。
所有部署脚本可以下github中找到:https://github.com/qinguanri/pg-ha
环境说明
- 操作系统centos7:
[root@10 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@10 ~]# uname -a
Linux 10.16.93.179 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
postgresql版本:9.2.15
- 2台主机:
角色 | IP |
---|---|
node1 | 10.16.93.179 |
node2 | 10.16.93.182 |
- 分配2个虚IP。我们的数据库对外提供服务时,暴露的是虚IP:
vip-master: 10.16.93.180
vip-slave: 10.16.93.181
配置Linux集群环境
注意:请严格按照下文所给的顺序进行配置
1.安装Pacemaker和Corosync包
需要在所有节点执行:
$ yum install -y pacemaker pcs psmisc policycoreutils-python postgresql-server
2.禁用防火墙
在所有节点执行:
$ systemctl disable firewalld.service
$ systemctl stop firewalld.service
3.启用pcs
在所有节点执行:
$ systemctl start pcsd.service
$ systemctl enable pcsd.service
ln -s '/usr/lib/systemd/system/pcsd.service' '/etc/systemd/system/multi-user.target.wants/pcsd.service'
$ echo hacluster | passwd hacluster --stdin
Changing password for user hacluster.
Changing password for user hacluster.
passwd: all authentication tokens updated successfully.
4.集群认证
在任何一个节点上执行,这里选择node1:
$ pcs cluster auth -u hacluster -p hacluster 10.16.93.179 10.16.93.182
10.16.93.179: Authorized
10.16.93.182: Authorized
5.同步配置
在node1上执行:
$ pcs cluster setup --last_man_standing=1 --name pgcluster 10.16.93.179 10.16.93.182
Shutting down pacemaker/corosync services...
Redirecting to /bin/systemctl stop pacemaker.service
Redirecting to /bin/systemctl stop corosync.service
Killing any remaining services...
Removing all cluster configuration files...
10.16.93.179: Succeeded
10.16.93.182: Succeeded
6.启动集群
在node1上执行。(有时需要稍等十几秒,才能看到命令的输出结果):
$ pcs cluster start --all
172.17.5.90: Starting Cluster...
172.17.5.91: Starting Cluster...
172.17.5.92: Starting Cluster...
7.检验
1)检验corosync
在node1上执行:
$ pcs status corosync
Membership information
----------------------
Nodeid Votes Name
1 1 10.16.93.179 (local)
2 1 10.16.93.182
2)检验pacemaker
root@10 data]# pcs status
Cluster name: pgcluster
WARNING: corosync and pacemaker node names do not match (IPs used in setup?)
Last updated: Mon Apr 4 23:45:48 2016 Last change: Fri Apr 1 09:17:03 2016 by r