Search Tutorials


Docker Swarm Tutorial - Deploying Services Using Stack | JavaInUse



Docker Swarm Tutorial - Deploying Services Using Stack

In a previous tutorial we had deployed services in a docker swarm. For this we had to manually start each service in the swarm. So we had performed following operations-
  • Created Overlay network
  • Start Employee Producer service
  • Start Employee Consumer service
But suppose there are hundreds of services to be deployed using Docker Swarm. In such a case manually starting services is not feasible. We had faced a similar problem when deploying multiple services on a single host machine and the solution for this problem was to use Docker Compose to deploy Docker Services. Since Docker swarm involves deploying multiple services across various nodes we cannot use Docker Compose. But Docker Swarm provides something similar to Docker Compose called Docker Stack which can be used for deploying services in the swarm.
Docker Stack Tutorial
In this tutorial we will be again making use of Play with docker for creating Docker Swarm. In the next tutorial we will be deploying the services in multiple AWS EC2 instances using Docker Swarm.

Docker Tutorial - Table Of Contents

Docker Deploying Spring Based WAR Application to Docker Deploying Spring Based JAR Application to Docker Deploying Multiple Spring Boot Microservices using Docker Networking Deploying Multiple Spring Boot Microservices using Docker Compose Deploying Spring Boot + MYSQL Application to Docker Publishing Docker Image to DockerHub Deploy Docker Swarm services using Play With Docker Deploy Docker Swarm services using Docker Stack Deploy Docker Swarm services to multiple AWS EC2 instances Docker Cheat Sheet

Video

This tutorial is explained in the below Youtube Video.

Lets Begin-

Implement Docker Swarm using Play With Docker

Let us now create the docker swarm using Play With Docker. Play with Docker provides us with multiple cloud instances with docker installed. Go to Play with Docker. Login with your dockerhub credentials and we get the following
Play with docker tutorial
Click on start session. The session is of 4 hours duration.
Play with docker session
We will be creating docker swarm with two nodes as follows-
Docker swarm with two node
We will be creating two instances and initializing docker swarm.
Play with docker instances
Now use the command to create worker node in the second instance
docker swarm join --token SWMTKN-1-589ajd3me7whpnck0478a0titc98pcojl3kuyeovgs48rvdcla-13eg6czt4wvmjncbv1g0zzgya 192.168.0.23:2377

Play with docker worker instance
In a previous tutorial we had learnt what is docker compose and created a docker compose file.
Docker stack file will be exactly similar to the docker compose file we had created in previous tutorial. Only change will be we will not be using local images but get images from dockerhub. So let us create the docker stack file named docker-compose.yaml. Create a file named as docker compose using vim as follows-
  vim docker-compose.yaml
 

create stack using vim
The content of the file will be as follows-
 version: "3"
services:
  consumer:
    image: javainuse/employee-consumer
    networks:
      - consumer-producer
    depends_on:
      - producer
 
  producer:
    image: javainuse/employee-producer
    ports:
      - "8080:8080"
    networks:
      - consumer-producer 

networks:
  consumer-producer:
 

docker stack file
Let us now deploy the docker stack as follows-
docker stack deploy -c docker-compose.yaml testStack

docker stack deploy
We can list the docker stack as follows-
docker stack ls

docker stack list

We can list the docker swarm services as follows-
docker service ls

Play with docker list service
The services can be deployed to any of the nodes in the docker swarm. You can check this by using the docker container ls command in swarm nodes.
 docker container ls
 
The employee producer service is deployed on the master node
Play with docker deploy tutorials
The employee consumer service is deployed on the worker node
Play with docker deploy tutorials

Let us check the consumer service logs as follows-
 docker container logs 56
 

Play with docker service log

Play with docker service logs
It is able to successfully consume the API's exposed by the producer module which is deployed on a separate node. Another important feature of a docker swarm is that the services in docker swarm are self healing. The employee consumer service depends on the employee producer service. Suppose we manually stop the employee producer service and remove it.
 docker container stop d1
 
 docker container rm d1
 

Play with docker stop and remove container
If we again do a list docker container command, we can see that docker has restarted the employee-producer service
Docker Self Healing Feature
So if due to any reason the service in a docker swarm goes down, swarm will automatically restart it and so it is self healing. In the next tutorial we will be deploying the services in multiple AWS EC2 instances using Docker Swarm.