How to setup Docker Swarm on Centos 7

Docker offers 3 types of cluster systems Kubernetes (Developed by Google) and Docker Swarm (Developed by Docker). Yes, there is a difference between both cluster systems, of course, there is the third one developed by Redhat what is purchasable ( Openshift ) witch also is very powerful and resourceful. But this time I’m creating a tutorial on Docker Swarm. I have played with both Kubernetes and Swarm and I prefer Swarm over Kubernetes. Of course, others might have a different opinion on this matter and everyone works with the soft that is comfortable for himself. I guess that I like Swarm more since it's much closer to Docker. Kubernetes is also awesome but they have removed many Docker commands replacing them with KUBECTL and so on and one. I’m sure that maybe in the future when I will have some more time I will create a tutorial or tutorials about Kubernetes and Openshift also. But this time no time to waste let’s create a Swarm tutorial.

Small information about Docker Swarm:

Docker Swarm mode is a container’s orchestration and clustering tool for managing the Docker host. Docker Swarm mode is the native part of the Docker engine. It means you don’t have to install anything except the Docker engine because Docker swarm mode is part of the Docker engine.

With the Docker Swarm, you will be able to – container self-healingload balancingcontainer scale up and scale down service discovery, and rolling updates. Believe me, I was skeptical about this but it turns out very fun when you see all the magic that is happening under the hood

In this tutorial, I will be using 3 servers. My 3 servers are running on Proxmox virtualization with Linux Centos 7. You can use our offer of VPS, since we don't limit the use of our VPS and you can use docker engine on it.

1 virtual server will be Master (If you want to run this in serious production I would recommend 2 Masters ar minimum)
2 Virtual servers will be as Slaves or as know Pods

Let’s call them
vm1 – Master
vm2 – Slave
vm3 – Slave

I hope that isn’t confusing.

Let’s start by updating /etc/hosts file with the following entries:
Open /etc/hosts file with your favorite editor (In my case it is nano)

nano /etc/hosts

When opened enter the following information This should be done on all masters and slaves. Replace IP with your own server IPS.  vm1  vm2  vm3

Let’s install Docker Engine on all the hosts

yum install -y yum-utils

Let’s create a Docker repository (On all servers)

nano /etc/yum.repos.d/docker.repo

Post this:

name=Docker Repository

Now let’s install the Docker engine

yum install -y docker-engine

Let’s start the Docker service

systemctl start docker

Let’s enable Docker to auto start on system startup

systemctl enable docker

Now when we have installed Docker (In my case on all 3 servers) then we would need to allow firewall connections. In my case, I have disabled Cento's built-in firewall and have set up IPtables which is, in my opinion, better if it is properly configured.

If you are sticking with Centos built-in firewall then do the following on the Master server

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/udp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

Now when you have entered each of the following above showed commands separately you should receive “success” after each. We need to restart the Docker engine to apply the configuration.

systemctl restart docker

Now let’s open the firewall on each pod (Slave)

firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent  --add-port=7946/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

When you have done the above firewall configuration on each pod (Slave) you need to restart the Docker engine on each pod (Slave) server

systemctl restart docker

Now on the Master server, we need to activate the Docker Swarm service and get a secure key, that we will need to provide to pods (Slaves). On the Master server enter the following (Change IP the IP should be Master IP):

docker swarm init --advertise-addr

The above command will make our node as a manager node and we are also advertising the IP address of the manager in the above command so that slave or worker node can join the cluster. If all is done correctly then you should see the following:

docker swarm join \
--token SWMTK-1-4everv323gt4g45vsdvejvnjnkml,elrlkmrg-111weeerregreger \

Of course, you will have a different token and your master IP but it should look like this. You would need to write it down and keep in a secure place since this will be used for your pods (Slaves) to connect to your master server and in the future, if you add new pods (Slaves) you will have to use the same command to add them to master.

Now we can run a command to check the status of our master.

docker node ls

If all is correct you will see your master ID, Hostname, and status as ready

Now let’s add our pods (Slaves) to the master server by entering the following command on each slave server (In my case it’s like this but you will have your own token and master IP)

docker swarm join \
--token SWMTK-1-4everv323gt4g45vsdvejvnjnkml,elrlkmrg-111weeerregreger \

Now on the master server enter the following command and it should show all servers

docker node ls

They should be status Ready and available Active. If so then we are done and have a successful running Docker Swarm cluster.

Now when you have an active Docker Swarm cluster you need to set up a reverse proxy service to roll external traffic to your Docker containers. For this, I will create a new tutorial with Traefik as a Docker reverse proxy container.

Thank you for ready my tutorial and I hope that it helped you to create a cluster. I will continue to create new tutorials when I have spare time. I believe this was the longes tutorial that I have written. Hope it was easy for you to understand I’m trying to create this thing simple as possible due to fact that some people want to learn but they don’t need complex things that can be done in easy steps ?

Warning: file_get_contents([YOUR SITE URL]): failed to open stream: Connection refused in /var/www/vhosts/ on line 821