Kubernetes Ingress Controllers and NGINX Ingress: A Complete Guide
December 24, 2024

Kubernetes Ingress Controllers and NGINX Ingress: A Complete Guide


Kubernetes Ingress Controller and NGINX Ingress

Ingress in Kubernetes is a key resource for managing HTTP and HTTPS traffic for services within the cluster. It allows you to define rules for routing traffic based on hostname, path, or other attributes. In order for Ingress to work, you need a Ingress controllerone of the most popular options is NGINX ingress controller.




What is Ingress in Kubernetes?

Ingress is a Kubernetes API resource that provides routing rules to manage external access to services. Rather than exposing multiple services through LoadBalancer or NodePort, Ingress provides a single entry point to manage and route traffic.



Key features of Ingress:

  1. host-based routing: Route traffic based on hostname.
  2. path based routing: Route traffic based on URL path.
  3. TLS termination: Use SSL/TLS for secure communication.
  4. Centralized management: Reduce dependence on multiple service exposure methods.



Ingress controller

one Ingress controller It is the component that implements the Ingress API. While the ingress resource defines routing rules, the controller enforces them by configuring the underlying load balancer or proxy.



Popular Ingress controllers:

  1. NGINX ingress controller
  2. transportation
  3. HA proxy
  4. contour
  5. AWS/GCP/Azure specific controllers

Without an Ingress Controller, the Ingress resource has no effect.




NGINX ingress controller

this NGINX ingress controller It is one of the most widely used controllers in Kubernetes. It utilizes the powerful and high-performance NGINX web server to route and manage traffic.



Why choose NGINX?

  • High performance and scalability.
  • Rich feature set (e.g., advanced routing, rate limiting, custom error pages).
  • Easily integrate with existing Kubernetes deployments.



NGINX variants:

  1. NGINX Ingress Controller (community support):

    • Open source.
    • Actively maintained by the Kubernetes community.
  2. NGINX Plus:

    • Commercial product with additional enterprise-grade features.



Install NGINX ingress controller

You can install the NGINX Ingress Controller using the following tools kubectlHelm or Kubernetes manifest.



Option 1: Install using Helm

  1. New Helm repository:
   helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
   helm repo update
Enter full screen mode

Exit full screen mode

  1. Install the NGINX ingress controller:
   helm install ingress-nginx ingress-nginx/ingress-nginx
Enter full screen mode

Exit full screen mode

  1. Verify installation:
   kubectl get pods -n ingress-nginx
Enter full screen mode

Exit full screen mode



Option 2: Install using a manifest

  1. App official list:
   kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
Enter full screen mode

Exit full screen mode

  1. Verify resources:
   kubectl get all -n ingress-nginx
Enter full screen mode

Exit full screen mode




Create entry resources

After deploying the NGINX Ingress Controller, you can create a Entrance Resources for routing traffic.



Example: Basic Ingress

This example routes traffic to a backend service based on hostname.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: backend-service
                port:
                  number: 80
Enter full screen mode

Exit full screen mode



Example: Path-based routing

Route traffic to different services based on URL path:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: path-based-ingress
spec:
  rules:
    - host: example.com
      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
Enter full screen mode

Exit full screen mode



Example: TLS-enabled entry

Secure traffic using SSL/TLS:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tls-ingress
spec:
  tls:
    - hosts:
        - example.com
      secretName: example-tls-secret
  rules:
    - host: example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: secure-service
                port:
                  number: 443
Enter full screen mode

Exit full screen mode




Notes on NGINX Ingress

Annotations allow fine-tuning the behavior of the NGINX Ingress Controller. Here are some common ones:

  • Rewrite target: Rewrite the URL before forwarding to the backend.
  nginx.ingress.kubernetes.io/rewrite-target: /
Enter full screen mode

Exit full screen mode

  • Custom error page: Define custom error response.
  nginx.ingress.kubernetes.io/custom-http-errors: "404,503"
Enter full screen mode

Exit full screen mode

  • rate limit: Limit requests to prevent abuse.
  nginx.ingress.kubernetes.io/limit-rps: "5"
Enter full screen mode

Exit full screen mode

  • Client body type: Control upload limits.
  nginx.ingress.kubernetes.io/proxy-body-size: "10m"
Enter full screen mode

Exit full screen mode




Monitor and debug NGINX Ingress

  1. View log:
   kubectl logs -n ingress-nginx 
Enter full screen mode

Exit full screen mode

  1. access metrics:

    • Integrate with Prometheus and Grafana for detailed monitoring.
    • Use the built-in NGINX metrics endpoint.
  2. Test entry rules:

    • use curl Verify routing:
     curl -H "Host: example.com" http://
    



NGINX Ingress Best Practices

  1. Use namespaces:

    • Deploy separate Ingress resources for different namespaces to isolate traffic.
  2. safe transportation:

    • Always configure TLS for production environments.
    • Get a free SSL certificate through tools like Let’s Encrypt certificate manager.
  3. zoom controller:

    • Use Kubernetes Horizontal Pod Autoscaler (HPA) to scale the Ingress Controller based on traffic.
  4. Test routing rules:

    • Validate all Ingress rules in staging before applying them to production.



Common commands

  kubectl get ingress
Enter full screen mode

Exit full screen mode

  kubectl describe ingress 
Enter full screen mode

Exit full screen mode

  kubectl logs -n ingress-nginx 
Enter full screen mode

Exit full screen mode




in conclusion

NGINX Ingress Controller provides a powerful and flexible solution for managing external traffic in Kubernetes. By learning how to set up Ingress resources and taking advantage of NGINX’s advanced features, you can create scalable, secure, and efficient traffic management for Kubernetes applications.


2024-12-24 05:03:21

Leave a Reply

Your email address will not be published. Required fields are marked *