Linux

How to Set Up a Docker Swarm Cluster on Ubuntu

How to Set Up a Docker Swarm Cluster on Ubuntu

Docker Swarm is a powerful tool for orchestrating Docker containers, allowing you to manage a cluster of Docker nodes as a single virtual system. Setting up a Docker Swarm cluster on Ubuntu is relatively straightforward and can significantly improve your container management capabilities. In this guide, we’ll walk you through the process of setting up a Docker Swarm cluster on Ubuntu, covering the installation, initialization, and management of your swarm.

Prerequisites
Before you start, ensure that you have the following prerequisites in place:

  • Ubuntu Server: You need at least two machines (physical or virtual) running Ubuntu 16.04 or later. One will act as the manager node, and the others will be worker nodes.
  • Docker Installed: Docker must be installed on all the nodes. You can follow the official Docker installation guide for Ubuntu.
  • Sudo Privileges: Ensure that you have sudo privileges on all nodes.
  • Network Configuration: All nodes should be on the same network, and they should be able to communicate with each other.

Step 1: Install Docker
If Docker is not already installed, use the following commands to install it on each node. Update your package index and install Docker:

sudo apt-get update
sudo apt-get install docker.io

After the installation is complete, enable and start the Docker service:

sudo systemctl enable docker
sudo systemctl start docker

Verify that Docker is installed correctly by checking the version:

docker –version

Step 2: Initialize Docker Swarm
Choose one of your nodes to act as the manager node. On this node, run the following command to initialize the swarm:

sudo docker swarm init –advertise-addr

Replace with the IP address of your manager node. After running this command, you should see output similar to the following:

  • Swarm initialized: current node is now a manager.

To add a worker to this swarm, run the following command:
docker swarm join –token SWMTKN-1-0b… :2377
Take note of the command provided to join worker nodes. You will need it in the next step.

Step 3: Join Worker Nodes
On each of the worker nodes, run the command you copied from the manager node’s output. For example:

sudo docker swarm join –token SWMTKN-1-0b… :2377

This command will allow the worker nodes to join the swarm. After executing it, you should see a confirmation message indicating that the node has successfully joined the swarm.

Step 4: Verify Swarm Status
To verify that the worker nodes have joined the swarm, go back to your manager node and run:

sudo docker node ls

This command will list all nodes in the swarm, along with their status. You should see your manager node and all the worker nodes listed. The status should show as “Ready” for each node.

Step 5: Deploy a Service
Now that your swarm cluster is set up, you can deploy services. For example, let’s deploy a simple Nginx service:

sudo docker service create –name my-nginx –replicas 3 -p 80:80 nginx

This command will create a service named “my-nginx” with three replicas and expose it on port 80. Docker will automatically distribute the replicas across the available nodes in the swarm.

To check the status of your service, use the following command:

sudo docker service ls

You can also check the running tasks associated with the service:

sudo docker service ps my-nginx

Step 6: Scale Your Service
One of the benefits of using Docker Swarm is the ability to easily scale your services. If you want to increase the number of replicas for your Nginx service, you can use the following command:

sudo docker service scale my-nginx=5

This command changes the number of replicas to five. Docker Swarm will handle the deployment of the new replicas automatically.

Step 7: Updating Services
Updating services is also straightforward in Docker Swarm. To update your Nginx service to a new version, you can run:

sudo docker service update –image nginx:latest my-nginx

This command updates the service to use the latest version of the Nginx image. Docker Swarm will perform a rolling update to ensure there is no downtime.

Nginx Official page

Docker Swarm Cluster

Thank you for visiting our page! Be sure to explore our other article via the link below to boost your Linux expertise. Also, don’t miss our guide on How to Enable and Use PipeWire for Superior Audio on Linux! 🙂

How to Enable and Use PipeWire for Superior Audio on Linux

Related Articles

Leave a Reply

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

Back to top button