在Linux上搭建PostgreSQL集群,通常指的是使用Patroni或Repmgr等工具来实现高可用性和故障转移。以下是使用Patroni搭建PostgreSQL集群的基本步骤:
sudo apt-get update
sudo apt-get install -y python3-pip postgresql postgresql-contrib patroni
编辑每个节点的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
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
host replication replicator 192.168.1.1/32 md5 # 替换为实际的IP地址
在第一个节点上初始化数据库:
sudo systemctl start postgresql
sudo -u postgres psql -c "CREATE USER replicator WITH REPLICATION PASSWORD 'your_password' LOGIN;"
sudo -u postgres psql -c "CREATE DATABASE your_database OWNER replicator;"
在每个节点上创建Patroni配置文件/etc/patroni.yml。
节点1 (master)
scope: postgresql
name: master
namespace: /db
restapi:
listen: 0.0.0.0:8008
connect_address: 192.168.1.1 # 替换为实际的IP地址
etcd:
host: 192.168.1.2 # 替换为实际的etcd IP地址
port: 2379
scheme: http
bootstrap:
dcs:
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_wal_senders: 10
wal_keep_segments: 64
hot_standby: on
max_connections: 100
shared_buffers: 25% of total RAM
work_mem: 4MB
maintenance_work_mem: 128MB
effective_cache_size: 75% of total RAM
checkpoint_completion_target: 0.9
wal_buffers: 16MB
default_statistics_target: 100
random_page_cost: 1.1
effective_io_concurrency: 2
synchronous_commit: remote_write
synchronous_standby_names: '*'
hot_standby_feedback: on
max_replication_slots: 4
max_wal_size: 2GB
min_wal_size: 800MB
replication:
listen: 0.0.0.0:5432
connect_address: 192.168.1.1 # 替换为实际的IP地址
slot_name: pgsql_master_slot
authentication:
username: replicator
password: your_password
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false
节点2 (standby)
scope: postgresql
name: standby
namespace: /db
restapi:
listen: 0.0.0.0:8009
connect_address: 192.168.1.2 # 替换为实际的IP地址
etcd:
host: 192.168.1.2 # 替换为实际的etcd IP地址
port: 2379
scheme: http
bootstrap:
dcs:
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_wal_senders: 10
wal_keep_segments: 64
hot_standby: on
max_connections: 100
shared_buffers: 25% of total RAM
work_mem: 4MB
maintenance_work_mem: 128MB
effective_cache_size: 75% of total RAM
checkpoint_completion_target: 0.9
wal_buffers: 16MB
default_statistics_target: 100
random_page_cost: 1.1
effective_io_concurrency: 2
synchronous_commit: remote_write
synchronous_standby_names: 'master'
hot_standby_feedback: on
max_replication_slots: 4
max_wal_size: 2GB
min_wal_size: 800MB
replication:
listen: 0.0.0.0:5433
connect_address: 192.168.1.1 # 替换为实际的IP地址
slot_name: pgsql_standby_slot
authentication:
username: replicator
password: your_password
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false
在每个节点上启动Patroni服务:
sudo systemctl start patroni
sudo systemctl enable patroni
使用Patroni提供的API来验证集群状态:
curl http://192.168.1.1:8008/postgres
curl http://192.168.1.2:8009/postgres
通过以上步骤,你应该能够在Linux上成功搭建一个PostgreSQL集群。