在Debian系統上搭建PostgreSQL集群,通常指的是使用Patroni和Corosync/Pacemaker來管理高可用性。以下是搭建PostgreSQL集群的基本步驟:
首先,更新你的包列表并安裝必要的軟件包:
sudo apt update
sudo apt install -y corosync pacemaker postgresql-13-pgpool2 patroni
編輯Corosync配置文件 /etc/corosync/corosync.conf:
sudo nano /etc/corosync/corosync.conf
添加以下內容:
totem {
version: 2
cluster_name: pg_cluster
transport: udpu
}
nodelist {
node {
ring0_addr: your_node_ip
nodeid: 1
}
node {
ring0_addr: another_node_ip
nodeid: 2
}
}
quorum {
provider: corosync_votequorum
}
logging {
to_logfile: yes
logfile: /var/log/corosync/corosync.log
to_syslog: yes
}
啟動并啟用Corosync服務:
sudo systemctl start corosync
sudo systemctl enable corosync
編輯Pacemaker配置文件 /etc/pacemaker/cib.xml:
sudo nano /etc/pacemaker/cib.xml
確保配置文件中包含你的節點和資源信息。
啟動并啟用Pacemaker服務:
sudo systemctl start pacemaker
sudo systemctl enable pacemaker
創建Patroni配置文件 /etc/patroni.yml:
sudo nano /etc/patroni.yml
添加以下內容:
scope: postgresql
name: your_node_name
namespace: /db
data_dir: /var/lib/postgresql/13/main
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_replicator_password
superuser:
username: postgres
password: your_superuser_password
restapi:
listen: 0.0.0.0
connect_address: your_node_ip:8008
bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 30
maximum_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
connect_address: your_node_ip:5432
data_dir: /var/lib/postgresql/13/main
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_replicator_password
superuser:
username: postgres
password: your_superuser_password
parameters:
wal_level: replica
max_connections: 100
hot_standby: on
tag:
version: 2
name: your_node_name
啟動Patroni服務:
sudo systemctl start patroni
sudo systemctl enable patroni
檢查Patroni和Pacemaker的狀態:
sudo systemctl status patroni
sudo systemctl status pacemaker
使用 crm_mon 命令查看集群狀態:
sudo crm_mon
通過以上步驟,你應該能夠在Debian系統上成功搭建一個PostgreSQL集群。