Tutorial: Manage Docker Swarm with Portainer
The Docker Swarm container orchestration engine is a great way to take advantage of a cluster for your container deployments. Although it might not be nearly as popular as Kubernetes, it’s still a great option if you’re looking for features like:
- Cluster management integrated with Docker Engine
- Decentralized design
- A declarative service model
- Scaling container deployments
- Desired state reconciliation
- Multi-host networking
- Service discovery
- Load balancing
- Secure deployments
- Rolling updates
And given how much easier Docker Swarm is to use (than Kubernetes), it’s a great way to introduce containers into your development lifecycle mix.
But even though Docker Swarm is quite easy to manage, there’s an even easier way… thanks to Portainer container management system. Once you have your Docker Swarm up and running, you can deploy Portainer and it will automatically pick up your controller and all of your nodes.
I want to walk you through the process of Deploying Docker Swarm and adding Portainer into the mix. Once you have this up and running, you’ll find it exponentially easier to manage your clustered Docker servers and the containers/services you’ve deployed.
The first thing we need to do, however, is spin up a Docker Swarm. I’ll be demonstrating this with three instances of Ubuntu Server 20.04. You can deploy this setup on any Linux machine, but you’ll need to modify the installation process to fit your distribution of choice.
Let’s get down to work.
Deploying Docker Swarm
Log into your first instance of Ubuntu and install the necessary dependencies with the command:
sudo apt-get install ca-certificates curl gnupg lsb-release -y
Once that installation completes, add the official Docker GPG key with the command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Next, we can add the stable Docker repository with the command:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Update apt with the command:
sudo apt-get update
Install Docker Engine (community edition) with:
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
When the installation completes, you’ll want to start and enable the Docker service with:
sudo systemctl enable --now docker
To be able to issue the docker command without sudo privileges (which can be a security issue), add your user to the Docker group with:
sudo usermod -aG docker $USER
Log out and log back in for the changes to take effect (or just issue the command newgrp docker and you’re good to go).
It’s important that you then repeat the above steps on every Docker node you plan to join to the Swarm.
Initializing the Swarm and Joining Nodes
Go back to the controller (the first machine you installed Docker on) and discover the IP address of that machine with:
You can now initialize the Swarm with the command:
docker swarm init --advertise-addr SERVER
Where SERVER is the IP address for the Docker Controller.
When that command completes, it’ll print out a join command that looks like this:
docker swarm join --token TOKEN 192.168.1.13:2377
Where TOKEN is a long string of random characters. Copy that command and run it on every node you want to join the swarm. Once you’ve joined all the nodes, go back to the Controller and issue the command:
You should see a line in the output that looks like:
All three nodes have successfully joined the Swarm.
The next trick is to deploy Portainer with the command (run on the controller):
docker run -d -p 8000:8000 -p 9443:9443 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
Give the container a moment to spin up and then point a web browser to http://SERVER:9443 (Where SERVER is the IP address of the Controller). Create an admin user and log in. Because Portainer has been deployed to a controller in a Swarm, you’ll see an entry for Swarm in the left sidebar (Figure 1).
Now the fun begins.
Let’s deploy a service to our Swarm. Click Services in the left sidebar and then click Add service. In the resulting window (Figure 2), you should see a Replicas entry.
Because we’re working with a Swarm that involves three total nodes, we can bump the replicas entry up to three, so the service we’re deploying will replicate to every node on the swarm (for high availability and failover).
Let’s deploy an NGINX service to our Swarm. Give the service a name and then type nginx:latest in the Image field. Bump Replicas up to three and click Create the service (Figure 3).
Once the service has successfully deployed to the Swarm, it’ll be listed in the Service list (Figure 4).
Another cool trick you can do via Portainer is to scale the service up and down. If you click the Scale button in the listing, the field will change (Figure 5) such that you can scale the service up or down as needed.
And that’s how easy it is to manage your Docker Swarm with the help of Portainer. If you have any intention of working with Docker Swarm for your business, I highly recommend giving this user-friendly, web-based GUI a try. You’ll find your work to be more productive, efficient, and reliable.