How to Install and Configure Docker Swarm on CentOS 7 / Ubuntu 16.04 / Fedora 26/25
Docker swarm is (aka Docker engine in swarm mode) a native clustering solution that makes a group of Docker hosts into a single logical virtual server. Swarm ensures availability and high performance of your application by distributing it over the number of Docker hosts inside a cluster.
Also, with Docker swarm, you can scale your application horizontally, i.e., increasing number of container instance for the same application.
Our Infrastructure
For the demonstration, I’m using mixed operating system environment:
HostName | IP Address | Operating System | Purpose |
---|---|---|---|
dockerm.itgeek.local | 192.168.12.10 | CentOS 7 | Acts as a Docker manager who manages Docker engine running on nodes. Docker manager will also take a part of a cluster ie, It will run containers as well. |
node1.holhol24.local | 192.168.12.20 | Ubuntu 16.04 | Worker Node 1 (Running Docker engine) |
node2.holhol24.local | 192.168.12.30 | Fedora 26/25 | Worker Node 2 (Running Docker engine) |
Docker swarm was previously a separate tool. It is now integrated with Docker engine from v 1.12. So, you can just install Docker engine on all of your hosts (Manager and Worker Nodes).
READ: How to Install Docker Community Edition on CentOS 7 / RHEL 7
READ: How to Install Docker Community Edition on Ubuntu 16.04
READ: How to Install Docker Community Edition on Fedora 26 / Fedora 25
Firewall
You would need to open following ports on the firewall for a swarm cluster to work properly.
CentOS / Fedora:
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=2377/tcp
firewall-cmd --permanent --add-port=80/tcp ## We are Testing Docker Swarm with WebService
firewall-cmd --reload
Ubuntu:
ufw status
ufw allow 2376/tcp
ufw allow 7946/tcp
ufw allow 7946/udp
ufw allow 2377/tcp
ufw allow 4789/udp
ufw allow 80/tcp ## We are Testing Docker Swarm with WebService
ufw reload
ufw enable
Restart the Docker engine after you have opened up the required ports as the firewall chain does affect the docker rules.
systemctl restart docker
Here onwards, all the commands mentioned are universal docker commands so you can execute the command on any operating system that Docker supports (In our case, CentOS 7, Ubuntu 16.04, and Fedora 26/25).
Create a Swarm
Let’s initiate a cluster creation using “swarm init“. Run the following command on your manager node (dockerm.holhol24.local).
[root@dockerm ~]# docker swarm init --advertise-addr 192.168.12.10
–advertise-addr option specifies the manager node to publish its address as 192.168.12.10 so that worker node can join the cluster.
Output:
Swarm initialized: current node (uhh38rpazd5tnzjph2g5rhgxy) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-5y1u36a0osykywxeme2akpjp4jgx2l67mbqbc4fnazs39bp314-e6djx8ma2qnevolztpplkn26j 192.168.12.10:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
The above output has the token to add worker nodes to the cluster.
View the current status of the swarm using the following command.
[root@dockerm ~]# docker info
Output:
Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 17.03.1-ce Storage Driver: devicemapper Pool Name: docker-253:1-297-pool . . . . . . Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata Library Version: 1.02.135-RHEL7 (2016-11-16) Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Swarm: active NodeID: uhh38rpazd5tnzjph2g5rhgxy Is Manager: true ClusterID: kp8tgowwcuiv3om0wzfgpngp7 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 Node Address: 192.168.12.10 Manager Addresses: 192.168.12.10:2377 Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-123.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 979.9 MiB Name: dockerm.holhol24.local ID: OTNI:UJZA:7CC4:TS5N:TVJ7:XXQE:5J7Q:ENWW:ZVPD:3VJI:F5DL:JYSP Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false
You can list down the Docker nodes in the swarm with the following command.
[root@dockerm ~]# docker node ls
Output:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS uhh38rpazd5tnzjph2g5rhgxy * dockerm.holhol24.local Ready Active Leader