Docker Swarm is a powerful container orchestration tool that allows you to create and manage clusters of Docker nodes. With Docker Swarm, you can easily deploy and scale containerized applications, providing high availability and fault tolerance. In this comprehensive guide, we will walk you through the process of installing and setting up Docker Swarm on Rocky Linux servers. By the end of this article, you will have a clear understanding of how to deploy and manage containerized applications using Docker Swarm.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  1. At least three Rocky Linux servers.
  2. A non-root user with sudo/administrator privileges.

For the purpose of this guide, we will use three Rocky Linux servers with the following details:

Hostname

IP Address

Role

swarm-manager1

192.168.5.100

Swarm Manager

worker1

192.168.5.120

Node

worker2

192.168.5.121

Node

Now that we have our prerequisites ready, let’s proceed with the installation of Docker on each server.

Installing Docker

To set up Docker Swarm, you need to install Docker Engine on every server. In this guide, we will install Docker on Rocky Linux using the official Docker repository. Here’s how:

  1. Add the Docker repository for the Rocky Linux system by running the following command:
sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
  1. Once the repository is added, verify the repository list and update all packages by running the following commands:
sudo dnf repo list
sudo dnf update
  1. Install Docker by running the following command:
sudo dnf install docker-ce docker-ce-cli containerd.io
  1. Start and enable the Docker service by running the following commands:
sudo systemctl enable docker
sudo systemctl start docker
  1. Verify that the Docker service is running by running the following command:
sudo systemctl status docker

If the Docker service is running, you should see an output indicating that the service is active and running.

  1. Optionally, if you want to run Docker with a non-root user, you can add the user to the ‘docker’ group by running the following command:
sudo usermod -aG docker shapehost

Replace ‘shapehost’ with the username of your non-root user.

With Docker installed on every server, let’s move on to setting up Firewalld.

Setting up Firewalld

It is recommended to enable the firewall on every server in a production environment. By default, Firewalld is enabled on Rocky Linux, so we just need to add some TCP and UDP ports for Docker Swarm deployment. Here’s how:

  1. Add the necessary Docker Swarm ports to Firewalld by running the following commands:
sudo firewall-cmd --add-port=2377/tcp --permanent
sudo firewall-cmd --add-port=7946/tcp --permanent
sudo firewall-cmd --add-port=7946/udp --permanent
sudo firewall-cmd --add-port=4789/udp --permanent
  1. Reload Firewalld rules by running the following command:
sudo firewall-cmd --reload
  1. Verify that the Docker Swarm ports are added to Firewalld by running the following command:
sudo firewall-cmd --list-port

You should see the TCP and UDP ports necessary for Docker Swarm deployment listed.

Now that Firewalld is configured, let’s proceed with initializing the Swarm Manager.

Initializing Swarm Manager

The Swarm Manager is the central management and orchestration component of Docker Swarm. It manages the state of your application deployments and provides a declarative service model. In this section, we will initialize the Swarm Manager on the server “swarm-manager1” with the IP address “192.168.5.100”. Here’s how:

  1. Run the following command on the “swarm-manager1” server to initialize the Swarm Manager:
sudo docker swarm init --advertise-addr 192.168.5.100 --default-addr-pool 10.10.0.0/16

Replace the IP address and address pool with your own configuration if necessary.

  1. After the Swarm Manager is initialized, you will see an output similar to the following:
Swarm initialized: currentnode(node1) is now a manager.

To add a worker tothis swarm, run the following command:

docker swarm join --tokenSWMTKN-1-4qxedy87gygenejrw06hlqpuwfm6erulccfj1jhnmsn0kehbnb-2ld4g3zo36bzu8d8ss4115rhq192.168.5.100:2377

To add a manager tothis swarm, run'docker swarm join-token manager' and follow the instructions.

Take note of the command to add worker nodes to the Swarm.

  1. Verify that the Swarm mode is activated by running the following command:
sudo docker info

You should see that the Swarm mode is active and the network IP address for services is as configured during the initialization process.

  1. Check the list of available nodes on your Docker Swarm by running the following command:
sudo docker node ls

At this point, you should see only one node on your Docker Swarm, which is the Swarm Manager.

With the Swarm Manager initialized, let’s proceed with adding worker nodes to the Swarm.

Adding Worker Nodes to Swarm

Worker nodes in Docker Swarm are responsible for executing and running containers. Each worker node must have Docker Engine installed. In this section, we will add the “worker1” and “worker2” servers to the Docker Swarm. Here’s how:

  1. Run the following command on each worker node (“worker1” and “worker2”):
