DEV Community

Cover image for DOCKER SWARM
Bibhu Sunder
Bibhu Sunder

Posted on

DOCKER SWARM

WHAT IS DOCKER?
Docker is a tool which is used to automate the deployment of applications in lightweight containers so that applications can work efficiently in different environments.
Point to be noted- Container is a software package that consists of all the dependencies required to run an application.

Advantages of docker-

  • Multiple containers run on the same hardware

  • Maintains isolated applications

  • High productivity

  • Quick and easy configuration

WHAT IS DOCKER SWARM?
Docker swarm is a service which allows user to create and manage a cluster of Docker nodes and schedule containers.

Each node of a Docker Swarm is a Docker daemon and all Docker daemons interact using the Docker API.

Here, services can be deployed and accessed by nodes of same cluster.

Now lets see how we can set-up Docker Swarm using AWS EC2 instances.

First of all create at least 4 EC2 instances. One of them will be used as master node and other three will be used as worker node.
Allow ssh, http and https in the inbound rules of the security group.

EC2 instances

Then we need to install docker in all the 4 instances using the command "yum install docker* -y"

docker installation

Once installation is completed we need to start docker by the following commands

  • systemctl start docker
  • systemctl enable docker

Now we need to configure the master node as the master node. the command for that is

  • docker swarm init

docker swarm init

To convert all the other nodes into worker node we need to copy the highlighted area given in the above picture and past that command in all other nodes which we want to make as worker node.

To check all the available docker nodes we can write

  • docker node ls

docker nodes

Then we need to create a docker file where we will write the script to launch an image and provide the web page which we want to launch.
To create the docker file we need to write "vi Dockerfile"

docker files

Now we need to built an image.

docker image

We need to perform the above two steps in all the worker nodes too.

Then we need to create service.
-docker service create --name app-server -p 80:80 appimage:1

services

It will create a container in any one node but if we hit the public IP address of any of the instance we can see the web page is running regardless in which instance the container is.

public ip

web page

It is very easy to increase or decrease the numbers of services in docker, we can simply do it by the command given below.

increase service

We can also view our containers using a docker visualizer, which is a graphical representation of all the containers and instances.
To set-up visualizer we need to run the given command.

  • docker service create --name=viz --publish=8080:8080/tcp --constraint=node.role==manager --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock dockersamples/visualizer

docker visualizer

Now if you hit the public ip of the master node mapped with port 8080 you can see the Docker visualizer.

docker visualizer

This was the whole process of how to set-up docker swarm.

Top comments (0)