Step-by-Step Guide to Setting Up Traefik with MicroK8s without Using Hetzner's Load Balancer
This guide will walk you through setting up Traefik as an Ingress Controller on a MicroK8s Kubernetes cluster hosted on Hetzner, without using Hetzner's paid Load Balancer service. Instead, we'll configure Traefik to bind directly to the host's network interfaces, allowing it to receive traffic on ports 80 and 443.
Prerequisites
- Ubuntu 20.04 LTS or later installed on your Hetzner server.
- MicroK8s installed on your server.
- sudo privileges on the server.
- Domain Name (optional but recommended). If you have a domain name, you can point it to your server's IP address for testing.
Step 1: Install MicroK8s
If you haven't already installed MicroK8s, run the following commands:
sudo snap install microk8s --classic
Add your user to the microk8s group:
sudo usermod -a -G microk8s $USER
Change ownership of the Kubernetes configuration directory:
sudo chown -f -R $USER ~/.kube
Log out and log back in to apply the group changes:
exit
Step 2: Enable Necessary MicroK8s Add-ons
Enable DNS and storage add-ons:
microk8s enable dns storage
Check the status of MicroK8s to ensure everything is running:
microk8s status --wait-ready
Step 3: Install Helm 3
Helm is a package manager for Kubernetes. Install Helm 3 using the MicroK8s add-on:
microk8s enable helm3
Step 4: Install Traefik Using Helm
4.1: Create a Namespace for Traefik
microk8s kubectl create namespace traefik
4.2: Add Traefik Helm Repository
microk8s helm3 repo add traefik https://helm.traefik.io/traefik
microk8s helm3 repo update
4.3: Create a Custom Values File for Traefik
Create a file named traefik-values.yaml and add the following configuration:
# traefik-values.yaml
ports:
web:
port: 80
hostPort: 80
expose: true
websecure:
port: 443
hostPort: 443
expose: true
service:
type: ClusterIP
deployment:
hostNetwork: true
podAnnotations:
prometheus.io/port: "8082"
prometheus.io/scrape: "true"
additionalArguments:
- "--providers.kubernetescrd"
- "--providers.kubernetesingress"
- "--entryPoints.web.address=:80"
- "--entryPoints.websecure.address=:443"
This configuration allows Traefik to bind directly to ports 80 and 443 on the host.
4.4: Install Traefik with Custom Values
microk8s helm3 install traefik traefik/traefik --namespace traefik -f traefik-values.yaml
Step 5: Verify Traefik Installation
Check that the Traefik pods are running:
microk8s kubectl get pods -n traefik
You should see something like:
NAME READY STATUS RESTARTS AGE
traefik-abcdef12345-xxxxx 1/1 Running 0 1m
Step 6: Deploy a Sample Application
6.1: Create a Namespace for Your Application
microk8s kubectl create namespace my-app
6.2: Deploy the Whoami Application
Create a file named whoami-deployment.yaml with the following content:
# whoami-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: whoami
namespace: my-app
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
ports:
- containerPort: 80
Apply the deployment:
microk8s kubectl apply -f whoami-deployment.yaml
6.3: Create a Service for Whoami
Create a file named whoami-service.yaml:
# whoami-service.yaml
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: my-app
spec:
selector:
app: whoami
ports:
- protocol: TCP
port: 80
targetPort: 80
Apply the service:
microk8s kubectl apply -f whoami-service.yaml
Step 7: Create an Ingress Resource
Create a file named whoami-ingress.yaml:
# whoami-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: whoami-ingress
namespace: my-app
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
rules:
- host: your.domain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: whoami
port:
number: 80
Replace your.domain.com with your actual domain or the server's public IP address.
Apply the ingress:
microk8s kubectl apply -f whoami-ingress.yaml
Step 8: Update DNS Records or Hosts File
If you have a domain name, update its DNS A record to point to your Hetzner server's public IP address. If you're testing without a domain, add an entry to your local /etc/hosts file:
On your local machine:
echo "YOUR_SERVER_IP your.domain.com" | sudo tee -a /etc/hosts
Step 9: Configure Firewall Rules
Ensure that ports 80 and 443 are open in your server's firewall settings.
On the server:
sudo ufw allow 80
sudo ufw allow 443
Check the firewall status:
sudo ufw status
Step 10: Test the Setup
10.1: Access the Application via Browser
Open a web browser and navigate to http://your.domain.com/. You should see output similar to:
Hostname: xxxxxxxxxx
IP: x.x.x.x
IP: x.x.x.x
RemoteAddr: x.x.x.x:xxxxx
10.2: Test with curl
curl http://your.domain.com/
Optional: Enable HTTPS with Let's Encrypt
To secure your application with HTTPS, configure Traefik to obtain Let's Encrypt certificates.
1. Update Traefik Values File
Modify your traefik-values.yaml to include Let's Encrypt configuration:
# Additional entries in traefik-values.yaml
certificatesResolvers:
letsencrypt:
acme:
email: your-email@example.com
storage: /data/acme.json
tlsChallenge: {}
additionalArguments:
- "--certificatesresolvers.letsencrypt.acme.tlschallenge=true"
- "--certificatesresolvers.letsencrypt.acme.email=your-email@example.com"
- "--certificatesresolvers.letsencrypt.acme.storage=/data/acme.json"
2. Update the Ingress Resource
Modify whoami-ingress.yaml to use the websecure entry point and Let's Encrypt certificate resolver:
# whoami-ingress.yaml modifications
metadata:
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: websecure
traefik.ingress.kubernetes.io/router.tls.certresolver: letsencrypt
spec:
tls:
- hosts:
- your.domain.com
3. Redeploy Traefik
Upgrade the Traefik installation with the updated values:
microk8s helm3 upgrade traefik traefik/traefik --namespace traefik -f traefik-values.yaml
4. Apply the Updated Ingress
microk8s kubectl apply -f whoami-ingress.yaml
5. Test HTTPS Access
Access https://your.domain.com/ in your browser. You should see the secure connection with a valid Let's Encrypt certificate.
Congratulations!
You've successfully set up Traefik as an Ingress Controller on MicroK8s without using Hetzner's paid Load Balancer service. Your applications are accessible via ports 80 and 443 directly, and you can manage routing with Kubernetes Ingress resources.
Important Notes
- Security: Exposing ports 80 and 443 directly requires careful security considerations. Always ensure your server is secured and monitored.
- Scalability: This setup is suitable for single-node or small clusters. For larger clusters, consider integrating a more robust load balancing solution.
- Monitoring and Logging: Implement monitoring and logging for your services and Traefik to maintain visibility into your cluster's operations.
- Updates: Keep MicroK8s, Traefik, and your applications up to date with the latest security patches and releases.
References
If you encounter any issues or have questions, feel free to ask for help!