Learn Kubernetes Β· 8/14

Ingress: Advanced HTTP Routing and Traffic Management

7 min read

Ingress: Advanced HTTP Routing and Traffic Management 🚦

Welcome back, future Kubernetes pro! Today, we're diving into one of the most powerful networking concepts in Kubernetes: Ingress. We'll explore how it acts as the smart traffic director for your cluster, guiding external requests to the right applications.


1. Ingress vs. LoadBalancers: Why We Need Ingress πŸ€”

The Problem: Too Many Doors

Imagine you're managing an apartment building (your Kubernetes cluster) with many different apartments (your applications or microservices). Now, what if you had to build a separate, expensive, public-facing front door for every single apartment? That's what using a LoadBalancer service for every application feels like.

In Kubernetes, you can expose an application to the internet using a Service of type LoadBalancer. When you do this on a cloud provider like AWS, GCP, or Azure, Kubernetes automatically provisions a cloud load balancer for that service.

This is great for a single application, but it quickly becomes:

  • Expensive: Each cloud load balancer comes with its own cost.
  • Complex: You end up managing multiple external IP addresses, one for each service.
  • Inefficient: It doesn't scale well when you have dozens of microservices.

The Solution: A Smart Receptionist (Ingress)

Ingress provides a much smarter and more cost-effective solution. Instead of many doors, imagine one main entrance to your apartment building with a very intelligent receptionist. This receptionist (the Ingress Controller) checks who the visitor wants to see (the requested hostname and URL path) and directs them to the correct apartment (the Kubernetes Service).

An Ingress is a Kubernetes API object that defines rules for routing external HTTP and HTTPS traffic to services within the cluster. It operates at the application layer (Layer 7), which allows it to make intelligent routing decisions based on things like the URL.

In short, Ingress gives you:

  • A single, stable entry point for your cluster.
  • The ability to route traffic to different services based on the requested domain or path.
  • Centralized management of traffic rules, SSL certificates, and more.

2. Setting up an Ingress Controller (NGINX ingress) βš™οΈ

The Problem: Rules Without an Enforcer

So you've created an Ingress resource with all your routing rules. You apply it and... nothing happens. Why? 🀷

Creating an Ingress resource is like writing down a list of rules for your building's receptionist. But if you haven't hired a receptionist yet, the rules are just words on paper.

In Kubernetes, that "receptionist" is the Ingress Controller. It's a separate application that runs in your cluster, watches the Kubernetes API for Ingress resources, and then actually implements the routing rules you've defined. It's the engine that makes Ingress work.

There are many popular Ingress controllers like NGINX, Traefik, and HAProxy. Today, we'll focus on the most widely-used one: the NGINX Ingress Controller.

Hands-On: Installing the NGINX Ingress Controller

The most common and recommended way to install the NGINX Ingress Controller is with Helm, the package manager for Kubernetes.

Step 1: Add the Ingress-NGINX Helm repository.
This command adds the official chart repository so Helm knows where to find the Ingress controller package.

bash
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

Step 2: Install the Ingress-NGINX chart.
This command installs the controller into its own ingress-nginx namespace. Creating a separate namespace is a good practice for keeping cluster components organized.

bash
helm install ingress-nginx ingress-nginx/ingress-nginx \
  --namespace ingress-nginx --create-namespace

This command is idempotent, meaning you can run it again to upgrade your controller if a new version is released.

Step 3: Verify the installation.
Check that the Ingress controller pod is up and running. You should also see a LoadBalancer service that exposes the controller to the internet.

bash
kubectl get pods --namespace=ingress-nginx

You should see a pod with a name like ingress-nginx-controller-... in the Running state. It might take a minute or two for the cloud provider to assign an external IP address to the service.

πŸŽ‰ Fact Check! You've just deployed the brains of your cluster's network traffic. This single controller will now act as the entry point for all the applications you choose to expose.


3. Configuring Name-Based Virtual Hosting 🌐

The Problem: One IP, Multiple Websites

Let's say you're running two different applications in your cluster: a company blog at blog.my-awesome-company.com and a shopping cart at shop.my-awesome-company.com. How can you direct traffic to the correct application based on the domain name the user enters?

The Solution: Name-Based Virtual Hosting

This is a core feature of Ingress. You can create rules that tell the Ingress controller to look at the Host header of an incoming HTTP request and route it to the appropriate service.

Example: Routing to a Blog and a Shop

First, let's assume you have two services already running: blog-service and shop-service.

Now, let's create an Ingress resource to manage the traffic.

multi-host-ingress.yaml

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: multi-host-example-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: "blog.my-awesome-company.com"
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: blog-service
            port:
              number: 80
  - host: "shop.my-awesome-company.com"
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: shop-service
            port:
              number: 80

Breakdown of the YAML:

  • ingressClassName: nginx: This tells Kubernetes which Ingress controller should handle this Ingress resource. It's important when you might have multiple controllers in a cluster.
  • spec.rules: This is an array where each item is a routing rule.
  • host: This is the domain name the rule applies to.
  • http: Defines the rules for HTTP traffic.
  • paths: An array of path-based rules for this host.
  • path: /: This matches all requests for that domain.
  • pathType: Prefix: This is the most common path type. It matches any URL that starts with the specified path.
  • backend: Specifies where to send the traffic.
  • service: The name and port of the Kubernetes Service to forward traffic to.

