Traefik is a modern HTTP reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with your existing infrastructure components (Docker, Swarm mode, Kubernetes, Marathon, Consul, Etcd, Rancher, Amazon ECS, ...) and configures itself automatically and dynamically. (Read more (opens in a new tab))
Architecture
For this tutorial, we will create a small lab that has three services, our blogging platform Ghost (opens in a new tab), file hosting service NextCloud (opens in a new tab) and a demo website using Nginx (opens in a new tab) demo container.
In front of these three services we will run Traefik (opens in a new tab) which will mange the routing between these services and lastly will create LocalXpose container to access Traefik from the internet.
Prerequisites
- Docker (opens in a new tab) and Docker-compose (opens in a new tab).
- LocalXpose (opens in a new tab).
Step 1 - Reserve wildcard custom domain
We will use a custom wildcard domain, for this example we will use this custom domain example.com
, so we need to reserve it:
loclx domain reserve --domain '*.example.com'
Then we should add a CNAME record in our DNS provider, You can read more about domain reservations here.
Step 2 - Prepare docker-compose stack file
Change the following *.example.com
with your actual reserved domain and replace the access token with yours.
version: "3.8"
services:
# LocalXpose service will expose traefik container to the internet
localxpose:
image: localxpose/localxpose:latest # read more here https://hub.docker.com/r/localxpose/localxpose
# forward any incoming requests to traefik container
command: tunnel -r http --reserved-domain "*.example.com" --to traefik:80
environment:
# Get your access token from your dashboard here https://localxpose.io/dashboard/access
ACCESS_TOKEN: YOURS_HERE
# Traefik service which will handle the routing between the other
# docker services
traefik:
image: traefik:v2.8
command:
# Enabling docker provider
- "--providers.docker=true"
# Do not expose containers unless explicitly told so
- "--providers.docker.exposedbydefault=false"
# Traefik will listen to incoming request on the port 80 (HTTP)
- "--entrypoints.web.address=:80"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
# Nginx service is a demo application
nginx:
image: nginxdemos/hello:0.3
labels:
# Explicitly tell Traefik to expose this container
- "traefik.enable=true"
# The domain the service will respond to
- "traefik.http.routers.nginx.rule=Host(`nginx.example.com`)"
# Allow request only from the predefined entry point named "web"
- "traefik.http.routers.nginx.entrypoints=web"
# Tell Traefik to use the port 80 to connect to `nginx` container
- "traefik.http.services.nginx.loadbalancer.server.port=80"
# Nextcloud is our file hosting service
nextcloud:
image: nextcloud:stable-apache
labels:
# Explicitly tell Traefik to expose this container
- "traefik.enable=true"
# The domain the service will respond to
- "traefik.http.routers.nextcloud.rule=Host(`nextcloud.example.com`)"
# Allow request only from the predefined entry point named "web"
- "traefik.http.routers.nextcloud.entrypoints=web"
# Tell Traefik to use the port 80 to connect to `nextcloud` container
- "traefik.http.services.nextcloud.loadbalancer.server.port=80"
# Ghost is our blogging platform
ghost:
image: ghost:5.5.0
environment:
url: http://blog.example.com
labels:
# Explicitly tell Traefik to expose this container
- "traefik.enable=true"
# The domain the service will respond to
- "traefik.http.routers.ghost.rule=Host(`blog.example.com`)"
# Allow request only from the predefined entry point named "web"
- "traefik.http.routers.ghost.entrypoints=web"
# Tell Traefik to use the port 2368 to connect to `ghost` container
- "traefik.http.services.ghost.loadbalancer.server.port=2368"
Step 3 - Start our stack file
docker-compose up
Step 4 - Access these services from the internet
Now you can access these services from the internet like https://nginx.example.com
, https://blog.example.com
and https://nextcloud.example.com
.