patroni
patroni 依赖第三方分布式存储, 可以托管 postgres 的配置管理和启停, 以及主备切换.
安装
- 安装epel源,安装pyhton3
yum -y install epel-release
yum -y install python3 python3-devel
- pip安装patroni
yum install -y python3-psycopg2
pip3 install patroni[etcd3,psycopg2]
[root@data8 data]
Config is empty.
pip3 卸载命令: pip3 uninstall xxxx
- 生成patroni配置文件
patroni --generate-sample-config /etc/patroni/patroni.yml
- patroni 配置文件样例
scope: 'pg'
namespace: '/service'
name: data7
log:
format: '%(asctime)s %(levelname)s: %(message)s'
level: INFO
max_queue_size: 1000
traceback_level: ERROR
type: plain
restapi:
connect_address: 172.16.20.152:8008
listen: 172.16.20.152:8008
etcd3:
hosts: 172.16.20.152:2379,172.16.20.153:2379,172.16.20.154:2379
bootstrap:
dcs:
loop_wait: 10
retry_timeout: 10
ttl: 30
postgresql:
parameters:
hot_standby: 'on'
max_connections: 200
max_locks_per_transaction: 64
max_prepared_transactions: 0
max_replication_slots: 10
max_wal_senders: 10
max_worker_processes: 8
track_commit_timestamp: 'off'
wal_keep_size: 128MB
wal_level: replica
wal_log_hints: 'on'
use_pg_rewind: true
use_slots: true
initdb:
- data-checksums
- encoding: UTF8
postgresql:
authentication:
replication:
password: 'replicator'
username: replicator
rewind:
password: 'rewind_user'
username: rewind_user
superuser:
password: '123456'
username: postgres
connect_address: 172.16.20.152:5432
data_dir: '/home/pgsql/data/'
bin_dir: '/usr/local/pgsql/bin/'
listen: 0.0.0.0:5432
parameters:
password_encryption: scram-sha-256
pg_hba:
- host all all 0.0.0.0/0 md5
- host replication replicator 172.16.20.0/24 md5
tags:
clonefrom: true
failover_priority: 1
noloadbalance: false
nostream: false
nosync: false
脚本示例:
#!/bin/bash
curl -X GET http://172.16.20.35:8085/ping/152/on_start
注意#!/bin/bash
解析器一定要写,不然patroni执行报错.
- 注册 patroni service
创建文件/usr/lib/systemd/system/patroni.service
[Unit]
Description=Runners to orchestrate a high-availability PostgreSQL
After=syslog.target network.target
[Service]
Type=simple
User=postgres
Group=postgres
ExecStart=/usr/local/bin/patroni /etc/patroni/patroni.yml
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
TimeoutSec=30
Restart=no
[Install]
WantedBy=multi-user.target
- 启动patroni
systemctl start patroni
systemctl status patroni
- 查询patroni节点状态
[root@data7 data]
+ Cluster: pg (7384360501716690603) -----------+----+-----------+----------------------+
| Member | Host | Role | State | TL | Lag in MB | Tags |
+--------+---------------+---------+-----------+----+-----------+----------------------+
| data7 | 172.16.20.152 | Leader | running | 1 | | clonefrom: true |
| | | | | | | failover_priority: 1 |
+--------+---------------+---------+-----------+----+-----------+----------------------+
| data8 | 172.16.20.153 | Replica | streaming | 1 | 0 | clonefrom: true |
| | | | | | | failover_priority: 1 |
+--------+---------------+---------+-----------+----+-----------+----------------------+
| data9 | 172.16.20.154 | Replica | streaming | 1 | 0 | clonefrom: true |
| | | | | | | failover_priority: 1 |
+--------+---------------+---------+-----------+----+-----------+----------------------+
错误
SCRAM authentication requires libpq version 10 or above
这个错误原因是 centos7 默认安装的 postgresql-libs
版本太低,需要升级.
[root@data8 data]
postgresql-libs.x86_64 9.2.24-9.el7_9 @updates
yum remove -y postgresql-libs.x86_64
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
- 安装最新的,注意:
centos
最高只支持 15
版本
yum install postgresql15-libs -y
[root@data8 data]
postgresql15-libs.x86_64 15.7-1PGDG.rhel7 @pgdg15
FATAL: Patroni requires psycopg2>=2.5.4, psycopg2-binary, or psycopg>=3.0.0
需要 重新安装
yum install -y python3-psycopg2