温馨提示×

Linux PostgreSQL如何进行集群搭建

小樊
50
2025-08-17 16:36:17
栏目: 云计算

在Linux上搭建PostgreSQL集群,通常指的是使用Patroni或Repmgr等工具来实现高可用性和故障转移。以下是使用Patroni搭建PostgreSQL集群的基本步骤:

前提条件

  1. 操作系统:确保你有一个支持的Linux发行版(如Ubuntu、CentOS等)。
  2. PostgreSQL:安装PostgreSQL数据库。
  3. Python:Patroni需要Python环境。
  4. 防火墙:配置防火墙以允许必要的端口通信。

步骤

1. 安装必要的软件包

sudo apt-get update
sudo apt-get install -y python3-pip postgresql postgresql-contrib patroni

2. 配置PostgreSQL

编辑每个节点的postgresql.confpg_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地址

3. 初始化数据库

在第一个节点上初始化数据库:

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;"

4. 配置Patroni

在每个节点上创建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

5. 启动Patroni

在每个节点上启动Patroni服务:

sudo systemctl start patroni
sudo systemctl enable patroni

6. 验证集群状态

使用Patroni提供的API来验证集群状态:

curl http://192.168.1.1:8008/postgres
curl http://192.168.1.2:8009/postgres

注意事项

  • 确保etcd集群已经正确配置并运行。
  • 根据实际需求调整配置文件中的参数。
  • 在生产环境中,建议使用SSL加密通信。

通过以上步骤,你应该能够在Linux上成功搭建一个PostgreSQL集群。

0