Docker Swarm is a Clustering and Orchestration Framework for the Docker ecosystem. Have a look at their official documentation for detailed information.
In this Tutorial we will Setup a 3 Node Docker Swarm Cluster and to Demonstrate How Easy it is to Deploy a Web Application with 2 Replicas from a Docker Image.
Overview of What we will be Doing
- Install Docker on 3 Servers with Ubuntu 16.04
- Initialize the Swarm and Join the Worker Nodes
- Create a Nginx Service with 2 Replicas
- Do some Inspection: View some info on the Service
3 Fresh Deployed Ubuntu 16.04 Servers. ( 1GB Memory Servers will be good for development )
What is Docker
Docker is a Open Source Technology that allows you to create lightweight, isolated, reproducible application instances which is called Containers. Docker is built on top of the LXC technology, so it uses Linux Containers and as mentioned, it’s lightweight compared to a traditional VM.
A Container is isolated and uses the Kernel of the Docker host, it also utilizes Kernel features such as cgroups and namespaces in order to make them isolated.
Installing Docker Community Edition
Remove any older versions of Docker that might be present and install the dependencies:
1 2 3
Get the needed repository to setup Docker Community Edition:
1 2 3
Update the repository index and Install Docker Community Edition:
1 2 3 4
Enable Docker on Startup and Start the Docker Engine:
If you would like to execute your docker commands without sudo, add your user to the docker group:
Test your Setup by Running a Hello World Container. You will see that if the image is not in the local docker image cache, it will pull the image from docker hub (or the respective docker registry), then once the image is saved locally, docker will then instantiate the container from that image:
1 2 3 4 5 6 7 8 9
If you have a DNS Server you can configure the A Records for these hosts on DNS, but for simplicity, I will add the noted IP Addresses from the previous step into my
/etc/hosts file so we can resolve names to IP’s
Open up the the hosts file:
In my example, my IP Addresses:
1 2 3
Repeat the above steps on the other 2 Servers and make note of the IP Addresses of each node. You should be able to ping and reach the nodes that was configured. Make sure to allow all traffic between these nodes.
Initialize the Swarm:
Now we will initialize the swarm on the manager node and as we have more than one network interface, we will specify the –advertise-addr option:
1 2 3 4 5 6 7 8 9 10
From the response above, we received the join token that allows the workers to register with the manager node. If its a scenario where you want to have more than one manager node, you can run
docker swarm join-token manager to receive the join token for additional manager.
Let’s add the two worker nodes to the manager:
To see the node status, so that we can determine if the nodes are active/available etc, from the manager node, list all the nodes in the swarm:
1 2 3 4 5
Reobtaining the Join Tokens
If at any time, you lost your join token, it can be retrieved by running the following for the manager token:
And the following to retrieve the worker token:
Swarm Services in Docker uses a declarative model which means that you define the desired state of the service, and rely on Docker to maintain this state. More information on this can be found on their Documentation
At this moment, we will see that we have no services running in our swarm:
Deploying our First Service
Now onto the creation of a standard nginx service with 2 replicas, which means that there will be 2 containers of nginx running in our swarm.
But first, we need to create a overlay network, which is a network driver that creates a distributed network among multiple Docker daemon hosts. Swarm takes care of the routing automatically, which is routed via the port mappings. So you can have that your container sits on worker-2, when you hit your manager node on the published port, it will route the request to the desired application that resides on the respective container.
To create a overlay network called mynet:
Now onto creating the Service. If any of these containers fail, they will handled by the manager node and will be spawned again to have the desired number that we set on the replica option:
Let’s have a look at our nginx service:
1 2 3
After we see that the replica count is 2/2 our service is ready.
To see on which nodes our containers are running that makes up our service:
1 2 3 4
From the above output, we can see that worker-1 and worker-2 are serving our containers for our service. We can also retrieve more information of our service by using the inspect option, which will give you a detailed response in json format of the service:
We can get the Endpoint Port info by using inspect and using the –format parameter to filter the output:
From the output we will find the PublishedPort is the Port that we Expose, which will be the listener. Our TargetPort will be the port that is listening on the container:
1 2 3 4 5 6 7 8
Now that we went through the inspection of our service, its time to test our base nginx service.
Testing Nginx in our Swarm
Make a request against your docker node manager address on the port that was exposed, in this case 8080:
1 2 3 4 5 6 7 8 9 10 11
Now we have successfull setup a 3 node docker swarm cluster and deployed a basic nginx service to our swarm. Please have a look at my other Docker Swarm Tutorials for other content.
Please feel free to show support by, sharing this post, making a donation, subscribing or reach out to me if you want me to demo and write up on any specific tech topic.
Thanks for reading!