1. What is a Kubernetes Deployment?
In Kubernetes, a Deployment is a higher-level abstraction used to manage the lifecycle of Pods. While you can create Pods directly, it’s not practical in real-world scenarios where:
- You need to ensure availability and fault-tolerance
- You need rolling updates to newer versions
- You want Kubernetes to automatically heal or recreate failed Pods
A Deployment automates the process of creating, scaling, updating, and rolling back Pods via ReplicaSets.
2. Why Use a Deployment Instead of a Pod?
Problems with managing Pods manually:
- If a Pod crashes, it’s not recreated automatically.
- Scaling the application to multiple Pods is manual.
- Updates or rollbacks are not versioned or controlled.
- No guarantee of high availability across nodes.
Benefits of using Deployments:
- Ensures desired number of identical Pods are always running.
- Supports automated rolling updates and rollbacks.
- Automatically replaces Pods in case of failure or node crash.
- Deployments manage ReplicaSets, which in turn manage Pods.
3. Anatomy of a Deployment YAML Manifest
Here is an example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Explanation of Sections:
| Field | Description |
|---|---|
apiVersion | API version to use (apps/v1 is required for Deployment) |
kind | Resource type: Deployment |
metadata | Metadata like name, labels, and optionally namespace |
spec.replicas | Desired number of Pod replicas |
strategy | Strategy for updating Pods (default is RollingUpdate) |
selector | Labels used by Deployment to find and manage Pods |
template | Pod template used to create new Pods |
template.spec | Same structure as a regular Pod manifest (containers, volumes, etc.) |
4. Deployment Strategies
A. RollingUpdate (Default)
This strategy updates Pods incrementally — new Pods are created while old ones are removed.
Fields:
maxSurge: Max number of extra Pods allowed during update.maxUnavailable: Max number of Pods allowed to be unavailable.
Both can be specified as:
- Absolute number (e.g.,
1) - Percentage (e.g.,
25%)
Use Case: When your app can tolerate old and new versions running together.
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
B. Recreate
All old Pods are deleted before new ones are created.
Use Case: When your app doesn’t support parallel versions, or must run in a clean state.
strategy: type: Recreate
5. How Deployments Work Internally
- Deployment → creates a ReplicaSet
- ReplicaSet → creates and manages Pods
ReplicaSet ensures:
- The desired number of Pods are always running
- If a Pod crashes, a new one is created automatically
If a node fails, the Deployment/ReplicaSet reschedules Pods to another healthy node.
6. Managing Deployments via kubectl
Create a Deployment
kubectl apply -f nginx-deployment.yaml
View All Deployments
kubectl get deployments
Columns Explained:
NAME: Deployment nameREADY: Ready Pods vs desired PodsUP-TO-DATE: Number of Pods updated to the latest versionAVAILABLE: Number of Pods available to serve trafficAGE: Time since deployment was created
7. Inspecting ReplicaSets and Pods
Get ReplicaSets Created by Deployment
kubectl get rs
Example Output:
NAME DESIRED CURRENT READY AGE nginx-deployment-6b474476c4 3 3 3 52m
Get Pods Managed by Deployment
kubectl get pods
Example Output:
NAME READY STATUS RESTARTS AGE nginx-deployment-6b474476c4-b6j4j 1/1 Running 0 3h31m
Filter Pods by Label
kubectl get pods -l app=nginx
8. Describe and Debug Deployment
View Detailed Info About a Deployment
kubectl describe deployment nginx-deployment
View Deployment Rollout History
kubectl rollout history deployment nginx-deployment
Undo Last Deployment
kubectl rollout undo deployment nginx-deployment
Undo to a Specific Revision
kubectl rollout undo deployment nginx-deployment --to-revision=2
9. Delete a Deployment
Command:
kubectl delete deployment nginx-deployment
This will also:
- Remove ReplicaSets created by it
- Delete all Pods under those ReplicaSets
10. Summary Table
| Feature | Description |
|---|---|
| Deployment | Manages desired number of identical Pods |
| ReplicaSet | Ensures correct number of Pods |
| Pod Template | Defines how each Pod looks (image, ports, labels) |
| RollingUpdate | Default update strategy — no downtime |
| Recreate | Strategy that replaces all Pods at once |
| Rollback | Rollback to previous Deployment revision |
| Self-Healing | Automatically recreates failed Pods |
11. Conclusion
A Kubernetes Deployment is the standard way to manage stateless applications in production. It ensures that:
- Your application is always available
- Scaling, updating, and recovery are seamless
- Downtime is minimized using Rolling Updates
- Full history and rollback support is available
