Learn how to deploy applications on Kubernetes using Pods, Deployments, and ReplicaSets. Master the declarative approach to application deployment and management.
Table of Contents
1. Deployment Basics
Kubernetes provides several objects for deploying applications:
- Pods: Basic unit - one or more containers
- ReplicaSets: Ensures a specified number of Pod replicas are running
- Deployments: Manages ReplicaSets and provides declarative updates
In practice, you typically use Deployments, which manage ReplicaSets, which manage Pods.
2. Creating Pods
While Pods are rarely created directly in production, understanding them is fundamental.
2.1 Pod Definition
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
2.2 Creating a Pod
# Create Pod from YAML
kubectl apply -f pod.yaml
# Create Pod imperatively
kubectl run nginx-pod --image=nginx:1.21
# Get Pod status
kubectl get pods
# Describe Pod details
kubectl describe pod nginx-pod
# View Pod logs
kubectl logs nginx-pod
2.3 Pod Limitations
Pods alone don't provide:
- High availability (if Pod dies, it's gone)
- Scaling capabilities
- Rolling updates
- Self-healing
This is why we use ReplicaSets and Deployments.
3. ReplicaSets
A ReplicaSet ensures that a specified number of Pod replicas are running at any given time.
3.1 ReplicaSet Definition
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
3.2 Key Components
- replicas: Desired number of Pod instances
- selector: Labels used to identify Pods managed by this ReplicaSet
- template: Pod template used to create new Pods
3.3 ReplicaSet Behavior
If a Pod fails or is deleted, the ReplicaSet creates a new one. If you manually scale, the ReplicaSet adjusts.
# Scale ReplicaSet
kubectl scale replicaset nginx-replicaset --replicas=5
# Get ReplicaSet status
kubectl get replicasets
4. Deployments
A Deployment provides declarative updates for Pods and ReplicaSets. It's the recommended way to deploy applications.
4.1 Deployment Definition
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
4.2 Creating a Deployment
# Create Deployment
kubectl apply -f deployment.yaml
# Get Deployments
kubectl get deployments
# Get Deployment details
kubectl describe deployment nginx-deployment
# Get Pods created by Deployment
kubectl get pods -l app=nginx
4.3 Deployment Benefits
- Manages ReplicaSets automatically
- Provides rolling updates
- Enables rollback capabilities
- Supports scaling
- Pauses and resumes deployments
5. Deployment Strategies
Kubernetes supports different deployment strategies:
5.1 Rolling Update (Default)
Gradually replaces old Pods with new ones. Zero downtime deployment.
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
- maxSurge: Maximum number of Pods that can be created over desired count
- maxUnavailable: Maximum number of Pods that can be unavailable during update
5.2 Recreate
Terminates all old Pods before creating new ones. Causes downtime but ensures consistency.
spec:
strategy:
type: Recreate
6. Updating Deployments
You can update Deployments in several ways:
6.1 Update Image
# Update image imperatively
kubectl set image deployment/nginx-deployment nginx=nginx:1.22
# Or edit YAML and apply
kubectl edit deployment nginx-deployment
kubectl apply -f deployment.yaml
6.2 Scale Deployment
# Scale to 5 replicas
kubectl scale deployment nginx-deployment --replicas=5
# Or update YAML
spec:
replicas: 5
6.3 Monitor Update
# Watch deployment status
kubectl rollout status deployment/nginx-deployment
# Get rollout history
kubectl rollout history deployment/nginx-deployment
7. Rolling Back Deployments
Kubernetes maintains rollout history, allowing you to rollback to previous versions.
7.1 View Rollout History
# View rollout history
kubectl rollout history deployment/nginx-deployment
# View details of specific revision
kubectl rollout history deployment/nginx-deployment --revision=2
7.2 Rollback to Previous Version
# Rollback to previous revision
kubectl rollout undo deployment/nginx-deployment
# Rollback to specific revision
kubectl rollout undo deployment/nginx-deployment --to-revision=2
# Monitor rollback status
kubectl rollout status deployment/nginx-deployment
7.3 Pause and Resume
# Pause deployment
kubectl rollout pause deployment/nginx-deployment
# Make multiple changes
kubectl set image deployment/nginx-deployment nginx=nginx:1.22
kubectl set resources deployment/nginx-deployment -c=nginx --limits=memory=256Mi
# Resume deployment
kubectl rollout resume deployment/nginx-deployment
8. Best Practices
8.1 Use Deployments, Not Pods
Always use Deployments for production workloads. They provide high availability, scaling, and update capabilities.
8.2 Set Resource Requests and Limits
Always specify resource requests and limits to ensure proper scheduling and prevent resource exhaustion.
8.3 Use Meaningful Labels
Use consistent, meaningful labels for easy identification and selection of resources.
8.4 Configure Health Checks
Always define liveness and readiness probes for your containers.
containers:
- name: nginx
image: nginx:1.21
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
8.5 Use Declarative Approach
Prefer YAML files and kubectl apply over imperative commands for better version control and reproducibility.
Summary: Deployments are the recommended way to deploy applications on Kubernetes. They manage ReplicaSets, which manage Pods, providing high availability, scaling, rolling updates, and rollback capabilities.
0 Comments