Sign inSign up

scalac/postgres-cluster

By scalac

Updated over 9 years ago

Docker container for PostgreSQL with PgPool

Image
1

707

scalac/postgres-cluster repository overview

PostgreSQL HA

This repository maintain a code needed to run PostgreSQL cluster in streaming replication mode with PgPool inside a Docker containers. It utilize Docker Swarm, but it can be run on a single host, ie. for testing purposes. It solves automatic and manual failover and manual recovery and failback. It uses constraints and labels to ensure proper containers scheduling.

At the moment this solution uses restart: none policy to avoid starting failed container during Docker host startup. It requires external governor like systemd.

Prerequisites:
  • HA mode

    • Docker Compose 1.9.0
    • Docker Machine 0.8.2
    • Docker Swarm 1.2.6
  • Single host:

    • Docker 1.13.0
    • Docker Compose 1.9.0
Architecture

img

Storage

All configuration and data files are stored locally on Docker host in directories defined in the configuration file by MASTER_VOLUME_* and STANDBY_VOLUME_* variables. Defined paths refer to the Docker host file system, not to the file system inside a containers. For more details see configuration section.

Network

To support load balancing between nodes both are using the same alias defined by PGPOOL_ALIAS variable. Application can connect to the active PgPool node through frontend network defined by POSTGRES_BACKEND_SUBNET variable. For more details see configuration section.

NetworkTypeDescription
POSTGRES_BACKEND_SUBNETinternalUsed for communication between PgPool and PostgreSQL and for streaming replication.
POSTGRES_FRONTEND_SUBNETexternalUsed for communication between client applications and PgPool.
Command line reference

The following list of commands is available for the ./bin/db_ctl.sh script.

CommandParametersDescription
buildBuilds and distributes postgres-cluster image.
init[-f]Initializes data directories, creates networks and configures streaming replication. -f flag is required to move any existing configuration and data files.
startStarts containers.
stopStops containers.
statusShows current cluster status.
failoverFailover gracefully.
recovery[-f]Recovers failed node. -f flag is required to perform full recovery an to move any existing configuration and data files.
failbackFailback gracefully.
user create<username> <password>Creates user account.
user delete<username>Deletes user account.
user password<username> <password>Updates the user's password.
Configuration

All configuration settings can be changed in etc/env.sh file. You can override default configuration file using -f flag, ie. ./bin/db_ctl.sh -f etc/env.prod.sh. Files matching etc/env.*.sh pattern are ignored by .gitignore.

VariableDefault valueDescription
POSTGRES_USERpostgresPostgres user name.
POSTGRES_PASSWORDsecretPostgres user password.
PGPORT5433PostgreSQL database port.
PGPOOL_PORT5432PgPool database port.
PGPOOL_ALIASdbPgPool network alias.
REPLICATION_USERreplicationPostgreSQL replication user's name.
REPLICATION_PASSWORDsecretPostgreSQL replication user's password.
PGDATA/var/lib/postgresql/dataPostgreSQL data directory inside a container.
CLUSTER_ARCHIVE/var/lib/postgresql/archivePostgreSQL archive logs directory inside a container.
PCP_USERpostgresPgPool administrator user' name.
PCP_PASSWORDsecretPgPool administrator user's password.
MASTER_NODEnode-0Docker host's name for master node used in docker-machine.
STANDBY_NODEnode-1Docker host's name for standby node used in docker-machine.
MASTER_IP10.0.0.11IP address of the master node used in the backend network.
STANDBY_IP10.0.0.12IP address of the standby node used in the backend network.
POSTGRES_BACKEND_SUBNET10.0.0.0/24Subnet of the backend network.
POSTGRES_FRONTEND_SUBNET10.0.1.0/24Subnet of the frontend network.
COMPOSE_PROJECT_NAMEpostgres-clusterUsed as a naming prefix for containers and networks. Useful for multi tenancy.
COMPOSE_HTTP_TIMEOUT300Docker API timeout.
DOCKER_HUB_IMAGEtrueUse Docker HUB image.
DOCKER_HUB_IMAGE_VERSIONlatestVersion tag for Docker HUB image.
DOCKER_REGISTRY_HOST192.168.56.10Docker Registry address. Not used in single mode.
DOCKER_REGISTRY_PORT5000Docker Registry port. Not used in single mode.
MASTER_VOLUME_ETC_PGPOOL/data/docker/postgres-cluster/master/etc/pgpool2PgPool configuration directory on master node.
MASTER_VOLUME_ETC_SSH/data/docker/postgres-cluster/master/etc/sshSSH server configuration directory on master node.
MASTER_VOLUME_ROOT_SSH/data/docker/postgres-cluster/master/root/sshSSH configuration directory for root user on master node.
MASTER_VOLUME_POSTGRES_SSH/data/docker/postgres-cluster/master/postgresql/sshSSH configuration directory for postgres user on master node.
MASTER_VOLUME_POSTGRES_ARCHIVE/data/docker/postgres-cluster/master/postgresql/archivePostgreSQL archive logs directory on master node.
MASTER_VOLUME_POSTGRES_DATA/data/docker/postgres-cluster/master/postgresql/dataPostgreSQL data directory on master node.
STANDBY_VOLUME_ETC_PGPOOL/data/docker/postgres-cluster/standby/etc/pgpool2PgPool configuration directory on standby node.
STANDBY_VOLUME_ETC_SSH/data/docker/postgres-cluster/standby/etc/sshSSH server configuration directory on standby node.
STANDBY_VOLUME_ROOT_SSH/data/docker/postgres-cluster/standby/root/sshSSH configuration directory for root user on standby node.
STANDBY_VOLUME_POSTGRES_SSH/data/docker/postgres-cluster/standby/postgresql/sshSSH configuration directory for postgres user on standby node.
STANDBY_VOLUME_POSTGRES_ARCHIVE/data/docker/postgres-cluster/standby/postgresql/archivePostgreSQL data directory on standby node.
STANDBY_VOLUME_POSTGRES_DATA/data/docker/postgres-cluster/standby/postgresql/dataPostgreSQL data directory on standby node.
DEBUG0Enables -x flag for bash debugging.
Examples
Notes
TODO

Tag summary

Content type

Image

Digest

Size

138.3 MB

Last updated

over 9 years ago

docker pull scalac/postgres-cluster