https://drive.google.com/file/d/1zDHaAE6VSwacb2Z4Bj1tgvMY2ZysmrDO/view?usp=sharing
apiVersion: v1
kind: Service
metadata:
name: nnappone-service
spec:
selector:
app: nnappone # โ Must match Pod labels
ports:
- protocol: TCP
port: 8080 # โ Service port (what clients connect to)
targetPort: 80 # โ Pod port (where app listens)
๐ Key Insight:
This Service creates a stable virtual IP (
ClusterIP) that load-balances traffic to all Pods with labelapp: nnappone.
| Component | Role |
|---|---|
selector |
Links Service to Pods (app: nnappone) |
port: 8080 |
Port exposed by the Service |
targetPort: 80 |
Port exposed by the Pod (matches containerPort) |
ClusterIP |
Virtual IP (e.g., 10.43.x.x) โ only reachable inside the cluster |
๐ฏ Flow:
curl http://nnappone-service:8080โ Service (
10.43.x.x:8080)โ Random Pod with
app: nnapponeโ:80
๐ก DNS:
From any Pod in the same namespace:
curl <http://nnappone-service> # Port 8080 by defaultFrom other namespaces:
curl <http://nnappone-service.default.svc.cluster.local>
๐ก You need a Pod with app: nnappone label (from earlier chapters):
# pod-for-service.yaml
apiVersion: v1
kind: Pod
meta
name: nnappone
labels:
app: nnappone # โ Must match Service selector
spec:
containers:
- name: web
image: nginx
ports:
- containerPort: 80
# Apply Pod
kubectl apply -f pod-for-service.yaml
# Apply Service
kubectl apply -f service-for-pod.yml
# Check if Service found the Pod
kubectl get endpoints nnappone-service
# โ
Expected:
# NAME ENDPOINTS AGE
# nnappone-service 10.42.0.10:80 10s
# โ If ENDPOINTS = <none> โ label mismatch!
# Start debug Pod
kubectl run debug --image=curlimages/curl -it --rm -- sh
# Inside shell:
curl <http://nnappone-service:8080>
# โ
Should return "Welcome to Network Nuts!"
# Exit
exit