Docker Certified Associate Exam Course

Kubernetes

Deployments

Kubernetes Deployments automate application upgrades, scaling, and self-healing in production environments. They enable rolling updates, controlled rollbacks, and pause/resume capabilities—all without downtime. In this guide, you’ll learn how Deployments manage ReplicaSets and Pods, define a Deployment manifest, and inspect your resources using kubectl.

Why Use a Deployment?

  • Rolling Updates: Replace pods one by one to avoid service interruption.
  • Rollbacks: Instantly revert to a previous version if something goes wrong.
  • Pause & Resume: Apply several changes as a batch and resume when ready.
  • Declarative Scaling: Increase or decrease replicas in your manifest.

How Deployments Work

A Deployment sits above ReplicaSets and Pods:

  1. Pod: The basic execution unit (one or more containers).
  2. ReplicaSet: Ensures a specified number of pod replicas run at any time.
  3. Deployment: Manages ReplicaSets and orchestrates updates, rollbacks, and scaling.

The image illustrates a Kubernetes deployment structure, showing a deployment with multiple pods and a replica set, along with versioning and control icons.

Resource Comparison

Resource TypePurposeExample Command
PodSingle instance of one or more containerskubectl run nginx --image=nginx
ReplicaSetMaintains desired pod replicaskubectl create -f replicaset-definition.yml
DeploymentDeclarative updates and rollbackskubectl apply -f deployment-definition.yml

Writing a Deployment Manifest

Create a YAML file (deployment-definition.yml) to declare your desired state:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
    app: myapp
    type: front-end
spec:
  replicas: 3
  selector:
    matchLabels:
      type: front-end
  template:
    metadata:
      labels:
        app: myapp
        type: front-end
    spec:
      containers:
        - name: nginx-container
          image: nginx:latest
          ports:
            - containerPort: 80
  • apiVersion: The API group (apps/v1).
  • kind: Must be Deployment.
  • metadata: Identifies the Deployment (name and labels).
  • spec.replicas: Desired number of pods.
  • spec.selector: Matches labels on pods.
  • spec.template: Defines the pod spec, just like a ReplicaSet.

Note

Using kubectl apply -f is recommended for idempotent updates. It creates or updates resources based on your manifest.

Deploying and Inspecting Resources

  1. Create or update the Deployment
    kubectl apply -f deployment-definition.yml
    
  2. View Deployments
    kubectl get deployments
    
    Example output:
    NAME              READY   UP-TO-DATE   AVAILABLE   AGE
    myapp-deployment  3/3     3            3           30s
    
  3. List ReplicaSets
    kubectl get rs
    
  4. Check Pods
    kubectl get pods
    
  5. See All Resources
    kubectl get all
    

Warning

If an update fails, rollback immediately:

kubectl rollout undo deployment/myapp-deployment

Next Steps & References

Watch Video

Watch video content

Previous
Demo Replica Sets