Skip to content

Commit 65a996d

Browse files
committed
Add pgpool2 configuration
1 parent 2b13f73 commit 65a996d

File tree

8 files changed

+163
-9
lines changed

8 files changed

+163
-9
lines changed

compose.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
x-base:
22
&base
3-
secrets:
4-
- postgres-password
5-
- replicator-password
6-
environment:
7-
POSTGRES_PASSWORD_FILE: /run/secrets/postgres-password
8-
POSTGRES_CIDR: ${POSTGRES_CIDR:-172.54.32.0/24}
9-
command: ["postgres", "-c", "log_statement=all"]
3+
secrets:
4+
- postgres-password
5+
- replicator-password
6+
environment:
7+
POSTGRES_PASSWORD_FILE: /run/secrets/postgres-password
8+
POSTGRES_CIDR: ${POSTGRES_CIDR:-172.54.32.0/24}
9+
command: ["postgres", "-c", "log_statement=all"]
1010

1111
services:
1212
primary:
1313
<<: *base
1414
build:
15-
context: .
15+
context: postgres
1616
target: primary
1717
volumes:
1818
- primary_data:/var/lib/postgresql/data
1919

2020
secondary:
2121
<<: *base
2222
build:
23-
context: .
23+
context: postgres
2424
target: secondary
2525
restart: on-failure:3
2626
volumes:
2727
- secondary_data:/var/lib/postgresql/data
2828

29+
pgpool2:
30+
build:
31+
context: pgpool2
32+
secrets:
33+
- postgres-password
34+
2935
secrets:
3036
postgres-password:
3137
file: dev/secrets/postgres-password

pgpool2/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM ubuntu:22.04 as pgpool
2+
3+
# https://www.pgpool.net/mediawiki/index.php/Apt_Repository
4+
5+
ENV DEBIAN_FRONTEND noninteractive
6+
7+
RUN apt-get update
8+
RUN apt-get install -y wget gnupg
9+
RUN echo "deb http://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list
10+
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
11+
RUN apt-get update
12+
RUN apt-get install -y pgpool2 libpgpool2 postgresql-16-pgpool2
13+
RUN apt-get install -y gettext-base
14+
15+
COPY pgpool2/ /etc/pgpool2/
16+
COPY bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
17+
18+
ENTRYPOINT [ "docker-entrypoint.sh" ]
19+
CMD [ "pgpool", "-n" ]

pgpool2/bin/docker-entrypoint.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#! /bin/bash
2+
3+
set -ex
4+
5+
export POSTGRES_PASSWORD=$(cat /run/secrets/postgres-password)
6+
cat /etc/pgpool2/pgpool.conf.tpl | envsubst > /etc/pgpool2/pgpool.conf
7+
cat /etc/pgpool2/pool_passwd.tpl | envsubst > /etc/pgpool2/pool_passwd
8+
unset POSTGRES_PASSWORD
9+
10+
export POSTGRES_PASSWORD_MD5=$(md5sum /run/secrets/postgres-password | awk '{print $1}')
11+
cat /etc/pgpool2/pcp.conf.tpl | envsubst > /etc/pgpool2/pcp.conf
12+
unset POSTGRES_PASSWORD_MD5
13+
14+
exec "$@"

pgpool2/pgpool2/pcp.conf.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# PCP Client Authentication Configuration File
2+
# ============================================
3+
4+
postgres:${POSTGRES_PASSWORD_MD5}

pgpool2/pgpool2/pgpool.conf.tpl

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# ----------------------------
2+
# pgPool-II configuration file
3+
# ----------------------------
4+
#
5+
# Docs: https://www.pgpool.net/docs/latest/en/html/configuring-pgpool.html#:~:text=conf%20is%20the%20main%20configuration,%24prefix%2Fetc%2Fpgpool.
6+
7+
#------------------------------------------------------------------------------
8+
# BACKEND CLUSTERING MODE
9+
#------------------------------------------------------------------------------
10+
11+
backend_clustering_mode = 'streaming_replication'
12+
13+
14+
#------------------------------------------------------------------------------
15+
# CONNECTIONS
16+
#------------------------------------------------------------------------------
17+
18+
# - pgpool Connection Settings -
19+
20+
listen_addresses = '*'
21+
port = 5432
22+
23+
# - pgpool Communication Manager Connection Settings -
24+
25+
pcp_listen_addresses = 'localhost'
26+
pcp_port = 9898
27+
28+
# - Backend Connection Settings -
29+
30+
backend_hostname0 = 'primary'
31+
backend_port0 = 5432
32+
backend_weight0 = 1
33+
backend_data_directory0 = '/var/lib/postgresql/data'
34+
backend_flag0 = 'ALLOW_TO_FAILOVER'
35+
backend_application_name0 = 'primary'
36+
37+
backend_hostname1 = 'secondary'
38+
backend_port1 = 5432
39+
backend_weight1 = 1
40+
backend_data_directory1 = '/var/lib/postgresql/data'
41+
backend_flag1 = 'ALLOW_TO_FAILOVER'
42+
backend_application_name1 = 'secondary'
43+
44+
# - Authentication -
45+
46+
enable_pool_hba = on
47+
48+
# - SSL Connections -
49+
50+
#------------------------------------------------------------------------------
51+
# LOGS
52+
#------------------------------------------------------------------------------
53+
54+
# - Where to log -
55+
log_per_node_statement = on
56+
57+
58+
#------------------------------------------------------------------------------
59+
# LOAD BALANCING MODE
60+
#------------------------------------------------------------------------------
61+
62+
load_balance_mode = on
63+
64+
65+
#------------------------------------------------------------------------------
66+
# STREAMING REPLICATION MODE
67+
#------------------------------------------------------------------------------
68+
69+
# - Streaming -
70+
71+
sr_check_period = 2
72+
sr_check_user = 'postgres'
73+
sr_check_password = '${POSTGRES_PASSWORD}'
74+
sr_check_database = 'postgres'
75+
76+
77+
#------------------------------------------------------------------------------
78+
# HEALTH CHECK GLOBAL PARAMETERS
79+
#------------------------------------------------------------------------------
80+
81+
health_check_period = 30
82+
health_check_timeout = 20
83+
health_check_user = 'postgres'
84+
health_check_password = '${POSTGRES_PASSWORD}'
85+
health_check_database = 'postgres'
86+
health_check_max_retries = 3
87+
health_check_retry_delay = 10
88+
89+
#------------------------------------------------------------------------------
90+
# FAILOVER AND FAILBACK
91+
#------------------------------------------------------------------------------
92+
failover_command = 'PGPASSWORD=${POSTGRES_PASSWORD} psql --host secondary -U postgres -c "SELECT pg_promote();"'
93+
failover_on_backend_error = on
94+
failover_on_backend_shutdown = on
95+
detach_false_primary = on
96+
97+
#------------------------------------------------------------------------------
98+
# ONLINE RECOVERY
99+
#------------------------------------------------------------------------------
100+
101+
auto_failback = on

pgpool2/pgpool2/pool_hba.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# https://www.pgpool.net/docs/latest/en/html/auth-pool-hba-conf.html
2+
3+
# "local" is for Unix domain socket connections only
4+
local all all trust
5+
# IPv4 local connections:
6+
host all all 127.0.0.1/32 trust
7+
host all all ::1/128 trust
8+
9+
host all all all scram-sha-256

pgpool2/pgpool2/pool_passwd.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
postgres:TEXT${POSTGRES_PASSWORD}
File renamed without changes.

0 commit comments

Comments
 (0)