Kubernetes Series Part 4: Mastering Kubernetes Networking with Ingress

Table of Contents

Kubernetes Series Part 3: Mastering Kubernetes Networking with Ingress

Welcome back to the Kubernetes series! In the previous post, we explored YAML, the language of choice for defining and managing your Kubernetes resources. Now, let’s unravel the magic of Kubernetes networking and learn how to expose your applications to the outside world using Ingress.

Networking Concepts in Kubernetes

Networking in Kubernetes can seem complex at first, but understanding a few key concepts will make your life much easier:

  • Pod-to-Pod Communication: Pods in Kubernetes have their own IP addresses, allowing them to communicate directly with each other within the cluster. Kubernetes ensures that any Pod can communicate with any other Pod, regardless of which node they’re running on.
  • Cluster Networking with Minikube: Minikube, our trusty local Kubernetes environment, provides a simple networking model where all Pods share the same virtual network. This makes it easy to get started with Kubernetes networking without worrying about complex network configurations.

Introduction to Ingress Controllers

While Services allow you to expose applications within the cluster, exposing them to the outside world requires a different approach. This is where Ingress comes in.

Ingress acts as a smart reverse proxy and entry point for your cluster, routing external traffic to the appropriate Services based on rules you define. It provides a single, unified way to manage external access to your applications, simplifying your networking architecture.

To use Ingress, you need an Ingress Controller, which is a specialized component that listens for Ingress resource definitions and configures the underlying network to route traffic accordingly. Popular Ingress Controllers include Nginx Ingress, Traefik, and Istio Gateway.

Hands-on Labs

Lab 1: Set up an Ingress controller in Minikube

  • Install an Ingress Controller:
minikube addons enable ingress

This command enables the Nginx Ingress Controller, a popular choice for Kubernetes.

  • Verify Instalation:
kubectl get pods -n ingress-nginx | grep ingress-nginx

You should see the Ingress Controller Pods running in the kube-system namespace.

Lab 2: Route traffic to the Nginx application using Ingress

  • Create an Ingress Resource: Create a file named nginx-ingress.yaml with the following content:
apiVersion: networking.k8s.io/v1

kind: Ingress
metadata:
    name: nginx-ingress
spec:
  rules:
  - host: nginx.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-service
            port:
              number: 80

This Ingress resource defines a rule that routes traffic for nginx.local to your Nginx Service.

  • Apply the Ingress Resource:
kubectl apply -f nginx-ingress.yaml
  • Test Your Ingress: Add an entry to your /etc/hosts file (or equivalent) to map nginx.local to the Minikube IP address:
<Minikube IP> nginx.local

You can get the Minikube IP address with minikube ip.

Now, try accessing http://nginx.local in your browser. You should see the Nginx welcome page, indicating that your Ingress is working correctly.

Lab 3: Manage traffic for a multi-service deployment with Ingress

  • Deploy a Second Application: Deploy another application, such as a simple “Hello World” app, and expose it with a Service.

  • Create an Ingress Resource for the Second Application: Create another Ingress resource similar to the one for Nginx, but with a different host and backend Service.

  • Apply the New Ingress Resource:

kubectl apply -f <second-app-ingress.yaml>
  • Test Both Applications: You should now be able to access both applications through their respective domains, demonstrating how Ingress can manage traffic for multiple services.

Conclusion

Ingress is a powerful tool for managing external access to your Kubernetes applications. By understanding the basics of Kubernetes networking and how to configure Ingress resources, you can simplify your networking architecture and expose your applications to the world with ease.