How to Install Docker Swarm in Centos 7
About Docker
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.
In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.
Docker Swarm Highlights:
- Cluster management integrated with Docker Engine
- Decentralized design
- Declarative service model
- Scaling
- Multi-host networking
- Service discovery
- Load balancing
- Secure by default
- Rolling updates
About this Article
We will be talking about the installation of Docker Swarm Cluster in CentOS 7 and how to deploy our first service and replicated along the cluster. We will perform a simple deployment with 1 Manager node and 2 Worker(Compute) nodes. We recommend to run nodes with at least 4GB of RAM. Because of Docker Swarm nature it will Load Balance all applications within all cluster nodes. The service we are deploying is a Apache Server.
Uninstall older versions of Docker.
yum remove docker docker-common docker-selinux docker-engine
Don’t worry if you have images, volumes or any data in your existing docker installation it will be preserve anyway.
Install Docker-CE
First we need to install yum-utils in order to install Docker-CE repository and devicemapper because it is use by Docker storage driver.
yum install -y yum-utils device-mapper-persistent-data lvm2
Next, we install Docker-CE repositories
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Now we install Docker-CE packages
yum install docker-ce
After that, we will have the latest stable packages from docker-ce repository.
At this point Docker is installed and we just need to check that it is working properly
systemctl start docker
systemctl enable docker
systemctl status docker
And to check Docker engine we could execute this command:
docker run hello-world
Setup Docker Swarm
With Docker up and running we can move forward to configure our Docker Swarm Cluster
We need to remember that we are deploying in three machines for this example, we will call them:
- master01
- compute01
- compute02
First we have to setup the /etc/hosts file in each machine of our cluster to looks like:
192.168.0.5 master01.example.com master01
192.168.0.6 compute01.example.com compute01
192.168.0.7 compute02.example.com compute02
It is important to set up this information in all nodes in order to be reachable between each other.
Next we need to configure machines hostname with this command
In master01
hostnamectl set-hostname master01
In compute01
hostnamectl set-hostname compute01
In compute02
hostnamectl set-hostname compute02
In order to allow our Swarm Cluster to communicate we have to open the ports in our firewall, in the case of this article we focus on firewalld which comes with CentOS by default:
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=7946/udp
firewall-cmd --permanent --add-port=4789/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd –reload
Now we are ready to initialized our cluster, first we have to go to our master01 node and run this command:
docker swarm init --advertise-addr 192.168.0.5
It should output something similar to this
Swarm initialized: current node (CJKVemqwq3by7WJ80FWegLsxc) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1yn39o5d0aeiuvdiufp45rwbdbg5gxhrvbp3v38s5q6kcjh0q0-CJKVemqwq3by7WJ80FWegLsxc 192.168.0.5:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
With our Docker Swarm Master configured we just need to copy the docker swarm join command from the output and copy it in our compute nodes.
Connect to the compute nodes and execute the command we got from the master
docker swarm join --token SWMTKN-1-1yn39o5d0aeiuvdiufp45rwbdbg5gxhrvbp3v38s5q6kcjh0q0-CJKVemqwq3by7WJ80FWegLsxc 192.168.0.5:2377
We should get a message confirming that our node is part of the Swarm Cluster
This node joined a swarm as a worker.
To check the status of the cluster we can run
# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ac6orrj3xchl4xn956krvbwvn * master01.example.com Ready Active Leader 18.09.6
pibw78gx1w89eckyxe8imtsd1 compute01.example.com Ready Active 18.09.6
tifegjhj3dxhfk0qbddu2nof9 compute02.example.com Ready Active 18.09.6
Deploy our first Service in our Swarm Cluster
It is important to perform the deployment from the master node
docker service create -p 80:80 --name webapp --replicas 3 httpd
With that we will have our WebApp running in all the machines within the cluster and we can access it just pointing any IP address of our machines in the browser. For example http://192.168.0.5
Useful commands:
List all swarm servicesdocker service ls
List all containers of our servicedocker services ps webapp
Scale up our service to 6 instancesdocker service scale webapp=6
Now we have it, Docker Swam Cluster running our first service replicated in all Swarm nodes.