DEV Community

Cover image for Deploying a Web Application on Docker Swarm
Gbenga Ojo-Samuel
Gbenga Ojo-Samuel

Posted on

Deploying a Web Application on Docker Swarm

Introduction
In my last article, I automated the creation of a Docker image for an e-commerce website and pushed the image to docker hub. Click the link below for the article.
https://dev.to/gbenga700/automating-infrastructure-deployment-with-docker-and-terraform-deploying-a-containerized-e-commerce-website-5hkp
In this project, I will use the same image to deploy the application using Docker Swam. See below link to the Docker image.
https://hub.docker.com/repository/docker/ojosamuel/e-commerce_terraform/general
Before we proceed, let’s get some basic understanding of Docker Swam as an orchestration tool.

What is Docker Swarm?
Docker Swarm is a container orchestration tool that facilitates the management and deployment of containerized applications. It extends the capabilities of Docker containers to a clustered environment, allowing organizations to scale their applications seamlessly. Docker Swarm enables the creation of a swarm, a group of Docker hosts, to deploy and manage containers across multiple nodes.
Key Features of Docker Swarm:
Orchestration: Docker Swarm provides orchestration capabilities, allowing users to define and manage complex multi-container applications effortlessly. It automates the deployment, scaling, and maintenance of containerized applications, ensuring efficient resource utilization.
Scalability: One of the significant advantages of Docker Swarm is its ability to scale applications horizontally. As the demand for resources increases, additional nodes can be added to the swarm, distributing the workload, and improving application performance.
High Availability: Docker Swarm ensures high availability by distributing containers across multiple nodes. In the event of a node failure, the swarm automatically reschedules containers to healthy nodes, minimizing downtime and enhancing application reliability.
Load Balancing: Load balancing is a crucial aspect of managing containerized applications. Docker Swarm includes an integrated load balancer that distributes incoming traffic among the containers within the swarm, optimizing resource utilization and improving overall application performance.
Service Discovery: Service discovery in Docker Swarm is simplified, allowing containers to communicate with each other seamlessly. Each service within the swarm is assigned a DNS name, making it easy for containers to discover and connect to other services.
Security: Docker Swarm provides robust security features, including built-in support for Transport Layer Security (TLS) to encrypt communication between nodes. Additionally, access control mechanisms and role-based authentication contribute to creating a secure containerized environment.

Step 1: Spin-up 3 Ubuntu servers in the AWS management console.

• Launch 3 Ubuntu Server using the default VPC and Subnet.
• Instance type- t2.micro (free tier)
• Keypair – Create a new one or use an existing keypair.
• Create a security group and open ports 22, 80 and 2377.
• Using “user data” to pass a script to install docker on all the 3 servers.
• Rename the servers for easy identification

Image description

Image description

Image description

Image description

Image description

  • Servers renamed.

Image description

• Verify that Docker is running on all 3 servers.
docker_swarm_manager – Connect to the server using EC2 instance Connect and run sudo systemctl status docker command.

Image description

docker_swarm_worker1 – Connect to the server using EC2 instance Connect and run sudo systemctl status docker command.

Image description

docker_swarm_worker2 – Connect to the server using EC2 instance Connect and run sudo systemctl status docker command.

Image description

Step 2: Initialize docker swarm on docker_swarm_manager.

  • Initialize docker swarm

Run

docker swarm init ---advertise -addr 18.117.82.82
Enter fullscreen mode Exit fullscreen mode

command

Image description

  • Add a worker to this swarm Run the following command as displayed after the docker swarm init command above
docker swarm join --token SWMTKN-1-5qbi2a2vvdaxe35u0xa1wbkbuhkc3ip7vnd5vcpelp1zuuvk40-8uaw7snh5ztn12v589hu82tai 18.117.82.82:2377
Enter fullscreen mode Exit fullscreen mode

We will run this command on both worker1 and worker2 to add them to the swarm cluster.

Image description

Image description

Run docker node ls command to view information about nodes in the swarm cluster - 3 nodes in the swam cluster ( 1 manager node and 2 worker nodes)

Image description

Step 3: Create service in the docker_swarm_manager.
The create service command will install the application through the docker image from the docker hub. Run command

 docker service create --name e-commerce_website --replicas 3 --publish 80:80 ojosamuel/e-commerce_terraform:latest
Enter fullscreen mode Exit fullscreen mode

Image description

Now we have 3 services running. lets confirm with command on the manager node.

docker service ls
Enter fullscreen mode Exit fullscreen mode

Image description

Lets check if the containers are deployed and running on the nodes with the command

docker container ps 
Enter fullscreen mode Exit fullscreen mode

docker_swarm_manager

Image description

docker_swarm_worker1

Image description

docker_swarm_worker2

Image description

Lets verify that our application is available and accessible from the public IP addresses of each node within the swarm cluster.

Image description

Copy and paste the public address in your browser in the format below referencing the port mapped to the container port on your local machine.

ip address:port 
18.117.82.82:80
18.189.20.246:80
18.116.35.208:80
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Conclusion:
Docker Swarm simplifies the management of containerized applications, offering a robust and scalable solution for organizations embracing container technology. With features like orchestration, scalability, high availability, and security, Docker Swarm provides a comprehensive platform for deploying and managing distributed applications efficiently.

Thanks for Reading!!!

Top comments (0)