Skip to content

Instantly share code, notes, and snippets.

@toff63
Last active March 7, 2016 08:54
Show Gist options
  • Save toff63/9e342e1d9ff1bb456998 to your computer and use it in GitHub Desktop.
Save toff63/9e342e1d9ff1bb456998 to your computer and use it in GitHub Desktop.
Docker Swarm demo with a frontend, backend and databse. It will use Consul for discovery and KV storage.

Create a KV store for network configs

docker-machine create -d virtualbox net-keystore
eval "$(docker-machine env net-keystore)"
docker run --restart=unless-stopped -d -p 8500:8500 -h consul progrium/consul -server -bootstrap

Create 4 hosts with a docker daemon storing network config in the KV store

echo "manager frontend backend store" | xargs -n 1  \
docker-machine create --driver virtualbox \
                     --engine-opt="cluster-store=consul://$(docker-machine ip net-keystore):8500" \
                     --engine-opt="cluster-advertise=eth1:2376" 

Start consul on manager node

eval "$(docker-machine env manager)"
docker run --restart=unless-stopped -d -p 8500:8500 -h consul progrium/consul -server -bootstrap

Start a Swarm manager container.

docker run --restart=unless-stopped  -d -p 3376:3376 \
          -t -v /var/lib/boot2docker:/certs:ro \
          swarm manage consul://$(docker-machine ip manager):8500/  \
         -H 0.0.0.0:3376 --tlsverify --tlscacert=/certs/ca.pem --tlscert=/certs/server.pem \
         --tlskey=/certs/server-key.pem

Joining the Swarm

eval "$(docker-machine env frontend)"
docker run -d swarm join --advertise=$(docker-machine ip frontend):2376 consul://$(docker-machine ip manager):8500
eval "$(docker-machine env backend)"
docker run -d swarm join --advertise=$(docker-machine ip backend):2376 consul://$(docker-machine ip manager):8500
eval "$(docker-machine env store)"
docker run -d swarm join --advertise=$(docker-machine ip store):2376 consul://$(docker-machine ip manager):8500

Use docker swarm manager to execute docker commands

export DOCKER_HOST="tcp://$(docker-machine ip manager):3376"
docker info
Containers: 3
Images: 3
Server Version: swarm/1.1.3
Role: primary
Strategy: spread
Filters: health, port, dependency, affinity, constraint
Nodes: 3
backend: 192.168.99.112:2376
  Status: Healthy
  Containers: 1
  Reserved CPUs: 0 / 1
  Reserved Memory: 0 B / 1.021 GiB
  Labels: executiondriver=native-0.2, kernelversion=4.1.13-boot2docker, operatingsystem=Boot2Docker 1.9.1 (TCL 6.4.1); master : cef800b - Fri Nov 20
19:33:59 UTC 2015, provider=virtualbox, storagedriver=aufs
  Error: (none)
  UpdatedAt: 2016-03-07T04:46:18Z
store: 192.168.99.113:2376
  Status: Healthy
  Containers: 1
  Reserved CPUs: 0 / 1
  Reserved Memory: 0 B / 1.021 GiB
  Labels: executiondriver=native-0.2, kernelversion=4.1.13-boot2docker, operatingsystem=Boot2Docker 1.9.1 (TCL 6.4.1); master : cef800b - Fri Nov 20
19:33:59 UTC 2015, provider=virtualbox, storagedriver=aufs
  Error: (none)
  UpdatedAt: 2016-03-07T04:46:18Z
frontend: 192.168.99.111:2376
  Status: Healthy
  Containers: 1
  Reserved CPUs: 0 / 1
  Reserved Memory: 0 B / 1.021 GiB
  Labels: executiondriver=native-0.2, kernelversion=4.1.13-boot2docker, operatingsystem=Boot2Docker 1.9.1 (TCL 6.4.1); master : cef800b - Fri Nov 20
19:33:59 UTC 2015, provider=virtualbox, storagedriver=aufs
  Error: (none)
  UpdatedAt: 2016-03-07T04:46:16Z
Kernel Version: 4.1.13-boot2docker
Operating System: linux
CPUs: 3
Total Memory: 3.064 GiB
Name: f91533a0c9d8

Let's setup overlay networks

docker network create frontend-network
docker network create backend-network

Let's download the code

git clone https://github.com/docker/swarm-microservice-demo-v1
cd swarm-microservice-demo-v1

Build docker images on hosts that will run them

docker -H tcp://$(docker-machine ip frontend):2376 build -t web-vote-app ./web-vote-app
docker -H tcp://$(docker-machine ip frontend):2376 pull redis
docker -H tcp://$(docker-machine ip backend):2376  build -t vote-worker ./vote-worker
docker -H tcp://$(docker-machine ip store):2376 build -t results-app ./results-app
docker -H tcp://$(docker-machine ip store):2376 pull postgres

Let's run redis and postgresql first

docker run --restart=unless-stopped --env="constraint:node==frontend" -p 6379:6379 \
           --name redis01 --net frontend-network  -d redis
docker run --restart=unless-stopped --env="constraint:node==store" --name pg \
           -e POSTGRES_PASSWORD=pg8675309 --net backend-network -p 5432:5432 -d postgres

Let's run the frontend next

docker run --restart=unless-stopped --env="constraint:node==frontend" \
           -d -p 5000:80 -e WEB_VOTE_NUMBER='01' --name frontend \
           --net frontend-network --hostname votingapp.local web-vote-app

Let's run worker

docker run --restart=unless-stopped --env="constraint:node==backend" \
-d -e WORKER_NUMBER='01' -e FROM_REDIS_HOST=1 \
-e TO_REDIS_HOST=1 --name worker01 --net frontend-network vote-worker
eval $(docker-machine.exe env backend)
docker network connect backend-network worker01

Let's run the result app

export DOCKER_HOST="tcp://$(docker-machine ip manager):3376"
docker run --restart=unless-stopped --env="constraint:node==store" \
-p 80:80 -d --name results-app --net backend-network  results-app

Check in your browser the frontend ip on port 5000 and the store ip on port 80.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment