
作者:杭州美创科技有限公司
得益于PostgreSQL的开源特性,越来越多的第三方集群管理软件填补了PostgreSQL在集群方面的易用性和可靠性,patroni+etcd提供了一系列的集群管理方案。etcd负责集群状态信息的存放,用来联系各个节点,patroni负责为集群提供高可用服务,两者的集合为
PostgreSQL集群提供了故障转移的高可用服务,它不仅配置简单,而且功能丰富:
支持手动和自动故障转移
支持一主多从、级联复制
支持同步、异步模式
支持使用watchdog防止脑裂
前期准备
节点规划。实验过程我们使用一主两从构建一套高可用环境。
主机名 | IP | 端口 | 备注 |
| node1 | 192.168.22.128 | 5432 | 主节点 |
| node2 | 192.168.22.129 | 5432 | 从节点 |
| node3 | 192.168.22.130 | 5432 | 从节点 |
关闭主机防火墙
# systemctl stop firewalld.service# systemctl disable firewalld.service
安装postgresql并搭建流复制环境(此步骤略)
在各个节点上部署etcd
安装必要的依赖包及etcd软件
# yum install -y gcc python-devel epel-release# yum install -y etcd
编辑配置文件(以下列出了需要修改的参数,并以主节点为例)
# 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"
启动etcd集群,并设置开机自启动
# systemctl start etcd# systemctl enable etcd
在各个节点上部署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
删除原2.7版本的软连接,添加新的软链接以使用python3
# rm -f /usr/bin/python# ln -s /usr/local/bin/python3 /usr/bin/python
在各个节点上部署patroni
安装必要的依赖包和patroni软件
# pip3 install psycopg2-binary -i https://mirrors.aliyun.com/pypi/simple/# pip3 install patroni -i https://mirrors.aliyun.com/pypi/simple/
修改patroni配置文件(以主节点为例)
# vim /etc/patroni.ymlscope: pgsqlnamespace: /pgsql/name: pgsql_node2restapi: listen: 192.168.22.128:8008 connect_address: 192.168.22.128:8008etcd: host: 192.168.22.128:2379bootstrap: # this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster # and all other cluster members will use it as a `global configuration` dcs: ttl: 30 loop_wait: 10 retry_timeout: 10 maximum_lag_on_failover: 1048576 master_start_timeout: 300 synchronous_mode: false postgresql: use_pg_rewind: true use_slots: true parameters: listen_addresses: "0.0.0.0" port: 5432 wal_level: logical hot_standby: "on" wal_keep_segments: 100 max_wal_senders: 10 max_replication_slots: 10 wal_log_hints: "on"# archive_mode: "on"# archive_timeout: 1800s# archive_command: gzip < %p > /data/backup/pgwalarchive/%f.gz# recovery_conf:# restore_command: gunzip < /data/backup/pgwalarchive/%f.gz > %ppostgresql: listen: 0.0.0.0:5432 connect_address: 192.168.22.128:5432 data_dir: /pgdata/patr2 bin_dir: /usr/pgsql-12/bin# config_dir: /etc/postgresql/9.6/main authentication: replication: username: repl password: repl superuser: username: postgres password: postgres#watchdog:# mode: automatic # Allowed values: off, automatic, required# device: /dev/watchdog# safety_margin: 5tags: nofailover: false noloadbalance: false clonefrom: false nosync: false
配置patroni服务单元
# vim /etc/systemd/system/patroni.service [Unit]Description=Runners to orchestrate a high-availability PostgreSQLAfter=syslog.target network.target[Service]Type=simpleUser=postgresGroup=postgres#StandardOutput=syslogExecStart=/usr/local/bin/patroni /etc/patroni.ymlExecReload=/bin/kill -s HUP $MAINPIDKillMode=processTimeoutSec=30Restart=no[Install]WantedBy=multi-user.target
启动patroni服务
# systemctl start patroni
当然地,我们也可以直接使用patroni命令来启动patroni服务,配置服务单元是为了更方便使用。
#/usr/local/bin/patroni /etc/patroni.yml > patroni.log 2>&1 &
集群环境使用
查看节点信息
# patronictl -c /etc/patroni.yml list+ Cluster: pgsql (6972099274779350082)+------+---------+----+-----------+| Member | Host | Role | State | TL | Lag in MB |+-------------+---------------------+--------+---------+----+-----------+| pgsql_node1 | 192.168.22.128:5432 | Leader | running | 3 | || pgsql_node2 | 192.168.22.129:5432 | | running | 3 | 0 || pgsql_node3 | 192.168.22.130:5432 | | running | 3 | 0 |+-------------+---------------------+--------+---------+----+-----------+
手动切换主从,选择某一可用的从节点,使其成为主节点角色
# patronictl -c /etc/patroni.yml switchoverMaster [pgsql_node1]: pgsql_node1Candidate ['pgsql_node2', 'pgsql_node3'] []: pgsql_node2When should the switchover take place (e.g. 2021-06-20T11:42 ) [now]: now
查看集群状态
# patronictl -c /etc/patroni.yml list+ Cluster: pgsql (6972099274779350082)+------+---------+----+-----------+| Member | Host | Role | State | TL | Lag in MB |+-------------+---------------------+--------+---------+----+-----------+| pgsql_node1 | 192.168.22.128:5432 | | running | 3 | 0 || pgsql_node2 | 192.168.22.129:5432 | Leader | running | 3 | || pgsql_node3 | 192.168.22.130:5432 | | running | 3 | 0 |+-------------+---------------------+--------+---------+----+-----------+
自动切换主从。重启node1节点所在主机。查看集群状态,node2自动提升为主,如果只是关闭节点实例,则patroni会再将数据库服务自动拉起。
# patronictl -c /etc/patroni.yml list+ Cluster: pgsql (6972099274779350082)+------+---------+----+-----------+| Member | Host | Role | State | TL | Lag in MB |+-------------+---------------------+--------+---------+----+-----------+| pgsql_node2 | 192.168.22.129:5432 | Leader | running | 3 | || pgsql_node3 | 192.168.22.130:5432 | | running | 3 | 0 |+-------------+---------------------+--------+---------+----+-----------+
初始化某一节点。当某一节点与主库不同步,或者节点异常运行时,可以使用此方法初始化节点信息以重新加入集群。
# patronictl -c /etc/patroni.yml reinit pgsql+ Cluster: pgsql (6972099274779350082)+------+---------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-------------+---------------------+--------+---------+----+-----------+
| pgsql_node1 | 192.168.22.128:5432 | | running | 3 | 0 |
| pgsql_node2 | 192.168.22.129:5432 | Leader | running | 3 | |
| pgsql_node3 | 192.168.22.130:5432 | | running | 3 | 0 |
+-------------+---------------------+--------+---------+----+-----------+
选择以下需要添加的节点名称:pgsql_node3
你确定要重新初始化成员 pgsql_node3?[y/N]:y
成功:为成员pgsql_node3执行初始化
patroni是基于python开发的模板,etcd集群是按照Raft算法和协议开发的,是一个强一致性的、分布式的key-value数据库。两者的结合相辅相成,使得PostgreSQL集群在使用和维护过程中变得更便捷、更透明。





新闻|Babelfish使PostgreSQL直接兼容SQL Server应用程序

更多新闻资讯,行业动态,技术热点,请关注中国PostgreSQL分会官方网站
https://www.postgresqlchina.com
中国PostgreSQL分会生态产品
https://www.pgfans.cn
中国PostgreSQL分会资源下载站
https://www.postgreshub.cn


点赞、在看、分享、收藏




