How to Configure NGINX Ingress Controller in Kubernetes
How to Configure NGINX Ingress Controller in Kubernetes
The NGINX Ingress Controller is a robust tool for managing external access to services in a Kubernetes cluster. It provides load balancing, SSL termination, and name-based virtual hosting. This tutorial guides you through the configuration steps necessary to set up NGINX Ingress Controller in Kubernetes.
Prerequisites
- A running Kubernetes cluster (version 1.14 or newer)
- kubectl configured to communicate with your cluster
- Basic understanding of Kubernetes Networking
Step 1: Install an Ingress Controller
Kubernetes does not automatically create Ingress controllers; they must be manually defined. For NGINX, this typically means installing it as a Deployment:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
This command deploys the NGINX Ingress Controller to your cluster. It may take a few minutes to be fully available.
Step 2: Confirm Installation
To ensure that the NGINX Ingress Controller was successfully deployed, run:
kubectl get pods -n ingress-nginx
Look for a pod with the name nginx-ingress-controller in the Running state.
Step 3: Configure Basic Authentication
To add basic authentication, you can create a Kubernetes secret that contains the usernames and passwords:
htpasswd -c auth admin
Encode the file and create a secret:
kubectl create secret generic basic-auth --from-file=auth
Step 4: Create an Ingress Resource
Define the routes for your application using Ingress resources. Below is an example of a simple Ingress resource:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: "Authentication Required - example.com"
spec:
rules:
- host: www.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: example-service
port:
number: 80
Apply the resource to your cluster:
kubectl apply -f example-ingress.yaml
Troubleshooting
If you encounter issues accessing the service, check the logs of the NGINX Ingress controller:
kubectl logs <nginx-ingress-pod> -n ingress-nginx
Make sure your DNS service for the desired domain points to the IP address of your load balancer.
Summary
- Installed NGINX Ingress Controller in your Kubernetes cluster
- Configured basic authentication
- Created a simple Ingress resource
- Troubleshooting steps for common issues
Explore further by implementing additional NGINX annotations and features to tailor the setup to your specific needs. If you’re working with other ingress solutions, such as HAProxy Ingress Controller, you might want to compare their functionalities.
