Service AutoDiscovery Using Traefik For Docker Containers

In a containerized environment it is essential to expose your service containers to the outer world so that end users can access your service containers.

simple expose of a container service can be done in Dockerfile using expose directive and with command line as well.

A more scaled and production use case could be accessing your container app using name-based/host routing or path based routing. this functionality can be easily implemented using Traefik edge routing.

Basically an edge router works a doorway to your platform which is able to check or intercept every request which comes to your platform. edge router like traefik listens on a particular port and receives traffic requests for your container platform in form of the path, the host, or headers and redirect them to the service which can handle that request based on rules defined during configuration.

traefik is easy configurable edge routing for your ingress routing needs which auto-discovers new services in your platform and adds route for them without any configuration. vice versa is also true. whereas traditional edge routing/reverse proxy apps need a configuration file that contains routes to your services. traefik just keep itself in contact to docker API and listens to any spin up or spin down of containers and accordingly add/deletes route for them.

connectors which fetch service information via cluster API are called providers and provide configuration to traefik in real-time.

Below is the working example of how to do the setup of traefik edge routing for docker containers in a cluster.

Illustration of edge routing with Traefik

to test this piece of code you should have docker and docker-compose installed in your machine.

Launching Traefik with docker provider

create a docker-compose.yml file in a directory, where you can use reverse-proxy service using traefik official image.

“version: ‘3’
# The official v2.0 Traefik docker image
image: traefik:v2.0
# Enables the web UI and tells Traefik to listen to docker command: –api.insecure=true –providers.docker
# The HTTP port
– “80:80”
# The Web UI (enabled by –api.insecure=true)
– “8080:8080”
# So that Traefik can listen to the Docker events
– /var/run/docker.sock:/var/run/docker.sock”

start your revery-proxy traefik service with below command

docker-compose up -d reverse-proxy

Accessing traefik console

<localhost can be replaced with host ip>

a simple routing service is enabled for you which can work as reverse proxy now.

let’s see how traefik auto-discovers on the launch of new services. to do so just add below line of code at the end of your docker-compose.yml

"# ...   
# A container that exposes an API to show its IP address     
 image: containous/whoami     

Start the whoami service with the following command:

docker-compose up -d whoami

When Traefik detects new services, it creates the corresponding routes so you can call them

  original article http://link

Show More


A Tech Geek | Meditator | Writer | Vedic Scholar | Speaker who loves to write about technology and humanity for personal and professional transformation.

Leave a Reply

Back to top button