Deploy Traefik as a reverse proxy with Kubernetes
Bensuperpc May 25, 2025 [Software] #Features #kubernetes #Reverse proxy #Nginx #TraefikDeploy Traefik as a reverse proxy with Kubernetes
I recommend learning the basics of Docker/containers and Kubernetes before starting this tutorial.
Dependencies and configure Kubernetes
Install the required packages for Kubernetes and Minikube on your system:
On Arch Linux/Manjaro:
start minikube:
Configure Kubernetes context to use Minikube:
&& &&
Enable and start kubelet service:
&& &&
Deploy Traefik with Helm
Let's deploy Traefik using Helm, which is a package manager for Kubernetes.
&& &&
Once the installation is complete, you can check the status of the traefik install:
Result:
You can also check the status of services:
You should see output similar to this:
|||||
| | | | |
|||||
| | | | |
| | | | |
| | | | |
| | | | |
|||||
The NAME (traefik) must match to ingressClassName in the Ingress controller.
You can also check the ingress controller:
Result:
Get the Traefik service URL:
Result:
You can check traefik
pod in the traefik-tutorial
namespace:
Result:
)
)
)
)
)
)
)
Deploy a sample application with Traefik
You can now deploy a sample application to test Traefik as a reverse proxy, we will deploy the whoami
application, which is a simple HTTP server that returns information about the request it receives.
traefik.ingress.whoami.yml : Ingress define to route traffic to the whoami
service.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: whoami-ingress
namespace: traefik-tutorial
annotations:
#kubernetes.io/ingress.class: traefik
traefik.ingress.kubernetes.io/router.entrypoints: web
#traefik.ingress.kubernetes.io/router.entrypoints: websecure
spec:
ingressClassName: traefik
rules:
- host: whoami.bensuperpc.org
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: whoami
port:
number: 80
- host: whoami.192.168.49.2.nip.io
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: whoami
port:
number: 80
whoami.deployment.yml : The Deployment specifies how the whoami
application runs inside a pod.
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: traefik-tutorial
labels:
app: whoami
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
ports:
- name: web
containerPort: 80
whoami.services.yml : The Service specifies how the application is made accessible on the network.
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: traefik-tutorial
spec:
type: ClusterIP
ports:
- name: web
port: 80
targetPort: 80
selector:
app: whoami
Apply the Ingress, Deployment, and Service configurations:
Check the status of the pods to ensure they are running:
You should see whoami
and traefik
pods running in the traefik-tutorial
namespace:
)
)
)
)
)
)
)
)
)
Now you can access the whoami
service through Traefik using the defined hostnames:
Result:
If you access with your browser to http://whoami.192.168.49.2.nip.io:31134/
, you should see the same result.
You may need to add the whoami.192.168.49.2.nip.io
and whoami.bensuperpc.org
domains to your /etc/hosts
file or use a DNS service that resolves it to the Minikube IP address.
Scaling the application
We can now add more whoami instances, and Traefik will route the traffic automatically based on the Ingress rules defined.
Let's scale the whoami
deployment to 5 replicas:
Now check the status of the pods again:
You should see multiple whoami
pods running:
)
)
)
)
)
)
)
)
)
You can access the whoami
service again, and it will now return responses from different pods, demonstrating that Traefik is load balancing the requests across the replicas.
Hostname
and IP
will change each time you call the service, as it will return the information from a different pod.
Rescale the whoami
deployment back to 1 replica:
Horizontal Pod Autoscaler
You need to install the metrics server to use HPA. You can install it with the following command:
Now update your whoami
deployment to use HPA to automatically scale the number of replicas.
whoami.deployment.yml
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: traefik-tutorial
labels:
app: whoami
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
ports:
- name: web
containerPort: 80
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "250m"
memory: "256Mi"
Add the Horizontal Pod Autoscaler configuration to automatically scale the whoami
deployment.
whoami.hpa.yml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: whoami
namespace: traefik-tutorial
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: whoami
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Apply the updated whoami
deployment and HPA configuration:
Now you can check the status of the Horizontal Pod Autoscaler:
Expected result:
Clean up
To clean up the resources created during this tutorial, you can delete the Ingress, Deployment, and Service configurations, as well as the Traefik installation.
Uninstall helm chart:
Delete all resources in the traefik-tutorial
namespace and cleanup Minikube:
&&
More info: