To expose your Nginx application using the Nginx Ingress Controller, follow these steps:
1. Install Nginx Ingress Controller
The Nginx Ingress Controller is required to manage incoming traffic. If it's not already installed, deploy it:
For Bare Metal or Minikube
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
For Cloud Providers (EKS, AKS, GKE)
If using a cloud provider, use Helm:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install nginx-ingress ingress-nginx/ingress-nginx --set controller.publishService.enabled=true
Check if the ingress controller is running:
kubectl get pods -n ingress-nginx
kubectl get svc -n ingress-nginx
The LoadBalancer or NodePort IP from the output is the external IP for accessing your application.
2. Deploy the Nginx Application
Ensure your Nginx deployment exists, or create one:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Apply the deployment:
kubectl apply -f nginx-deployment.yaml
3. Create a Service for Nginx
Expose the deployment using a ClusterIP service:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
Apply the service:
kubectl apply -f nginx-service.yaml
4. Create an Ingress Resource
Define an Ingress to route traffic to the Nginx service:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: nginx.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
Apply the Ingress:
kubectl apply -f nginx-ingress.yaml
5. Test the Ingress Route
If Using Minikube
Run:
minikube addons enable ingress
minikube tunnel
Then, add the following entry to your /etc/hosts
file (Linux/macOS) or C:\Windows\System32\drivers\etc\hosts
(Windows):
127.0.0.1 nginx.local
If Using a Cloud Provider
-
Get the EXTERNAL IP of the Ingress Controller:
kubectl get svc -n ingress-nginx
-
Add the IP to your
/etc/hosts
:<EXTERNAL_IP> nginx.local
Access Nginx
Now, open your browser or run:
curl http://nginx.local
You should see the Nginx welcome page. 🎉