Skip to main content

Traefik Install and configuration - Part 1

Introduction

In this tutorial we are going to install and configure Traefik Proxy. As quoted on their website "Traefik is a leading modern reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with your existing infrastructure components and configures itself automatically and dynamically." All our applications will be front-ended by traefik.

Objective

The core objective for this tutorial is to:
  1. Configure an A record in our DNS server (pfsesne firewall) to point to our traefik proxy
  2. Configure and install Traefik in docker
  3. Configure and install two http based applications (homer and heimdall) in docker that will be accessed via traefik

Topology



The above image represents our topology. Our pfsense firewall is connected directly to the Internet and is NAT'ing all traffic from the internal network to the outside world. We have a single LAN segment (192.168.11.0/24) defined on pfsense and we have the following two PC's connected directly to the firewall:
  • Windows PC - This PC is used to install and configure traefik and for testing
  • Ubuntu server - This PC runs docker and will host traefik and our applications
Following is what we are trying to achieve:
  • From the windows PC we will open a browser and navigate to http://homer.home.local and http://heimdall.home.local
  • Since pfsense is our DNS server, the URL's will resolve to the ubuntu server's IP address and traefik will pick up the request as we will expose port 80 on the container
  • Traefik will then route the request to the appropriate container 
  • If all goes well we should see the homer and heimdall pages load on our Windows PC browser

Assumptions

  1. A DNS server that can be configured with an A record to point to a traefik proxy. We will be using the pfsense firewall
  2. An installation of docker and docker-compose. We will be running docker on Ubuntu 20.04.3

DNS Configuration

The goal is to access our application using freindly URL names as opposed to IP's and port numbers. In order to do so we need to identify a domain name. We are going to choose home.local as our domain name. This domain is used internally within our network and will not exist on the Internet. We need our DNS server to resolve *.home.local to our Ubuntu server. So homer.home.local and heimdall.home.local will both resolve to our Ubuntu server IP address 192.168.11.2.

In pfsense navigate to:
  1. Services --> DNS Resolver --> General Settings
  2. Enable DNS Resolver
  3. Scroll down and Display Customer Options and past the following config
 server:
 local-zone: "home.local" redirect
 local-data: "home.local 3600 IN A 192.168.11.2"
The windows PC and ubuntu server are configured to use pfsense (192.168.11.1) as the DNS server. If we ping home.local it should resolve to our Ubuntu server and ping successfully
 ping home.local
 PING home.local (192.168.11.2) 56(84) bytes of data.
 64 bytes from pserver (192.168.11.2): icmp_seq=1 ttl=64 time=0.032 ms
 64 bytes from pserver (192.168.11.2): icmp_seq=2 ttl=64 time=0.089 ms
 64 bytes from pserver (192.168.11.2): icmp_seq=3 ttl=64 time=0.109 ms

Configure and Install Traefik

Create a directory to store the docker files and traefik config files 

 mkdir traefik
 cd traefik

Create a docker-compose.yml file using an editor of your choice

 vim docker-compose.yml
Paste the following config in docker-compose.yml
version: "3.3"
services:
  traefik:
    container_name: traefik
    image: traefik:2.3 # Download traefik v2.3
    restart: always # Restart the container if stops
    ports:
      - 80:80 # Expose port 80 on the host machine
    volumes:
      - ./static_config.yml:/etc/traefik/traefik.yml # Traefik's static config file 
      - /var/run/docker.sock:/var/run/docker.sock # Allow Traefik access to docker API's to dynamically learn applications
    labels:
      # Traefil uses lables to learn about containers and dynamically configures itself. Expose the traefik dashboard on port 80
      - traefik.http.routers.traefik-http.rule=Host(`dashboard.home.local`)
      - traefik.http.routers.traefik-http.entrypoints=web
      - traefik.http.routers.traefik-http.service=api@internal
    # Use the customer network web that is defined below
    networks:
      - web      
networks:
  web:

Create a traefik static file using an editor of your choice

 vim static_config.yml
Paste the following config in static_config.yml
 log:
  level: DEBUG # Enable logging
api:
  dashboard: true # Enable the traefik dashboard 