Apply it with kubectl apply -f multi-host-ingress.yaml, and your smart receptionist now knows exactly where to send visitors for the blog and the shop!


4. Path-Based Routing and SSL/TLS Termination πŸ›£οΈπŸ”’

The Problem: One Domain, Multiple Microservices

Name-based routing is great, but what if you have a single domain, api.my-awesome-company.com, and want to route traffic to different microservices based on the URL path? For instance:

  • api.my-awesome-company.com/users -> users-service
  • api.my-awesome-company.com/payments -> payments-service

And how do you secure this communication with HTTPS?

The Solution Part 1: Path-Based Routing

Ingress easily handles this by allowing you to specify different backend services for different paths under the same host.

Example: Routing an API Gateway

path-based-ingress.yaml

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api-gateway-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: "api.my-awesome-company.com"
    http:
      paths:
      - path: /users
        pathType: Prefix
        backend:
          service:
            name: users-service
            port:
              number: 80
      - path: /payments
        pathType: Prefix
        backend:
          service:
            name: payments-service
            port:
              number: 80

With this configuration, the Ingress controller inspects the URL path and routes the request accordingly.

The Solution Part 2: SSL/TLS Termination

Handling encryption and decryption for every single one of your applications is a headache. TLS Termination is the process where the Ingress controller handles the HTTPS part of the communication. It decrypts the incoming traffic, sends it as plain, unencrypted HTTP to your services inside the cluster, and encrypts the response on the way back out.

This is incredibly efficient because:

  • Your applications don't need to handle the computational overhead of encryption.
  • You only need to manage your TLS certificates in one place.

Hands-On: Securing Your Ingress

Step 1: Get a TLS Certificate.
For production, you would get this from a Certificate Authority (CA) like Let's Encrypt. For this guide, let's assume you have two files: tls.crt (your certificate) and tls.key (your private key).

Step 2: Create a Kubernetes Secret.
Kubernetes stores sensitive information like TLS certificates in an object called a Secret.

bash
kubectl create secret tls my-tls-secret \
  --cert=path/to/your/tls.crt \
  --key=path/to/your/tls.key

This command creates a secret named my-tls-secret containing your certificate and key.

Step 3: Update your Ingress resource.
Add a tls section to your Ingress YAML to tell the controller to use your secret for a specific host.

secure-api-ingress.yaml

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api-gateway-ingress
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - api.my-awesome-company.com
    secretName: my-tls-secret # <-- Reference to the secret
  rules:
  - host: "api.my-awesome-company.com"
    http:
      paths:
      - path: /users
        pathType: Prefix
        backend:
          service:
            name: users-service
            port:
              number: 80
      - path: /payments
        pathType: Prefix
        backend:
          service:
            name: payments-service
            port:
              number: 80

Now apply the updated manifest. The NGINX Ingress controller will automatically grab the certificate from the secret and start serving traffic for api.my-awesome-company.com over HTTPS!


πŸ“š Extra Reading & References

Challenge

You are a DevOps engineer tasked with configuring external access for a multi-service application running in the production namespace. Create a single Kubernetes Ingress manifest file to manage the traffic according to the following requirements:

  • Resource: The manifest must define an Ingress resource using the API version networking.k8s.io/v1.
  • Metadata:
    • Name the Ingress resource production-gateway-ingress.
    • Place it in the production namespace.
  • Routing Rules:
    1. Website Host (Name-Based):
      • For the hostname shop.my-awesome-company.com, route all incoming traffic (/) to a service named storefront-service on port 80.
    2. API Host (Path-Based):
      • For the hostname api.my-awesome-company.com, implement path-based routing:
        • Requests to the exact path /auth should be directed to the auth-service on port 9000.
        • Requests to any path that begins with /inventory should be directed to the inventory-api-service on port 5000.
  • TLS/SSL Termination:
    • Secure both hostnames (shop.my-awesome-company.com and api.my-awesome-company.com) using TLS.
    • The TLS certificate and key are stored in a Kubernetes Secret named my-company-tls-cert. Your Ingress manifest must reference this secret to enable HTTPS.

Expected result

Your submission should be a single, valid Kubernetes Ingress manifest in YAML format.

The manifest must contain:

  • apiVersion set to networking.k8s.io/v1.
  • kind set to Ingress.
  • metadata with the correct name and namespace as specified in the instructions.
  • A spec section that includes:
    • A tls block specifying both hostnames in the hosts list and the correct secretName.
    • A rules array containing two distinct host rule definitions.
      • The first rule for shop.my-awesome-company.com should route the path / (using pathType: Prefix) to the storefront-service on port 80.
      • The second rule for api.my-awesome-company.com must contain two paths:
        • The path /auth with pathType: Exact, routing to auth-service on port 9000.
        • The path /inventory with pathType: Prefix, routing to inventory-api-service on port 5000.
yaml

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.