Managing Docker Swarm with Portainer – The New Stack

the Docker Swarm The container orchestration engine is a great way to leverage a cluster for your container deployments. While it might not be 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
  • Continuous 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.

But even though Docker Swarm is quite easy to manage, there is an even easier way… thanks to the Portainer container management system. Once your Docker Swarm is up and running, you can deploy Portainer and it will automatically pick up your controller and all your nodes.

I want to walk you through the process of deploying Docker Swarm and adding Portainer into the mix. Once this is up and running you will find it exponentially easier to manage your clustered Docker servers and the containers/services you have deployed.

The first thing we need to do, however, is launch a Docker Swarm. I will demonstrate this with three instances of Ubuntu Server 20.04. You can deploy this setup to any Linux machine, but you’ll need to modify the installation process to suit your distribution of choice.

Let’s get to work.

Deployment of Docker Swarm

Connect to 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 this installation is complete, 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

Then 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

Once the installation is complete, you will 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 may 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 run the command newgrp docker and you are good to go).

It is important that you then repeat the above steps on each Docker node you plan to join the Swarm.

Swarm initialization and node joining

Go back to the controller (the first machine where you installed Docker) and find out the IP address of this machine with:

ip a

You can now initialize the Swarm with the command:

docker swarm init --advertise-addr SERVER

Where SERVER is the IP address of the Docker controller.

When this command completes, it prints 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 this command and run it on each node you want to join the swarm. Once you have joined all the nodes, return to the controller and issue the command:

docker info

You should see a line in the output that looks like:

Nodes: 3

All three nodes have successfully joined the Swarm.

Deploy Portainer

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 start, then point a web browser to http://SERVER:9443 (where SERVER is the controller’s IP address). Create an administrator user and log in. Since Portainer was deployed to a controller in a Swarm, you’ll see an entry for Swarm in the left sidebar (Figure 1).

Figure 1: Portainer adds a Swarm entry when part of a cluster.

Now the fun begins.

Let’s deploy a service on our Swarm. Click Services in the left sidebar, then click Add Service. In the resulting window (Figure 2), you should see a Replicas entry.

Replica entry

Figure 2: Creating a new service via Portainer.

Since we are working with a swarm that involves three nodes in total, we can increase the entry of replicas up to three, so the service we are deploying will replicate to every node in the swarm (for high availability and failover).

Let’s deploy an NGINX service on our Swarm. Give the service a name, then type nginx: the latest in the Image control. Bump Replicas up to three and click Create Service (picture 3).

Deploying a service on Docker Swarm

Figure 3: A very basic service to deploy on our Docker Swarm.

Once the service is successfully deployed to Swarm, it will be listed in the list of services (Figure 4).

Node replication

Figure 4: Our tnsDockerTest service has been replicated to all Swarm nodes.

Another neat trick you can do through Portainer is to scale the service up and down. If you click the Scale button in the list, the field will change (Figure 5) so you can scale the service up or down as needed.

Scale a service with Portainer.

Figure 5: Increasing or decreasing a service is very easy thanks to Portainer.

And it’s that easy to manage your Docker Swarm with the help of Portainer. If you intend to work with Docker Swarm for your business, I highly recommend trying out this user-friendly web GUI. You will find that your work is more productive, efficient and reliable.

The New Stack is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker.


Source link

Steven L. Nielsen