How to get started with Load balancing using Docker swarm

Table of contents

Introduction

In the world of containerization and microservices, orchestrating and managing containers efficiently is essential. Docker Swarm is a powerful tool that simplifies container orchestration, making it easier to deploy and manage containers at scale. In this blog, we’ll explore Docker Swarm, its key features, and how it can benefit your containerized applications.

Prerequisites

What is Docker Swarm?

Docker Swarm is Docker’s built-in solution for grouping and managing containers. It enables you to create and manage a swarm of Docker nodes as a single virtual system. This swarm can be used to deploy and manage containerized applications across multiple machines, making it easy to scale your applications horizontally.

Key Features of Docker Swarm

1. Scalability

One of the primary reasons to use Docker Swarm is its ability to scale your applications effortlessly. You can add or remove worker nodes from the swarm to accommodate changes in load or application requirements. Docker Swarm ensures that containers are evenly distributed across the swarm, optimizing resource utilization.

2. Service Discovery

Docker Swarm includes built-in service discovery, allowing containers within the swarm to find and communicate with each other by name. This simplifies networking for microservices architectures, as you don’t need to manage IP addresses or complex networking configurations manually.

3. Load Balancing

Docker Swarm offers load balancing for services, distributing incoming requests evenly across containers. This ensures high availability and fault tolerance, as traffic is automatically redirected to healthy containers in case of failures.

4. Rolling Updates and Rollbacks

Updating containerized applications can be challenging, but Docker Swarm makes it easier. You can perform rolling updates, which gradually replace old containers with new ones, minimizing downtime. If an update causes issues, Docker Swarm supports rollbacks to the previous version, ensuring service continuity.

5. Secure by Default

Security is a top most concern when working with containers. Docker Swarm provides security features like mutual TLS (Transport Layer Security) authentication and encryption for container-to-container communication. It also integrates with external identity providers for access control.

Docker Swarm Initialization

Getting started with Docker Swarm is relatively straightforward:

Initialize a Swarm: Use the docker swarm init command on the manager node to create a new swarm. This node acts as the control plane for managing the swarm.

docker swarm init --advertise-addr <Your-device-IP>

(To find your device ip use the following command, hostname -I)

The final command should be,

docker swarm init --advertise-addr xxx.xxx.xxx.xxx

Output:

You should have received a similar output with different values.

Copy the command highlighted above in your computer.

Add Worker Nodes

Other machines can join the swarm as worker nodes using the docker swarm join command. This expands the swarm’s capacity and resources.

On a second machine with docker installed, paste the command copied above. It should be similar to this output command below,

docker swarm join --token SWMTKN-1-387uwpzxbr77spml3k3xerh95h46pyxzgi43utfoxs9mxy7j7s-17jx93l0hths5skufaljgq55c 192.168.0.200:2377

Deploy Services: Define the services you want to run in the swarm using Docker Compose or the docker service command. Docker Swarm will distribute and manage these services across the nodes.

 To check the number of nodes connected to your swarm, Enter the following commands in your host system:

docker node ls

Output:

Now, you can deploy a service to the Docker Swarm. A service represents a long-running task or application, such as a web server or a database. Here’s an example of deploying an Nginx web server as a service:

docker service create --replicas 3 --name web-server nginx

Output:

To list the services running in the Swarm, use:

docker service ls

Output:

To access service, we need to expose it by using the following command,

docker service update web-server --publish-add 8080:80

(By default, Nginx will run on port 80 by default to check the same, Open a browser and check the same)

Output:

Scale and Manage: You can scale services up or down, update services, and monitor the swarm using Docker commands or orchestration tools like Portainer or Kubernetes.

You can scale the service up or down by changing the number of replicas. For example, to scale the “web-server” service to 5 replicas,

docker service scale web-server=5

To get more details about a service, 

docker service inspect web-server

Output:

You can update a service by changing its image or configuration. For example, to update the “web-server” service to use a different image,

docker service update --image httpd:latest web-server

Output:

To remove a service,

docker service rm <service name>

To create a service from an existing docker-compose file docker stack command is used.

Create a docker-compose file

For example:

services:

  web:
	image: nginx:latest
	ports:
  	- "8080:80"

In the host/manager pc run,

 docker stack deploy -c docker-compose.yaml stack-1

To verify that the services have been deployed successfully, you can use the following commands:

docker stack ls

To list services within a stack,

docker stack services stack-1

Services can also be scaled to increase the number of replicas,

docker service scale stack-1_web=3

Output:

To redeploy changes after updating the Docker Compose file, use the same command you used to create the stack,

 docker stack deploy -c docker-compose.yaml stack-1

To delete a stack,

docker stack rm stack-1

Output:

If you want to remove a node from the Swarm, run the following command in the second machine,

docker swarm leave

To remove the swarm from host server,

docker swarm leave --force

Note: The above command will delete all the services created.

Run Docker As Normal User

To run docker as normal user, you have to add your current user in the docker group. To do so, Enter the following command,

sudo usermod -aG docker $USER

after entering the above command, logout and login again for the changes to take effect.

Conclusion:

Docker Swarm simplifies container orchestration by providing a user-friendly and integrated solution for deploying and managing containers at scale. Whether you’re running microservices, legacy applications, or experimenting with containerization, Docker Swarm can help you achieve scalability, high availability, and ease of management. As containers continue to play a crucial role in modern software development, Docker Swarm remains a valuable tool in your container orchestration toolkit.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top