centos

centos pgadmin高可用性配置

小樊
55
2025-07-18 22:15:51
栏目: 智能运维

在CentOS上配置PostgreSQL的高可用性,可以使用多种方法,其中最常见的是使用Patroni和Pgpool-II。以下是使用Patroni和Pgpool-II配置高可用性的基本步骤:

使用Patroni和Pgpool-II配置高可用性

1. 安装必要的软件包

首先,确保你的CentOS系统已经安装了PostgreSQL、Patroni和Pgpool-II。

sudo yum install postgresql-server patroni pgpool2

2. 配置PostgreSQL

编辑PostgreSQL的配置文件/var/lib/pgsql/data/postgresql.conf/var/lib/pgsql/data/pg_hba.conf,确保它们支持复制和高可用性。

postgresql.conf

listen_addresses = '*'
wal_level = replica
max_wal_senders = 10
wal_keep_segments = 64
hot_standby = on

pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    replication     replicator      0.0.0.0/0               md5
host    all             all             0.0.0.0/0               md5

3. 初始化复制

在主节点上初始化复制。

sudo systemctl start postgresql
sudo -u postgres psql -c "CREATE USER replicator WITH REPLICATION PASSWORD 'your_password' LOGIN;"
sudo -u postgres psql -c "ALTER USER replicator WITH SUPERUSER;"

在从节点上,停止PostgreSQL服务并编辑/var/lib/pgsql/data/recovery.conf文件。

sudo systemctl stop postgresql
echo "standby_mode = 'on'" | sudo tee /var/lib/pgsql/data/recovery.conf
echo "primary_conninfo = 'host=master_ip port=5432 user=replicator password=your_password'" | sudo tee -a /var/lib/pgsql/data/recovery.conf
echo "restore_command = 'cp /var/lib/pg_xlog/archive/%f %p'" | sudo tee -a /var/lib/pgsql/data/recovery.conf

将主节点的数据目录复制到从节点。

sudo rsync -avz /var/lib/pgsql/data/ /var/lib/pgsql/data/

启动从节点上的PostgreSQL服务。

sudo systemctl start postgresql

4. 安装和配置Patroni

在所有节点上安装Patroni。

sudo yum install python3-pip
sudo pip3 install patroni

创建Patroni配置文件/etc/patroni.yml

master节点

scope: postgres
name: master
namespace: /db/
restapi:
  listen: 0.0.0.0:8008
  connect_address: master_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_wal_senders: 10
      wal_keep_segments: 64
      hot_standby: on
    data_dir: /var/lib/pgsql/data
    replication:
      listen: 0.0.0.0:5432
      connect_address: master_ip:5432
      replication_role: replica
      triggers:
        - name: pg_hba_reload
          type: WALReceiver
          trigger: 'pg_hba_reload()'
          module_path: custom
          module_call: reload_hba
      slots: 1
    authentication:
      replication:
        username: replicator
        password: your_password
      superuser:
        username: postgres
        password: your_password

standby节点

scope: postgres
name: standby
namespace: /db/
restapi:
  listen: 0.0.0.0:8008
  connect_address: standby_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_wal_senders: 10
      wal_keep_segments: 64
      hot_standby: on
    data_dir: /var/lib/pgsql/data
    replication:
      listen: 0.0.0.0:5432
      connect_address: master_ip:5432
      replication_role: standby
      triggers:
        - name: pg_hba_reload
          type: WALReceiver
          trigger: 'pg_hba_reload()'
          module_path: custom
          module_call: reload_hba
      slots: 1
    authentication:
      replication:
        username: replicator
        password: your_password
      superuser:
        username: postgres
        password: your_password

在所有节点上启动Patroni。

sudo patroni /etc/patroni.yml

5. 安装和配置Pgpool-II

在所有节点上安装Pgpool-II。

sudo yum install pgpool2

创建Pgpool-II配置文件/etc/pgpool2/pgpool.conf

backend_hostname0 = 'master_ip'
backend_port0 = 5432
backend_weight0 = 1

backend_hostname1 = 'standby_ip'
backend_port1 = 5432
backend_weight1 = 1

listen_addresses = '*'
port = 9999
auth_type = md5
auth_provider = 1
auth_file = '/etc/pgpool2/pool_passwd'
load_balance_mode = on
failover_command = '/usr/bin/pg_ctl promote -D /var/lib/pgsql/data'
recovery_command = '/usr/bin/pg_ctl promote -D /var/lib/pgsql/data'

创建密码文件/etc/pgpool2/pool_passwd

'master_ip' 'replicator' 'your_password'
'standby_ip' 'replicator' 'your_password'

设置密码文件权限。

sudo chmod 600 /etc/pgpool2/pool_passwd

启动Pgpool-II。

sudo systemctl start pgpool2

验证配置

确保所有节点上的Patroni和Pgpool-II都在运行,并且PostgreSQL集群正常工作。

sudo systemctl status patroni
sudo systemctl status pgpool2

通过访问Pgpool-II的监听地址和端口(例如http://your_ip:9999),你可以查看集群的状态和健康状况。

通过以上步骤,你可以在CentOS上配置一个高可用的PostgreSQL集群,使用Patroni管理集群状态,使用Pgpool-II进行负载均衡和故障转移。

0
看了该问题的人还看了