How to Configure Traefik for Routing Applications in Kubernetes

By Anish Nath, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

This tutorial explains how to use Traefik as an Ingress controller for a Kubernetes cluster. We will be covering:

  • Setting up Traefik
  • Traefik Dashboard configuration
  • Define Name based Routing
  • Define Path Based routing

Before You Begin

You only need to have running Kubernetes cluster. In case you are new to Alibaba Cloud, you can get $10 worth in credit through my referral link to get started. Then, head on to this tutorial to learn how you can set up a fully functioning Kubernetes cluster.

Exposing Services to External Clients

In Kubernetes, you can make service accessible externally through NodePort, Loadbalancer, and Ingress Resource. This tutorial is dedicated to using Ingress Resource through the Traefik Kubernetes Ingress Controller

About Traefik

Traefik is a modern HTTP reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with your existing infrastructure components and configures itself automatically and dynamically. Pointing Traefik at your orchestrator should be the only configuration step you need.

Image for post
Image for post

Traefik Ingresses operate at the application layer of the network stack (HTTP) and can provide features such as cookie-based session affinity and the like, which services can’t.

Setting Up Traefik

Helm is a tool for managing Kubernetes charts. Charts are packages of pre-configured Kubernetes resources. Before setting up Traefik, we need to setup helm first install helm and then configure it on the k8s cluster. You can ignore these steps and jump directly to helm Traefik installation if the helm is already configured.

Download the latest version of helm

Then, initialize helm to both set up the local environment and to install the server portion, Tiller, on your cluster

Make sure Tiller, on your cluster is up and running.

Installing Traefik Using Helm

The below command will setup the traefik ingress in the kube-system namespace, and setup necessary RBAC in k8 cluster to operated ingress correctly and also setup the traefik dashboard, which is accessible through

This command produces a lot of output, so let’s take it one step at a time. First, we get information about the release that’s been deployed

Next, we get the resources that were actually deployed by the stable/traefik chart

Well if everything goes well then, Check the traefik pods are running in the kube-system namespace

Once ‘EXTERNAL-IP’ is no longer ‘’: it’s time to access your traefik dashboard, open the url

Image for post
Image for post

In the Traefik dashboard and you should see a frontend for dashboard.traefik host. Along with a backend listing for dashboard.traefik service with a server set up for each pod.

Upto this point we have successfully installed and configured traefik, now let’s define the routing (name based and path based ) of your application.

Launching Demo Application

We are going here to setup three sample nginx cheese web application, the docker images are located here .

  1. Docker image: errm/cheese:wensleydale
  2. Docker image: errm/cheese:cheddar
  3. Docker image: errm/cheese:stilton
Image for post
Image for post

Name Based Routing

The Name-Based Routing performs routing by name and support routing HTTP traffic to multiple host names at the same IP address but different domain names. Let’s start by launching the pods for the cheese websites.

Deployment of Cheese Web Application

The YAML file for the cheese application

To provide some explanations about the file content:

  • We define a deployment ()
  • The name of the object is “stilton” ()
  • We want one replica ()
  • It will deploy pods that have the label app:cheese ()
  • Then we define the pods ()
  • The Pods will have the cheese label ()
  • The Pods will host a container using the image tag errm/cheese:stilton ()
  • The same deployment is repeated for cheddar and wensleydale

Now provision these nginx cheese application.

Make sure all the cheese deployment pods are up and running

Service Cheese Web Application

Next we need to setup a Service for each of the cheese pods.

At this point, we have deployment and Service ready in the K8 cluster, and we’re about to define the ingress rules so that the world can eat the required service.

To provide some explanations about the file content:

  • We define a Ingress ()
  • The name of the object is “cheese” ()
  • Then we define the rules ()
  • For each service there is hostname defined for example the hostname stilton.minikube is mapped to stilton service.
  • The rules are repeated for each service.
  • Let’s apply this rule in k8 cluster

Verify the Ingress, all the hosts can be accessed with the ingress port 80

Now visit the Traefik dashboard and you should see a frontend for each host. Along with a backend listing for each service with a server set up for each pod.

Open the web browser and start eating your favorite cheese

Image for post
Image for post
Image for post
Image for post

PATH Based Routing

Path based routing differ from Name based routing in a sense, we don’t have multiple domains names, all the URI is distinguished and routed from the PATH prefix under a single domain, for example the above cheese application can be access through the single URI.

Image for post
Image for post

Let’s create the PATH base routing for the cheese application

View the Ingress, as you can notices the newly created ingress cheeses can be accessed through the hostname cheeses.minikube

You should now be able to visit the websites in your browser.

Final Notes

  • The above traefik installation is not secure, and shouldn’t be used for production, for production setting refer the helm stable/traefik charts, for example the below chart value will set traefik in SSL mode, and set the BASIC AUTH enabled
  • It is advisable to install the traefik in the kube-system namespace
  • Always measure your resource needs, and adjust requests and limits accordingly.

Reference:

https://www.alibabacloud.com/blog/594720?spm=a2c41.12820772.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

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