How to Install and Configure Docker Swarm on CentOS 7 / Ubuntu 16.04 / Fedora 26/25

Adding Nodes to a Swarm Cluster

If you remember the output of “swarm init” command we ran on the manager,  the output has the token to add nodes to the cluster. Just go back to that output and copy it, then paste that on the worker nodes.

Node 1:

Install and Configure Docker Swarm on CentOS 7 – Adding Node 1 to Docker Swarm

Node 2:

Install and Configure Docker Swarm on CentOS 7 – Adding Node 2 to Docker Swarm

On Docker manager, run the “docker node ls” command to list down the worker nodes.

[root@dockerm ~]# docker node ls

Output:

ID                           HOSTNAME               STATUS  AVAILABILITY  MANAGER STATUS
azaocjsw83llg4n08sqx8rg05    node2.itzgeek.local    Ready   Active
n6eal0ysgzhz3mug3tx3ssf3u    node1.itzgeek.local    Ready   Active
uhh38rpazd5tnzjph2g5rhgxy *  dockerm.itzgeek.local  Ready   Active        Leader

Troubleshooting Tip

Retrieve Token

If you had lost the token of any swarm, run the following command on the manager node to retrieve it.

[root@dockerm ~]# docker swarm join-token worker

Output:

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

    docker swarm join \
    --token SWMTKN-1-5y1u36a0osykywxeme2akpjp4jgx2l67mbqbc4fnazs39bp314-e6djx8ma2qnevolztpplkn26j \
    192.168.12.10:2377

Now you can use the above command to attach the nodes to swarm.

Certificate Error:

If you get a certificate error, ensure all the nodes are in time sync.

Error response from daemon: x509: certificate has expired or is not yet valid

Launching service in Docker Swarm mode

Now, the Docker Swarm is up and running. Let’s test a swarm by deploying service on it. Here, we will run a service called “web” with the image of “httpd“.

Run the following command on the Docker manager.

[root@dockerm ~]# docker service create --name web -p 80:80 httpd
4vpj4xihx7i3bim00f60zfbwc
[root@dockerm ~]#

If you specify –replicas <unit>, docker swarm will create “unit” number of tasks (or containers) of the same image for the specified service across the Docker worker nodes.

To know which services are running on a cluster and how many replicas are there.

[root@dockerm ~]# docker service ls
ID            NAME  MODE        REPLICAS  IMAGE
4vpj4xihx7i3  web   replicated  1/1       httpd:latest
[root@dockerm ~]#

To find on which node the service is running on by using the following command.

[root@dockerm ~]# docker service ps web
ID            NAME   IMAGE         NODE                 DESIRED STATE  CURRENT STATE               ERROR  PORTS
vz5wp563rok2  web.1  httpd:latest  node2.itzgeek.local  Running        Running about a minute ago
[root@dockerm ~]#

In our example, the service “web” is running on “node2.itzgeek.local“. Since we are running a web service, you can verify the service by pointing your browser to http://node2.itzgeek.local or http://192.168.12.30. You should get the following page.

Install and Configure Docker Swarm on CentOS 7 – Service Running on Node 1

Mesh Networking

Let’s see, what is mesh networking?. Try to point your browser to any another node. (Ex. http://node1.itzgeek.local or http://192.168.12.20) on your Docker Swarm.

Install and Configure Docker Swarm on CentOS 7 – Service running on Node 2

Viola! You got the same page.

A service (Ex. “web”) running on a node can be accessed on any other node of the Docker cluster, not just the one it is running on.

Scale up and Scale down service in the swarm

In Docker swarm, we can scale up or down a service by creating additional instances. As you know, currently we have only one container for service “web” running on Docker manager. To test the scalability, let’s scale the service “web” to “five” instance.

[root@dockerm ~]# docker service scale web=5
web scaled to 5
[root@dockerm ~]#

Check where your instances are started.

[root@dockerm ~]# docker service ps web


ID            NAME   IMAGE         NODE                   DESIRED STATE  CURRENT STATE          ERROR  PORTS
0gu4lkbjq12x  web.1  httpd:latest  node1.itzgeek.local    Running        Running 5 hours ago
5jgd6308ntp1  web.2  httpd:latest  node2.itzgeek.local    Running        Running 5 hours ago
3wpqwalt3urj  web.3  httpd:latest  dockerm.itzgeek.local  Running        Running 6 seconds ago
lxn71f7766xt  web.4  httpd:latest  dockerm.itzgeek.local  Running        Running 5 seconds ago
nfonpcu7ecol  web.5  httpd:latest  node1.itzgeek.local    Running        Running 5 hours ago

In some cases, we may likely to scale down.

[root@dockerm ~]# docker service scale web=3
web scaled to 3
[root@dockerm ~]#

Verify the instances by running following command. Only three containers are running right now.

[root@dockerm ~]# docker service ps web

ID            NAME   IMAGE         NODE                   DESIRED STATE  CURRENT STATE               ERROR  PORTS
0gu4lkbjq12x  web.1  httpd:latest  node1.itzgeek.local    Running        Running 5 hours ago
5jgd6308ntp1  web.2  httpd:latest  node2.itzgeek.local    Running        Running 5 hours ago
3wpqwalt3urj  web.3  httpd:latest  dockerm.itzgeek.local  Running        Running about a minute ago

You can also verify the “web” service by going to “http://192.168.12.10“,  “http://192.168.12.20” and “http://192.168.12.30“. You should get the following page.

Install and Configure Docker Swarm on CentOS 7 – Verifying the Service

High Availability or Container Self-Healing

Container self-healing feature in the swarm mode provides high availability for your service. If anything goes wrong with a container or a worker node, the manager will make sure atleast 3 containers must be running for service “web“.

To test the self-healing, just stop the Docker service running on any one of the worker nodes. For Ex, I’m stopping the Docker service on “node2.itzgeek.local

[root@node2 ~]# service docker stop
Redirecting to /bin/systemctl stop  docker.service
[root@node2 ~]#

Now we can see that a new container is launched on “node1.itzgeek.local” to make the service “web” highly available.

[root@dockerm ~]# docker service ps web

ID            NAME       IMAGE         NODE                   DESIRED STATE  CURRENT STATE          ERROR  PORTS
0gu4lkbjq12x  web.1      httpd:latest  node1.itzgeek.local    Running        Running 5 hours ago
twr2u1k3jlg3  web.2      httpd:latest  node1.itzgeek.local    Running        Running 5 hours ago
5jgd6308ntp1   \_ web.2  httpd:latest  node2.itzgeek.local    Shutdown       Running 5 hours ago
3wpqwalt3urj  web.3      httpd:latest  dockerm.itzgeek.local  Running        Running 8 minutes ago

That’s All.

centos 7dockerfedora 25swarmubuntu 16.04
Comments (0)
Add Comment