Kubernetes Step-by-Step Guide to Setting Up a Kubernetes Ingress Controller

As organizations delve deeper into containerized applications, managing traffic becomes a crucial aspect of a Kubernetes environment. This is where Kubernetes Ingress Controllers come into play. In this guide, we will walk you through the steps to set up a Kubernetes Ingress Controller, ensuring your applications are accessible to external users while optimizing traffic flow.

What is Kubernetes Ingress?

Kubernetes Ingress is an API object that manages external access to services within a Kubernetes cluster. It provides HTTP and HTTPS routing to services based on defined rules. An Ingress Controller is responsible for fulfilling the Ingress, handling incoming requests and routing them to the appropriate services.

Why Use an Ingress Controller?

  1. Traffic Management: Route traffic to multiple services using a single external IP.
  2. SSL Termination: Handle SSL certificates efficiently.
  3. Simplified Access: Access services using URLs rather than IP addresses.
  4. Load Balancing: Distribute traffic amongst multiple backend services.
  5. Authentication & Authorization: Enhance security via middleware approaches.

Prerequisites

Before we start, ensure you have:

  • A running Kubernetes cluster (Minikube, GKE, EKS, etc.)
  • kubectl installed and configured
  • Basic knowledge of Kubernetes resources

Step 1: Deploy a Sample Application

We need a backend application to route traffic. Here, we will deploy a simple Nginx application as an example.

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-app
spec:
replicas: 2
selector:
matchLabels:
app: sample-app
template:
metadata:
labels:
app: sample-app
spec:
containers:

  • name: sample-app
    image: nginx
    ports:

    • containerPort: 80

      apiVersion: v1
      kind: Service
      metadata:
      name: sample-app
      spec:
      type: ClusterIP
      ports:

  • port: 80
    targetPort: 80
    selector:
    app: sample-app

Apply the configuration:

bash
kubectl apply -f sample-app.yaml

Step 2: Choose and Install an Ingress Controller

For demonstration, we’ll use the NGINX Ingress Controller, a popular choice. To install it, you can either use Helm or deploy it directly with Kubernetes YAML files.

Using Helm:

Make sure you have the Helm CLI installed. Then run the following commands to install the NGINX Ingress Controller:

bash
kubectl create namespace ingress-nginx
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install my-nginx ingress-nginx/ingress-nginx –namespace ingress-nginx

Using YAML:

You can also directly apply the official manifest:

bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml

Step 3: Configure Ingress Resource

Now, let’s create an Ingress resource that will route traffic to our sample-app service.

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: sample-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:

  • host: sample-app.local
    http:
    paths:

    • path: /
      pathType: Prefix
      backend:
      service:
      name: sample-app
      port:
      number: 80

Apply the Ingress resource:

bash
kubectl apply -f sample-app-ingress.yaml

Step 4: Update Local Host File

In order to access your application using the specified host (sample-app.local), you need to map it to your Kubernetes cluster IP. If you’re using Minikube, retrieve its IP:

bash
minikube ip

Then, update your local /etc/hosts file:

sample-app.local

Step 5: Test Your Ingress Configuration

With everything set up, you should test the accessibility of your application. Open a web browser and navigate to:

http://sample-app.local

You should see the Nginx welcome page.

Step 6: Manage Ingress Controller

To check the status and logs of the NGINX Ingress Controller, you can run:

bash
kubectl get pods -n ingress-nginx
kubectl logs -n ingress-nginx

Conclusion

Setting up a Kubernetes Ingress Controller simplifies the management of external access to your applications, providing a robust and flexible solution for traffic routing. By following this step-by-step guide, you can efficiently expose your Kubernetes services to the outside world.

Next Steps

  1. Explore Annotations: Investigate various annotations available for customizing the NGINX Ingress Controller.
  2. SSL Certificates: Consider setting up TLS to secure your applications.
  3. Advanced Routing: Explore advanced routing capabilities and features, such as URL rewrites and rate limiting.

By navigating the potential of Kubernetes Ingress, you can optimize your cloud-native application deployments, enhancing both user experience and operational efficiency. Happy deploying!