Cloud Native 9 min read

Master Docker Swarm: Build, Deploy, and Manage High‑Availability Clusters

This guide explains Docker Swarm’s architecture, installation methods, and step‑by‑step procedures for creating a high‑availability Swarm cluster, including manager and node setup, TLS security, service discovery with Consul, and essential Docker commands for operating and testing the cluster.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Docker Swarm: Build, Deploy, and Manage High‑Availability Clusters

Docker Swarm Overview

Docker Swarm is Docker’s official clustering tool that turns multiple Docker hosts into a single virtual host. It follows the Docker API standard, so tools such as Dokku, Docker Compose, Docker Machine, Jenkins, and the Docker client can transparently scale across hosts.

Understanding Swarm Cluster Creation

Creating a Swarm cluster starts by pulling the Swarm image and configuring a manager and its nodes. The basic steps are: open a TCP port on each node for manager communication, install Docker on each node, and manage TLS certificates to secure the cluster. Installation can be manual or automated with Docker Machine, which also generates the required certificates.

Installation Methods

There are two ways to install Swarm: (1) launch a container from the Swarm image, which requires no binary installation and always runs the latest version, and (2) install the Swarm binary directly on the host, which is useful for developers testing code changes. The official recommendation is the container‑based method.

Building a Swarm Cluster

Two approaches are provided: running Swarm in a virtual machine on macOS/Windows (using Docker Toolbox) or running it directly on Linux for production. For macOS/Windows, the steps include installing Docker Toolbox, creating three VMs, generating a Swarm discovery token, creating the manager and nodes, and managing the Swarm. The Linux example uses five hosts.

Setup Example

Host roles: manager0 and manager1 act as primary and replica managers; node0 and node1 are worker nodes; consul0 runs the Consul service for discovery. The setup steps are creating VMs, installing Ubuntu Server, configuring SSH, installing Docker, and configuring the environment (including a private registry).

Discovery Service

Run a Consul container as the backend for service discovery:

docker run -d -p 8500:8500 --name=consul swarm/consul -server -bootstrap

Creating Managers and Nodes

Start the primary manager (primary manager):

docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise <manager0_ip>:4000 consul://<consul_ip>:8500

Start the replica manager and worker nodes similarly:

docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise <manager1_ip>:4000 consul://<consul_ip>:8500
docker run -d swarm join --advertise=<node_ip>:2375 consul://<consul_ip>:8500

Operating the Swarm

Useful commands: docker -H :4000 info – view manager and node information. docker -H :4000 run hello-world – run an application in the cluster. docker -H :4000 ps – list containers running on the cluster.

To test failure, remove the primary manager container and recreate it:

docker rm -f <id_name>
docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise <manager0_ip>:4000 consul://<consul_ip>:8500

View logs with:

sudo docker logs <id_name>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDevOpsCluster Managementcontainer orchestrationDocker Swarm
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.