DEV Community

Cover image for How to Install and Configure Docker Swarm on Linux.
Surya Shankar
Surya Shankar

Posted on • Updated on

How to Install and Configure Docker Swarm on Linux.

Docker Swarm is a clustering tool that turns a group of Docker hosts into a single virtual server.
Docker Swarm ensures availability and high performance for your application by distributing it over the number of Docker hosts inside a cluster.
Docker Swarm also allows you to increase the number of container instance for the same application.

Docker Swarm is made up of two main components:

1-Manager Nodes / Master Nodes
2-Worker Nodes

Image description

Manager Nodes Manager nodes are used to handle cluster management tasks such as maintaining cluster state, scheduling services, and serving swarm mode HTTP API endpoints.
If any Manager node dies unexpectedly, other one can pick up the tasks and restore the services to a stable state.

Worker Nodes Worker nodes are used to execute containers.
You can create a swarm of one Manager node, but you cannot have a Worker node without at least one Manager node.

You can also promote a worker node to be a Manager when you take a Manager node offline for maintenance.

To do this lab we need a master node and some workers node.
Lets launch 3 instance and name one of it as master and other as workers as shown below

Image description

Now install docker package in all the above instances.

yum install docker* -y
systemctl start docker
systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Here I have used centos...
For Ubnuntu - apt install ..

Image description

Now install docker swarm in master node only

docker swarm init
Enter fullscreen mode Exit fullscreen mode

Image description
Now this instance becomes your master node.
After running that command ,it will give a token.. run that command in all your workers node

Worker-1

Image description

worker-2

Image description
as you can see both the instances becomes worker nodes

How to host a website using docker file

Ist create a docker file using vi editor inside master node

vi Dockerfile
Enter fullscreen mode Exit fullscreen mode

Image description

Our docker file is ready now lets build this using command

docker image build -t appimage:1 .
Enter fullscreen mode Exit fullscreen mode

Image description

It will build your docker file

Image description

Note
Follow the same process to deploy a website in workers node too..
or you can use docker registry or using docker hub push that image into docker hub and pull it in worker node and build it..

Launch web service in Docker Swarm

Run a service

docker service ls
Enter fullscreen mode Exit fullscreen mode
docker service create --name app-server -p 80:80 appimage:1
Enter fullscreen mode Exit fullscreen mode

Image description

You can also scale up as shown below

Image description

Test Docker Swarm
Apache web server is now running on Manager Node. You can now access web server by pointing your web browser to the Manager Node IP

or Worker Node IP as shown below:

Image description
Here I have attached a domain to my IP and used LetsEncryt to secure my website.

Top comments (0)