What Are Deployments?
A Deployment in Kubernetes is a resource that provides a declarative way to manage Pod creation, updates, and scaling. Deployments enable you to maintain desired states and implement strategies for updating applications without downtime.
Why Use Deployments?
- Declarative Management: Define the desired state of your application, and Kubernetes ensures it.
- Scaling: Easily scale applications up or down based on load.
- Rolling Updates: Update applications without downtime.
- Self-healing: Automatically replace failed Pods to maintain availability.
- Version Control: Roll back to previous states if issues arise.
Deployment Architecture
A Deployment manages a ReplicaSet, which in turn manages the Pods. The architecture looks like this:
- Deployment: The top-level controller.
- ReplicaSet: Ensures the desired number of Pods is running.
- Pods: The smallest deployable units hosting your application.
Creating a Deployment
Step 1: Write a YAML File
Here’s an example deployment file for an Nginx application. Save this as nginx-deployment.yaml:
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
Step 2: Apply the Deployment
Use the following command to create the Deployment:
kubectl apply -f nginx-deployment.yaml
Step 3: Verify the Deployment
Check the Deployment status:
kubectl get deployments
Example Output:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 1m
Verify the Pods:
kubectl get pods
Scaling Deployments
Scaling allows you to adjust the number of Pods in a Deployment to handle varying loads.
Step 1: Scale Up
Increase the number of Pods to 5:
kubectl scale deployment nginx-deployment --replicas=5
Verify the new replica count:
kubectl get pods
Step 2: Scale Down
Reduce the Pods back to 3:
kubectl scale deployment nginx-deployment --replicas=3
Updating Deployments
Updates allow you to deploy new application versions without downtime.
Step 1: Update the Image
Use the following command to update the Nginx image version:
kubectl set image deployment/nginx-deployment nginx=nginx:1.23
Step 2: Check the Rollout
Monitor the rollout status:
kubectl rollout status deployment/nginx-deployment
Example Output:
deployment "nginx-deployment" successfully rolled out
Step 3: Verify the Update
Check the updated Pods:
kubectl describe deployment nginx-deployment
Step 4: Rollback if Needed
If the update causes issues, rollback to the previous version:
kubectl rollout undo deployment/nginx-deployment
Implementing Rolling Updates
Rolling updates replace Pods gradually, ensuring zero downtime.
1. Edit the Deployment YAML to a new version:
kubectl edit deployment nginx-deployment
2. Change the image:
containers:
- name: nginx
image: nginx:1.23
3. Save and monitor the rollout:
kubectl rollout status deployment/nginx-deployment
Canary Deployments
Canary deployments allow gradual introduction of new versions to minimize risks.
Step 1: Create a Canary Deployment
Here’s an example YAML file (nginx-canary.yaml):
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-canary
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx-canary
template:
metadata:
labels:
app: nginx-canary
spec:
containers:
- name: nginx
image: nginx:1.23
ports:
- containerPort: 80
Step 2: Apply the Canary Deployment
kubectl apply -f nginx-canary.yaml
Step 3: Monitor Traffic
Route a small percentage of traffic to the Canary Deployment using tools like Istio or Traefik (covered in advanced chapters).
Production Monitoring
Monitor Deployment Resource Usage
Install and use the metrics server to monitor deployments:
kubectl top pods
kubectl top nodes
Check Events
kubectl describe deployment nginx-deployment
Centralized Monitoring with Prometheus
- Install Prometheus using Helm:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/prometheus
2. Access the Prometheus dashboard and set alerts for your deployments.
Troubleshooting Deployments
Scenario 1: Pods Not Ready
1. Check the Deployment status:
kubectl rollout status deployment/nginx-deployment
2. Inspect the Pods:
kubectl describe pods
kubectl logs <pod-name>
Scenario 2: Rollout Stuck
1. Check events:
kubectl describe deployment nginx-deployment
2. Rollback to a stable state:
kubectl rollout undo deployment/nginx-deployment
Conclusion
By mastering Deployments, you’ve learned how to:
- Create, scale, and update applications.
- Ensure zero-downtime updates using rolling updates.
- Monitor and troubleshoot production workloads effectively.