Service Discovery using Docker Swarm

Image for post
Image for post
Discover your services

What is a docker swarm?

Docker swarm uses overlay network. The overlay network driver creates a distributed network among multiple Docker daemon hosts. This network sits on top of (overlays) the host-specific networks, allowing containers connected to it (including swarm service containers) to communicate securely when encryption is enabled. Docker transparently handles routing of each packet to and from the correct Docker daemon host and the correct destination container.

Docker swarm network

  • an overlay network called ingress, which handles control and data traffic related to swarm services. When you create a swarm service and do not connect it to a user-defined overlay network, it connects to the ingress network by default.
  • a bridge network called docker_gwbridge, which connects the individual Docker daemon to the other daemons participating in the swarm.

Let’s do some example and see how the service discovery works in swarm. I have 4 EC2 Instances, 1 as docker swarm manager and 3 as docker swarm workers.

Initialize docker swarm manager

Add other 3 nodes as docker swarm worker

docker swarm join-token worker

Check if all nodes are added to swarm cluster

Image for post
Image for post

To avoid any container creation on docker swarm master, I have set the availability of the swarm to Drain. Use below command to set the availability of a node to Drain.

docker node update — availability drain ip-172–31–22–94

To verify if the node availability becomes drain.

docker node inspect ip-172–31–22–94| grep -i Availability

Create a custom overlay network

Image for post
Image for post

Create swarm service on our custom network

docker service create — name myrabbitmq — replicas 1 — network useast1overlay — publish 15672:15672 — publish 5672:5672 rabbitmq:latest

docker service create — name myredis — replicas 2 — network useast1overlay — publish 6379:6379 redis:latest

Here, I have created 3 services, myapache with 2 replicas, myrabbitmq with 1 replica, myredis with 2 replicas. Below snap shows that the mynginx service randomly picked 2 nodes from swarm cluster and launched the containers in it.

Image for post
Image for post

Let’s login in to the container and dig our service.

Image for post
Image for post

As we see the service mynginx is resolving properly. But when you see the IP of the service mynginx its resolving to 10.10.0.24 only. Let’s login to each mynginx container and see the ip’s assigned to each of the containers.

root@50be79089b49:/# hostname -I

10.0.0.26 172.18.0.6 10.10.0.26

root@5b77d3615f69:/# hostname -I

10.0.0.25 172.18.0.3 10.10.0.25

Both the containers have IP’s as 10.10.0.25 and 10.10.0.26. When you actually inspect the service mynginx you will see these two containers are already in a VIP load balancer and the IP 10.10.0.24 comes from that VIP.

docker service inspect mynginx (check the virtual IP section).

Image for post
Image for post

Summary

AWS Cloud Engineer, Bangalore

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store