Docker Swarm is a powerful container orchestration tool that allows you to create and deploy a cluster of Docker nodes on multiple servers. With Docker Swarm, you can easily manage and orchestrate containers, making it an essential tool for developers and system administrators. In this step-by-step guide, we will walk you through the process of installing Docker Swarm on Ubuntu 22.04 servers.
Prerequisites
Before we begin, let’s make sure you have everything you need to install Docker Swarm on your Ubuntu 22.04 servers. Here’s what you’ll need:
- 3 Ubuntu 22.04 servers: One will be used as the Swarm Master/Manager, and the other two will be used as Worker Nodes.
- A non-root user with sudo administrator privileges.
Setting Up Systems
To prepare your systems for Docker Swarm installation, there are a few tasks you need to complete. Let’s go through them one by one.
Open Ports for Docker Swarm
First, you need to open some ports that are used by Docker Swarm on all of your servers. This can be achieved using the Uncomplicated Firewall (UFW). To open the necessary ports, follow these steps:
- Add the OpenSSH application profile and open the default SSH port 22 by running the following command:
sudo ufw allow OpenSSH
- Start and enable UFW by running the following command:
sudo ufw enable
- Allow ports 30000 to 35000 for services by executing the following command:
sudo ufw allow 30000:35000/tcp
- Open ports 2377, 7946, and 4789 for Docker Swarm by running the following commands:
sudo ufw allow 2377/tcp sudo ufw allow 7946/tcp sudo ufw allow 7946/udp sudo ufw allow 4789/udp
- Finally, reload UFW and verify the status by executing the following commands:
sudo ufw reload sudo ufw status
Adding Docker Repository
Next, you need to add the official Docker repository to your servers. This will allow you to install Docker Engine using the official Docker packages. Follow these steps to add the Docker repository:
- Install the necessary packages by running the following command:
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release -y
- Add the Docker GPG key and repository to your systems with the following commands:
sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg sudo chmod a+r /etc/apt/keyrings/docker.gpg echo "deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo \"$VERSION_CODENAME\") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Update and refresh your Ubuntu repository by running the following command:
sudo apt update
- You should now have the Docker repository available on your servers.
Installing Docker Engine
With the systems prepared, it’s time to install Docker Engine on your Ubuntu servers. Here’s how to do it:
- Install Docker Engine by running the following command:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
- Verify that the Docker service is running by executing the following command:
sudo systemctl is-enabled docker
sudo systemctl status docker
- You should see that the Docker service is enabled and running on your servers.
- (Optional): If you plan to run Docker containers using a non-root user, you need to add your user to the docker group. This will allow the user to execute the Docker command and run containers. Run the following command to add your user to the docker group:
sudo usermod -aG docker shapehost
- Log in as the non-root user and verify your Docker configuration by executing the following command:
su - shapehost docker run hello-world
- If the configuration is successful, you should see the output of the “hello-world” container.
Initializing Docker Swarm
Now that Docker Engine is installed on your servers, it’s time to initialize Docker Swarm and set up the Swarm Manager and Worker Nodes. Let’s get started:
- To initialize Docker Swarm, run the following command on the Swarm Master/Manager server:
docker swarm init --advertise-addr <manager-ip> --default-addr-pool <container-ip-range>
Replace <manager-ip>
with the IP address of the Swarm Master/Manager server, and <container-ip-range>
with the IP address range for the containers.
- If the initialization process is successful, you will see the generated token for adding Nodes to Docker Swarm.
- To check the Swarm mode status, run the following command:
docker info
If the Swarm mode is enabled, you should see the details of the NodeID, Manager, and Node.
- To verify the list of Nodes in the Docker Swarm, execute the following command:
docker node ls
At this point, you should see only the Swarm Master/Manager Node.
Adding Worker Nodes to Docker Swarm
With the Docker Swarm initialized, it’s time to add Worker Nodes to the Swarm. Follow these steps:
- Run the following command on the Swarm Master/Manager server to show the generated token for adding Worker Nodes:
docker swarm join-token worker
Take note of the token displayed.
- Move to the Worker Node server and execute the following command to join the Docker Swarm as a Worker Node:
docker swarm join --token <token><manager-ip>:2377
Replace <token>
with the token you obtained in the previous step, and <manager-ip>
with the IP address of the Swarm Master/Manager server.
- If the process is successful, you will see the output “This node joined a swarm as a worker”.
- Return to the Swarm Master/Manager server and run the following command to verify the list of available Nodes in the Docker Swarm:
docker node ls
You should now see the Swarm Manager/Worker Node and the newly added Worker Node.
Running Services in Docker Swarm
Now that your Docker Swarm is up and running with the necessary nodes, it’s time to deploy and run services in the Swarm. In this example, we will deploy a simple HTTP service using the Nginx image. Follow these steps:
- Create a new service in the Docker Swarm by running the following command on the Swarm Master/Manager server:
docker service create --replicas 1 --name test-nginx -p 30001:80 nginx:alpine
This command creates a new service named “test-nginx” with 1 replica, exposes port 30001, and uses the nginx:alpine image.
- Check the details of the “test-nginx” service by executing the following command:
docker service inspect test-nginx
You will see detailed information about the service.
- Verify the list of Docker services in the Swarm by running the following command:
docker service ls
If successful, you should see the “test-nginx” service running on the Swarm Manager/Worker Node.
- Access the “test-nginx” service by using the host IP address and port 30001 with the following command:
curl <manager-ip>:30001
Replace <manager-ip>
with the IP address of the Swarm Manager/Worker Node.
- You should see the index.html source code and the details of the HTTP headers.
Scaling Services in Docker Swarm
One of the key benefits of Docker Swarm is the ability to scale services effortlessly. Let’s learn how to scale the “test-nginx” service in your Docker Swarm:
- Scale the “test-nginx” service to 3 replicas by running the following command on the Swarm Master/Manager server:
docker service scale test-nginx=3
- Verify the “test-nginx” service by executing the following command:
docker service ps test-nginx
You should see that the service is running on all available Worker Nodes.
- Move to one of the Worker Node terminals and run the following command to ensure that the “test-nginx” service is running:
curl <worker-ip>:30001
Replace <worker-ip>
with the IP address of the Worker Node.
- If everything is set up correctly, you should see the container running the “test-nginx” service.
Deleting Services in Docker Swarm
To clean up your environment and remove the services from your Docker Swarm, follow these steps:
- Delete the “test-nginx” service by running the following command on the Swarm Master/Manager server:
docker service rm test-nginx
- Verify the list of available services in the Docker Swarm by executing the following command:
docker service ls
The “test-nginx” service should no longer be listed.
- Finally, remove the nginx:alpine image and check the list of downloaded images on each server by running the following commands:
docker rmi nginx:alpine
docker images
Conclusion
Congratulations! You have successfully installed Docker Swarm on your Ubuntu 22.04 servers. You have learned how to deploy, scale, and remove services in Docker Swarm, making it easier to manage and orchestrate your containerized applications. Docker Swarm is a powerful tool that simplifies your application deployment process and provides a high level of availability for your applications.
If you’re looking for reliable and scalable cloud hosting solutions, consider Shape.host’s Linux SSD VPS. Shape.host offers industry-leading cloud hosting services with top-notch performance and security. Take your business to the next level with Shape.host’s high-performance hosting solutions.
Remember, Docker Swarm is just one of the many tools available to help you streamline your development and deployment processes. Stay curious and keep exploring new technologies to enhance your skills and improve your workflows. Happy coding!