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

Infodiscover

Task A. Scale web to 5 replicas by editing web-deploy.yaml, not with kubectl scale.

sed -i 's/replicas: 3/replicas: 5/' web-deploy.yaml
kubectl apply -f web-deploy.yaml
kubectl get deploy web -o wide

Look at the image: back to 1.27. Step 8 changed the live object, not the file - apply rolled it back (drift, Lab 04).

Task B. Delete the Deployment but keep its ReplicaSets and Pods running. What owns the Pods afterwards?

kubectl delete deploy web --cascade=orphan
kubectl get rs,pods -l app=web
kubectl get rs -l app=web -o jsonpath='{.items[*].metadata.ownerReferences}{"\n"}'

The ReplicaSets have no owner now; the Pods are still owned by their ReplicaSet.

Task C. Re-apply web-deploy.yaml. Does the Deployment create new Pods or adopt the orphans?

kubectl apply -f web-deploy.yaml
kubectl get rs -l app=web

It adopts the ReplicaSet whose template matches - no new Pods for that revision.

Clean up

kubectl delete -f web-deploy.yaml
kubectl delete rs -l app=web --ignore-not-found
kubectl delete pod stray --ignore-not-found