fidanf.postgresql_ha
PostgreSQL HA
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ホストについて)
- PATHにPython3
- PATHにPip3
- psycopg2-binary(libpq-dev aptパッケージが必要)
Python 3の設定例はprepare.ymlを参照してください。
インストール
シェル
ansible-galaxy collection install community.crypto
ansible-galaxy install fidanf.postgresql_ha
要件ファイル
---
roles:
- src: https://github.com/fidanf/ansible-role-postgresql-ha
name: fidanf.postgresql_ha
version: master
collections:
- community.crypto
依存関係
- cryptography(ansibleホスト)
例のインベントリ
詳細なグループおよびホストの変数については、inventory.ymlを参照してください。
ロール変数
ロールのデフォルトの変数は2つのファイルに分かれています:
これらの変数の目的や有効な値を正確に確認するためには、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
または、pg_rewindを使って repmgr node rejoin を使用することができます。
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.