Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Last active December 9, 2017 15:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save subfuzion/87144f39749145ff440186bb20513f55 to your computer and use it in GitHub Desktop.
Save subfuzion/87144f39749145ff440186bb20513f55 to your computer and use it in GitHub Desktop.
Bootstrapping a local Docker 1.12 swarm with multiple virtualbox machines

Overview

It's easy enough to set up your machine as a swarm manager for local development on a single node swarm. But how about setting up multiple local nodes using Docker Machine in case you want to simulate a multiple node environment (maybe to test HA features)?

The following script demonstrates a simple way to specify the number of manager and worker nodes you want and then bootstrap a swarm.

You can also check out the sample as a Github project here.

Details

Simple script to set up a Docker 1.12 swarm for local testing. The nodes are provisioned using docker-machine with the virtualbox driver.

There is no difference between nodes other than the naming convention we use to distinguish between managers and workers. In production, however, you might choose to use higher capacity instances (cpu+memory) for managers.

Prerequisites

  • Docker v1.12
  • Docker Machine v0.8.0

For Mac and Windows, use Docker for Mac and Docker for Windows respectively. For Ubuntu, see:

https://docs.docker.com/engine/installation/linux/ubuntulinux/ https://github.com/docker/machine/releases/

My gist may help, but you need to update for the correct version of ubuntu you're running (ex, ubuntu-wily) and update the docker-machine version from v0.7.0 to v0.8.0`.

bootstrap

Update the script to set the number of managers and workers you want to use. For managers, you always want to use 1, 3, or 5. For virtualbox on localhost, 3 should is enough if you're evaluating HA, otherwise just use 1.

Using the swarm

In this example, the bootstrap script was configured with MANAGERS=1 AND WORKERS=2.

List nodes

$ docker $(m config m1) node ls

Start a sample service

$ docker $(docker-machine config m1) service create --replicas 3 --name pinger alpine ping docker.com
5lxnahrd0895hxj181e9oxdsn

List the service

$ docker $(docker-machine config m1) service ls
ID            NAME    REPLICAS  IMAGE   COMMAND
5lxnahrd0895  pinger  3/3       alpine  ping docker.com

List the service tasks (corresponds to containers)

$ docker $(docker-machine config m1) service ps pinger
ID                         NAME      IMAGE   NODE  DESIRED STATE  CURRENT STATE           ERROR
3g0qenvnnu6szwbhbvwevt9n4  pinger.1  alpine  m1    Running        Running 51 seconds ago
1y3t534iszzc0ln691k7pax22  pinger.2  alpine  w1    Running        Running 51 seconds ago
d3tc9i8h83ws5qwo4y3rprhx4  pinger.3  alpine  w2    Running        Running 51 seconds ago
#!/bin/bash
# Specify the number of managers and workers for the swarm
MANAGER=3
WORKER=3
# Create the Docker hosts
# There is no difference between nodes other than the naming convention we use to
# distinguish between managers and workers. In production, you might choose to use
# higher capacity instances (cpu+memory) for managers.
for i in $(seq 1 $MANAGER); do docker-machine create --driver virtualbox m$i; done
for i in $(seq 1 $WORKER); do docker-machine create --driver virtualbox w$i; done
# Init the swarm
docker $(docker-machine config m1) swarm init --advertise-addr $(docker-machine ip m1):2377
TM=$(docker $(docker-machine config m1) swarm join-token manager)
TW=$(docker $(docker-machine config m1) swarm join-token worker)
TOKENMANAGER=$([[ $TM =~ .*(SWMTKN[a-z0-9-]*[^ ]?).* ]] && echo ${BASH_REMATCH[1]})
TOKENWORKER=$([[ $TW =~ .*(SWMTKN[a-z0-9-]*[^ ]?).* ]] && echo ${BASH_REMATCH[1]})
# Add additional manager(s)
if [ $MANAGER -gt 1 ]; then
for i in $(seq 2 $MANAGER); do
docker $(docker-machine config m$i) swarm join --token $TOKENMANAGER --advertise-addr $(docker-machine ip m$i):2377 $(docker-machine ip m1):2377
done
fi
# Add workers
for i in $(seq 1 $WORKER); do
docker $(docker-machine config w$i) swarm join --token $TOKENWORKER $(docker-machine ip m1):2377
done
# Display nodes
docker $(docker-machine config m1) node ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment