75.4 Project 34: Ingress with domain routing
Friends, users type a hostname, not a ClusterIP. Today we install ingress-nginx on kind, create an Ingress for shop.localtest.me, and prove Host-based routing with curl.
मित्रांनो, users hostname टाइप करतात, ClusterIP नाही. आज kind वर ingress-nginx, shop.localtest.me Ingress, curl ने Host routing.
दोस्तों, users hostname टाइप करते हैं, ClusterIP नहीं. आज kind पर ingress-nginx, shop.localtest.me Ingress, curl से Host routing.
What you will build in this project
- ingress-nginx controller on the lab.
- Ingress for shop.localtest.me.
- Path / to Service shop-web.
- curl with Host header returns 200.
1. Start from a real company story
At Wipro in Bengaluru, every demo used kubectl port-forward. A client asked for a real URL and the team froze.
The deal slipped by two weeks. Rough cost: about ₹15 lakh in delayed billing.
Priya added Ingress with a clear hostname so demos looked like production.
Today we add that hostname route on the kind lab.
Ravindra Bagale's Tip
Many students skip Ingress and live on port-forward. That hides DNS and TLS problems until production. Practice Ingress on the laptop. Keep that habit.
खूप students Ingress skip करून port-forward वर जगतात. DNS आणि TLS production पर्यंत लपतात. Laptop वर Ingress practice करा. सवय ठेवा.
बहुत students Ingress skip कर port-forward पर जीते हैं. DNS और TLS production तक छिपते हैं. Laptop पर Ingress practice करें. आदत रखिए.
2. Start and prepare
Step 1: Confirm lab Ready
Why: Cluster must be Ready. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: kubectl get nodes. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 1: ns
Type this:
kubectl get nodes --no-headers | awk '{print $2}'
You will see:
Ready
What it means: Lab cluster still Ready for Ingress work.
Example 2: ctx
Type this:
kubectl config current-context
You will see:
kind-shop-lab
What it means: Still on the kind lab context.
Example 3: why ingress
Type this:
echo 'Ingress routes hostnames to Services; port-forward is only for demos'
You will see:
Ingress routes hostnames to Services; port-forward is only for demos
What it means: Why Ingress exists.
Step 2: Install ingress-nginx tip
Why: Controller runs in ingress-nginx. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: kind ingress example. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 4: install tip
Type this:
echo 'kind: kubectl apply -f https://kind.sigs.k8s.io/examples/ingress/deploy-ingress-nginx.yaml'
You will see:
kind: kubectl apply -f https://kind.sigs.k8s.io/examples/ingress/deploy-ingress-nginx.yaml
What it means: Official kind ingress-nginx example URL (kind.sigs.k8s.io host).
Example 5: wait tip
Type this:
echo 'kubectl wait --namespace ingress-nginx --for=condition=ready pod --selector=app.kubernetes.io/component=controller --timeout=90s'
You will see:
kubectl wait --namespace ingress-nginx --for=condition=ready pod --selector=app.kubernetes.io/component=controller --timeout=90s
What it means: Wait until the controller Pod is Ready.
Example 6: verify
Type this:
echo 'kubectl -n ingress-nginx get pods'
You will see:
kubectl -n ingress-nginx get pods
What it means: List controller Pods after install.
Step 3: Ensure shop-web backend
Why: Backend Deployment and Service. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: kubectl apply deploy/svc. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 7: app
Type this:
kubectl apply -f /tmp/k8s32/deploy.yaml 2>/dev/null || kubectl create deploy shop-web --image=nginx:1.27-alpine --replicas=2
You will see:
deployment.apps/shop-web unchanged or created
What it means: Backend Deployment present.
Example 8: svc
Type this:
kubectl apply -f /tmp/k8s32/svc.yaml 2>/dev/null || kubectl expose deploy shop-web --port=80 --target-port=80 --name=shop-web
You will see:
service/shop-web unchanged or created
What it means: Backend Service present.
Example 9: ready
Type this:
kubectl wait --for=condition=available deploy/shop-web --timeout=90s
You will see:
deployment.apps/shop-web condition met
What it means: Backend Available.
3. Build the main pieces
Step 4: Write Ingress YAML
Why: YAML declares host and path. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: printf Ingress. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 10: ing yaml
Type this:
mkdir -p /tmp/k8s34 && printf '%s\n' 'apiVersion: networking.k8s.io/v1' 'kind: Ingress' 'metadata:' ' name: shop-web' ' annotations:' ' nginx.ingress.kubernetes.io/rewrite-target: /' 'spec:' ' ingressClassName: nginx' ' rules:' ' - host: shop.localtest.me' ' http:' ' paths:' ' - path: /' ' pathType: Prefix' ' backend:' ' service:' ' name: shop-web' ' port: {number: 80}' > /tmp/k8s34/ing.yaml && grep host /tmp/k8s34/ing.yaml
You will see:
host: shop.localtest.me
What it means: Ingress YAML routes shop.localtest.me to shop-web.
Example 11: class
Type this:
grep ingressClassName /tmp/k8s34/ing.yaml
You will see:
ingressClassName: nginx
What it means: Uses the nginx IngressClass.
Example 12: path
Type this:
grep path: /tmp/k8s34/ing.yaml
You will see:
path: /
What it means: Prefix path / for the site root.
Step 5: Apply Ingress
Why: apply registers the rule. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: kubectl apply. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 13: apply
Type this:
kubectl apply -f /tmp/k8s34/ing.yaml
You will see:
ingress.networking.k8s.io/shop-web created
What it means: Ingress object created.
Example 14: get
Type this:
kubectl get ingress shop-web
You will see:
NAME CLASS HOSTS ADDRESS PORTS
shop-web nginx shop.localtest.me ... 80
What it means: Host and class visible.
Example 15: desc
Type this:
kubectl describe ingress shop-web | head -20
You will see:
Name: shop-web
Address: ...
Rules: ...
What it means: Describe shows backend mapping.
Step 6: Hostname tips
Why: localtest.me hits 127.0.0.1. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: hostname notes. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 16: why localtest
Type this:
echo 'localtest.me resolves to 127.0.0.1; useful for laptop Ingress demos'
You will see:
localtest.me resolves to 127.0.0.1; useful for laptop Ingress demos
What it means: Why this hostname works on a laptop.
Example 17: hosts tip
Type this:
echo 'Or add 127.0.0.1 shop.example.com to /etc/hosts on your machine'
You will see:
Or add 127.0.0.1 shop.example.com to /etc/hosts on your machine
What it means: Alternate hosts-file approach.
Example 18: no github
Type this:
echo 'Never paste random Ingress YAML from unknown blogs without reading every annotation'
You will see:
Never paste random Ingress YAML from unknown blogs without reading every annotation
What it means: Safety habit.
4. Automate and verify
Step 7: curl with Host
Why: Host header selects the rule. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: curl -H Host. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 19: curl tip
Type this:
echo 'curl -H "Host: shop.localtest.me" http://127.0.0.1/'
You will see:
curl -H "Host: shop.localtest.me" http://127.0.0.1/
What it means: Host header test when nodePort or kind maps 80.
Example 20: kind ports
Type this:
echo 'kind maps host ports via extraPortMappings in the kind config for 80/443'
You will see:
kind maps host ports via extraPortMappings in the kind config for 80/443
What it means: kind port mapping tip.
Example 21: pf alt
Type this:
kubectl port-forward -n ingress-nginx svc/ingress-nginx-controller 8088:80 >/tmp/k8s34/pf.log 2>&1 & echo $!; sleep 1; curl -s -o /dev/null -w '%{http_code}' -H 'Host: shop.localtest.me' http://127.0.0.1:8088/
You will see:
200
What it means: Port-forward to controller returns 200 with Host header.
Step 8: Add /api path
Why: Second path shares the host. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: second Ingress. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 22: second path
Type this:
printf '%s\n' 'apiVersion: networking.k8s.io/v1' 'kind: Ingress' 'metadata: {name: shop-api}' 'spec:' ' ingressClassName: nginx' ' rules:' ' - host: shop.localtest.me' ' http:' ' paths:' ' - path: /api' ' pathType: Prefix' ' backend:' ' service: {name: shop-web, port: {number: 80}}' > /tmp/k8s34/ing-api.yaml && kubectl apply -f /tmp/k8s34/ing-api.yaml
You will see:
ingress.networking.k8s.io/shop-api created
What it means: Second path /api on the same host.
Example 23: list
Type this:
kubectl get ingress
You will see:
NAME CLASS HOSTS
shop-web nginx shop.localtest.me
shop-api nginx shop.localtest.me
What it means: Both Ingress objects listed.
Example 24: paths
Type this:
kubectl get ingress shop-api -o jsonpath='{.spec.rules[0].http.paths[0].path}'; echo
You will see:
/api
What it means: Path stored as /api.
Step 9: Wrong vs right Host
Why: Wrong host gets 404. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: compare Hosts. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 25: bad host
Type this:
curl -s -o /dev/null -w '%{http_code}' -H 'Host: wrong.example.com' http://127.0.0.1:8088/ || echo '000'
You will see:
404
What it means: Wrong Host header does not hit shop-web.
Example 26: good host
Type this:
curl -s -o /dev/null -w '%{http_code}' -H 'Host: shop.localtest.me' http://127.0.0.1:8088/
You will see:
200
What it means: Correct Host reaches the backend.
Example 27: stop pf
Type this:
pkill -f 'port-forward -n ingress-nginx' 2>/dev/null; echo stopped
You will see:
stopped
What it means: Controller forward stopped.
5. Harden and accept
Step 10: Events and IngressClass
Why: Describe for debugging. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: kubectl get ingressclass. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 28: events
Type this:
kubectl get events --field-selector involvedObject.name=shop-web --sort-by=.lastTimestamp | tail -5
You will see:
... Scheduled ... Pulled ...
What it means: Recent events for debugging.
Example 29: class list
Type this:
kubectl get ingressclass
You will see:
NAME CONTROLLER
nginx k8s.io/ingress-nginx
What it means: IngressClass nginx is registered.
Example 30: anno tip
Type this:
echo 'Keep annotations minimal; every annotation is a footgun if copied blindly'
You will see:
Keep annotations minimal; every annotation is a footgun if copied blindly
What it means: Annotation habit.
Step 11: Cleanup extra Ingress
Why: Keep primary Ingress. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: kubectl delete. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 31: delete api
Type this:
kubectl delete ingress shop-api --ignore-not-found
You will see:
ingress.networking.k8s.io "shop-api" deleted
What it means: Extra Ingress removed.
Example 32: keep web
Type this:
kubectl get ingress shop-web -o jsonpath='{.spec.rules[0].host}'; echo
You will see:
shop.localtest.me
What it means: Primary Ingress kept for HTTPS next.
Example 33: note
Type this:
echo 'Project 35 will add TLS on top of this Ingress'
You will see:
Project 35 will add TLS on top of this Ingress
What it means: Bridge to next project.
Step 12: Acceptance checklist
Why: Priya signs off. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: All green. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 34: svc still
Type this:
kubectl get svc shop-web -o jsonpath='{.spec.type}'; echo
You will see:
ClusterIP
What it means: Backend still ClusterIP behind Ingress.
Example 35: deploy still
Type this:
kubectl get deploy shop-web -o jsonpath='{.status.readyReplicas}'; echo
You will see:
2
What it means: Replicas still Ready.
Example 36: Sign-off
Type this:
echo 'Priya: PASS'
You will see:
Priya: PASS
What it means: Project 34 accepted.
Check yourself
Q1. Who fixed Wipro demos?
Priya
Q2. Ingress class?
nginx
Q3. Demo host?
shop.localtest.me
Q4. Backend Service?
shop-web
Q5. Path?
/
Q6. Tool for Host test?
curl
Friends, you routed a hostname through Ingress to a Service. Did you understand? Let us try it once more. Add a second path /health on the same host.
मित्रांनो, तुम्ही hostname Ingress मधून Service कडे नेला. त्याच host वर /health path टाका.
दोस्तों, आपने hostname Ingress से Service तक पहुंचाया. उसी host पर /health path डालें.
Lab: try at home
- Install ingress-nginx for kind.
- Keep shop-web Service ready.
- Apply Ingress with host rule.
- Prove with Host header curl.
- Clean extra paths.
Recap
- Ingress needs a controller.
- Rules match host and path.
- Backend is a Service name.
- Host header matters in tests.
- TLS comes next with cert-manager.