Table of Contents
Create Docker Swarm and install services to Docker swarm using docker stack deploy
Introduction
Setting up of cluster node topology and managing nodes have always been a pain for any developer or infrastructure engineers. Docker swarm makes it ridiculously easy to create a node cluster topology and get a service up and running in a matter of minutes.
Docker Swarm is a cluster of Docker nodes, where you deploy services. In case your next question was – what is a Docker service. Then please note that a Docker service is a group of containers of the same image. So basically it’s all about deploying multiple containers on multiple nodes.
For this tutorial, I will demonstrate how to create a docker swarm using 1 manager node and a couple of worker nodes.
Manager nodes maintain cluster services and schedule services whereas worker nodes just run Docker containers.
Before proceeding ahead make sure that Docker has been installed on these machines. I had installed Docker on Ubuntu Server VM’s from Microsoft Azure Marketplace, which comes with Docker installation.
Creating the manager node
To create a manager node log on to the machine terminal using ssh or the bash terminal provided over browser and run the following command
1 2 3 | docker swarm init |
If you got following output that would mean the manager node is created
1 2 3 4 5 6 7 8 9 10 11 12 | root@nevado-docker:~# docker swarm init Swarm initialized: current node (21pszxtzjslvkbm62qjmf2r37) is now a manager. To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-0h02u7on5rwlknkz4u6nvyjcxi90n0jtqi4ct1p9rf67y9rfqo-eg0kbjnaru5g0wusm4im8zhwl \ 10.0.0.4:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. |
Creating Worker nodes
To create a worker node all you need is to log on to machine and copy and run the output from node manager
1 2 3 4 5 | docker swarm join \ --token SWMTKN-1-0h02u7on5rwlknkz4u6nvyjcxi90n0jtqi4ct1p9rf67y9rfqo-eg0kbjnaru5g0wusm4im8zhwl \ 10.0.0.4:2377 |
That’s all, you can add as many worker nodes as you want by using above command.
Checking Docker nodes
Use the command docker node ls to check if all the nodes and worker manager are part of swarm
1 2 3 4 5 6 | ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 21pszxtzjslvkbm62qjmf2r37 * docker-manage Ready Active Leader ma14oo7jf32gwnb4y4x5b591w docker-slave Ready Active yhifs9of76pr0w89egpbt4333 docker-slave2 Ready Active |
Deploying service to Docker swarm
Though we can deploy Docker service using something like $ docker service createa better is to deploy using docker deploy. The configuration can be handled in a YAML file and multiple depending services can be added at one go.
So log on to Docker -manager node and create a file name docker-stack.yml
For this tutorial and simplicity sake, I will use nginx image and deploy 3 containers of that image. This is how my docker-compose.yml looks like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | version: "3" services: nginx: image: nginx ports: - 80:80 - 443:443 deploy: mode: replicated replicas: 3 restart_policy: condition: on-failure delay: 30s max_attempts: 3 window: 120s |
Now run command
1 2 3 | docker stack deploy --compose-file docker-compose.yml TEST |
and if you see output like this that would mean your service has been deployed
1 2 3 4 | Creating network TEST_default Creating service TEST_nginx |
Now to check on which nodes the service has been installed run
1 2 3 | docker service ps TEST_nginx |
You should see some output like this
1 2 3 4 5 | yd22w8q9bnjm TEST_nginx.1 nginx:latest docker-manager Running Running 3 minutes ago n7ucjsckf2yz TEST_nginx.2 nginx:latest docker-slave Running Running 3 minutes ago u986n00r0126 TEST_nginx.3 nginx:latest docker-slave2 Running Running 3 minutes ago |
There are several options available in the deploy where you can say restrict some containers only to run on Worker Nodes constraints: [node.role == worker].
For more options, you can refer to page.
To uninstall the whole stack use dokcer stack rm TEST this would remove all the services and network created.
Please follow below video for above steps.