Docker Swarm: Container Orchestration Using Docker Swarm

Share At:

Introduction

A swarm consists of multiple Docker hosts that run in swarm mode and act as managers (to manage membership and delegation) and workers (which run swarm services). A given Docker host can be a manager, a worker, or perform both roles.

When Docker is running in swarm mode, you can still run standalone containers on any of the Docker hosts participating in the swarm, as well as swarm services. A key difference between standalone containers and swarm services is that only swarm managers can manage a swarm, while standalone containers can be started on any daemon.

In this demonstration, we will see how to configure the docker swarm and how to perform basic tasks.

Pre-requisites

  1. For our demonstration, we will be using centos-07.
  2. We will be using 3 machines for our lab, 1 machine as a swarm Manager node and 2 swarm worker nodes. These servers have below IP details:

192.168.33.76 managernode.unixlab.com
192.168.33.77 workernode1.unixlab.com
192.168.33.78 workernode2.unixlab.com

3. The memory should be at least 2 GB and there should be at least 2 core CPUs for each node.

Please perform steps (1–4) on all the nodes — Master nodes and Worker nodes:

  1. Set the hostname and update /etc/hosts file.

2. Run below Docker installation script. This script will do the installation for you.

curl -fsSL get.docker.com/ | sh

3. Once the docker has been installed, add the “vagrant” user to be part of the “docker” group.

4. Now “start” and “enable” the docker service.

Our docker has been installed, configured, and now ready for use.

Now to Initialize the docker swarm, run the below Commands as mentioned below:

5. On Master Node Only:

docker swarm init — advertise-addr 192.168.33.76

Running the above command will generate a token that will be used for other worker nodes to join the swarm. In our case we have the below output:

docker swarm join — token SWMTKN-1–4jc5twl7y7fo0rey8s4x100hk7zekj1ww8fnqt6m2jz2nj7vnl-1zd68yh4xy4v67oe8lr4ou131 192.168.33.76:2377

6. On Worker Nodes only: Run below Docker swarm join command on all the worker nodes to join the swarm:

docker swarm join — token SWMTKN-1–4jc5twl7y7fo0rey8s4x100hk7zekj1ww8fnqt6m2jz2nj7vnl-1zd68yh4xy4v67oe8lr4ou131 192.168.33.76:2377

You will get output like “This node joined a swarm as a worker” after successful join.

7. On Manager Node: Run the command “ docker node ls”. This will show that two worker nodes have joined the swarm cluster. You will also notice that our manager node is shown as leader.

Our Docker Swarm cluster is ready for use now.

Performing simple Deployment to see the functioning of docker swarm:

Now let’s create a service for Docker swarm and deploy a container inside it.

8. Running the below command on manage rnode will deploy a container named “helloworld” using “alpine” as base image and will create 1 replica of it“ping docker.com” section is used to keep the container alive.

docker service create — replicas 1 — name helloworld alpine ping docker.com

9. The “docker service ls” command will show that 1 replica of the container is running.

10. If you need more information related to the service, run the below command:

docker service inspect — pretty helloworld

Scaling up the Deployment:

11. Now below command will scale up our deployment to 3 replicas:

docker service scale helloworld=3

12. Below command will show on which nodes the container is running:

docker service ps helloworld

Deleting a service:

13. Run the below command to delete our service called “helloworld”.

docker service rm helloworld

14. Running “docker service ls” shows no service is running.

This concludes our tutorial for “Container Orchestration Using Docker Swarm”.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back To Top

Contact Us