https://drive.google.com/file/d/15f1qmvb0fFdgfy36AyHULdF8q7y14OP1/view?usp=sharing
# deployment-webserver-with-service.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nn-web
namespace: learning
spec:
selector:
matchLabels:
run: nn-web # โ Must match Pod template labels
replicas: 2
template:
metadata:
labels:
run: nn-web # โ Labels for Pods
spec:
containers:
- name: nn-webserver
image: lovelearnlinux/webserver:v1
ports:
- containerPort: 80 # โ Good practice (though optional)
๐ Key Insight:
This YAML only defines the Deployment.
The Service is created separately via:
kubectl expose deployment/nn-web --namespace=learning
| Component | Role |
|---|---|
| Deployment | Manages replicated Pods with label run=nn-web |
| Service | Provides a stable IP/DNS name to access those Pods |
| Selector | run=nn-web โ links Service to Pods |
| Endpoints | Auto-generated list of Pod IPs that match the selector |
๐ฏ Flow:
curl <http://nn-web.learning.svc.cluster.local>โ Service (ClusterIP)
โ Random Pod with
run=nn-weblabel
# Create namespace
kubectl create namespace learning
# Apply Deployment
kubectl apply -f deployment-webserver-with-service.yml
# Verify Pods
kubectl get pods -n learning -l run=nn-web -o wide
# Create ClusterIP Service (default)
kubectl expose deployment/nn-web --port=80 --target-port=80 -n learning
# Verify Service
kubectl get svc nn-web -n learning
# NAME TYPE CLUSTER-IP PORT(S)
# nn-web ClusterIP 10.43.123.45 80/TCP
# Check Endpoints (Pod IPs)
kubectl get endpoints nn-web -n learning
# NAME ENDPOINTS AGE
# nn-web 10.42.0.10:80,10.42.1.15:80 10s
๐ --port=80 = Service port
-target-port=80= Pod port (matchescontainerPort)
๐ก In k3s, you can curl from any node or from a debug Pod.
# Start a debug Pod in the same namespace
kubectl run debug -n learning --image=curlimages/curl -it --rm -- sh
# Inside the shell:
curl <http://nn-web>
# โ
Should return "Welcome to Network Nuts!"
# Exit
exit