Home » Backend Dev » kubernetes » 30 Days kubernetes » Day 15: Kubernetes – Ingress Controllers

Day 15: Kubernetes – Ingress Controllers

Introduction to Ingress Controllers

In Kubernetes, managing external access to services running in a cluster can be challenging. While Services like NodePort and LoadBalancer can expose applications, they lack advanced routing and management capabilities. Kubernetes Ingress solves this by offering a way to configure HTTP and HTTPS routing to services based on hostnames, paths, and more.

This guide explains the concept of Ingress and Ingress Controllers, how to configure them, and practical examples for managing traffic to your Kubernetes applications.


What is Ingress?

Ingress is a Kubernetes API object that:

  1. Provides HTTP and HTTPS routing to services within the cluster.
  2. Enables hostname- and path-based routing.
  3. Supports SSL termination and redirect rules.

Key Benefits of Ingress:

  • Centralized Traffic Management: Manage all HTTP/HTTPS traffic rules in one place.
  • Flexibility: Route requests based on paths, hostnames, or both.
  • SSL Termination: Handle HTTPS connections with ease.

What is an Ingress Controller?

An Ingress Controller is a component that implements the Ingress API. It acts as a reverse proxy to process Ingress resources.

Popular Ingress Controllers include:

  1. NGINX Ingress Controller
  2. Traefik
  3. HAProxy Ingress
  4. AWS ALB Ingress Controller

Setting Up an Ingress Controller

Step 1: Installing an NGINX Ingress Controller

  1. Deploy the NGINX Ingress Controller using Helm:helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx helm repo update helm install ingress-nginx ingress-nginx/ingress-nginx --namespace ingress-nginx --create-namespace
  2. Verify the installation:kubectl get pods -n ingress-nginx

Step 2: Expose the Ingress Controller

Ensure the Ingress Controller is accessible via a Service (e.g., LoadBalancer or NodePort).

kubectl get svc -n ingress-nginx

Configuring an Ingress Resource

Example: Defining an Ingress Resource

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: web-service
            port:
              number: 80

Step 1: Apply the Ingress Resource

kubectl apply -f example-ingress.yaml

Step 2: Test Access

  1. Update your /etc/hosts file to map example.com to your cluster’s IP.
  2. Access the application via http://example.com.

Advanced Features of Ingress

1. SSL Termination

To secure your application, you can terminate HTTPS traffic using SSL certificates.

Step 1: Create a Secret for SSL

kubectl create secret tls tls-secret --key tls.key --cert tls.crt

Step 2: Update the Ingress Resource

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

Step 3: Apply and Test

kubectl apply -f secure-ingress.yaml

Access the application via https://example.com.

2. Path-Based Routing

Route traffic to different services based on URL paths.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: multi-path-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

3. Redirect Rules

Use annotations to configure redirection.

metadata:
  annotations:
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"

Best Practices for Ingress

  1. Choose the Right Controller: Select an Ingress Controller suited to your environment.
  2. Use Annotations Wisely: Configure advanced features with annotations.
  3. Monitor Traffic: Use monitoring tools like Prometheus and Grafana.
  4. Secure Traffic: Always use SSL/TLS for sensitive applications.
  5. Optimize Resource Usage: Scale your Ingress Controller to handle traffic loads effectively.

Conclusion

Ingress Controllers provide a robust way to manage HTTP and HTTPS traffic in Kubernetes clusters. By leveraging features like path-based routing, SSL termination, and redirection, you can build scalable and secure applications.


References

*** Your support will help me continue to bring new Content. Love Coding *** ❤️


Feedback and Discussion

Have questions or feedback? Comment below! Let’s build a collaborative learning environment. Check out more articles on Node.js, Express.js, and System Design.

Leave a Comment

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