fidanf.postgresql_ha
PostgreSQL 高可用性
安装和配置一个使用 repmgr 管理的 PostgreSQL 高可用性集群。添加依赖项、扩展、数据库和用户。它也适用于独立安装。
测试环境:
- Debian 10.x :heavy_check_mark:
- Debian 11.x :heavy_check_mark:
- Ubuntu 18.04.x :heavy_check_mark:
- Ubuntu 20.04.x :heavy_check_mark:
要求
- Python >=3.8
- Ansible-core >=2.12
有关用于开发角色的详细依赖项,请参阅 ./requirements.txt。
推荐:每个 PostgreSQL 主机
- Python3 在路径中
- Pip3 在路径中
- psycopg2-binary (需要 libpq-dev apt 包)
您可以查看 prepare.yml 来了解 Python 3 的示例设置。
安装
通过命令行
ansible-galaxy collection install community.crypto
ansible-galaxy install fidanf.postgresql_ha
通过 requirements 文件
---
roles:
- src: https://github.com/fidanf/ansible-role-postgresql-ha
name: fidanf.postgresql_ha
version: master
collections:
- community.crypto
依赖项
- cryptography (Ansible 主机)
示例清单
有关详细的组和主机变量,请查看 inventory.yml。
角色变量
角色的默认变量分为两个文件:
为了确切了解这些变量的用途和有效值,请随时检查 templates 目录中的所有 Jinja 模板。原始的默认配置文件也已包含(.orig
)。
示例剧本
---
- hosts: pgcluster
gather_facts: yes
become: yes
roles:
- name: fidanf.postgresql_ha
vars:
# 必需的配置项
repmgr_target_group: pgcluster
repmgr_master: pgsql01
repmgr_failover: automatic
repmgr_promote_command: /usr/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file
repmgr_follow_command: /usr/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n
repmgr_monitoring_history: "yes"
repmgr_connection_check_type: query
repmgr_log_level: DEBUG
repmgr_reconnect_attempts: 2
repmgr_reconnect_interval: 10
# 基本设置
postgresql_version: 13
postgresql_cluster_name: main
postgresql_cluster_reset: false # TODO: 需要对 repmgr 测试
postgresql_listen_addresses: "*"
postgresql_port: 5432
postgresql_wal_level: "replica"
postgresql_max_wal_senders: 10
postgresql_max_replication_slots: 10
postgresql_wal_keep_segments: 100
postgresql_hot_standby: on
postgresql_archive_mode: on
postgresql_archive_command: "test ! -f /tmp/%f && cp %p /tmp/%f"
postgresql_ext_install_repmgr: yes
postgresql_shared_preload_libraries:
- repmgr
# PostgreSQL 日志
postgresql_log_checkpoints: on
postgresql_log_connections: on
postgresql_log_disconnections: on
postgresql_log_temp_files: 0
# pg_hba.conf
postgresql_pg_hba_custom:
- { type: "host", database: "all", user: "all", address: "192.168.56.0/24", method: "md5" }
- { type: "host", database: "replication", user: "{{ repmgr_user }}", address: "192.168.56.0/24", method: "trust" }
- { type: "host", database: "replication", user: "{{ repmgr_user }}", address: "127.0.0.1/32", method: "trust" }
- { type: "host", database: "{{ repmgr_database }}", user: "{{ repmgr_user }}", address: "127.0.0.1/32", method: "trust" }
- { type: "host", database: "{{ repmgr_database }}", user: "{{ repmgr_user }}", address: "192.168.56.0/32", method: "trust" }
# 数据库
postgresql_databases:
- name: "{{ repmgr_database }}"
owner: "{{ repmgr_user }}"
encoding: "UTF-8"
- name: testdb
owner: admin
encoding: "UTF-8"
# 用户
postgresql_users:
- name: "{{ repmgr_user }}"
pass: "{{ repmgr_password }}"
- name: admin
pass: secret # PostgreSQL >=10 不接受未加密密码
encrypted: yes
# 角色
postgresql_user_privileges:
- name: "{{ repmgr_user }}"
db: "{{ repmgr_database }}"
priv: "ALL"
role_attr_flags: "SUPERUSER,REPLICATION"
- name: admin
db: testdb
role_attr_flags: "SUPERUSER"
首次安装后可运行的有用命令
验证集群功能
ansible pgcluster -b --become-user postgres -m shell -a "repmgr cluster crosscheck"
显示集群状态
ansible pgcluster -b --become-user postgres -m shell -a "repmgr cluster show"
列出节点及其属性
ansible pgcluster -b --become-user postgres -m shell -a "repmgr node status"
注册(克隆)额外的备用节点
# 假设当前主节点的主机名为 pgsql01
ansible-playbook myplaybook.yml -l 'pgsql04' -e 'repmgr_primary_hostname=pgsql01' -vv
在自动故障转移后将前主节点注册为备用节点
postgres@pgsql01:~$ pg_ctlcluster 13 main stop
postgres@pgsql01:~$ repmgr standby clone --force -h pgsql02 -U repmgr -d repmgr
postgres@pgsql01:~$ pg_ctlcluster 13 main start
postgres@pgsql01:~$ repmgr standby register --force
或者您可以使用 repmgr node rejoin 和 pg_rewind
repmgr node rejoin -d repmgr -U repmgr -h pgsql02 --verbose --force-rewind=/usr/lib/postgresql/13/bin/pg_rewind
许可证
MIT / BSD
关于项目
Install and configure PostgreSQL cluster managed with repmgr. Add dependencies, extensions, databases and users.
安装
ansible-galaxy install fidanf.postgresql_ha
许可证
mit
下载
676
拥有者
DevOps Engineer ~ Ansible lover & CI/CD builder.
I'm learning to more effectively deploy and monitor apps every day.