Skip to content

Instantly share code, notes, and snippets.

@frank-leap
Last active March 16, 2023 13:22
Show Gist options
  • Save frank-leap/33d8c0c034f98d995e5520bbcad2990b to your computer and use it in GitHub Desktop.
Save frank-leap/33d8c0c034f98d995e5520bbcad2990b to your computer and use it in GitHub Desktop.
Script to create a Docker Swarm cluster in Digital Ocean
#!/bin/bash
# Configuration
#export DIGITALOCEAN_ACCESS_TOKEN= # Digital Ocean Token (mandatory to provide)
export DIGITALOCEAN_SIZE=512mb # default
export DIGITALOCEAN_REGION=nyc3 # default
export DIGITALOCEAN_PRIVATE_NETWORKING=true # default=false
#export DIGITALOCEAN_IMAGE="ubuntu-15-04-x64" # default
# For other settings see defaults in https://docs.docker.com/machine/drivers/digital-ocean/
# DigitalOcean uses this version thus to avoid issues with the beta... --engine-install-url https://test.docker.com/ should work though
#export DOCKER_API_VERSION=1.23
# Consul Key-Value Store
echo "Create Consul KV Store"
docker-machine create -d digitalocean --engine-install-url https://test.docker.com/ docker-consul
docker $(docker-machine config docker-consul) run -d --net host progrium/consul --server -bootstrap-expect 1
consulip=$(docker-machine ip docker-consul)
# Swarm Manager
echo "Create Swarm Manager"
docker-machine create \
-d digitalocean \
--swarm --swarm-master \
--swarm-discovery consul://${consulip}:8500 \
--engine-install-url https://test.docker.com/ \
--engine-opt "cluster-store consul://${consulip}:8500" \
--engine-opt "cluster-advertise eth1:2376" \
docker-swarm-manager
# Swarm Agents
agents="docker-swarm-agent-01 docker-swarm-agent-02"
for agent in $agents; do
(
echo "Creating ${agent}"
docker-machine create \
-d digitalocean \
--swarm \
--swarm-discovery consul://${consulip}:8500 \
--engine-install-url https://test.docker.com/ \
--engine-opt "cluster-store consul://${consulip}:8500" \
--engine-opt "cluster-advertise eth1:2376" \
$agent
) &
done
wait
# Information
echo ""
echo "<< Docker Swarm Cluster >>"
echo "Environment variables to connect trough docker cli"
docker-machine env --swarm docker-swarm-manager
@frank-leap
Copy link
Author

Couple of comments:

  • A previous option I tried before Consul was token=$(docker run --rm swarm create) but it failed with a timeout, probably because I tested behind a firewall and did not pass the HTTP proxy
  • Pay attention to the ethernet interface: previously I tested with CentOS 7 (instead of Ubuntu 15) and the interface to be used was enp0s3 (while for Ubuntu 15 is eth1)

@frank-leap
Copy link
Author

frank-leap commented Jun 26, 2016

More issues I faced:

  • Running Docker Machine from Ubuntu the script failed due to I/O Timeout; I ended up trying from Mac and then it worked like a charm...?
  • ...well, not really! In OSX the Docker version was 1.12 while in DigitalOcean is 1.11 thus the script failed with a different error:
    Error response from daemon: client is newer than server (client API version: 1.24, server API version: 1.23)
  • ...to solve that I followed the workaround to downgrade mentioned here docker/machine#3518:
    export DOCKER_API_VERSION=1.23
  • However the best approach is to specify test.docker.com when creating the image, see: --engine-install-url https://test.docker.com...?
  • Not really! The URL to be provided is https://experimental.docker.com/, see: docker/machine#3518 (again)

@frank-leap
Copy link
Author

screen shot 2016-06-26 at 4 41 40 pm

@frank-leap
Copy link
Author

frank-leap commented Jun 26, 2016

An introductory tutorial about Docker Swarm is here: https://docs.docker.com/engine/swarm/swarm-tutorial/
An Ansible playbook for having Docker Swarm 1.12 in Digital Ocean is here: https://github.com/russmckendrick/digitalocean-docker-swarm

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