Deploying Services in Docker Swarm with Docker Stack: A Comprehensive Guide

In this article, we will learn what is docker stack in the docker swarm cluster. We are covering docker advanced concepts we have already learned docker basic and swarm cluster in our previous blogs I recommend you to have a look at my docker guide to get started with docker from the beginning From Zero to Docker Hero.

In our previous blogs on docker swarm, we have already learned how to create a swarm cluster, how to run a service and how networking works in a docker swarm cluster.
But still, now we have talked about running individual containers.
In the past we have learned about docker-compose which is used to run multiple containers using a single command is docker-compose up similarly

But how does it work on a swarm cluster?
We can also use the docker stack command here to run multiple containers using a single command instead of creating and running each service separately.

But not only that stacks have many other features we can specify in which worker node the container must be deployed, limit the resources such as CPU, and memory for that specific container, define the replica count etc.

Let’s get in a practical example.
For example, we have a web, database and a Redis container in our application which is going to deploy in a swarm cluster consisting of 2 workers and one manager.
I prefer to keep the web container inside the manager node and don’t have a preference for other containers also I want my web container to have 2 replicas and also I want to limit the usage of the Redis container to consume not more than 5 % of the total CPU.

How can we deploy this architecture?

For this, we can create a docker-compose file and define our services in it

version: '3.8'

services:
  web:
    image: your-web-image
    deploy:
      replicas: 2
      placement:
        constraints:
          - node.role == manager

  database:
    image: your-database-image
    deploy:
      placement:
        constraints:
          - node.labels.type == worker

  redis:
    image: your-redis-image
    deploy:
      resources:
        limits:
          cpus: '0.05'

We have defined our containers inside the services property which tells the compose file to run this container as a docker service under each service we are defining another property called deploy property By using the deploy property, you can control how your services are deployed, replicated, and managed within a Docker swarm cluster, providing flexibility and scalability for your applications.

Under the deploy property, we are defining the replica counts for our webserver container, using the replica and also placing the container in the manager node using the placement constraints property also we are limiting the CPU for our Redis container using the resource section

For deploying our stack we can use the docker stack deploy command

docker stack deploy <stack_name> –compose-file <composefile.yml>

You can include various options under the deploy key to define deployment-specific configurations for services in a stack. Here are some commonly used options:

  • replicas: Specify the desired number of replicas for a service.
  • placement: Define constraints or preferences for service placement (e.g., node labels, node availability, etc.).
  • resources: Configure resource constraints and reservations for a service, such as CPU and memory limits.
  • restart_policy: Define the restart policy for service in case of failures.
  • update_config: Configure rolling updates for a service, including update parallelism and delay.
  • rollback_config: Specify rollback behaviour for failed updates.
  • labels: Attach custom metadata labels to the service for organizational purposes.
  • update_order: Control the order in which services are updated.
  • endpoint_mode: Specify the endpoint mode for a service (e.g., vip or dnsrr).
  • logging: Configure logging options for service.

You don’t need to remember all the stack options you can refer to the official docker documentation for more options
https://docs.docker.com/compose/compose-file/compose-file-v3/#service-configuration-reference.

Summary:
We have learned how to deploy services in docker swarm using docker-stack.

Leave a Reply

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