banner
云野阁

云野阁

闲云野鹤,八方逍遥

Docker Swarm Cluster Deployment

Task Platform

Three virtual machines, one as the manager node and the other two as worker nodes.

Deployment Guide

Install Docker#

# Download and install Docker files and dependencies
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
# Start Docker and set it to start on boot
systemctl start docker  
systemctl enable docker
# Check version
docker -v

Configure Firewall to Open Ports#

TCP protocol port 2377: Cluster management port
TCP protocol port 7946: Communication port between nodes (if not open, load balancing will fail)
UDP protocol port 4789: Overlay network communication port

firewall-cmd --zone=public --add-port=2377/tcp --permanent
firewall-cmd --zone=public --add-port=7946/tcp --permanent
firewall-cmd --zone=public --add-port=7946/udp --permanent
firewall-cmd --zone=public --add-port=4789/tcp --permanent
# Reload firewall
firewall-cmd --reload
# Check if port 80 is open
firewall-cmd --query-port=80/tcp
# View all allowed ports
firewall-cmd --zone=public --list-ports

Create Swarm Cluster on Manager Node#

docker swarm init --advertise-addr=local_ip:2377 --listen-addr=local_ip:2377

Swarm initialized: current node (608u180nsa654xbxdthdhl0f6) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-13dv43qm3tdux7243z3c0najcetizjpgly1urd4uchtcooxe87-4eh15dbayxttxipm34s5tod6t 172.20.1.51:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Execute the command generated after running (as highlighted in red above) on the other hosts, and the result will be as follows, indicating a successful join.

Alternatively, execute the command below to generate a token with a periodic rotation plan (recommended)

docker swarm join-token --rotate worker

This node joined a swarm as a worker.

(Extension) Command to join as a manager node

docker swarm join-token --rotate manager

On the manager node, enter docker node ls to view all nodes.

image-20240323221029238-1711203035847-1

Create a Custom Overlay Network for Swarm Services#

# Method One
# The --attachable option indicates that this network is attachable, meaning other containers can connect to this network  
docker network create -d overlay --attachable my-overlay

# Method Two
# --subnet: subnet --gateway: gateway
docker network create --driver overlay --subnet 10.0.9.0/24 --gateway 10.0.9.99 my-overlay
  
# View Docker networks
docker network ls

Test Cross-Host Container Communication#

  1. Deploy a container on each of the three hosts and ping each other, results as shown in the figure below.
# Host One
docker run -it --name master --net=my-overlay ubuntu:latest
# Host Two
docker run -it --name node1 --net=my-overlay ubuntu:latest
# Host Three
docker run -it --name node2 --net=my-overlay ubuntu:latest
  1. Install Testing Tools
apt-get update
# Install ping tool
apt-get install inetutils-ping -y
# Install IP viewing tool
apt-get install net-tools -y

image-20240323221151231-1711203113839-3

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.