How to add nodes to a Docker swarm?

DockerDockerBeginner
Practice Now

Introduction

Docker Swarm is a powerful tool for orchestrating and managing Docker containers at scale. In this tutorial, you will learn how to add new nodes to your existing Docker swarm cluster, allowing you to expand your infrastructure and improve the availability and resilience of your applications.

Understanding Docker Swarm Basics

What is Docker Swarm?

Docker Swarm is a native clustering and scheduling solution for Docker containers. It allows you to manage a group of Docker engines, called a swarm, as a single virtual Docker engine. This means you can deploy your applications across multiple hosts, ensuring high availability and scalability.

Key Concepts in Docker Swarm

  • Node: A node is an instance of the Docker engine participating in the swarm. Nodes can be either managers or workers.
  • Manager Node: Manager nodes are responsible for managing the swarm, including scheduling tasks, maintaining cluster state, and handling failover.
  • Worker Node: Worker nodes receive and execute tasks dispatched from the manager nodes.
  • Service: A service defines the desired state of a set of tasks (containers) in the swarm. Services are the building blocks for deploying applications in a swarm.
  • Task: A task is a single container that is part of a service and is scheduled to run on a node.

Advantages of Docker Swarm

  • Simplicity: Docker Swarm is built into the Docker engine, making it easy to set up and manage.
  • High Availability: Swarm provides automatic load balancing and failover, ensuring your applications are highly available.
  • Scalability: You can easily scale your applications by adding or removing nodes to the swarm.
  • Security: Docker Swarm uses built-in security features, such as encrypted communication and role-based access control.

Getting Started with Docker Swarm

To get started with Docker Swarm, you'll need to set up a swarm cluster. This involves initializing a manager node and then adding worker nodes to the swarm. Let's look at an example using Ubuntu 22.04:

## Initialize the swarm on a manager node
docker swarm init

## Join a worker node to the swarm
docker swarm join --token <TOKEN> <MANAGER-NODE-IP>:2377

Once you have a swarm set up, you can start deploying your applications as services.

Adding Nodes to a Docker Swarm Cluster

Joining Worker Nodes to the Swarm

To add worker nodes to your Docker Swarm cluster, you can use the docker swarm join command. This command requires the join token, which can be obtained from the manager node.

On the manager node, run the following command to get the join token:

docker swarm join-token worker

This will output a command that you can use to join a worker node to the swarm, for example:

docker swarm join --token SWMTKN-1-49nj1cmql0jkz5s7riia...

Now, on the worker node, run the command provided by the manager node to join the swarm.

Joining Manager Nodes to the Swarm

You can also add manager nodes to your Docker Swarm cluster. This is useful for increasing the fault tolerance and high availability of your swarm.

To add a new manager node, run the following command on the new node:

docker swarm join --token SWMTKN-1-61ztec9roubhglb7xbc...

The token used to join the manager node can be obtained from an existing manager node using the following command:

docker swarm join-token manager

Verifying Node Status

After adding nodes to the swarm, you can use the docker node ls command to view the status of all the nodes in the cluster. This will show you the role of each node (manager or worker) and their current status.

docker node ls

This will output a table with information about each node in the swarm.

Removing Nodes from the Swarm

If you need to remove a node from the swarm, you can use the docker node rm command. For example, to remove a worker node:

docker node rm worker-node

To remove a manager node, you'll need to first demote it to a worker node, and then remove it:

docker node demote manager-node
docker node rm manager-node

Remember to plan for node removal carefully, as it may impact the availability and resilience of your swarm.

Managing and Monitoring Swarm Nodes

Monitoring Swarm Nodes

To monitor the health and status of your Docker Swarm nodes, you can use the following commands:

## List all nodes in the swarm
docker node ls

## Inspect a specific node
docker node inspect node-name

## View node metrics
docker node ps node-name

The docker node ls command will give you an overview of all the nodes in your swarm, including their role (manager or worker), status, and availability.

The docker node inspect command provides detailed information about a specific node, such as its IP address, resources, and labels.

The docker node ps command allows you to view the running tasks and containers on a specific node.

Managing Swarm Nodes

In addition to monitoring, you can also manage the nodes in your Docker Swarm cluster using various commands:

## Promote a worker node to a manager
docker node promote worker-node

## Demote a manager node to a worker
docker node demote manager-node

## Update node labels
docker node update --label-add label=value node-name

## Drain a node (prevent new tasks from being scheduled)
docker node update --availability drain node-name

## Make a node active again
docker node update --availability active node-name

The docker node promote and docker node demote commands allow you to change the role of a node within the swarm.

You can use the docker node update command to add, modify, or remove labels on a node. Labels can be used to apply specific constraints or preferences when scheduling tasks.

The docker node update --availability drain command allows you to temporarily take a node out of service, preventing new tasks from being scheduled on it. This can be useful for performing maintenance or upgrades on a node.

Backup and Restore Swarm State

It's important to regularly back up the state of your Docker Swarm cluster to ensure you can recover from any issues or failures. You can use the docker swarm init --force-new-cluster command to initialize a new swarm using the backup data.

By following these best practices for managing and monitoring your Docker Swarm nodes, you can ensure the reliability and resilience of your containerized applications.

Summary

By the end of this tutorial, you will have a solid understanding of how to add nodes to a Docker swarm cluster. You will be able to scale your Docker infrastructure, ensuring your applications can handle increased workloads and providing high availability for your services. This knowledge will be invaluable as you continue to build and manage your Docker-based solutions.

Other Docker Tutorials you may like