debian

Debian下PostgreSQL集群如何搭建

小樊
50
2025-04-14 09:07:19
栏目: 云计算

在Debian系统上搭建PostgreSQL集群,通常指的是使用Patroni和Corosync/Pacemaker来管理高可用性。以下是搭建PostgreSQL集群的基本步骤:

前提条件

  1. Debian系统:确保你有一个运行Debian的系统。
  2. PostgreSQL:安装PostgreSQL数据库。
  3. Corosync/Pacemaker:用于集群管理和故障转移。
  4. Patroni:用于管理PostgreSQL的高可用性。

步骤

1. 安装必要的软件包

首先,更新你的包列表并安装必要的软件包:

sudo apt update
sudo apt install -y corosync pacemaker postgresql-13-pgpool2 patroni

2. 配置Corosync和Pacemaker

编辑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

3. 配置Pacemaker

编辑Pacemaker配置文件 /etc/pacemaker/cib.xml

sudo nano /etc/pacemaker/cib.xml

确保配置文件中包含你的节点和资源信息。

启动并启用Pacemaker服务:

sudo systemctl start pacemaker
sudo systemctl enable pacemaker

4. 配置Patroni

创建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

5. 验证集群状态

检查Patroni和Pacemaker的状态:

sudo systemctl status patroni
sudo systemctl status pacemaker

使用 crm_mon 命令查看集群状态:

sudo crm_mon

注意事项

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

0
看了该问题的人还看了