在Debian上实现PostgreSQL集群部署可以通过多种方式,其中最常见的是使用Patroni和Pacemaker来管理高可用性和故障转移。以下是一个基本的步骤指南:
在所有节点上执行以下命令来安装Patroni和Pacemaker:
sudo apt-get update
sudo apt-get install -y pacemaker patroni postgresql-12-pgpool
在每个节点上编辑PostgreSQL配置文件/etc/postgresql/12/main/postgresql.conf和pg_hba.conf,确保它们允许集群内的通信。
例如,在postgresql.conf中添加或修改以下行:
listen_addresses = '*' # 或者指定具体的IP地址
wal_level = replica
max_wal_senders = 10
wal_keep_segments = 64
hot_standby = on
在pg_hba.conf中添加以下行以允许节点间的认证:
host replication replicator <其他节点的IP>/32 md5
host all all 0.0.0.0/0 md5
在一个节点上初始化数据库集群:
sudo systemctl stop postgresql
sudo pg_createcluster 12 main --start --no-password
sudo systemctl start postgresql
在每个节点上创建Patroni配置文件/etc/patroni.yml。以下是一个示例配置:
scope: postgresql
name: node1 # 每个节点的名称应该不同
namespace: /db
data_dir: /var/lib/postgresql/12/main
pg_hba:
- host replication replicator <其他节点的IP>/32 md5
- host all all 0.0.0.0/0 md5
authentication:
replication:
username: replicator
password: <复制用户的密码>
superuser:
username: postgres
password: <超级用户的密码>
restapi:
listen: 0.0.0.0:8008
connect_address: <当前节点的IP>:8008
etcd:
host: <etcd服务器的IP>:2379
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true
use_slots: true
parameters:
wal_level: replica
max_connections: 100
hot_standby: on
在每个节点上运行Patroni来启动集群:
sudo patroni /etc/patroni.yml
你可以使用以下命令来验证集群的状态:
sudo systemctl status patroni
或者访问Patroni的REST API来获取集群状态:
curl http://<当前节点的IP>:8008/postgres
通过以上步骤,你应该能够在Debian上成功部署一个PostgreSQL集群。根据具体需求,你可能还需要进行更多的配置和优化。