Red Green Repeat Adventures of a Spec Driven Junkie

Working with Multiple Node Docker Swarm

A Sunday on La Grande Jatte — 1884, 1884/86, Georges Seurat

In my previous post, Getting Started with Docker Swarm, I started a Docker Swarm on a single node and showed how to manage services on it.

This time, I will start a two node Docker Swarm and go over commands to manage multiple nodes, such as:

  • starting the Swarm
  • viewing Swarm status
  • joining the Swarm
  • obtaining join-tokens
  • and leaving the Swarm

Requirements

If you would like to follow along:

  • Install Virtualbox
  • Install Vagrant
  • Download this Vagrantfile to your local folder and modify this line:
WORKER_COUNT = 0

to be:

WORKER_COUNT = 1

Notes on Vagrantfile configuration

This makes two different virtualboxes, one named: manager, another named: worker1.

With the way the Vagrantfile configuration, connects each virtualbox to a private network with IP Addresses 192.168.100.10 and 192.168.100.11, for manager and worker1, respectively.

All actions taken on manager & worker1 are equivalent to working on separate computers.

Boot the manager

Let’s start by creating the manager virtualbox by issuing command:

$ vagrant up manager
Bringing machine 'manager' up with 'virtualbox' provider...
==> manager: Checking if box 'ubuntu/xenial64' is up to date...
...
==> manager: Machine booted and ready!
==> manager: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> manager: flag to force provisioning. Provisioners marked to run always will still run.

Now the manager is running. Confirm its status by:

$ vagrant status manager
Current machine states:

manager                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

Let’s go and configure manager for Swarm!

Start Swarm on manager

Let’s get into the manager box to start working with Docker Swarm!

$ vagrant ssh manager
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-134-generic x86_64)
...
Last login: Tue Sep 25 00:19:39 2018 from 10.0.2.2

Let’s check if Docker Swarm is running or not:

vagrant@manager:~$ sudo docker service ls
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

Good, now that means we can start Docker Swarm with command:

vagrant@manager:~$ sudo docker swarm init
Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (10.0.2.15 on enp0s3 and 192.168.100.10 on enp0s8) - specify one with --advertise-addr
vagrant@manager:~$ sudo docker swarm init --advertise-addr 192.168.100.10
Swarm initialized: current node (vcvxowyvz2q80sfcymdqumzfd) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-0d4lym64rmugfp6nhybhy0r6i8v108gar84nzzpie8q6xvn9v1-dnz6k2mm70gjt19v1pvbhzuvx 192.168.100.10:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Save this output for worker1 to join manager as part of the swarm.

Lost join-token?

IF you ever need to get the worker join token again from the manager, this would be the command:

vagrant@manager:~$ sudo docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-0d4lym64rmugfp6nhybhy0r6i8v108gar84nzzpie8q6xvn9v1-dnz6k2mm70gjt19v1pvbhzuvx 192.168.100.10:2377

You know, asking for a friend. ;-)

Current Swarm Status

Now that we have initialized a Swarm, how do we know its status? By using the node command:

vagrant@manager:~$ sudo docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
vcvxowyvz2q80sfcymdqumzfd *   manager             Ready               Active              Leader              18.06.1-ce

So far, there’s only the manager node on the swarm. Let’s fix that by adding worker1 using the join-token (you printed a copy of that, right?! If not, use the join-token command.

Start worker1

Just like with manager, let’s boot up worker1 and login:

$ vagrant up worker1
Bringing machine 'worker1' up with 'virtualbox' provider...
==> worker1: Checking if box 'ubuntu/xenial64' is up to date...
...
==> worker1: Machine booted and ready!
==> worker1: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> worker1: flag to force provisioning. Provisioners marked to run always will still run.
$ vagrant ssh worker1
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-134-generic x86_64)
...
Last login: Tue Sep 25 00:19:37 2018 from 10.0.2.2

Verify worker1’s status by:

$ vagrant status worker1
Current machine states:

worker1                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

Let’s get worker1 on manager’s swarm:

Join manager’s Swarm

With worker1 running, let’s use the worker join-token and connect to the manager Swarm. Lost the join-token? Get it again!

vagrant@worker1:~$ sudo docker swarm join --token SWMTKN-1-0d4lym64rmugfp6nhybhy0r6i8v108gar84nzzpie8q6xvn9v1-dnz6k2mm70gjt19v1pvbhzuvx 192.168.100.10:2377
This node joined a swarm as a worker.

Tada~ Wasn’t that easy? But, how can we see workers in swarm? By running node ls command on the manager instance:

vagrant@manager:~$ sudo docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
vcvxowyvz2q80sfcymdqumzfd *   manager             Ready               Active              Leader              18.06.1-ce
1w0g3yqymvaykrrbpoc15poy6     worker1             Ready               Active                                  18.06.1-ce

So, worker1 is officially part of the Swarm!

Leave Swarm

Now that worker1 is part of the Swarm, let’s leave it.

vagrant@worker1:~$ sudo docker swarm leave
Node left the swarm.

And the best way to check, use the node ls command from manager:

vagrant@manager:~$ sudo docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
vcvxowyvz2q80sfcymdqumzfd *   manager             Ready               Active              Leader              18.06.1-ce
1w0g3yqymvaykrrbpoc15poy6     worker1             Down                Active                                  18.06.1-ce

Interesting, worker1 left the swarm, but manager just sees worker1’s status as: Down.

Conclusion

This has been a review on how to work with multiple node Docker Swarm commands such as:

starting the Swarm from a manager node: sudo docker swarm init
obtaining worker join-token from a manager node: sudo docker swarm join-token worker
viewing Swarm status from a manager node: sudo docker node ls
joining the Swarm use the Swarm join token, which is in the form: sudo docker swarm join ...
leaving the Swarm from the leaving node: sudo docker swarm leave

This is just the start of managing multiple node Docker Swarm and I will continue my explorations in future articles.