How to Set Up NGINX Ingress Controller in Kubernetes

Simplify application traffic management in Kubernetes by setting up NGINX Ingress Controller with this detailed guide, ensuring seamless and efficient routing.

NGINX Ingress Controller is a popular solution for managing access to your Kubernetes services. It acts as a reverse proxy and load balancer, directing external traffic to the appropriate services within your cluster. This comprehensive guide will walk you through the process of setting up the NGINX Ingress Controller in a Kubernetes cluster, providing clear steps, commands, and examples to ensure a smooth setup.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Step 1: Prepare Your Kubernetes Cluster
  4. Step 2: Install NGINX Ingress Controller
  5. Step 3: Configure Ingress Resources
  6. Step 4: Verify Ingress Controller Setup
  7. Step 5: Deploy a Sample Application
  8. Step 6: Create Ingress Rules
  9. Step 7: Test the Ingress Controller
  10. Conclusion
  11. References

Introduction

An Ingress Controller manages external access to the services in a Kubernetes cluster, typically HTTP and HTTPS. NGINX Ingress Controller is one of the most widely used and trusted options. This guide provides step-by-step instructions to set up the NGINX Ingress Controller, configure Ingress resources, and verify the setup by deploying a sample application.

Prerequisites

Before you begin, ensure you have the following:

  • A running Kubernetes cluster.
  • kubectl command-line tool configured to interact with your cluster.
  • Basic knowledge of Kubernetes concepts like Pods, Services, and Deployments.
  • Administrative access to your Kubernetes cluster.

Step 1: Prepare Your Kubernetes Cluster

Ensure your Kubernetes cluster is up and running. You can check the status of your nodes with:

bash

Copy code

kubectl get nodes

If your nodes are ready, you can proceed to install the NGINX Ingress Controller.

Step 2: Install NGINX Ingress Controller

The NGINX Ingress Controller can be installed using various methods. Here, we will use the kubectl apply method with the official YAML manifests.

  1. Apply the NGINX Ingress Controller manifest:

    bash

    Copy code

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

  2. Verify the installation:

    bash

    Copy code

    kubectl get pods -n ingress-nginx

    You should see the NGINX Ingress Controller pods running in the ingress-nginx namespace.

Step 3: Configure Ingress Resources

An Ingress resource is a Kubernetes object that manages external access to the services in a cluster. It provides HTTP and HTTPS routing to services based on rules you define.

  1. Create a namespace for your application:

    bash

    Copy code

    kubectl create namespace demo

  2. Create a sample deployment and service:

    bash

    Copy code

    kubectl create deployment web --image=nginx --namespace=demo kubectl expose deployment web --port=80 --target-port=80 --namespace=demo

Step 4: Verify Ingress Controller Setup

Check that the NGINX Ingress Controller is properly set up and functioning:

  1. Get the external IP of the Ingress Controller:

    bash

    Copy code

    kubectl get svc -n ingress-nginx

    Look for the ingress-nginx-controller service and note its external IP address.

Step 5: Deploy a Sample Application

Deploying a sample application helps verify that the Ingress Controller is routing traffic correctly.

  1. Deploy a sample NGINX application:

    bash

    Copy code

    kubectl create deployment nginx --image=nginx --namespace demo kubectl expose deployment nginx --port=80 --target-port=80 --namespace demo

Step 6: Create Ingress Rules

Ingress rules define how traffic should be routed to your services.

  1. Create an Ingress resource:

    yaml

    Copy code

    apiVersion: networking.k8s.io/v1 kind: Ingress metadata:  name: web-ingress  namespace: demo  annotations:    nginx.ingress.kubernetes.io/rewrite-target: / spec:  rules:  - host: your-domain.com    http:      paths:      - path: /        pathType: Prefix        backend:          service:            name: web            port:              number: 80

    Save this file as ingress.yaml and apply it:

    bash

    Copy code

    kubectl apply -f ingress.yaml

Step 7: Test the Ingress Controller

  1. Update your DNS settings:

    Point your domain (e.g., your-domain.com) to the external IP address of the NGINX Ingress Controller.

  2. Access the application:

    Open a web browser and navigate to http://your-domain.com. You should see the NGINX welcome page.

Conclusion

Setting up the NGINX Ingress Controller in a Kubernetes cluster simplifies the management of external traffic, providing a robust and scalable solution for routing HTTP and HTTPS traffic to your services. This guide has provided step-by-step instructions to install, configure, and verify the NGINX Ingress Controller, helping you streamline your Kubernetes environment.

Compelling Summary: Simplify application traffic management in Kubernetes by setting up NGINX Ingress Controller with this detailed guide, ensuring seamless and efficient routing.

References

By following these steps, you will have a fully functional NGINX Ingress Controller managing your Kubernetes cluster's traffic, enabling efficient and scalable routing for your applications.