Kubernetes Ingress Implementation Guide
This guide explains how to deploy an application using Kubernetes Deployment, Service, and NGINX Ingress.
1. Create Deployment
deployment.yaml
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
spec:
replicas: 2
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Apply:
kubectl apply -f deployment.yaml
Verify pods:
kubectl get pods
2. Create Service
service.yaml
yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: nginx-app
ports:
- port: 8080
targetPort: 80
type: LoadBalancer
Apply:
kubectl apply -f service.yaml
Check:
kubectl get svc
3. Install NGINX Ingress Controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
Wait for controller pods:
kubectl get pods -n ingress-nginx
4. Confirm IngressClass
kubectl get ingressclass
Expected output:
nginx
5. Create Ingress
ingress.yaml
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-app
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: nginx.cloudmateria.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-svc
port:
number: 8080
Apply:
kubectl apply -f ingress.yaml
Check:
kubectl get ingress
6. Map Domain Locally (Windows)
Edit:
C:\Windows\System32\drivers\etc\hosts
Add:
127.0.0.1 nginx.cloudmateria.com
7. Test Ingress
Using curl:
curl -H "Host: nginx.cloudmateria.com" http://localhost
Or open in browser:
http://nginx.cloudmateria.com
You should see the NGINX welcome page.
8. Clean Up / Destroy Resources
To remove all resources created for this setup:
bash
kubectl delete ingress nginx-app
kubectl delete svc nginx-svc
kubectl delete deployment nginx-app
kubectl delete pods -n ingress-nginx # optional, to clean controller pods if desired
kubectl delete namespace ingress-nginx # optional, if you want to remove the ingress controller completely
Verify deletion:
kubectl get pods,svc,ingress
All should be removed.