entryPoints:
  web:
    address: ":80" # Define the name called web and expose port 80
providers:
  docker: {} # Tells traefik to monitor docker for changes and autoconfigure

Start Traefik

docker-compose up -d
The aforementioned command will pull the traefik image and start the container. The traefik dashboard should be reachable via http://dashboard.home.local as seen below

Configure and Install Homer

Create a folder called homer your home path
 mkdir homer
 cd homer

Create a docker-compose.yml file using an editor of your choice

 vim docker-compose.yml
Paste the following config in docker-compose.yml
version: "3.5"

services:
  homer:
    image: b4bz/homer
    container_name: homer
    volumes:
      - ./assets/:/www/assets
    ports:
      - 8080:8080
    restart: unless-stopped
    labels: # auto-detects port 80 in next line
      traefik.http.routers.homer.rule: Host(`homer.home.local`)
    networks:
      - traefik_web
networks:
  traefik_web:
    external: true

Start Homer

docker-compose up -d
The aforementioned command will pull the homer image and start the container. Since traefik is monitoring docker labels, it will process the label defined in line 13 and automatically configure itself and route to homer. The traefik container does not need to be restarted and that is the power of Traefik. The homer dashboard should be reachable via http://homer.home.local as seen below

Configure and Install Heimdall

Create a folder called homer your home path
 mkdir heimdall
 cd heimdall

Create a docker-compose.yml file using an editor of your choice

 vim docker-compose.yml
Paste the following config in docker-compose.yml
version: "2.1"
services:
  heimdall:
    image: lscr.io/linuxserver/heimdall
    container_name: heimdall
    volumes:
      - ./config:/config
    ports:
      - 8081:80
    restart: unless-stopped
    labels: # auto-detects port 80 in next line
      traefik.http.routers.heimdall.rule: Host(`heimdall.home.local`)
    networks:
      - traefik_web
networks:
  traefik_web:
    external: true

Start Heimdall

docker-compose up -d
The aforementioned command will pull the heimdall image and start the container. The heimdall dashboard should be reachable via http://heimdall.home.local as seen below

Conclusion

We installed the traefik reverse proxy and setup a DNS server to direct all requests to a domain (home.local) to the reverse proxy. We also installed two applications (homer and heimdall) that are accessible via the reverse proxy. Everything was installed in docker and traefik can automatically learn about the application to self-configure itself to route traffic to the appropriate application. In the next part we will configure traefik to route traffic to an external application (non-docker application), enable HTTPS and secure HTTP applications with HTTPS

References

Comments

Popular posts from this blog

Run your Meteor App on a Production Ubuntu 16.04 Server with Nginx

Introduction Meteor enables developers to create apps and quickly test them on a development webserver. Once you have created your app the big question is how to run it on a production server. In this tutorial we will demostrate how to run your Meteor app on Ubuntu 16.04 using Nginx Credits and Acknowledgements This entire post is based off this Digital Ocean article. The article was modified for issues we encountered and adapted for Ubuntu 16.04. The entire credit goes to Daniel Speichert Objective In this tutorial we will: Install and Configure Nginx with HTTPS enabled Install MongoDB Install NodeJS Bundle your Meteor App Create a startup script to automatically start your app on reboot Assumptions We assumue the following: You already have a fresh install of Ubuntu 16.04 Server SSH enabled on your fresh install You have root privelages on your server You have a different server where you can insall meteor and budle your app

Part 1- Install Django using Virtualenv, Apache and MySQL on Ubuntu Server 14.10

Introduction I have been using Microsoft Excel to do my Personal Budget. Excel is extremely powerful and has fulfilled my needs. But I always wanted to build a web application which is accessible via a browser and in order to do so we need the following: An admin interface (website) to plug in my expenses A database to store all the entries Code to parse the database and calculate the expenses per month A public interface (website) to display expense charts After browsing the web I stumbled across Django. Django (as copied from the project page) is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. It focuses on automating as much as possible and adhering to the DRY principle. In the first part of this tutorial we will walk through the installation of Django using Virtualenv. In the following series we will build our Personal Budget application using Django. What is virtualenv and why use it? Virtualenv (as copied from th