Docker Swarm Cheatsheet

I’ve been studying docker-swarms lately and wanted to write a cheatsheet of setup and commands to maintain a swarm both for my reference and yours.

These commands are for docker version: 17.03.0-ce as of August 2017.

Install docker with

curl -ssl https://get.docker.com | bash

Preparing the server

Create several instances of the one-click docker image on Ubuntu 14.

Initialize the swarm

Log into the server you want to be your manager and run:

docker swarm init --advertise-addr server_ip_address

It will give you a command to paste into the other servers. Paste the worker command into the other instances to join this server as a worker and the manager command to join as a manager.

Get worker join command

If you forgot the command or didn’t save it somewhere you can ssh back into a manager server and run.

docker swarm join-token worker

Get manager join command

docker swarm join-token manager

Enabling experimental features

Some commands won’t work in the default Digital Ocean instances. You can enable some of these commands by editing /etc/default/docker file.

nano /etc/default/dockerDOCKER_OPTS="--experimental=true"

After making edits restart docker

service docker restart

How to setup your domain name

When you connect a domain name to your ip, add A records for a few ip addresses in the swarm so if one fails the domain can connect through the other. The built-in load-balancer runs on every instance so even pointing your domain to a worker node ip will work.

Upload files to server

scp -r environment.env .env production.yml root@server_ip:~/

Deploy a new stack or update an existing stack from a compose file

SSH into your manager server. These commands only work from a manager.

ssh root@server_ip

NOTE: The new prune feature removes services no longer referenced in the compose file on updates. It may not work on your version of docker.

NOTE: Each time you run deploy it will override any manual scaling you have done to individual services and instead match the yml file scale parameters.

env $(cat .env | grep ^[A-Z] | xargs) docker stack deploy -c production.yml --with-registry-auth --prune stack_name

env $(cat .env | grep ^[A-Z] | xargs) will read variables in the .env file into your deployment.

See services running

docker stack services stack_name

See Logs for a service

stack_name_ + service_name

docker service logs stack_name_service_name

Scale a service

Creates more instances of a services containers and distributes them across qualified nodes.

docker service scale STACK_NAME_SERVICE_NAME=REPLICAS

Ex:

docker service scale main_api=3

Find out what nodes there are and which are managers

docker node ls

Promote a worker node to manager

You get the node_name from docker node ls

docker node promote node_name

List running swarms

docker stack ls

Remove an entire swarm

WARNING: Deletes all volume data (such as database information)

docker stack rm stack_name

Have a node leave a swarm

docker swarm leave

If you lose a majority of managers

Docker swarm always requires a majority of mangers out of the total number of managers to be running. So if you have 5 managers, you can only lose two and still have a majority. If you have 3 managers you can only lose 1 and still have a majority.

However if you lose the majority you can reinitialize a majority by forcing a new swarm. Just go to 1 manager node still running and run

docker swarm init --force-new-cluster --advertise-addr your_ip

This will remove all other managers and leave only the one you’re in as a manager, which will be the new majority. Then you can promote other nodes to managers. It will still be aware of your containers and running services and volumes and shouldn’t interfere. However until you force a new cluster you can’t alter, add, or remove any of the running services.

Force a rebalance of containers after adding a new node to the stack

It’s better to use scale services to gradually add containers to a new node, however you can relaunch all containers to distribute to all nodes including the one added.

NOTE: This will disrupt production

docker service ls -q > dkr_svcs && for i in `cat dkr_svcs`; do docker service update "$i" --detach=false --force ; done