Cluster
Master primary¶
Swarm config primary
- name: Deploy Postgres on Docker Swarm
hosts: vps
tasks:
- name: Creazione config pg_hba.conf
community.docker.docker_config:
name: postgres-master-hba
data: "{{ lookup('file', './config/pg_hba.conf') | b64encode }}"
data_is_b64: true
state: present
- name: Creazione config postgresql.conf
community.docker.docker_config:
name: postgres-master-conf
data: "{{ lookup('file', './config/postgresql.conf') | b64encode }}"
data_is_b64: true
state: present
- name: Deploy Postgres service
community.docker.docker_swarm_service:
name: postgres-primary
image: postgres:17
state: present
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
configs:
- config_name: postgres-master-hba
filename: /conf-postgres/pg_hba.conf
mode: "777"
uid: "999"
gid: "999"
- config_name: postgres-master-conf
filename: /conf-postgres/postgresql.conf
mode: "777"
uid: "999"
gid: "999"
mounts:
- type: volume
source: postgres-primary
target: /var/lib/postgresql/data
args:
[
"postgres",
"-c", "config_file=/conf-postgres/postgresql.conf",
"-c", "hba_file=/conf-postgres/pg_hba.conf"
]
restart_config:
condition: any
placement:
constraints:
- node.role == manager
networks:
- front
mode: replicated
replicas: 1
endpoint_mode: dnsrr
Slave¶
Swarm config replica
- name: Deploy Postgres on Docker Swarm
hosts: vps
tasks:
- name: Deploy Postgres replica
community.docker.docker_swarm_service:
name: postgres-replica
image: postgres:17
user: "postgres"
state: present
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
mounts:
- type: volume
source: postgres-replica
target: /var/lib/postgresql/data
args:
[
"bash",
"-c",
"chown -R 999:999 /var/lib/postgresql/data && chmod 700 /var/lib/postgresql/data && \
if [ ! -s /var/lib/postgresql/data/PG_VERSION ]; then \
pg_basebackup -h postgres-primary -D /var/lib/postgresql/data -U postgres -Fp -Xs -P -R -S replica1; \
fi && \
exec postgres"
]
restart_config:
condition: any
placement:
constraints:
- node.role == manager
networks:
- front
mode: replicated
replicas: 1
endpoint_mode: dnsrr
postgresql.conf
listen_addresses = '*'
port = 5432
wal_level = replica
max_wal_senders = 10
max_replication_slots = 10
wal_keep_size = 128MB
checkpoint_timeout = 120s
# Configurazione dei log
log_destination = 'stderr'
logging_collector = off
log_statement = 'none'
Comandi Utili¶
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'password';
pg_basebackup -h postgres-master -U replicator -D restore/ -Fp -Xs -P -R
SELECT * FROM pg_create_physical_replication_slot('my_slot');