以下是在Debian上配置PostgreSQL集群的步骤(以Patroni + etcd为例):
确保所有节点使用相同的Debian版本,且节点间网络互通。
在所有节点上安装PostgreSQL、Patroni和etcd:
sudo apt update
sudo apt install -y postgresql postgresql-contrib python3-pip
pip3 install patroni[etcd]
sudo apt install etcd
编辑/etc/etcd/etcd.conf,设置集群信息,例如:
name: node1
data-dir: /var/lib/etcd
listen-peer-urls: http://0.0.0.0:2380
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://<节点IP>:2379
initial-cluster: node1=http://<节点IP>:2380,node2=http://<节点IP>:2380
initial-cluster-token: pg-cluster
initial-cluster-state: new
启动etcd服务:
sudo systemctl start etcd
sudo systemctl enable etcd
在每个节点上创建/etc/patroni.yml,示例配置如下:
scope: postgres-cluster
namespace: /db/
name: node1 # 每个节点名称需唯一
restapi:
listen: 0.0.0.0:8008
connect_address: <节点IP>:8008
etcd:
hosts: [<节点IP1>:2379,<节点IP2>:2379]
ttl: 30
retry_timeout: 10
postgresql:
listen: 0.0.0.0:5432
connect_address: <节点IP>:5432
data_dir: /var/lib/postgresql/13/main
bin_dir: /usr/lib/postgresql/13/bin
pg_hba:
- host replication replicator 0.0.0.0/0 md5
- host all all 0.0.0.0/0 md5
authentication:
replication:
username: replicator
password: your_password
superuser:
username: postgres
password: your_password
parameters:
wal_level: replica
max_connections: 100
hot_standby: on
在主节点上执行:
sudo patroni /etc/patroni.yml
使用以下命令查看集群状态:
sudo patroni --config /etc/patroni.yml status