作者:杭州美创科技有限公司
得益于PostgreSQL的开源特性,越来越多的第三方集群管理软件填补了PostgreSQL在集群方面的易用性和可靠性,patroni+etcd提供了一系列的集群管理方案。etcd负责集群状态信息的存放,用来联系各个节点,patroni负责为集群提供高可用服务,两者的集合为PostgreSQL集群提供了故障转移的高可用服务,它不仅配置简单,而且功能丰富:
- 支持手动和自动故障转移
- 支持一主多从、级联复制
- 支持同步、异步模式
- 支持使用watchdog防止脑裂
前期准备
节点规划。实验过程我们使用一主两从构建一套高可用环境。
关闭主机防火墙
# systemctl stop firewalld.service # systemctl disable firewalld.service
bash
- 1
- 2
安装postgresql并搭建流复制环境(此步骤略)
在各个节点上部署etcd
安装必要的依赖包及etcd软件
# yum install -y gcc python-devel epel-release # yum install -y etcd
bash
- 1
- 2
编辑配置文件(以下列出了需要修改的参数,并以主节点为例)
# vim /etc/etcd/etcd.conf #[Member] ETCD_DATA_DIR="/var/lib/etcd/node1.etcd" ETCD_LISTEN_PEER_URLS="http://192.168.22.128:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.22.128:2379,http://127.0.0.1:2379" #[Clustering] ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.22.128:2380" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.22.128:2379" ETCD_INITIAL_CLUSTER="node1=http://192.168.22.128:2380,node2=http://192.168.22.129:2380, node3=http://192.168.22.130:2380"
bash
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
启动etcd集群,并设置开机自启动
# systemctl start etcd # systemctl enable etcd
bash
- 1
- 2
在各个节点上部署python3
需要使用高版本的python来使用patroni服务,一般的linux环境内置了2.7版本的python环境,因此我们需要升级python,这里采用源码编译安装方式安装
# wget -c https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz # ./configure # make # make install
bash
- 1
- 2
- 3
- 4
删除原2.7版本的软连接,添加新的软链接以使用python3
# rm -f /usr/bin/python # ln -s /usr/local/bin/python3 /usr/bin/python
bash
- 1
- 2
在各个节点上部署patroni
安装必要的依赖包和patroni软件
# pip3 install psycopg2-binary -i https://mirrors.aliyun.com/pypi/simple/ # pip3 install p
bash
- 1