How to Create a Docker Swarm and deploy stack of services - Little Big Extra Skip to main content

How to Create a Docker Swarm and deploy stack of services

Create Docker Swarm and install services to Docker swarm using docker stack deploy


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

If you got following output that would mean the manager node is created

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

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

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

Please note that Docker deploy only works with Version-3 of composing file

Now run command

and if you see output like this that would mean your service has been deployed

Now to check on which nodes the service has been installed run

You should see some output like this

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.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *