How to Install and Configure Docker Swarm Mode on CentOS 7

Requirements

Launch Alibaba Cloud ECS Instance

Getting Started

nano /etc/hosts192.168.0.102  managernode
192.168.0.103 workernode1
192.168.0.104 workernode2

Install Docker Engine

systemctl start docker
systemctl enable docker

Configure Firewall

firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=2377/tcp
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --reload
systemctl restart docker

Create a Swarm

Swarm initialized: current node (viwovkb0bk0kxlk98r78apopo) is now a manager.To add a worker to this swarm, run the following command:    docker swarm join --token SWMTKN-1-3793hvb71g0a6ubkgq8zgk9w99hlusajtmj5aqr3n2wrhzzf8z-    1s38lymnir13hhso1qxt5pqru 192.168.0.102:2377To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.12.0-ce
Storage Driver: devicemapper
Pool Name: docker-253:0-618740-pool
Pool Blocksize: 65.54kB
Base Device Size: 10.74GB
Backing Filesystem: xfs
Udev Sync Supported: true
Data file: /dev/loop0
Metadata file: /dev/loop1
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 11.8MB
Data Space Total: 107.4GB
Data Space Available: 3.817GB
Metadata Space Used: 581.6kB
Metadata Space Total: 2.147GB
Metadata Space Available: 2.147GB
Thin Pool Minimum Free Space: 10.74GB
Deferred Removal Enabled: true
Deferred Deletion Enabled: true
Deferred Deleted Device Count: 0
Library Version: 1.02.140-RHEL7 (2017-05-03)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: active
NodeID: viwovkb0bk0kxlk98r78apopo
Is Manager: true
ClusterID: ttauawqrc8mmd0feluhcr1b0d
Managers: 1
Nodes: 1
Orchestration:
Task History Retention Limit: 5
Raft:
Snapshot Interval: 10000
Number of Old Snapshots to Retain: 0
Heartbeat Tick: 1
Election Tick: 3
Dispatcher:
Heartbeat Period: 5 seconds
CA Configuration:
Expiry Duration: 3 months
Force Rotate: 0
Autolock Managers: false
Root Rotation In Progress: false
Node Address: 192.168.0.102
Manager Addresses:
192.168.0.102:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 89623f28b87a6004d4b785663257362d1658a729
runc version: b2567b37d7b75eb4cf325b77297b140ea686ce8f
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-693.11.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.102GiB
Name: centOS-7
ID: DN4N:BHHJ:6DJ7:SZPG:FJJC:XP6T:23R4:CESK:E5PO:SJ6B:BOST:HZQ5
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
viwovkb0bk0kxlk98r78apopo * centOS-7 Ready Active Leader

Join the Worker nodes to the Manager node

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
viwovkb0bk0kxlk98r78apopo * managernode Ready Active Leader
yf6nb2er69pydlp6drijdfmwd workernode1 Ready Active
yyavdslji7ovmw5fd3v7l62g8 workernode2 Ready Active

Deploy Service in Docker Swarm Mode

bky6uhg2agdxeqgc2b1a5tcsm
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
bky6uhg2agdx webservice replicated 3/3 httpd:latest *:80->80/tcp
docker service ps webservice
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE       ERROR           PORTS
xsa5wb0eg2ln webservice.1 httpd:latest managernode Running Running about a minute ago
3sbscs7m9lnh webservice.2 httpd:latest workernode2 Running Running about a minute ago
yao2m5wi54kz webservice.3 httpd:latest workernode2 Running Running about a minute ago

Container Self-Healing

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
0dfc71537e18 httpd:latest "httpd-foreground" 9 minutes ago Up 9 minutes 80/tcp webservice.3.yao2m5wi54kzs7iskefupuq6a
9b01b0a55cb7 httpd:latest "httpd-foreground" 9 minutes ago Up 9 minutes 80/tcp webservice.1.xsa5wb0eg2ln5bud1sbjf9e9e
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE              ERROR                              PORTS
z1spatkk1jj7 webservice.1 httpd:latest workernode2 Running Preparing 29 seconds ago
xsa5wb0eg2ln \_ webservice.1 httpd:latest workernode2 Shutdown Failed 30 seconds ago "task: non-zero exit (137)"
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE       ERROR           PORTS
xsa5wb0eg2ln webservice.1 httpd:latest managernode Running Running about 10 minutes ago
3sbscs7m9lnh webservice.2 httpd:latest workernode2 Running Running about 10 minutes ago
yao2m5wi54kz webservice.3 httpd:latest workernode2 Running Running about 10 minutes ago
dfg2mswa52sf webservice.4 httpd:latest workernode1 Running Running about 15 seconds ago
kah1j5hs14as webservice.5 httpd:latest workernode1 Running Running about 15 seconds ago

Protecting Your Servers

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store