Skip to content

Instantly share code, notes, and snippets.

@ankur20us
Last active January 28, 2022 08:54
Show Gist options
  • Save ankur20us/6f086d9199113cafbc900aa09e7631d3 to your computer and use it in GitHub Desktop.
Save ankur20us/6f086d9199113cafbc900aa09e7631d3 to your computer and use it in GitHub Desktop.
Postgres Docker Cluster with Connected Pgadmin
version: '2'
services:
cluster_master:
image: 'bitnami/postgresql:latest'
networks:
- cluster_network
ports:
- '5432'
volumes:
- 'cluster_master_volume:/bitnami/postgresql'
environment:
- POSTGRESQL_POSTGRES_PASSWORD=postgres
- POSTGRESQL_REPLICATION_MODE=master
- POSTGRESQL_USERNAME=my_user
- POSTGRESQL_PASSWORD=my_password
- POSTGRESQL_DATABASE=my_database
- POSTGRESQL_REPLICATION_USER=my_repl_user
- POSTGRESQL_REPLICATION_PASSWORD=my_repl_password
cluster_slave:
image: 'bitnami/postgresql:latest'
networks:
- cluster_network
ports:
- '5432'
depends_on:
- cluster_master
environment:
- POSTGRESQL_POSTGRES_PASSWORD=postgres
- POSTGRESQL_REPLICATION_MODE=slave
- POSTGRESQL_USERNAME=my_user
- POSTGRESQL_PASSWORD=my_password
- POSTGRESQL_REPLICATION_USER=repl_user
- POSTGRESQL_REPLICATION_PASSWORD=repl_password
- POSTGRESQL_MASTER_HOST=cluster_master
- POSTGRESQL_MASTER_PORT_NUMBER=5432
- POSTGRESQL_REPLICATION_USER=my_repl_user
- POSTGRESQL_REPLICATION_PASSWORD=my_repl_password
cluster_pgadmin:
image: 'dpage/pgadmin4'
networks:
- cluster_network
ports:
- '80'
volumes:
- 'cluster_pgadmin_volume:/var/lib/pgadmin'
depends_on:
- cluster_master
environment:
- [email protected]
- PGADMIN_DEFAULT_PASSWORD=admin
volumes:
cluster_master_volume:
cluster_pgadmin_volume:
networks:
cluster_network:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.55.0.0/24
gateway: 172.55.0.1
# COMMAND TO EXECUTE:
# docker-compose -p "pgc" up --detach --scale cluster_master=1 --scale cluster_slave=3
# EXPLANATION:
# 1. This command for docker compose will start postgres in master slave cluster with 1 master and 3 slaves
# 2. This will start the conneted PGAdmin tool as well under same network.
# 3. Once the Docker Compose is done, you will have 5 containers:
# 3.1 pgc_cluster_master_1
# 3.2 pgc_cluster_slave_1
# 3.3 pgc_cluster_slave_2
# 3.4 pgc_cluster_slave_3
# 3.5 pgc_cluster_pgadmin_1
# 4. Check the mapped port of 3.5 and open in browser
# 5. Add the new server and under:
# 5.1 GENERAL TAB:
# 5.1.1 Name = Anything You want to keep, eg: pgc_master
# 5.2 CONNECTION TAB:
# 5.2.1 host = pgc_cluster_master_1
# 5.2.2 Maintenance Database = postgres
# 5.2.3 port = 5432
# 5.2.4 username = postgres
# 5.2.5 password = postgres
# ** REPEAT 5 for adding slave connections too
# 6. Now when you change anything under MASTER connection , it will reflect in the slave connections too,
# NOTE : Please refresh the object, to update the changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment