Search Tutorials


Docker Swarm Tutorial using Play With Docker | JavaInUse

Docker Swarm Tutorial using Play With Docker

In a previous tutorial we had deployed services in multiple docker containers and then had these services interact with each other using Docker Networking. However the Docker containers were running in a single host machine.
Docker Networking Service

This approach is fine if you have a few docker containers running simultaneously. But in real work scenarios there may exist hundreds of microservices that interact with each other. Each docker container can run only a single microservice. So we will need hundreds of docker containers running in a single host machine if we use docker networking. As you can see this is not a good approach.
Docker Services
Using Docker Swarm we can scale the services horizontally by deploying the docker containers across multiple host machines. These host machines are called as nodes of a swarm. Docker Swarm is self managed. At any given time we just tell the swarm to make a service up and running, then the swarm it self starts the service on any one of its node. So swarm on it's own distributes the services among the nodes. Another feature of docker swarm is that is self healing. If due to any reason a running service gets terminated on a node, it starts it again. We will see this self healing feature of swarm in the next tutorial we will be deploying the services in Docker Swarm using Docker Stack.
Start Swarm nodes

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-

Let us have a look at Docker Swarm basics by creating a docker swarm with a single node. Later using Play with Docker we will see how to deploy multiple microservices using multiple nodes. Also in the next post we will be looking into how to deploy docker swarm with multiple microservices using Amazon Webservices(AWS) EC2 instances.

Docker swarm with single node
Here we have deployed the employee-consumer and employee-producer in a single node of the docker swarm. Start docker-
systemctl start docker

docker-start
Start docker swarm-
docker swarm init

Start Swarm init
We have created a single node in a docker swarm. This is the manager node. In docker swarm nodes can be either manager node or worker node.
Docker swarm nodes
The nodes in the docker swarm can be viewed using-
 docker node ls
 

Docker swarm node
When using Docker Networking we had created a network using which multiple docker instances interacted with each other. This network was of type Bridge. When using services in a swarm, we will again need a network so that multiple docker instances can interact with each other. However this time the network will be of type Overlay.
So let us first create a network of type overlay
 docker network create --driver overlay producer-consumer

Docker network overlay

We can now list the available networks as follows-
  docker network ls
 

Docker network list
Next we will be creating a service in the docker swarm using the employee producer image we had uploaded to docker hub in previous tutorial.
 docker service create --network producer-consumer --name producer -p 8080:8080 javainuse/employee-producer 

Docker swarm create service

The service named producer has been created in the docker swarm. We can list the services in the swarm as follows-
docker service ls

Docker swarm list services
Also using the docker container command we can see the details of the docker container started by the service
 docker container ls
 

Docker swarm container
Now let us create the employee consumer service in the docker swarm-
  docker service create --network producer-consumer --name consumer javainuse/employee-consumer 
 

Docker swarm create service tutorial

The service named producer has been created in the docker swarm. We can list the services in the swarm as follows-
docker service ls

Docker swarm tutorial
List containers created by docker swarm services-
 docker container ls
 

Docker swarm containers
Let us check the employee-consumer service log-
 docker container logs d0
 

Docker swarm service log

Docker swarm service logs
So employee consumer correctly consumes the REST API exposed by the employee producer.

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.
Play with docker instances
Now let us initialize the docker swarm-
docker swarm init

Play with docker init
In play with docker we need to use the syntax as they have mentioned above for docker swarm init. 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
We can check the nodes as follows-
 docker node ls
 

Play with docker nodes

Create the overlay network as follows-
 docker network create --driver overlay producer-consumer

Play with docker overlay network
Let us start a service using docker swarm. We will be first deploy the employee producer using the image we had uploaded to DockerHub
 docker service create --network producer-consumer --name producer -p 8080:8080 javainuse/employee-producer 

Play with docker create service
We can list the docker swarm service as follows-
docker service ls

Play with docker list service
The service created in Docker swarm 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. For me it has created in the worker node
 docker container ls
 

Play with docker container

Let us now deploy the employee consumer using the image we had uploaded to Docker Hub.
  docker service create --network producer-consumer --name consumer javainuse/employee-consumer 
 

Play with docker deploy service
We can list the docker swarm service as follows-
docker service ls

Play with docker deploy services list
Again this service 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
 

Play with docker deploy tutorials
Let us check the consumer service logs as follows-
 docker container logs e2
 

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. In the next tutorial we will be deploying the services in Docker Swarm using Docker Stack.