How to setup a cluster with Docker Swarm
In this article, I will walk you through the process of setting up a cluster using Docker swarm.
We have 3 local or cloud machine with Ubuntu Server 16.04 and above installed. Out of these, one server will act as a manager node and other two servers will act as worker nodes.
We have configured static IP addresses for all the nodes.
192.168.0.100 for Manager node.
192.168.0.101 for Worker node01.
192.168.0.102 for Worker node02.
Each node must also be running Docker. I have used docker version 18.05.0-ce.
Install docker
The installation process is well documented elsewhere and you can read Docker’s documentation for details. The simplest method is to run the following commands on each machine:
$ curl -sSL https://get.docker.com | sh
This will install the latest version of Docker engine.
Create Swarm Manager
Creating the swarm manager node can be done with a single command. We will use host 192.168.0.100 for the manager.
$ sudo docker swarm init --advertise-addr 192.168.0.100Swarm 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.100:2377To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Remember the token from the above output. This is used to join worker nodes to the manager node.
You can also see the list of nodes in your cluster.
$ sudo docker node lsID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
viwovkb0bk0kxlk98r78apopo * ubuntu-16.04 Ready Active Leader
Add nodes
Manager node is now ready. Next, you need to add Worker node to the Manager node.
You can do this by running docker swarm join command on both Worker node.
$ sudo docker swarm join --token SWMTKN-1-3793hvb71g0a6ubkgq8zgk9w99hlusajtmj5aqr3n2wrhzzf8z-1s38lymnir13hhso1qxt5pqru 192.168.0.100:2377This node joined a swarm as a worker.
On the manager node, run the following command to check the node status.
$ sudo docker node lsID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
viwovkb0bk0kxlk98r78apopo * managernode Ready Active Leader
yf6nb2er69pydlp6drijdfmwd workernode1 Ready Active
yyavdslji7ovmw5fd3v7l62g8 workernode2 Ready Active
Deploy service
With the active nodes, we can now deploy a service. Let’s create a service for nginx web server.
$ sudo docker service create -p 80:80 --name webserver --replica 3 nginxbky6uhg2agdxeqgc2b1a5tcsm
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
The above command will create a service named webserver and containers will be launched from docker image nginx. The service containers are deployed across all the cluster nodes.
Now, you can list and check the status of the service.
$ sudo docker service lsID NAME MODE REPLICAS IMAGE PORTS
bky6uhg2agdx webservice replicated 3/3 httpd:latest *:80->80/tcp
Verify the Service from Manager node and see whether the new container is up.
$ sudo docker service ps webserviceID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
xsa5wb0eg2ln webserver.1 httpd:latest managernode Running Running about 10 minutes ago
3sbscs7m9lnh webserver.2 httpd:latest workernode2 Running Running about 10 minutes ago
yao2m5wi54kz webserver.3 httpd:latest workernode2 Running Running about 10 minutes ago
At the moment, our web server service is running as a single instance on Docker machines.
We can scale up the service to 5 instances running on 3 machines.
$ sudo docker service scale webserver=5
Verify the Service from Manager node and see whether a new container is started or not.
$ sudo docker service ps webserverID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
xsa5wb0eg2ln webserver.1 httpd:latest managernode Running Running about 10 minutes ago
3sbscs7m9lnh webserver.2 httpd:latest workernode2 Running Running about 10 minutes ago
yao2m5wi54kz webserver.3 httpd:latest workernode2 Running Running about 10 minutes ago
dfg2mswa52sf webserver.4 httpd:latest workernode1 Running Running about 15 seconds ago
kah1j5hs14as webserver.5 httpd:latest workernode1 Running Running about 15 seconds ago
You may also use yaml file for deploying your service and save with nginx.yml
version: '3.3'services:
nginx:
image: nginx:latest
ports:
- "80:80"
You can deploy using this yaml.
$ sudo docker stack deploy -c nginx.yml nginx
Clustering made easy with Swarm
That’s the gist of creating a Docker swarm and creating a service on your new cluster. To learn more about what Docker swarm can do, issue the command docker swarm — help to see the other commands you can use for Docker swarm.
At 47Billion, we specialize in helping companies build awesome products. If you are looking for a an experienced team to build your complete solution, please contact us at info@47billion.com.