armsultan
1/26/2017 - 3:53 PM

Docker-machine Docker Swarm with Consul

Docker-machine Docker Swarm with Consul

#!/bin/bash

# Script to generate Swarm cluster using VMware fusion:
# -  consul-machine, consul for service discovery
# -  swarm-master
# -  swarm-node-01
# -  swarm-node-02

# Note:
# This script can be easily modified for Virtualbox. typically this involves changing the driver and entering the correct ethernet adapter name. e.g. 
# -d virtualbox 
#  --engine-opt="cluster-advertise=eth1:2376" \

# Reference
# 1. [Get started with multi-host networking](http://docs.docker.com/engine/userguide/networking/get-started-overlay/)
# 2. [Docker Machine and VMware vSphere](https://gist.github.com/philipz/2eb126f6c68568a1ef57)
# 3. [Docker Machine and Oracle virtualbox](https://docs.docker.com/machine/drivers/virtualbox/)
# 4. [Docker Machine and Oracle virtualbox](https://docs.docker.com/machine/drivers/vm-fusion/)

# Docker Machine for Consul
docker-machine \
   create \
   -d vmwarefusion \
   consul-machine

# Start Consul
docker $(docker-machine config consul-machine) run -d --restart=always \
         -p "8500:8500" \
         -h "consul" \
         progrium/consul -server -bootstrap

# Docker Swarm master
docker-machine \
  create \
  --driver vmwarefusion \
  --swarm \
  --swarm-master \
  --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" \
  --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" \
  --engine-opt="cluster-advertise=eth0:2376" \
  swarm-master

# Docker Swarm node-01
docker-machine \
  create \
  -d vmwarefusion \
  --swarm \
  --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" \
  --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" \
  --engine-opt="cluster-advertise=eth0:2376" \
  swarm-node-01

# Docker Swarm node-02
docker-machine \
  create \
  -d vmwarefusion \
  --swarm \
  --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" \
  --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" \
  --engine-opt="cluster-advertise=eth0:2376" \
  swarm-node-02

# list docker-machine instances
docker-machine ls 

# Configure to use Docker Swarm cluster
# Point your Docker environment to the machine running the swarm master
eval "$(docker-machine env --swarm swarm-master)"
docker info