sudo docker swarm join --tokenSWMTKN-1-4qxedy87gygenejrw06hlqpuwfm6erulccfj1jhnmsn0kehbnb-2ld4g3zo36bzu8d8ss4115rhq192.168.5.100:2377

Replace the token and IP address with the ones provided during the Swarm Manager initialization.

  1. Once the command is executed successfully, you should see an output indicating that the worker node has joined the Swarm.
  2. Verify the list of nodes on your Docker Swarm by running the following command on the Swarm Manager:
sudo docker node ls

You should see all three nodes – the Swarm Manager and the two worker nodes.

With the worker nodes added, let’s move on to managing services on Docker Swarm.

Managing Services on Docker Swarm

In Docker Swarm, services are the units of work that represent your containerized applications. You can deploy and manage services using Docker commands. In this section, we will learn how to deploy and scale services on Docker Swarm.

Deploying a Service

To deploy a service on Docker Swarm, you need to specify the base image, ports, service name, and the number of replicas. Let’s create a new service called “test-httpd” with one replica, exposing port 8000 on the Swarm node. We will use the “httpd:alpine” image for this example. Here’s how:

  1. Run the following command to create the “test-httpd” service:
sudo docker service create --replicas1 --name test-httpd -p 8000:80 httpd:alpine

This command creates a service with one replica, maps port 8000 on the host to port 80 in the container, and uses the “httpd:alpine” image.

  1. Verify that the service is running by running the following command:
sudo docker service ls

You should see the “test-httpd” service listed with the desired configuration.

  1. Check the details of the “test-httpd” service by running the following command:
sudo docker service inspect test-httpd

This command provides detailed information about the service, including the replica count and the image used.

  1. Verify that the container is running on the Swarm Manager by running the following command:
sudo docker service ps test-httpd

You should see the container running on the Swarm Manager.

  1. Open port 8000 on all servers by running the following command:
sudo firewall-cmd --add-port=8000/tcp

This command allows incoming traffic on port 8000.

  1. Verify the “test-httpd” service by running the following curl command:
curl 192.168.5.30:8000

Replace the IP address with the IP address of your Swarm Manager. You should see the default index.html page of the “test-httpd” service.

Scaling a Service

To scale a service on Docker Swarm, you can increase or decrease the number of replicas. Let’s scale the “test-httpd” service to three replicas. Here’s how:

  1. Run the following command to scale the “test-httpd” service to three replicas:
sudo docker service scale test-httpd=3

This command increases the number of replicas to three.

  1. Verify that the service has been scaled by running the following command:
sudo docker service ps test-httpd

You should see additional replicas running on both “worker1” and “worker2” servers.

  1. Verify the service on each worker node by running the following curl command:
curl worker1:8000
curl worker2:8000

Replace the worker node names with the appropriate IP addresses. You should see the default index.html page of the “test-httpd” service on both worker nodes.

With the service deployed and scaled, let’s move on to deleting services on Docker Swarm.

Deleting Services on Docker Swarm

To clean up your Docker Swarm environment, you can delete services that are no longer needed. Let’s remove the “test-httpd” service from Docker Swarm. Here’s how:

  1. Run the following command to remove the “test-httpd” service:
sudo docker service rm test-httpd

This command removes the “test-httpd” service from Docker Swarm.

  1. Verify that the service has been deleted by running the following command:
sudo docker service inspect test-httpd

You should see an output indicating that the service does not exist.

  1. Additionally, you can verify the list of running containers and images on your Swarm by running the following commands:
sudo docker ps
sudo docker images

You should see that the container has been removed and the “httpd:alpine” image is still available.

Congratulations! You have successfully deployed and managed Docker Swarm on Rocky Linux. You now have the knowledge and skills to deploy containerized applications using Docker Swarm. Remember, Docker Swarm provides a scalable and reliable platform for running your containerized applications.

If you are looking for a reliable and scalable hosting solution for your containerized applications, consider Shape.host’s Linux SSD VPS services. Shape.host offers high-performance VPS hosting with SSD storage, ensuring fast and efficient deployment of your Docker Swarm environment.

In conclusion, Docker Swarm is a powerful tool for managing containerized applications. With its ease of use and scalability, Docker Swarm is an excellent choice for deploying and managing your applications. By following the steps outlined in this guide, you can confidently set up and utilize Docker Swarm on your Rocky Linux servers. Happy containerizing!