在CentOS上部署PostgreSQL集群,通常可以使用Patroni或Repmgr等工具来实现高可用性和故障转移。以下是使用Patroni和Repmgr进行PostgreSQL集群部署的基本步骤:
安装必要的软件包:
sudo yum install -y postgresql12-server patroni
配置PostgreSQL:
编辑/var/lib/pgsql/12/data/postgresql.conf和/var/lib/pgsql/12/data/pg_hba.conf文件,确保配置了适当的网络访问和认证方式。
初始化Patroni:
创建一个Patroni配置文件/etc/patroni.yml,示例配置如下:
scope: postgresql
namespace: /db/
name: pg1
restapi:
listen: 0.0.0.0:8008
connect_address: 127.0.0.1:8008
etcd:
host: 127.0.0.1:2379
ttl: 30
retry_timeout: 10
max_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true
use_slots: true
parameters:
wal_level: replica
max_connections: 100
hot_standby: on
postgresql:
listen: 0.0.0.0:5432
connect_address: 127.0.0.1:5432
data_dir: /var/lib/pgsql/12/data
pg_hba:
- host replication replicator 127.0.0.1/32 md5
- host all all 0.0.0.0/0 md5
authentication:
replication:
username: replicator
password: your_replicator_password
startup:
pgdata: /var/lib/pgsql/12/data
timeout: 300
retries: 5
启动Patroni:
sudo systemctl enable patroni
sudo systemctl start patroni
验证集群状态: 访问Patroni的REST API来检查集群状态:
curl http://127.0.0.1:8008/v2/cluster
安装必要的软件包:
sudo yum install -y postgresql12-server repmgr4
配置PostgreSQL:
编辑/var/lib/pgsql/12/data/postgresql.conf和/var/lib/pgsql/12/data/pg_hba.conf文件,确保配置了适当的网络访问和认证方式。
初始化Repmgr: 初始化Repmgr并配置主节点:
sudo repmgr -f /etc/repmgr.conf -d /var/lib/pgsql/12/data -D /var/log/repmgr/ -e local
sudo systemctl enable repmgrd
sudo systemctl start repmgrd
配置从节点:
在从节点上编辑/etc/repmgr.conf文件,设置node_name、connection_string和primary_conninfo等参数。
启动Repmgr: 在从节点上启动Repmgr服务:
sudo systemctl enable repmgrd
sudo systemctl start repmgrd
验证集群状态: 使用Repmgr命令检查集群状态:
sudo repmgr -f /etc/repmgr.conf node status
通过以上步骤,你可以在CentOS上使用Patroni或Repmgr实现PostgreSQL集群的高可用性和故障转移。根据具体需求选择合适的工具,并根据实际情况调整配置。