Skip to content

Commit c274066

Browse files
author
ravishankar
committed
feat: postgres 11 replication setup docker compose file
1 parent 5bda25f commit c274066

7 files changed

+191
-2
lines changed

README.md

-2
This file was deleted.

master-stack.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3.1'
2+
services:
3+
ms:
4+
command: "-c 'config_file=/etc/postgresql/postgresql.conf'"
5+
image: postgres:12
6+
volumes:
7+
- /mnt/dsk/data-01/staging/postgres/master:/var/lib/postgresql/data
8+
- ./postgres-master.conf:/etc/postgresql/postgresql.conf
9+
- ./setup-node.sh:/docker-entrypoint-initdb.d/setup-node.sh
10+
restart: always
11+
ports:
12+
- 4000:5432
13+
environment:
14+
POSTGRES_PASSWORD: password
15+
POSTGRES_USER: rav
16+
ROLE: master
17+
PG_REP_USER: replicator
18+
PG_REP_PASSWORD: password

postgres-master.conf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
listen_addresses = '*'
2+
wal_level = hot_standby
3+
archive_mode = on
4+
archive_command = 'cd .'
5+
max_wal_senders = 8
6+
wal_keep_segments = 8
7+
hot_standby = on

postgres-slave.conf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
listen_addresses = '*'
2+
wal_level = hot_standby
3+
archive_mode = on
4+
archive_command = 'cd .'
5+
max_wal_senders = 8
6+
wal_keep_segments = 8
7+
hot_standby = on
8+
promote_trigger_file='/tmp/failover.log'

readme.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Postgres Replication Setup
2+
3+
Postgres supports multiple methods of replication.
4+
The replication method we are going to setup is Streaming Replication or Log Replication.
5+
Refer [this documentation](https://www.postgresql.org/docs/12/warm-standby.html#STREAMING-REPLICATION) to read more about Streaming Replication.
6+
7+
8+
### Steps to set up Streaming Replication in PostgreSQL 12
9+
10+
#### MASTER SETUP
11+
12+
##### Step 1
13+
```
14+
Initialize and start PostgreSQL, if not done already on the Master.
15+
```
16+
17+
##### Step 2
18+
```
19+
Modify the parameter listen_addresses to allow a specific IP interface or all (using *).
20+
Modifying this parameter requires a restart of the PostgreSQL instance to get the change into effect.
21+
```
22+
23+
##### Step 3
24+
```
25+
Create a User for replication in the Master.
26+
It is discouraged to use superuser postgres in order to setup replication, though it works.
27+
```
28+
29+
##### Step 4
30+
```
31+
Allow replication connections from Standby to Master by appending a similar line as following to the pg_hba.conf file of the Master.
32+
33+
# Config need to added
34+
host replication all 0.0.0.0/0 md5
35+
```
36+
37+
----
38+
39+
#### SLAVE SETUP
40+
41+
##### Step 1
42+
```
43+
Initialize and start PostgreSQL, if not done already on the Master.
44+
```
45+
46+
##### Step 2
47+
```
48+
Modify the parameter listen_addresses to allow a specific IP interface or all (using *).
49+
Modifying this parameter requires a restart of the PostgreSQL instance to get the change into effect.
50+
```
51+
52+
#### Step 3
53+
```
54+
Delete everything in the data directory
55+
```
56+
57+
##### Step 4
58+
```
59+
You may use `pg_basebackup` to backup the data directory of the Master from the Standby.
60+
While creating the backup, you may also tell pg_basebackup to create the replication specific files and entries in the data directory using "-R" .
61+
```
62+
63+
----
64+
65+
### If you think it's a lot of steps to setup the replication ???
66+
67+
### Answer is here
68+
69+
```
70+
All the steps above mentioned has been automated into two steps.
71+
First step is to deploy the master stack.
72+
Another step is to deploy the slave stack with the replication configuration.
73+
74+
Info: Master and slave runs on a two different machines.
75+
Note: Please modify credentials and port before deploying it
76+
```
77+
78+
#### Deploy Master
79+
Please ensure the data volume path is correct in yml file
80+
```
81+
sudo docker-compose -f master-stack.yml start
82+
# ensure master stack is up
83+
```
84+
85+
#### Deploy slave
86+
Please ensure the data volume path is correct in yml file
87+
```
88+
sudo docker-compose -f slave-stack.yml start
89+
# ensure replication works by creating a table in master node and check if the table present in slave node
90+
```

setup-node.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
# check if the role is specified
4+
if [ -z "$ROLE" ]; then
5+
echo >&2 'Error: You need to specify ROLE, values are master or slave'
6+
exit 1
7+
fi
8+
9+
# check if the replication user is specified
10+
if [ -z "$PG_REP_USER" ]; then
11+
echo >&2 'Error: You need to specify PG_REP_USER'
12+
exit 1
13+
fi
14+
15+
# check if the replication user password is specified
16+
if [ -z "$PG_REP_PASSWORD" ]; then
17+
echo >&2 'Error: You need to specify PG_REP_PASSWORD'
18+
exit 1
19+
fi
20+
21+
# create replication user and also update hba conf to allow remote access of replication users
22+
if [ "$ROLE" == "master" ]; then
23+
echo "master is setting up"
24+
echo "host replication all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"
25+
set -e
26+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
27+
CREATE USER $PG_REP_USER WITH REPLICATION ENCRYPTED PASSWORD '$PG_REP_PASSWORD';
28+
EOSQL
29+
fi
30+
31+
32+
# creates a pgpass file to save the password and perform the pg_basebackup of the master into slave data directory also automatically configures the slave
33+
if [ "$ROLE" == "slave" ]; then
34+
echo "$PG_MASTER_HOST:$PG_MASTER_PORT:*:$PG_REP_USER:$PG_REP_PASSWORD" > "$HOME"/.pgpass
35+
chmod 0600 "$HOME"/.pgpass
36+
export PGPASSFILE="$HOME/.pgpass"
37+
rm -rf $PGDATA/*
38+
until pg_basebackup -h "${PG_MASTER_HOST}" -p "${PG_MASTER_PORT}" -D "${PGDATA}" -U "${PG_REP_USER}" -Fp -Xs -P -R
39+
do
40+
echo "Waiting for master to connect..."
41+
sleep 1s
42+
done
43+
echo "host replication all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"
44+
fi
45+
46+
echo "Awesome ${ROLE} done!"
47+

slave-stack.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3.1'
2+
services:
3+
sl:
4+
command: "-c 'config_file=/etc/postgresql/postgresql.conf'"
5+
image: postgres:12
6+
volumes:
7+
- /mnt/dsk/data-01/staging/postgres/slave:/var/lib/postgresql/data
8+
- ./postgres-slave.conf:/etc/postgresql/postgresql.conf
9+
- ./setup-node.sh:/docker-entrypoint-initdb.d/setup-node.sh
10+
restart: always
11+
ports:
12+
- 4001:5432
13+
environment:
14+
POSTGRES_PASSWORD: password
15+
POSTGRES_USER: rav
16+
PG_REP_USER: replicator
17+
PG_REP_PASSWORD: password
18+
PG_MASTER_HOST: 10.132.0.7
19+
PG_MASTER_PORT: 4000
20+
ROLE: slave
21+

0 commit comments

Comments
 (0)