Hands-on Implementation
Prerequisites
- Minikube cluster running
- kubectl configured
- Existing deployment and service (from Day 37)
Step 1: Verify Existing Resources
# Check existing pods
kubectl get pods
# Check existing services
kubectl get service
You should see the deployment and NodePort service from the previous lesson.
# Get Minikube IP
minikube ip
# Example output: 192.168.64.11
# Test the service
curl http://192.168.64.11:<NODE_PORT>
# Expected: "Learn devops with some strong foundational knowledge"
Step 2: Create Ingress Resource
Create a file named ingress.yaml:
“`yaml name=ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-example
spec:
rules:
- host: foo.bar.com http: paths:
- path: /bar
pathType: Prefix
backend:
service:
name:
port:
number: 80
- path: /bar
**Replace `<YOUR_SERVICE_NAME>`** with your actual service name from `kubectl get svc`.
Apply the Ingress:
bash
kubectl apply -f ingress.yaml
Check the Ingress:
bash
kubectl get ingress
**Note:** The ADDRESS field will be empty at this point because we haven't installed an Ingress Controller yet.
### Step 3: Install NGINX Ingress Controller
#### For Minikube
Minikube provides a simple addon for NGINX Ingress Controller:
bash
minikube addons enable ingress
#### For Production Clusters (EKS, GKE, AKS, etc.)
For production environments, follow the official documentation:
1. Visit: https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/
2. Choose your preferred Ingress Controller (e.g., NGINX)
3. Follow installation instructions (usually via Helm)
**Example for NGINX Ingress Controller:**
bash
Add the NGINX Helm repository
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
Install NGINX Ingress Controller
helm install nginx-ingress ingress-nginx/ingress-nginx
### Step 4: Verify Ingress Controller Installation
bash
Check all pods across all namespaces
kubectl get pods -A | grep nginx
You should see something like:
ingress-nginx ingress-nginx-controller-xxxx 1/1 Running
Check the Ingress Controller logs:
bash
kubectl logs -n ingress-nginx
You should see log entries indicating it has discovered your Ingress resource:
Successfully synced ‘default/ingress-example’
### Step 5: Verify Ingress Address
bash
kubectl get ingress
Now the ADDRESS field should be populated with an IP address (e.g., `192.168.64.11`).
### Step 6: Configure Local DNS (Minikube Only)
For local development, you need to map the domain name to the Ingress IP address.
**On Linux/Mac:**
bash
sudo nano /etc/hosts
Add this line:
192.168.64.11 foo.bar.com
**On Windows:**
Edit `C:\Windows\System32\drivers\etc\hosts` as Administrator and add:
192.168.64.11 foo.bar.com
> **Note:** In production, you would use a real domain name registered with a DNS provider (e.g., GoDaddy) and configure DNS records properly. This step is only needed for local testing.
### Step 7: Test the Ingress
bash
Test with curl
curl http://foo.bar.com/bar
Expected output: “Learn devops with some strong foundational knowledge”
**Alternative method without modifying /etc/hosts:**
bash
curl –resolve foo.bar.com:80:192.168.64.11 http://foo.bar.com/bar
---
## Advanced Ingress Configurations
### Path-Based Routing
Route different URL paths to different services:
yaml name=ingress-path-based.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: path-based-ingress
spec:
rules:
- host: example.com http: paths:
- path: /app1
pathType: Prefix
backend:
service:
name: service-app1
port:
number: 80 - path: /app2
pathType: Prefix
backend:
service:
name: service-app2
port:
number: 80
- path: /app1
### Host-Based Routing
Route different domains to different services:
yaml name=ingress-host-based.yaml
apiVersion: networking. k8s.io/v1
kind: Ingress
metadata:
name: host-based-ingress
spec:
rules:
- host: app1.example.com http: paths:
- path: /
pathType: Prefix
backend:
service:
name: service-app1
port:
number: 80
- path: /
- host: app2.example. com http: paths:
- path: /
pathType: Prefix
backend:
service:
name: service-app2
port:
number: 80
- path: /
### TLS/SSL Termination (HTTPS)
Secure your Ingress with TLS certificates:
yaml name=ingress-tls.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: tls-ingress
spec:
tls:
- hosts:
- secure.example.com
secretName: tls-secret
rules:
- secure.example.com
- host: secure. example.com http: paths:
- path: /
pathType: Prefix
backend:
service:
name: secure-service
port:
number: 443
- path: /
**Create TLS Secret:**
bash
kubectl create secret tls tls-secret \
–cert=path/to/cert.crt \
–key=path/to/cert.key
“`
Leave a Reply