Setting up a Docker Swarm Cluster
Prerequisites
Before setting up a Docker Swarm cluster, ensure that you have the following:
- At least two Ubuntu 22.04 hosts (physical or virtual) with the Docker Engine installed.
- SSH access to the hosts.
Step 1: Initialize the Swarm
On one of the hosts, run the following command to initialize the Swarm:
docker swarm init --advertise-addr <HOST_IP_ADDRESS>
Replace <HOST_IP_ADDRESS>
with the IP address of the host.
This command will output a join token that you'll use to add other nodes to the Swarm.
Step 2: Add Worker Nodes to the Swarm
On the other hosts, run the command provided by the previous step to join the Swarm as a Worker node:
docker swarm join --token <TOKEN> <HOST_IP_ADDRESS>:2377
Replace <TOKEN>
with the token provided by the Swarm initialization, and <HOST_IP_ADDRESS>
with the IP address of the Manager node.
Step 3: Verify the Swarm
Run the following command on the Manager node to view the nodes in the Swarm:
docker node ls
You should see the Manager node and the Worker nodes in the output.
Step 4: Deploy a Service
To test your Swarm setup, deploy a simple service:
docker service create --name nginx --publish 80:80 nginx
This will create a service named "nginx" and deploy it across the Swarm.
graph TD
A[Manager Node] --> B[Worker Node]
B[Worker Node] --> C[Worker Node]
C[Worker Node] --> A[Manager Node]
A -- "docker swarm init" --> D[Swarm]
B -- "docker swarm join" --> D[Swarm]
C -- "docker swarm join" --> D[Swarm]
D -- "docker service create" --> E[Nginx Service]