Skip to content

Commit 2be8456

Browse files
authored
fix promote to master in streaming replication (kartoza#370)
* fix promote to master in streaming replication * remove tests in example compose * Fix grammar error
1 parent 55137f4 commit 2be8456

File tree

13 files changed

+71
-118
lines changed

13 files changed

+71
-118
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ The two main replication methods allowed are
597597
598598
599599
### Database permissions and password authentication
600-
Replication uses a dedicated user `REPLICATION_USER`. The role ${REPLICATION_USER}
600+
Replication uses a dedicated user `REPLICATION_USER`. The role `${REPLICATION_USER}`
601601
uses the default group role `pg_read_all_data`. You can read more about this
602602
from the [PostgreSQL documentation](https://www.postgresql.org/docs/14/predefined-roles.html)
603603
@@ -616,7 +616,7 @@ layer are saved, they are automatically propagated to the replicant. Note also t
616616
replicant is read-only.
617617
618618
```shell
619-
docker run --name "streaming-replication" -e REPLICATION=true -e WAL_LEVEL='replica' -d -p 25432:5432 kartoza/postgis:13.0
619+
docker run --name "streaming-replication" -e REPLICATION=true -e WAL_LEVEL='replica' -d -p 25432:5432 kartoza/postgis:14.3.2
620620
```
621621
622622
**Note** If you do not pass the env variable `REPLICATION_PASS` a random password
@@ -635,20 +635,20 @@ we can't write new data to it. The whole database cluster will be replicated.
635635
636636
#### Database permissions
637637
638-
Since we are using a role ${REPLICATION_USER}, we need to ensure that it has access to all
638+
Since we are using a role `${REPLICATION_USER}`, we need to ensure that it has access to all
639639
the tables in a particular schema. So if a user adds another schema called `data`
640640
to the database `gis` he also has to update the permission for the user
641-
with the following SQL assuming the ${REPLICATION_USER} is called replicator
641+
with the following SQL assuming the `${REPLICATION_USER}` is called replicator
642642
643643
```sql
644644
ALTER DEFAULT PRIVILEGES IN SCHEMA data GRANT SELECT ON TABLES TO replicator;
645645
```
646646
647-
**NB** You need to set up a strong password for replication otherwise the
648-
default password for ${REPLICATION_USER} will default to `replicator`
647+
**Note** You need to set up a strong password for replication otherwise the
648+
default password for `${REPLICATION_USER}` will default to random generated string
649649
650-
To experiment with the replication abilities, you can see a [docker-compose.yml](sample/replication/docker-compose.yml)
651-
sample. There are several environment variables that you can set, such as:
650+
To experiment with the streaming replication abilities, you can see a [docker-compose.yml](replication_examples/replication/docker-compose.yml).
651+
There are several environment variables that you can set, such as:
652652
653653
Master settings:
654654
- **ALLOW_IP_RANGE**: A `pg_hba.conf` domain format which will allow specified host(s)
@@ -676,15 +676,15 @@ Slave settings:
676676
- **REPLICATION_USER** User to initiate streaming replication
677677
- **REPLICATION_PASS** Password for a user with streaming replication role
678678
679-
To run the sample replication, follow these instructions:
679+
To run the example streaming_replication, follow these instructions:
680680
681681
Do a manual image build by executing the `build.sh` script
682682
683683
```shell
684684
./build.sh
685685
```
686686
687-
Go into the `sample/replication` directory and experiment with the following Make
687+
Go into the `replication_examples/streaming_replication` directory and experiment with the following Make
688688
command to run both master and slave services.
689689
690690
```shell
@@ -701,31 +701,31 @@ To view logs for master and slave respectively, use the following command:
701701
702702
```shell
703703
make master-log
704-
make slave-log
704+
make node-log
705705
```
706706
707707
You can try experiment with several scenarios to see how replication works
708708
709709
#### Sync changes from master to replicant
710710
711711
You can use any postgres database tools to create new tables in master, by
712-
connecting using POSTGRES_USER and POSTGRES_PASS credentials using exposed port.
713-
In the sample, the master database was exposed on port 7777.
712+
connecting using `POSTGRES_USER` and `POSTGRES_PASS` credentials using exposed port.
713+
In the streaming_replication example, the master database was exposed on port 7777.
714714
Or you can do it via command line, by entering the shell:
715715
716716
```shell
717717
make master-shell
718718
```
719719
720-
Then made any database changes using psql.
720+
Then make any database changes using psql.
721721
722722
After that, you can see that the replicant follows the changes by inspecting the
723723
slave database. You can, again, use database management tools using connection
724724
credentials, hostname, and ports for replicant. Or you can do it via command line,
725725
by entering the shell:
726726
727727
```shell
728-
make slave-shell
728+
make node-shell
729729
```
730730
731731
Then view your changes using psql.
@@ -738,14 +738,14 @@ into slave environment and set `DESTROY_DATABASE_ON_RESTART: 'False'`.
738738
739739
After this, you can make changes to your replicant, but master and replicant will not
740740
be in sync anymore. This is useful if the replicant needs to take over a failover master.
741-
However it is recommended to take additional action, such as creating a backup from the
741+
However, it is recommended to take additional action, such as creating a backup from the
742742
slave so a dedicated master can be created again.
743743
744744
#### Preventing replicant database destroy on restart
745745
746746
You can optionally set `DESTROY_DATABASE_ON_RESTART: 'False'` after successful sync
747747
to prevent the database from being destroyed on restart. With this setting you can
748-
shut down your replicant and restart it later and it will continue to sync using the existing
748+
shut down your replicant and restart it later, and it will continue to sync using the existing
749749
database (as long as there are no consistencies conflicts).
750750
751751
However, you should note that this option doesn't mean anything if you didn't
@@ -762,7 +762,7 @@ To activate the following you need to use the environment variable
762762
docker run --name "logical-replication" -e WAL_LEVEL=logical -d kartoza/postgis:13.0
763763
```
764764
765-
For a detailed example see the docker-compose in the folder `sample/logical_replication`.
765+
For a detailed example see the docker-compose in the folder `replication_examples/logical_replication`.
766766
767767
### Docker image versions
768768
@@ -786,4 +786,4 @@ please consider taking out a [Support Level Agreeement](https://kartoza.com/en/s
786786
- Rizky Maulana (rizky@kartoza.com)
787787
- Admire Nyakudya (admire@kartoza.com)
788788
789-
March 2021
789+
April 2022

docker-compose.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# docker-compose build
2-
version: '2.1'
2+
version: '3.9'
33
volumes:
44
dbbackups:
55
postgis-data:
66

77
services:
88

99
db:
10-
image: kartoza/postgis:14-3.1
10+
image: kartoza/postgis:14-3.2
1111
volumes:
1212
- postgis-data:/var/lib/postgresql
1313
- dbbackups:/backups
@@ -20,13 +20,13 @@ services:
2020
# Add extensions you need to be enabled by default in the DB. Default are the five specified below
2121
- POSTGRES_MULTIPLE_EXTENSIONS=postgis,hstore,postgis_topology,postgis_raster,pgrouting
2222
ports:
23-
- 25432:5432
23+
- "25432:5432"
2424
restart: on-failure
2525
healthcheck:
2626
test: "exit 0"
2727

2828
dbbackups:
29-
image: kartoza/pg-backup:14-3.1
29+
image: kartoza/pg-backup:14-3.2
3030
hostname: pg-backups
3131
volumes:
3232
- dbbackups:/backups
@@ -36,7 +36,6 @@ services:
3636
- POSTGRES_PASS=docker
3737
- POSTGRES_PORT=5432
3838
- POSTGRES_HOST=db
39-
- POSTGRES_DBNAME=gis
4039
restart: on-failure
4140
depends_on:
4241
db:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
up:
2+
docker-compose up -d
3+
4+
down:
5+
docker-compose down
6+
7+
scale:
8+
docker-compose up -d --scale pg-node=3
9+
10+
unscale:
11+
docker-compose up -d --scale pg-node=1
12+
13+
status:
14+
docker-compose ps
15+
16+
master-shell:
17+
docker-compose exec pg-master /bin/bash
18+
19+
node-shell:
20+
docker-compose exec pg-node /bin/bash
21+
22+
master-log:
23+
docker-compose logs -f --tail=30 pg-master
24+
25+
node-log:
26+
docker-compose logs -f --tail=30 pg-node
27+

sample/replication/docker-compose.yml renamed to replication_examples/streaming_replication/docker-compose.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

2-
version: '2.1'
2+
version: '3.9'
33

44
volumes:
55
pg-master-data-dir:
6-
pg-slave-data-dir:
6+
pg-node-data-dir:
77

88

99
services:
1010
pg-master:
11-
image: kartoza/postgis:14-3.1
11+
image: kartoza/postgis:14-3.2
1212
restart: 'always'
1313
# You can optionally mount to volume, to play with the persistence and
1414
# observe how the slave will behave after restarts.
1515
volumes:
1616
- pg-master-data-dir:/var/lib/postgresql
17-
- ./tests:/tests
17+
- ./scripts/setup-master.sql:/docker-entrypoint-initdb.d/setup-master.sql
1818
environment:
1919
# ALLOW_IP_RANGE option is used to specify additionals allowed domains
2020
# in pg_hba.
@@ -34,16 +34,15 @@ services:
3434
healthcheck:
3535
test: "exit 0"
3636

37-
pg-slave:
38-
image: kartoza/postgis:14-3.1
37+
pg-node:
38+
image: kartoza/postgis:14-3.2
3939
restart: 'always'
4040
# You can optionally mount to volume, but we're not able to scale it
4141
# in that case.
4242
# The slave will always destroy its database and copy from master at
4343
# runtime
4444
volumes:
45-
- pg-slave-data-dir:/var/lib/postgresql
46-
- ./tests:/tests
45+
- pg-node-data-dir:/var/lib/postgresql
4746

4847
environment:
4948
# ALLOW_IP_RANGE option is used to specify additionals allowed domains
@@ -77,7 +76,7 @@ services:
7776
# If specified with any value, then it will convert current slave into
7877
# a writable state. Useful if master is down and the current slave needs
7978
# to be promoted until manual recovery.
80-
# PROMOTE_MASTER: 'True'
79+
#PROMOTE_MASTER: 'True'
8180

8281
# For now we don't support different credentials for replication
8382
# so we use the same credentials as master's superuser, or anything that
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Create a table
2+
CREATE TABLE IF NOT EXISTS sweets
3+
(
4+
id SERIAL,
5+
name TEXT,
6+
price DECIMAL,
7+
CONSTRAINT sweets_pkey PRIMARY KEY (id)
8+
);
9+
10+
-- Inserts records into the table
11+
INSERT INTO sweets (name, price) VALUES ('strawberry', 4.50), ('Coffee', 6.20), ('lollipop', 3.80);

sample/replication/Makefile

Lines changed: 0 additions & 50 deletions
This file was deleted.

sample/replication/tests/replication_test_master.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

sample/replication/tests/replication_test_slave.sh

Lines changed: 0 additions & 14 deletions
This file was deleted.

scripts/env-data.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SQLDIR="/usr/share/postgresql/${POSTGRES_MAJOR_VERSION}/contrib/postgis-${POSTGI
1717
SETVARS="POSTGIS_ENABLE_OUTDB_RASTERS=1 POSTGIS_GDAL_ENABLED_DRIVERS=ENABLE_ALL"
1818
LOCALONLY="-c listen_addresses='127.0.0.1'"
1919
PG_BASEBACKUP="/usr/bin/pg_basebackup"
20-
PROMOTE_FILE="/tmp/pg_promote_master"
20+
NODE_PROMOTION="/usr/lib/postgresql/${POSTGRES_MAJOR_VERSION}/bin/pg_ctl"
2121
PGSTAT_TMP="/var/run/postgresql/"
2222
PG_PID="/var/run/postgresql/${POSTGRES_MAJOR_VERSION}-main.pid"
2323

scripts/setup-replication.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ if [[ "$WAL_LEVEL" == 'replica' && "${REPLICATION}" =~ [Tt][Rr][Uu][Ee] ]]; then
3535
fi
3636
# Promote to master if desired
3737
if [[ ! -z "${PROMOTE_MASTER}" ]]; then
38-
touch ${PROMOTE_FILE}
38+
su - postgres -c "${NODE_PROMOTION} promote -D ${DATADIR}"
3939
fi
4040

4141
fi

0 commit comments

Comments
 (0)