Kubernetes Tutorial · Workloads · Lesson 7 of 26

Deployments

Run stateless applications with a desired number of replicas that Kubernetes keeps alive.

Create a free account to save this page, take notes and track your progress. Sign in

A Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels: {app: web}
  template:                       # the pod template
    metadata:
      labels: {app: web}
    spec:
      containers:
        - name: web
          image: nginx:1.29-alpine
          ports: [{containerPort: 80}]

The Deployment creates a ReplicaSet, which keeps the requested number of pods running. Delete a pod and a replacement appears within seconds; change the template and the Deployment rolls out a new ReplicaSet.

Self-healing replicas
kubectl create namespace demo-deploy > /dev/null
trap 'kubectl delete namespace demo-deploy --wait=false > /dev/null' EXIT
kubectl -n demo-deploy create deployment web --image=nginx:1.29-alpine --replicas=3 > /dev/null
kubectl -n demo-deploy rollout status deployment/web --timeout=120s > /dev/null
echo "running pods: $(kubectl -n demo-deploy get pods -l app=web --field-selector=status.phase=Running -o name | wc -l)"
victim=$(kubectl -n demo-deploy get pods -l app=web -o name | head -1)
kubectl -n demo-deploy delete "$victim" --wait=true > /dev/null
kubectl -n demo-deploy rollout status deployment/web --timeout=120s > /dev/null
echo "after deleting one pod: $(kubectl -n demo-deploy get pods -l app=web --field-selector=status.phase=Running -o name | wc -l) running (a replacement was created)"
echo "owned by ReplicaSet: $(kubectl -n demo-deploy get rs -l app=web -o name | wc -l)"

Output

running pods: 3
after deleting one pod: 3 running (a replacement was created)
owned by ReplicaSet: 1

Run this on your computer — see “Get Started” for setup.

Test yourself

You delete one pod of a 3-replica Deployment. What happens?

  1. The Deployment is deleted
  2. The ReplicaSet creates a replacement to get back to 3
  3. Nothing until you reapply
  4. All pods restart
Show answer

Answer: The ReplicaSet creates a replacement to get back to 3

Controllers reconcile actual state to desired state.

Was this page helpful?