Lab 8.1 - NGINX Ingress Configuration
Lab Objectives
By the end of this lab, you will be able to:
- Install an NGINX Ingress Controller.
- Understand the role of an Ingress Controller.
- Create a basic Ingress resource.
- Expose an application via Ingress.
- Test access to the application via Ingress.
Estimated Duration
45-60 minutes
Prerequisites
- kubectl installed and configured.
- Functional local Kubernetes cluster (minikube or kind).
- Knowledge of Ingress (Chapter 8.1 and 8.2).
Part 1: Installing the NGINX Ingress Controller
Step 1.1: Enable Ingress on Minikube
If you are using minikube, enable the Ingress addon:
minikube addons enable ingress
Verify that the Ingress Controller is installed:
kubectl get pods -n ingress-nginx
You should see Pods from the NGINX Ingress Controller.
Step 1.2: Manual Installation (Alternative)
If you are not using minikube or for a manual installation, use:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.1/deploy/static/provider/cloud/deploy.yaml
Wait for the Pods to be ready:
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=90s
Part 2: Deploying a Test Application
Before configuring Ingress, let's deploy a simple application.
Step 2.1: Create a Deployment
Create a file app-deployment.yaml:
# app-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 2
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80
Apply the Deployment:
kubectl apply -f app-deployment.yaml
Step 2.2: Create a Service
Create a file app-service.yaml:
# app-service.yaml
apiVersion: v1
kind: Service
metadata:
name: web-app-service
spec:
selector:
app: web-app
ports:
- port: 80
targetPort: 80
type: ClusterIP
Apply the Service:
kubectl apply -f app-service.yaml
Verify that the Service is created:
kubectl get svc web-app-service
Part 3: Creating an Ingress Resource
Step 3.1: Create the Ingress
Create a file ingress-basic.yaml:
# ingress-basic.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-app-ingress
spec:
ingressClassName: nginx # Specifies the Ingress Controller to use
rules:
- host: webapp.local # Hostname (for local, use this name or modify /etc/hosts)
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-app-service
port:
number: 80
Explanation:
ingressClassName: nginx: Indicates to use the NGINX Ingress Controller.host: webapp.local: The hostname to access the application.path: /: All paths starting with/.backend: The backend Service that handles the traffic.
Apply the Ingress:
kubectl apply -f ingress-basic.yaml
Step 3.2: Verify the Ingress
Verify that the Ingress is created:
kubectl get ingress
Check the details:
kubectl describe ingress web-app-ingress
You should see the IP address of the Ingress Controller (on minikube, this may take a few seconds).
Part 4: Accessing the Application
Step 4.1: Get the Ingress IP Address
On minikube, get the IP:
minikube ip
Or check the Ingress:
kubectl get ingress web-app-ingress
Step 4.2: Configure the Hostname (Local)
For a local cluster, you need to add the entry to /etc/hosts (Linux/Mac) or C:\Windows\System32\drivers\etc\hosts (Windows):
# On Linux/Mac, edit /etc/hosts
sudo nano /etc/hosts
# Add this line (replace <INGRESS_IP> with the Ingress IP):
<INGRESS_IP> webapp.local
On Windows, edit C:\Windows\System32\drivers\etc\hosts with administrator permissions.
Step 4.3: Test Access
Test access to the application:
curl http://webapp.local
Or open http://webapp.local in your browser.
You should see the default NGINX page.
Step 4.4: Alternative with Port-Forward (Without Modifying /etc/hosts)
If you do not want to modify /etc/hosts, use port-forward to the Ingress Controller:
# Find the name of the Ingress Controller Pod
INGRESS_POD=$(kubectl get pods -n ingress-nginx -l app.kubernetes.io/component=controller -o jsonpath='{.items[0].metadata.name}')
# Port-forward
kubectl port-forward -n ingress-nginx $INGRESS_POD 8080:80
Then test:
curl -H "Host: webapp.local" http://localhost:8080
Part 5: Checking the Logs
Check the Ingress Controller logs to see the requests:
kubectl logs -n ingress-nginx -l app.kubernetes.io/component=controller --tail=20
You should see the incoming HTTP requests.
Part 6: Cleanup
Delete the created resources:
kubectl delete ingress web-app-ingress
kubectl delete -f app-service.yaml
kubectl delete -f app-deployment.yaml
To disable Ingress on minikube:
minikube addons disable ingress
Lab Summary
In this lab, you installed and configured an NGINX Ingress Controller, created a basic Ingress resource, and exposed an application via Ingress. You learned how to access the application via the configured hostname.
Next Steps
The next lab will show you how to configure path-based routing to direct traffic to different applications.
Lab 8.2: Path-based Routing
Lab created: December 2024