Skip to content

Mastering Kubernetes Ingress - A Step-by-Step Guide

Published: at 06:30 AM

Mastering Kubernetes Ingress: A Step-by-Step Guide

Kubernetes Ingress is a powerful resource that manages external access to services within a cluster. In this guide, we’ll walk through the process of deploying a simple application, creating a service for it, and exposing it to the external world using Ingress. Let’s dive in!

Prerequisites

Before we begin, ensure you have:

  1. A running Kubernetes cluster
  2. kubectl configured to communicate with your cluster
  3. Docker installed on your local machine
  4. An Ingress Controller installed in your cluster (e.g., NGINX Ingress Controller)

Step 1: Building and Pushing the Docker Image

First, let’s build our application image:

docker build -t hello-world .

Tag and push the image to Docker Hub:

docker tag hello-world <dockerhub-username>/hello-world
docker push <dockerhub-username>/hello-world

Replace <dockerhub-username> with your Docker Hub username.

Step 2: Creating the Deployment

Create a file named deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: <dockerhub-username>/hello-world:latest
          ports:
            - containerPort: 3000

Apply the deployment:

kubectl apply -f deployment.yaml

Step 3: Creating the Service

Create a file named service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000

Apply the service:

kubectl apply -f service.yaml

Step 4: Creating the Ingress

Create a file named ingress.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-world-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: hello-world.exposed
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 80

Apply the Ingress:

kubectl apply -f ingress.yaml

Step 5: Verifying the Setup

  1. List the Ingress object:

    kubectl get ingress
  2. Add an entry to your /etc/hosts file:

    <ingress-controller-ip> hello-world.exposed

    Replace <ingress-controller-ip> with the external IP of your Ingress controller.

  3. Test the application:

    curl http://hello-world.exposed

    You should see the “Hello World” message.

Key Takeaways

  1. Ingress Controller: Ensure an Ingress Controller is running in your cluster. It’s crucial for Ingress rules to take effect.

  2. Host-based Routing: Ingress allows you to route traffic based on hostnames, enabling multiple services to share a single IP address.

  3. Path-based Routing: You can route to different services based on URL paths, allowing for flexible application architectures.

  4. Annotations: Ingress annotations provide a way to customize the behavior of your Ingress controller.

  5. SSL/TLS: While not covered in this basic example, Ingress can be configured to manage SSL/TLS termination for secure communication.

Conclusion

Kubernetes Ingress provides a powerful way to expose your services to the outside world. By following this guide, you’ve learned how to deploy an application, create a service, and expose it using Ingress. As you continue your Kubernetes journey, explore more advanced Ingress features like SSL/TLS termination, authentication, and rate limiting to build robust, production-ready applications.

Remember, Ingress is just one piece of the Kubernetes networking puzzle. Combine it with other resources like Services, NetworkPolicies, and external load balancers to create comprehensive networking solutions for your applications.