How to use Traefik and Docker to route traffic to different services part 1
Table of contents
No headings in the article.
Last week I was exploring traefik. I had to deploy two microservices on the same server. Treafik is a proxy that helps you deploy microservices. I did not expect this but I liked and also saved me time.
This post has two parts. In this part, I will show you how to run multiple services from the same server. In the next part, I will explain SSL configuration with let's encrypt.
For this blog, I have created two node.js apps that run on ports 8081 and 8082. I have created docker files to run these services in docker containers. I have also created a docker network traefik-apps. I will use this network to run node.js services. Now let's see compose file for traefik-proxy.
services:
traefik:
image: traefik:latest
ports:
- 80:80
- 443:443
restart: always
labels:
# enable traefik for this services
- traefik.enable=true
# Define the port inside of the Docker service to use
- traefik.http.services.traefik-dashboard.loadbalancer.server.port=8080
# Make Traefik use this domain in HTTP
- traefik.http.routers.traefik-dashboard-http.entrypoints=http
- traefik.http.routers.traefik-dashboard-http.rule=Host(`dash.localhost`)
# make traefik use this network
- traefik.docker.network=traefik-apps
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
command:
#enable traefik so that it can read labels from docker services
- --providers.docker
# To expose services over http
- --api.insecure
# do not expose all services by default
- --providers.docker.exposedbydefault=false
# traefik http entrypoint listening on port 80
- --entrypoints.http.address=:80
- --accesslog
- --log
- --api
networks:
- traefik-apps
volumes:
towapp-certificates:
networks:
traefik-apps:
external: true
The code defines a Docker Compose service called "traefik". The service exposes ports 80 and 443. Labels are used to enable Traefik for this service, specify the port to use inside the container, and define the HTTP entry points and routing rules for the Traefik dashboard.
The service is configured to use the traefik-apps
network and has access to the Docker socket through a volume mount. The command specified for the service enables Traefik to read labels from Docker services, exposes services over HTTP, sets the default to not expose all services, and specifies the HTTP entry point to listen on port 80. Logging and API features are also enabled.
I have also created towapp-certificates
volume for certificates and a traefik-apps
external network. The command --api
is to enable the traefik dashboard. Now we can run this compose file with docker-compose -f docker-compose.traefik.yml up -d
command. We can check the traefik dashboard at http://dash.localhost
. The dashboard should be like this.
Now let's move to the load balancing of two node.js APIs.
services:
first:
restart: always
container_name: first
build:
context: ./first
dockerfile: Dockerfile
labels:
- traefik.enable=true
- api.insecure=true
- traefik.http.services.app.loadbalancer.server.port=8081
- traefik.http.routers.first.entrypoints=http
- traefik.http.routers.first.rule=Host(`first.localhost`)
- traefik.docker.network=traefik-apps
command: [ "node", "app.js" ]
networks:
- traefik-apps
second:
restart: always
container_name: second
build:
context: ./second
dockerfile: Dockerfile
labels:
- traefik.enable=true
- api.insecure=true
- traefik.http.services.app.loadbalancer.server.port=8082
- traefik.http.routers.second.entrypoints=http
- traefik.http.routers.second.rule=Host(`second.localhost`)
- traefik.docker.network=traefik-apps
command: [ "node", "server.js" ]
networks:
- traefik-apps
networks:
traefik-apps:
external: true
Here I have defined a Docker Compose file that creates two services named "first" and "second" that will be deployed on an external Docker network called traefik-apps
The "first" service is built from a Dockerfile in the "./first" directory, It is labeled with Traefik settings for load balancing and routing, and is accessible via the URL http://first.localhost
The "second" service is built from a Dockerfile in the "./second" directory, and runs another Node.js application using the command "node server.js". It is also labeled with Traefik settings for load balancing and routing and is accessible via the URL http://second.localhost
.
Now we can run this docker file with docker-compose up -d
. This will start both services on defined routes. You can also check out this project here Learn Traefik.
You can ask me doubts in the comments section. Let me know if are any errors.
Thanks for scrolling.
Poka Poka