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:
- Pod: The basic execution unit (one or more containers).
- ReplicaSet: Ensures a specified number of pod replicas run at any time.
- Deployment: Manages ReplicaSets and orchestrates updates, rollbacks, and scaling.
Resource Comparison
Resource Type | Purpose | Example Command |
---|---|---|
Pod | Single instance of one or more containers | kubectl run nginx --image=nginx |
ReplicaSet | Maintains desired pod replicas | kubectl create -f replicaset-definition.yml |
Deployment | Declarative updates and rollbacks | kubectl 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
andlabels
). - 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
- Create or update the Deployment
kubectl apply -f deployment-definition.yml
- View Deployments
Example output:kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE myapp-deployment 3/3 3 3 30s
- List ReplicaSets
kubectl get rs
- Check Pods
kubectl get pods
- See All Resources
kubectl get all
Warning
If an update fails, rollback immediately:
kubectl rollout undo deployment/myapp-deployment
Next Steps & References
- Learn more about Kubernetes Deployments
- Explore the kubectl Cheat Sheet
- Official Kubernetes Documentation
Watch Video
Watch video content