DEV Community

Josef Polar
Josef Polar

Posted on

Adding Ingress with HAProxy

After getting the Service running, the next thing I needed was a way to route external HTTP traffic to it. That's where Ingress comes in — and I went with the HAProxy ingress controller.

Installing the HAProxy ingress controller

helm repo add haproxytech https://haproxytech.github.io/helm-charts
helm repo update
helm install haproxy-ingress haproxytech/kubernetes-ingress \
  --namespace ingress-controller \
  --create-namespace
Enter fullscreen mode Exit fullscreen mode

Then I checked it came up:

kubectl get pods -n ingress-controller
Enter fullscreen mode Exit fullscreen mode

Writing the Ingress resource

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    kubernetes.io/ingress.class: haproxy
spec:
  rules:
    - host: my-app.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: my-app-service
                port:
                  number: 80
Enter fullscreen mode Exit fullscreen mode

The host field is the domain I want to route. The backend.service.name matches the Service I created earlier — that chain of connections (Deployment → Service → Ingress) is the pattern that keeps repeating in Kubernetes.

Applying it

kubectl apply -f ingress.yaml
kubectl get ingress
Enter fullscreen mode Exit fullscreen mode

Testing it locally

If I don't have a real domain yet, I can fake it by adding an entry to /etc/hosts:

127.0.0.1 my-app.example.com
Enter fullscreen mode Exit fullscreen mode

Then hitting http://my-app.example.com routes traffic through HAProxy into the service and down to the pods.

Top comments (0)