Git Product home page Git Product logo

ha-postgres-docker-stack's Introduction

HA Postgresql cluster on docker

This is a docker compose file and some helper scripts to demonstrate how to deploy a highly available postgres cluster with automatic failover using docker swarm.

I've written some blog posts that explain what's happening here in more depth, you can find them here:

The complete stack is:

  • docker swarm mode (orchestration)
  • haproxy (endpoint for db write/reads)
  • etcd (configuration, leader election)
  • patroni (governs db repliation and high availability)
  • postgres

Not implemented by default but present:

  • wal-e log shipping to s3
  • cron:
    • logical backup and test logical backups
    • physical backup and test physical backups
  • sample callback scripts for patroni events e.g. email admin on failover.

Documentation:

asciicast

Test / Development

Use the docker-stack.test.yml when running the suite or testing the stack on docker for mac for example.

Prerequisites

Tested on docker 17.09.0-ce.

If you are using the deploy and test scripts you will also need to install curl, wget, awscli, and jq.

There is also a .alias file included with useful shortcut commands. Installation instructions are here.

Once you have the docker daemon installed and running on you dev machine initiate swarm mode with the following command:

docker swarm init

Test setup

A basic test suite is included that covers cluster initiation, replication and failover.

First copy the test env template to test.env

cp test.env.tmpl test.env

Patroni will not boot without these environment variables present.

Run the test suite with

scripts/run_tests.sh [-a to keep the stack up]

The test setup also includes the pagila test dataset. See the steps in the test script for more details on how to load it.

Development

Assuming you have loaded the aliases into your shell environment you can bring the test stack by running

tsu

If you want to use docker-stack.yml instead you'll need to remove the deploy restriction conditions or else the services will never start.

You can use patroni's cli to check the cluster's status

pcli list pg-test-cluster

You can also access cluster information via http requests to the api.

curl localhost:8008/patroni | jq .

The master db is accessible on localhost:5000 and the replicas on 5001

psql -p 5000

Staging

The staging setup has been tested with docker-machine on AWS and Digital Ocean. You will find a sample deploy script included in the scripts folder for aws deployment.

Environment variables

The staging setup is slightly different to the test stack in that each dbnode expects its own env file to be present at the root of the repo e.g. db-1.env

The absolute minimum setup required to get the stack up is a combination of the inline environment variables from docker-stack.test.yml and those in test.env.tmpl.

For full configuration options consult the patroni documentation.

AWS

For deploying on AWS you will need aws cli installed, jq for json parsing and you will need to have your AWS credentials set. Docker machine will pick up on the standard aws environment variables.

scripts/deploy_aws.sh

With aws there are a lot of user specific variables which may prevent the script from working out of the box. Please consult the docker-machine aws plugin docs for more on how to configure your local environment.

Another option is to use Dockers cloudformation script to provision a docker ready environment from scratch on AWS.

Digital ocean

You will need to retrieve your credentials as described here before preceding.

For each node run:

docker-machine create
    --driver digitalocean
    --digitalocean-access-token=your-secret-token
    --digitalocean-image=ubuntu-16-04-x64
    --digitalocean-region=ams-3
    --digitalocean-size=512mb
    --digitalocean-ipv6=false
    --digitalocean-private-networking=false
    --digitalocean-backups=false
    --digitalocean-ssh-user=root
    --digitalocean-ssh-port=22

The rest of the stack setup is identical to the docker commands run in the aws deploy script.

Virtualbox

Make sure you have allocated enough RAM to run three nodes locally and then run

docker-machine create --driver virtualbox node-name

for each node.

Deploying the stack

Make sure you are executing these commands in the context of your node manager:

eval $(docker-machine env db-1)

To deploy your stack run

docker stack deploy -c docker-stack.yml pg_cluster

To check the state of your services run

docker service ls

For logs run:

docker service logs pg_cluster_haproxy 

Cleanup

Remove the hosts:

docker-machine rm db-1 db-2 db-3

Reset your docker environment:

eval $(docker-machine env --unset)

ha-postgres-docker-stack's People

Contributors

64kramsystem avatar djmaze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ha-postgres-docker-stack's Issues

There's no licence attached to this project

Hi,

I have a small project I'm experimenting with which builds off this project and while working on it today I noticed you have no licence attached to this source so I'm not sure how, where or even if I'm allowed to be doing what I'm doing.

Thanks

Dynamic config for haproxy

If the shared storage issue get sorted this is the only other blocker to easily scaling dbs e.g.

docker service scale db=5

The patroni dev dockerfile setup use confd and toml templates to generate the haproxy configuration dynamically link.

Could take this approach but I wonder is it possible to handle this with docker swarms secrets/conifgs api?

env file

Hi, I have cloned this project to my public directory on my local Centos 7 server but when I get to deploy the stack the error is "/var/www/html/pg_docker/test.env: no such file or directory". I'm running the stack from within the clonned directory.
what could be the issue, looks like I'm probably missing on where to place the project files.

thanks.

standalone clusters

Hi buddy!. thanks again for this good piece of writing. I have deployed the cluster in development and I'm pleased and confident it could hold even in a production environment, which is what I want to do.

Is there a way I can duplicate the same cluster but configure it to act as a standalone. I want to have different applications in the same production environment(same servers) but have them have independent DB clusters. how can I maybe tweak the configs? please help!
Thanks.

Cron

Best way to handle this?

Currently I have an alpine based cron service that mounts the docker socket so it can access other containers in the swarm and do what it needs to do.

Alternatively could run cron using systemd in each db container.

Database Authentication

Hi, thanks for the excellent post/repo. I have followed through but when I get to creating the database I'm stuck! first it says 'database admin' does not exist when I run export PGPASSWORD=admin from psql shell; so I cant really continue from here. Maybe I'm doing things wrongly from this point.
I have tried a non root user account but still can't get past. What could I be doing wrong? thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.