Start docker swarm-
docker swarm init
![Start Swarm init Start Swarm init](static/dock-10-2-min.JPG)
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 Docker swarm nodes](static/dock-10-37-min.JPG)
The nodes in the docker swarm can be viewed using-
docker node ls
![Docker swarm node Docker swarm node](static/dock-10-5-min.JPG)
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
We can now list the available networks as follows-
docker network ls
![Docker network list Docker network list](static/dock-10-4-min.JPG)
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
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 Docker swarm list services](static/dock-10-7-min.JPG)
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 Docker swarm container](static/dock-10-9-min.JPG)
Now let us create the employee consumer service in the docker swarm-
docker service create --network producer-consumer --name consumer javainuse/employee-consumer
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 Docker swarm tutorial](static/dock-10-11-min.JPG)
List containers created by docker swarm services-
docker container ls
![Docker swarm containers Docker swarm containers](static/dock-10-12-min.JPG)
Let us check the employee-consumer service log-
docker container logs d0
![Docker swarm service logs Docker swarm service logs](static/dock-10-14-min.JPG)
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
Click on start session. The session is of 4 hours duration.
![Play with docker session Play with docker session](static/dock-10-17-min.JPG)
We will be creating docker swarm with two nodes as follows-
![Docker swarm with two node Docker swarm with two node](static/dock-misc-1-min.JPG)
We will be creating two instances.
![Play with docker instances Play with docker instances](static/dock-10-18-min.jpg)
Now let us initialize the docker swarm-
docker swarm init
![Play with docker init Play with docker init](static/dock-10-19-min.jpg)
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 Play with docker worker instance](static/dock-10-20-min.jpg)
We can check the nodes as follows-
docker node ls
Create the overlay network as follows-
docker network create --driver overlay producer-consumer
![Play with docker overlay network Play with docker overlay network](static/dock-10-22-min.jpg)
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 Play with docker create service](static/dock-10-23-min.jpg)
We can list the docker swarm service as follows-
docker service ls
![Play with docker list service Play with docker list service](static/dock-10-24-min.jpg)
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
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 Play with docker deploy service](static/dock-10-26-min.jpg)
We can list the docker swarm service as follows-
docker service ls
![Play with docker deploy services list Play with docker deploy services list](static/dock-10-27-min.jpg)
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 Play with docker deploy tutorials](static/dock-10-28-min.jpg)
Let us check the consumer service logs as follows-
docker container logs e2
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.