Docker Swarm is a powerful container orchestration tool that allows you to deploy and manage containerized applications across multiple servers. In this comprehensive guide, we will walk you through the process of setting up Docker Swarm on Debian 11 servers. We will cover everything from installing Docker to initializing the Swarm Manager and adding worker nodes. By the end of this guide, you will have a solid understanding of how to leverage Docker Swarm for efficient application deployment.
Before we begin, let’s ensure that we have all the necessary prerequisites in place. You will need multiple Debian 11 servers and a non-root user with sudo/root administrator privileges. For the purpose of this guide, we will assume the following server details:
Make sure you have SSH access to all the servers and have the necessary network connectivity established.
2. Installing Docker
To begin the installation process, we will first add the official Docker repository to ensure that we are installing the latest version of Docker. Open a terminal on each of your Debian servers and follow the steps below:
- Install the required packages:
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release -y
- Add the GPG key for the Docker repository:
sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- Add the Docker repository for Debian:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Update the package index:
sudo apt update
- Install Docker packages:
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
- Start and enable the Docker service:
sudo systemctl start docker sudo systemctl enable docker
- Verify the Docker service status:
sudo systemctl status docker
Congratulations! You have successfully installed Docker on all your Debian servers. Next, let’s proceed to set up the firewall.
3. Setting up Firewall
To ensure the security and proper functioning of your Docker Swarm, it is essential to set up a firewall. In this guide, we will use UFW (Uncomplicated Firewall) as the default firewall for your Debian servers. Follow the steps below to configure the firewall:
- Install UFW:
sudo apt install ufw -y
- Allow SSH connections:
sudo ufw allow "OpenSSH"
- Enable UFW:
sudo ufw enable
- Open the required ports for Docker Swarm:
sudo ufw allow 2377/tcp sudo ufw allow 7946/tcp sudo ufw allow 7946/udp sudo ufw allow 4789/udp
- Verify the enabled UFW rules:
sudo ufw status
Great! Your firewall is now configured to allow the necessary traffic for Docker Swarm. Let’s move on to initializing the Swarm Manager.
4. Initializing Swarm Manager
The Swarm Manager is the central node of the Docker Swarm deployment. It manages the swarm state and provides centralized management and orchestration. In this section, we will initialize the Swarm Manager using the following command:
docker swarm init --advertise-addr 192.168.5.100 --default-addr-pool 10.20.0.0/16
192.168.5.100 with the IP address of your Swarm Manager. Note that the
--default-addr-pool flag specifies the IP address range for the containers in the Swarm. Once the Swarm Manager is initialized, you will see a join token that can be used to add worker nodes to the Swarm.
To verify that Swarm mode is enabled, run the following command:
You should see the Swarm mode as “active” with the pool of IP addresses defined earlier. Additionally, you can check the list of nodes in the Swarm using the following command:
docker node ls
At this point, we have successfully initialized the Swarm Manager. It’s time to add worker nodes to the Swarm.
5. Adding Worker Nodes to Swarm
Worker nodes are responsible for executing and running containers in the Docker Swarm. To add worker nodes to the Swarm, follow these steps:
- On each worker node, run the following command with the join token obtained from the Swarm Manager initialization:
docker swarm join --tokenSWMTKN-1- 5tui5dveqw6n2b0m6suut0w7nf9w0wb5jyvfhmykb4jdehps1j-ag99f7750mmnim6ao5yp0x3s2 192.168.5.100:2377
Replace the token and IP address with the corresponding values from your setup.
- On the Swarm Manager, verify the list of nodes in the Swarm:
docker node ls
You should see the newly added worker nodes with the status “Ready.”
Congratulations! You have successfully added worker nodes to your Docker Swarm. Now let’s move on to managing services on the Swarm.
6. Managing Services on Docker Swarm
Services in Docker Swarm represent containerized applications that can be deployed and managed across the Swarm. In this section, we will cover the basic deployment and management of services on Docker Swarm.
To create a new service, use the following command:
docker service create --replicas 1 --name test-httpd -p 8000:80 httpd:alpine
This command creates a service named “test-httpd” with one replica, exposing port 8000 on the Swarm node. We are using the “httpd:alpine” image for this example.
To verify the running services, use the following command:
docker service ls
You should see the “test-httpd” service listed with its details, including the number of replicas and the exposed ports.
To get more information about a specific service, use the following command:
docker service inspect test-httpd
This command provides detailed information about the service, including the replicas, image, and other configuration options.
If you want to check the running containers for a specific service, use the following command:
docker service ps test-httpd
This command lists the running containers for the “test-httpd” service.
To access the service, open port 8000 on all servers using the following command:
sudo ufw allow 8000/tcp
Now you can access the service using the Swarm Manager’s IP address and the specified port:
Congratulations! You have successfully deployed and accessed a service on your Docker Swarm. Let’s move on to scaling the services.
7. Scaling Services on Docker Swarm
Scaling services in Docker Swarm allows you to increase or decrease the number of replicas for a specific service. This ensures high availability and load balancing. To scale a service, use the following command:
docker service scale test-httpd=3
This command scales the “test-httpd” service to three replicas. You can adjust the number of replicas as per your requirements.
To verify the running containers for the scaled service, use the following command:
docker service ps test-httpd
This command lists the running containers for the scaled service, which should now show three replicas spread across the Swarm nodes.
You can also check the running containers on each worker node individually using the following commands:
You should see the containers running on the respective worker nodes.
Congratulations! You have successfully scaled your service on the Docker Swarm. Now let’s proceed to deleting services.
8. Deleting Services on Docker Swarm
When you no longer need a service in your Docker Swarm, you can easily remove it. To delete a service, use the following command:
docker service rm test-httpd
This command removes the “test-httpd” service from the Docker Swarm.
To verify that the service has been deleted, use the following command:
docker service inspect test-httpd
You should see an error message indicating that the service does not exist.
Additionally, you can check the list of running containers and images on your Swarm using the following commands:
docker ps docker images
You should see that the containers have been removed, but the Docker images are still available on your servers.
Congratulations! You have successfully deleted the service from your Docker Swarm. Let’s move on to the conclusion.
We covered everything from installing Docker to initializing the Swarm Manager, adding worker nodes, managing services, scaling services, and deleting services. By following this guide, you have gained a solid understanding of how to leverage Docker Swarm for efficient application deployment in a clustered environment.
Docker Swarm provides a simple and straightforward way to manage and orchestrate containers, allowing you to scale your applications and ensure high availability. With the ability to add multiple Swarm Managers, you can create a fault-tolerant environment that can handle large-scale deployments.
If you are looking for a reliable and efficient cloud hosting solution for your Docker Swarm deployment, consider Shape.host’s Linux SSD VPS services. With Shape.host, you can benefit from their expertise in providing secure and scalable hosting solutions. Visit Shape.host to learn more about their services and take your Docker Swarm deployment to the next level.