Lab 08 - ReplicaSets and Deployments
Lab 08 - ReplicaSets and Deployments
Goal: watch the reconciliation loop, see the selector do the binding, and run your first Deployment. Chapter: ReplicaSets and Deployments
ReplicaSet
# 1 - a ReplicaSet by hand (the only time you will write one)
cd ~
cat > web-rs.yaml <<'YAML'
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-rs
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
YAML
kubectl apply -f web-rs.yaml
kubectl get rs,pods -l app=web -o wide
# 2 - self-healing
kubectl delete pod "$(kubectl get pods -l app=web -o name | head -1)"
kubectl get pods -l app=web
The same loop that created the Pods counted two, wanted three, created one.
# 3 - scaling is the same loop with a different number
kubectl scale rs web-rs --replicas=5
kubectl scale rs web-rs --replicas=2
kubectl get pods -l app=web
# 4 - the selector binds Pods, not the name
kubectl run stray --image nginx:1.27 --labels app=web
kubectl get pods -l app=web
stray was adopted - the ReplicaSet counted three and deleted one. An
accidental label match is an accidental takeover.
# 5 - a changed template does NOT touch running Pods
sed -i 's/nginx:1.27/nginx:1.28/' web-rs.yaml
kubectl apply -f web-rs.yaml
kubectl get pods -l app=web -o jsonpath='{.items[*].spec.containers[0].image}{"\n"}'
kubectl delete -f web-rs.yaml
Still 1.27 - no rollout logic. That gap is what a Deployment fills.
Deployment
# 6 - generate, apply, look at the three layers
kubectl create deploy web --image nginx:1.27 --replicas 3 \
--dry-run=client -o yaml > web-deploy.yaml
kubectl apply -f web-deploy.yaml
kubectl get deploy,rs,pods -l app=web
# 7 - follow the ownership chain Pod -> ReplicaSet -> Deployment
POD=$(kubectl get pods -l app=web -o name | head -1)
kubectl get $POD -o jsonpath='{.metadata.ownerReferences[0].kind}/{.metadata.ownerReferences[0].name}{"\n"}'
RS=$(kubectl get rs -l app=web -o name | head -1)
kubectl get $RS -o jsonpath='{.metadata.ownerReferences[0].kind}/{.metadata.ownerReferences[0].name}{"\n"}'
# 8 - change the image: a new ReplicaSet appears
kubectl set image deploy/web nginx=nginx:1.28
kubectl rollout status deploy/web
kubectl get rs -l app=web
Old ReplicaSet at 0, new at 3. Strategies, history and rollback: Day 3.
Discovery
InfodiscoverTask A. Scale web to 5 replicas by editing web-deploy.yaml, not with
kubectl scale.
Task B. Delete the Deployment but keep its ReplicaSets and Pods running. What owns the Pods afterwards?
Task C. Re-apply web-deploy.yaml. Does the Deployment create new Pods or
adopt the orphans?
Clean up
kubectl delete -f web-deploy.yaml
kubectl delete rs -l app=web --ignore-not-found
kubectl delete pod stray --ignore-not-found