Docker Swarm - Handson

Project - 5

·

3 min read

  • Using docker swarm we create one manager node and one/multiple worker nodes. Then the manager must distribute the load according to available resources so that the web application runs smoothly.

  • This type of system also helps when any of the worker nodes may fail, other nodes can still provide the application.

  • From a security perspective also there is a token generation for worker nodes that helps in keeping our application secure.

Create a ready-to-configure instance to set up the docker swarm. Launch the 2 Virtual Machines.

Setup Manager and worker nodes

We created two Virtual Machines and make one of them a manager and the other one a worker nodes. Here swarm-manager is the manager node and swarm-worker1 are the worker nodes. Docker should be installed in all the nodes.

sudo usermod -aG docker $USER

Check the docker version Master & Worker Nodes

docker swarm init

In the manager, the node ensures to open inbound port in security group 2377 to open for all.

Using docker node ls we can see the manager and worker nodes present in our swarm.

Add Worker node

Creating the service

The next thing that we would do is to create a service. The service would be created by the manager node. The worker nodes would be running the tasks To find out who is the worker node we run docker info which gives detailed info about the whole swarm.

docker service create --replicas 3 -p 80:80 --name Webserver nginx

We would also be verifying if our service is created using docker service ls and if the container is up and running using docker service ps name

Running our Web Application

Through Public IP along with the port number, we can view the webpages in our browser. Ensure to allow a particular port in inbound rules under the security group of instances.

You can run a docker node inspection on a manager node to view the details for an individual node. The output defaults to JSON format, but you can pass the --pretty flag to print the results in a human-readable format.

docker node inspect self --pretty

Docker swarm using YAML file

We can create a single configuration file (YAML) to create the cluster. It is a faster way to deploy our app using a single file.

version: '3.7'

services:

django-app:

image: docrek9/react-django-

app:latest

ports:

- "8000:8001"

mysql-db:

image: mysql:latest

ports: - "3306:3306"

environment:

MYSQL_ROOT_PASSWORD: test@123

Deploy the cluster and add replicas

docker stack deploy -c test.yml django

docker swarm leave -- Remove the swarm Master & Worker Node

Hope you enjoyed the concepts. Happy Learning...!!!