Start docker swarm-
docker 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.
The nodes in the docker swarm can be viewed using-
docker node ls
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
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
Also using the docker container command we can see the details of the docker container started by the service
docker container ls
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
List containers created by docker swarm services-
docker container ls
Let us check the employee-consumer service log-
docker container logs d0
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.
We will be creating docker swarm with two nodes as follows-
We will be creating two instances.
Now let us initialize the docker swarm-
docker swarm 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
We can check the nodes as follows-
docker node ls
Create the overlay network as follows-
docker network create --driver overlay producer-consumer
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
We can list the docker swarm service as follows-
docker service ls
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
We can list the docker swarm service as follows-
docker service ls
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
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.