0
0
Kubernetesdevops~5 mins

Ingress controllers (Nginx, Traefik) in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run multiple apps inside Kubernetes, you need a way to let users reach them from outside. Ingress controllers like Nginx and Traefik help by managing how external traffic gets routed to your apps safely and easily.
When you want to expose several web apps on the same IP but different URLs or paths.
When you need to manage SSL certificates for your apps automatically.
When you want to control traffic rules like redirects or load balancing inside your cluster.
When you want to monitor and log incoming requests centrally.
When you want to simplify external access without creating many LoadBalancer services.
Config File - ingress.yaml
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - host: example.local
    http:
      paths:
      - path: /app1(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: app1-service
            port:
              number: 80
      - path: /app2(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: app2-service
            port:
              number: 80

This file defines an Ingress resource that routes traffic based on URL paths to two different services inside the cluster.

metadata.annotations tells Nginx to rewrite the URL path before sending it to the service.

spec.ingressClassName specifies that this Ingress uses the Nginx controller.

spec.rules define the host and paths to route traffic to the right backend services.

Commands
This command creates the Ingress resource in Kubernetes, telling the Ingress controller how to route external traffic to your services.
Terminal
kubectl apply -f ingress.yaml
Expected OutputExpected
ingress.networking.k8s.io/example-ingress created
This command checks the status and details of the Ingress resource to confirm it was created and see its rules.
Terminal
kubectl get ingress example-ingress
Expected OutputExpected
NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress nginx example.local 192.168.99.100 80 10s
This command shows detailed information about the Ingress, including backend services and any events or errors.
Terminal
kubectl describe ingress example-ingress
Expected OutputExpected
Name: example-ingress Namespace: default Address: 192.168.99.100 Default backend: <none> Rules: Host Path Backends ---- ---- -------- example.local /app1(/|$)(.*) app1-service:80 (10.1.1.10:80) /app2(/|$)(.*) app2-service:80 (10.1.1.11:80) Annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Sync 10s nginx-ingress-controller Scheduled for sync
This command verifies that the Nginx Ingress controller pods are running in the cluster namespace where it is installed.
Terminal
kubectl get pods -n ingress-nginx
Expected OutputExpected
NAME READY STATUS RESTARTS AGE nginx-ingress-controller-5d7f9f7d7f-abc12 1/1 Running 0 5m
Key Concept

If you remember nothing else from this pattern, remember: Ingress controllers route external traffic to your Kubernetes services based on rules you define.

Common Mistakes
Not specifying the correct ingressClassName matching the installed controller
The Ingress resource won't be picked up by any controller, so traffic won't route correctly.
Check which ingress controller is installed and use its class name exactly in the Ingress spec.
Forgetting to create or install the Ingress controller before applying Ingress resources
Ingress resources depend on the controller to function; without it, rules have no effect.
Install the Nginx or Traefik Ingress controller first using their official manifests or Helm charts.
Using incorrect path syntax or missing rewrite annotations
Requests may not reach the backend services correctly or cause 404 errors.
Use proper pathType and rewrite-target annotations as per the controller's documentation.
Summary
Create an Ingress resource to define routing rules for external traffic to services.
Use kubectl apply to deploy the Ingress and kubectl get/describe to verify it.
Ensure the Ingress controller (Nginx or Traefik) is installed and running to handle the traffic.