溫馨提示×

PostgreSQL在Linux上的集群如何搭建

小樊
54
2025-09-21 03:33:43
欄目: 云計算

在Linux上搭建PostgreSQL集群可以通過多種方式實現,其中最常見的是使用Patroni和Repmgr。以下是使用Patroni和Repmgr搭建PostgreSQL集群的步驟:

使用Patroni搭建PostgreSQL集群

  1. 安裝必要的軟件包 在所有節點上安裝PostgreSQL、Patroni和相關的依賴包。

    sudo apt-get update
    sudo apt-get install -y postgresql-13 postgresql-13-patroni
    
  2. 配置PostgreSQL 在每個節點上編輯/etc/postgresql/13/main/postgresql.conf文件,確保以下參數設置正確:

    listen_addresses = '*'  # 或者指定具體的IP地址
    wal_level = replica
    max_wal_senders = 10
    wal_keep_segments = 64
    hot_standby = on
    

    編輯/etc/postgresql/13/main/pg_hba.conf文件,添加允許其他節點連接的條目:

    host    replication     replicator     <其他節點的IP>/32    md5
    host    all             all             0.0.0.0/0               md5
    
  3. 初始化Patroni配置 在主節點上創建Patroni配置文件/etc/patroni.yml,示例如下:

    scope: postgresql
    name: master
    namespace: /db/
    restapi:
      listen: 0.0.0.0:8008
      connect_address: 192.168.1.101  # 主節點的IP地址
    etcd:
      host: 192.168.1.102  # etcd服務器的IP地址
      port: 2379
      use_ssl: false
    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
    postgresql:
      listen: 0.0.0.0:5432
      connect_address: 192.168.1.101
      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
    

    在從節點上創建類似的配置文件,修改scope、nameconnect_address字段。

  4. 啟動Patroni 在主節點上啟動Patroni:

    sudo systemctl start patroni
    sudo systemctl enable patroni
    

    在從節點上啟動Patroni:

    sudo systemctl start patroni
    sudo systemctl enable patroni
    
  5. 驗證集群狀態 使用Patroni提供的API檢查集群狀態:

    curl http://192.168.1.101:8008/postgres
    

使用Repmgr搭建PostgreSQL集群

  1. 安裝必要的軟件包 在所有節點上安裝PostgreSQL和Repmgr。

    sudo apt-get update
    sudo apt-get install -y postgresql-13 repmgr
    
  2. 配置PostgreSQL 在每個節點上編輯/etc/postgresql/13/main/postgresql.conf文件,確保以下參數設置正確:

    listen_addresses = '*'  # 或者指定具體的IP地址
    wal_level = replica
    max_wal_senders = 10
    wal_keep_segments = 64
    hot_standby = on
    

    編輯/etc/postgresql/13/main/pg_hba.conf文件,添加允許其他節點連接的條目:

    host    replication     replicator     <其他節點的IP>/32    md5
    host    all             all             0.0.0.0/0               md5
    
  3. 初始化Repmgr配置 在主節點上編輯/etc/repmgr/repmgr.conf文件,示例如下:

    node_id 1
    node_name master
    connect_address 192.168.1.101  # 主節點的IP地址
    pg_bind_address 192.168.1.101
    listen_address 192.168.1.101
    auth_method md5
    auth_key 'your_auth_key'
    

    在從節點上編輯/etc/repmgr/repmgr.conf文件,修改node_idnode_name字段。

  4. 啟動Repmgr 在主節點上啟動Repmgr:

    sudo systemctl start repmgrd
    sudo systemctl enable repmgrd
    

    在從節點上啟動Repmgr:

    sudo systemctl start repmgrd
    sudo systemctl enable repmgrd
    
  5. 初始化集群 在主節點上運行以下命令初始化集群:

    sudo repmgr -f /etc/repmgr/repmgr.conf standby clone
    
  6. 驗證集群狀態 使用Repmgr提供的命令檢查集群狀態:

    sudo repmgr cluster show
    

通過以上步驟,你可以在Linux上成功搭建一個PostgreSQL集群。選擇Patroni還是Repmgr取決于你的具體需求和環境。Patroni提供了更高級的功能和更好的自動化管理,而Repmgr則是一個更輕量級的解決方案。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女