CKAD — Creating Deployments

2 minute read

Why Deployments Matter

A Deployment is the most common way to run applications in Kubernetes. It manages a set of identical Pods — ensuring the desired number are always running, handling updates, and enabling rollbacks.

This chapter covers the Application Deployment domain (20% of the CKAD exam). You need to be fast at creating Deployments, performing rolling updates, rolling back, and scaling.

Deployments vs Pods vs ReplicaSets

Resource Purpose Use Directly?
Pod Runs a single instance of a container Rarely (for testing only)
ReplicaSet Ensures N identical Pods are running No — use Deployments instead
Deployment Manages ReplicaSets, handles updates & rollbacks Yes — this is what you use

A Deployment creates a ReplicaSet, which creates the Pods. You almost never create ReplicaSets directly.

Creating Deployments

Imperative (Fastest for Exams)

# Create a Deployment with 3 replicas
kubectl create deployment web --image=nginx:1.26 --replicas=3

# Verify
kubectl get deployment web
# Expected:
# NAME   READY   UP-TO-DATE   AVAILABLE   AGE
# web    3/3     3            3           10s

Declarative (YAML)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  labels:
    app: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:1.26
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
kubectl apply -f web-deployment.yaml

Generating YAML Quickly

On the CKAD exam, generate a YAML template and edit it:

# Generate YAML without creating the Deployment
kubectl create deployment web --image=nginx:1.26 --replicas=3 \
  --dry-run=client -o yaml > deployment.yaml

# Edit as needed
vi deployment.yaml

# Apply
kubectl apply -f deployment.yaml

Key Fields Explained

Field Purpose
spec.replicas Number of Pod copies to run
spec.selector.matchLabels How the Deployment finds its Pods
spec.template Pod template — defines what each Pod looks like
spec.template.metadata.labels Must match selector.matchLabels

Important: The selector.matchLabels must match the Pod template’s labels. If they do not match, the Deployment will not manage the Pods.

Deployment YAML Structure

apiVersion: apps/v1
kind: Deployment
metadata:
  name: <name>
spec:                              ← Deployment spec
  replicas: 3                     ← Desired Pod count
  selector:                       ← How to find managed Pods
    matchLabels:
      app: <name>
  strategy:                       ← Update strategy
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  template:                       ← Pod template
    metadata:
      labels:
        app: <name>               ← Must match selector
    spec:
      containers:
      - name: <container>
        image: <image>
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
            memory: 128Mi

Scaling Deployments

# Scale to 5 replicas
kubectl scale deployment web --replicas=5

# Scale to 0 (stop all Pods without deleting the Deployment)
kubectl scale deployment web --replicas=0

# Scale back up
kubectl scale deployment web --replicas=3

# Verify
kubectl get deployment web
kubectl get pods -l app=web

Scaling Behavior

Action What Happens
Scale up (3 → 5) 2 new Pods created immediately
Scale down (5 → 3) 2 Pods terminated (newest first)
Scale to 0 All Pods terminated, Deployment still exists
Scale from 0 → 3 3 new Pods created

Exam tip: kubectl create deployment ... --replicas=3 and kubectl scale deployment ... --replicas=N are the two commands you’ll use most during the exam. Practice until they’re muscle memory.


Next: Learn how updates work with Rolling Updates →

Updated: