Kubernetes Pods, Deployments, and Services in YAML
You will define a Deployment and a Service in YAML, apply them, then perform a rolling update and rollback. By the end you will manage workloads declaratively, the way real clusters are operated under GitOps.
Learning Objectives
- Write Deployment and Service manifests.
- Perform a zero-downtime rolling update.
- Roll back a bad release in one command.
- Time: ~3 hours · Difficulty: Intermediate · Prereqs: a running cluster from the basics lab.
Architecture Overview
graph TD
Dep[Deployment] --> RS[ReplicaSet]
RS --> P1[Pod]
RS --> P2[Pod]
Svc[Service ClusterIP] --> P1
Svc --> P2
Environment Setup
You will need: a running cluster and kubectl context set.
Before you begin: keep manifests in a directory so you can version them in Git.
Step-by-Step Execution
01
Write the Deployment + Service
# web.yaml
apiVersion: apps/v1
kind: Deployment
metadata: { name: web }
spec:
replicas: 3
selector: { matchLabels: { app: web } }
template:
metadata: { labels: { app: web } }
spec:
containers:
- name: web
image: nginx:1.25
---
apiVersion: v1
kind: Service
metadata: { name: web }
spec:
selector: { app: web }
ports: [{ port: 80 }]
02
Apply it
kubectl apply -f web.yaml && kubectl rollout status deploy/web03
Roll out a new image version
$ kubectl set image deploy/web web=nginx:1.27 && kubectl rollout status deploy/web
deployment "web" successfully rolled out
04
Roll back if needed
kubectl rollout undo deploy/webProgress So Far
graph LR
A[01 Manifest] -->|done| B[02 Apply]
B -->|done| C[03 Rolling update]
C -->|done| D[04 Rollback]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
style D fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
kubectl rollout history deploy/web && kubectl get svc webYou should see multiple revisions and a Service with a ClusterIP. If so, you can deploy, update, and undo declaratively.
Troubleshooting
- Rollout stuck: a new pod is crashing; inspect with
kubectl describeandlogs. - Service has no endpoints: the selector labels must match the pod labels exactly.
- Image not updating: use a specific tag, not
latest, so changes are detected.
Extension Ideas
- Expose externally and add ingress in Kubernetes Networking.
- Add readiness/liveness probes and resource limits.
- Manage manifests with Kustomize or Helm under GitOps.
Key Results
- Deployed a 3-replica workload from declarative YAML.
- Performed a zero-downtime rolling update.
- Rolled back a release in a single command.
- Exposed pods through a stable Service ClusterIP.