What is a Deployment in Kubernetes?
In Kubernetes, a Deployment is a higher-level controller that manages Pods and their ReplicaSets. It helps you declaratively define, update, and maintain the desired state of your application.
Key Features of a Deployment
1. Declarative Configuration
You describe the desired state of your application in a YAML or JSON file — including:
- What container image to run
- How many replicas (pods) you want
- What labels to use
- Resource limits (CPU/memory)
- Rolling update strategy
Kubernetes then takes this configuration and ensures that the actual state in the cluster matches the desired state.
2. Pod Management via ReplicaSets
- The Deployment automatically creates and manages a ReplicaSet, which in turn manages the Pods.
- If a Pod crashes or is deleted, the ReplicaSet will auto-heal by starting a new one to maintain the desired replica count.
3. Rolling Updates and Rollbacks
- Deployments allow rolling updates, meaning it will gradually update Pods to a new version (e.g., a new image tag) without downtime.
- If something goes wrong, you can rollback to a previous version easily.
Example:
kubectl rollout undo deployment my-app
4. Scaling (Manual and Auto)
- You can scale your application by changing the replica count:
kubectl scale deployment my-app --replicas=5
- Or configure Horizontal Pod Autoscaling to do it automatically based on CPU/memory.
5. Self-healing (Auto-recovery)
- If a Pod fails or the Node it's on crashes, the Deployment’s ReplicaSet ensures the lost Pod is automatically replaced.
- You don’t need to manually restart failed Pods.
6. Seamless Image Updates
- You can update container images by modifying the Deployment:
kubectl set image deployment my-app my-container=my-image:v2
- The Deployment will update Pods in batches, monitoring health during the update.
Deployment Workflow
- You write a
Deployment
manifest (YAML/JSON) describing your app. - You apply it using
kubectl apply -f deployment.yml
. - Kubernetes:
- Creates a ReplicaSet
-
The ReplicaSet creates the specified number of Pods
- If you update the Deployment:
Kubernetes creates a new ReplicaSet for the new version
-
Gradually replaces old Pods with new ones (rolling update)
- If issues occur:
You can rollback to the previous version
Benefits of Using a Deployment
- High availability through replicas
- Automated updates and rollbacks
- Fault tolerance and auto-recovery
- Simplified management of stateless applications
Task Overview
Create a Kubernetes Deployment file (deployment.yml
) that:
- Deploys a sample todo-app
-
Supports:
- Auto-healing (via ReplicaSet & Deployment controller)
- Auto-scaling (via Horizontal Pod Autoscaler)
🔧 Step 1: Create deployment.yml
Here's a simple example of a deployment.yml
file for a todo-app
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app
spec:
replicas: 2 # Desired number of pods (auto-healing via controller)
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-container
image: kodekloud/webapp # Example TODO app
ports:
- containerPort: 80
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
---
apiVersion: v1
kind: Service
metadata:
name: todo-service
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
Step 2: Enable Metrics Server (Required for Auto-Scaling)
Run this command to enable the Metrics Server in Minikube:
minikube addons enable metrics-server
Step 3: Apply the Deployment
Apply the deployment using:
kubectl apply -f deployment.yml
Check if pods are running:
kubectl get pods
Step 4: Add Horizontal Pod Autoscaler (Auto-scaling)
Now add an HPA (Horizontal Pod Autoscaler) to auto-scale based on CPU usage:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: todo-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: todo-app
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Save this as hpa.yml
and apply it:
kubectl apply -f hpa.yml
What we have built
- A
todo-app
deployed on Kubernetes - Auto-healing via ReplicaSet and Deployment controller
- Auto-scaling via HPA and Metrics Server
Top comments (0)