Simple Reverse Proxy With Traefik

Introduction

I usually like used Nginx as proxy to host multiple services on the same server, but however, when we need to use a quick and simple configuration, it is good to use Traefik as our proxy server.

So let’s use Traefik !

What exactly is Traefik ???

An open-source reverse proxy and load balancer for HTTP and TCP-based applications that is easy, dynamic, automatic, fast, full-featured, production proven, provides metrics, and integrates with every major cluster technology.

Basically traefik is a reverse proxy and load balancer for HTTP / TCP apps . Is linked with various technologies and can be easily installed in any stack.

In particular Docker and Kubernetes liked well compatibility with, this article would like to center the Docker + Traefik.

Environment

In this case we will launch simple a blog and wiki service under domain test.localdomain

Domains:

For this test add the following items to the host file to try it out locally.

root@vagrant:/home/vagrant# echo '127.0.0.1     blog.test.localdomain' > /etc/hosts
root@vagrant:/home/vagrant# echo '127.0.0.1 wiki.test.localdomain' >> /etc/hosts
root@vagrant:/home/vagrant# cat /etc/hosts
127.0.0.1 blog.test.localdomain
127.0.0.1 wiki.test.localdomain

Docker services:

When we decide to managing multiple Docker containers, Docker Compose is convenient, so I created a config file immediately.

  • Basic docker-compose for blog and wiki:
version: '3.2'

services:
blog:
image: ghost
ports:
- 8082:2368
volumes:
- 'blog_data:/var/lib/ghost/content'
wiki:
image: 'docker.io/bitnami/dokuwiki:20200729-debian-10'
ports:
- '8083:8080'
volumes:
- 'wiki_data:/bitnami/dokuwiki'
volumes:
wiki_data:
driver: local
blog_data:
driver: local

Now we can run docker-compose up -d and then our two services will be accessible blog on port 8082 and wiki on port 8083.

Now we can introduced Traefik to our configuration. Since we are using docker, let’s install Traefik’s Docker image.

  • Docker-compose for Traefik
version: '3.2'

services:
proxy:
image: traefik:2.0
ports:
- 8081:8080
- 80:80
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik.yml:/etc/traefik/traefik.yml

As we can se it is mounted volumes, and docker.sock which is necessary to mount it because it can access the Docker API except for the file that defines the Traefik settings . Traefik has the ability to constantly monitor the status of the service and disable the reverse proxy when the service is not running.

  • traefik.yml

Now we need to define http.routers for services and then we can open our services with domains http://wiki.test.localdomain and http://blog.test.localdomain .

  • Configuration
version: '3.2'

services:
blog:
image: ghost
ports:
- 8082:2368
volumes:
- 'blog_data:/var/lib/ghost/content'
labels:
- "traefik.http.routers.blog.rule=Host(`blog.test.localdomain`)"


wiki:
image: 'docker.io/bitnami/dokuwiki:20200729-debian-10'
ports:
- '8083:8080'
volumes:
- 'wiki_data:/bitnami/dokuwiki'
labels:
- "traefik.http.routers.wiki.rule=Host(`wiki.test.localdomain`)"

Below there is full docker-compose.yml

Conclusion

If we don’t need to set complicated rules and just need a simple reverse proxy, Traefik it’s a good choice

 by the author.

--

--

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