CKAD field manual

k8s deployment.yaml example

A Deployment owns a ReplicaSet. Scale and roll out through the Deployment, not the Pods.

An officer watches an identical squad hold position around a docked shuttle. An officer watches an identical squad hold position around a docked shuttle.
Create
kubectl create deployment web --image=nginx:1.27 --replicas=3
YAML then apply
kubectl create deployment web --image=nginx:1.27 --replicas=3 -o yaml --dry-run=client > deployment.yaml
# edit deployment.yaml
kubectl apply -f deployment.yaml
YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.27
          ports:
            - containerPort: 80
Rollout
kubectl scale deploy/web --replicas=5
kubectl set image deploy/web nginx=nginx:1.28
kubectl rollout status deploy/web
kubectl rollout history deploy/web
kubectl rollout undo deploy/web
kubectl get deploy,rs,po -l app=web

Fields

selector
Must match template.metadata.labels. Immutable after create.
template
The Pod. Labels a Service should match go here, not on the Deployment.
replicas
Desired Pod count. Scale the Deployment, not the Pods.
containers.name
kubectl create deployment web --image=nginx names this nginx, not web.
kubectl create deployment
Short name: deploy. Container name is the image name, not the Deployment name.
kubectl set image
Starts a rollout. Then watch with rollout status.
kubectl rollout undo
Back one revision. Do not delete Pods to roll back.

Watch

  • selector.matchLabels and template.metadata.labels must match. The selector is immutable after create.
  • kubectl create deployment web --image=nginx names the container nginx, not web.
  • Do not delete Pods to roll back. kubectl rollout undo deploy/<name>.
  • The usual filename is deployment.yaml. deploy.yml is the same file.

Official docs Deployments

Practice these objects on a live cluster →