Setup a 3 Node Docker Swarm on Ubuntu 16

In this guide we will go through the steps on setting up a 3 node Docker Swarm. For more detailed information, and setting up a scalable application have a look at this post

Getting Started:

Bootstrap Docker Swarm Setup with Docker Compose on Ubuntu 16.04:

Installing Docker:

Run the following on all 3 Nodes as the root user:

$ apt-get update && apt-get upgrade -y
$ apt-get remove docker docker-engine -y
$ apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common python-setuptools -y

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

$ apt-get update
$ apt-get install docker-ce -y
$ systemctl enable docker
$ systemctl restart docker

$ easy_install pip
$ pip install docker-compose

$ usermod -aG docker <your-user>

Testing:

Test Docker by running a Test Container

$ docker --version
$ docker-compose --version
$ docker run hello-world

In this example I do not rely on DNS, and for simplicity, I just added the host entry data into the /etc/hosts files on each node:

$ cat /etc/hosts
172.31.18.90    manager
172.31.20.94    worker-1
172.31.21.50    worker-2

Initialize the Swarm:

Now we will initialize the swarm on the manager node and as we have more than one network interface, we will specify the --advertise-addr option:

[manager] $ docker swarm init --advertise-addr 172.31.18.90
Swarm initialized: current node (siqyf3yricsvjkzvej00a9b8h) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-0eith07xkcg93lzftuhjmxaxwfa6mbkjsmjzb3d3sx9cobc2zp-97s6xzdt27y2gk3kpm0cgo6y2 \
    172.31.18.90:2377

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

If this is a scenario where we would like to add more than one manager, we would need to run the above command to add more managers.

Join the Worker nodes to the Manager:

Now, to join the worker nodes to the swarm, we will run the docker swarm join command that we received in the swarm initialisation step, like below:

[worker-1] $ docker swarm join --token SWMTKN-1-0eith07xkcg93lzftuhjmxaxwfa6mbkjsmjzb3d3sx9cobc2zp-97s6xzdt27y2gk3kpm0cgo6y2 172.31.18.90:2377
This node joined a swarm as a worker.

And to join the second worker to the swarm:

[worker-2] $ docker swarm join --token SWMTKN-1-0eith07xkcg93lzftuhjmxaxwfa6mbkjsmjzb3d3sx9cobc2zp-97s6xzdt27y2gk3kpm0cgo6y2 172.31.18.90:2377
This node joined a swarm as a worker.

To see the node status, so that we can determine if the nodes are active/available etc, from the manager node, list all the nodes in the swarm:

[manager] $ docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
j14mte3v1jhtbm3pb2qrpgwp6    worker-1  Ready   Active
siqyf3yricsvjkzvej00a9b8h *  master    Ready   Active        Leader
srl5yzme5hxnzxal2t1efmwje    worker-2  Ready   Active

If at any time, you lost your join token, it can be retrieved by running the following for the manager token:

$ docker swarm join-token manager -q
SWMTKN-1-67chzvi4epx28ii18gizcia8idfar5hokojz660igeavnrltf0-09ijujbnnh4v960b8xel58pmj

And the following to retrieve the worker token:

$ docker swarm join-token worker -q
SWMTKN-1-67chzvi4epx28ii18gizcia8idfar5hokojz660igeavnrltf0-acs21nn28v17uwhw0oqg5ibwx

At this moment, we will see that we have no services running in our swarm:

[manager] $ docker service ls
ID  NAME  MODE  REPLICAS  IMAGE

Deploying our First Service:

Let's create a nginx service with 2 replicas, which means that there will be 2 containers of nginx running in our swarm.

If any of these containers fail, they will be spawned again to have the desired number that we set on the replica option:

[manager] $ docker service create --name my-web --publish 8080:80 --replicas 2 nginx

Let's have a look at our nginx service:

[manager] $ docker service ls
ID            NAME    MODE        REPLICAS  IMAGE
1okycpshfusq  my-web  replicated  2/2       nginx:latest

After we see that the replica count is 2/2 our service is ready.

For deploying a scalable application on docker swarm have a look at this post