This guide covers advanced deployment strategies and troubleshooting techniques for Kubernetes Deployments, essential knowledge for the CKAD exam and real-world kubernetes administration.
Beyond the basic RollingUpdate and Recreate strategies, there are several patterns you can implement using Kubernetes resources:
Blue-Green deployment involves running two identical environments (blue and green), with only one active at a time. This allows for zero-downtime deployments with immediate rollback capability.
Implementation in Kubernetes:
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
selector:
app: my-app
version: v1 # Initially points to blue deployment
ports:
- port: 80
targetPort: 8080
Test the green deployment thoroughly
Switch traffic by updating the Service selector:
kubectl patch service my-app -p '{"spec":{"selector":{"version":"v2"}}}'
If issues occur, switch back to the blue deployment
Once confirmed working, the old deployment can be scaled down or removed
Canary deployments involve gradually routing a small percentage of traffic to a new version to test it with real users before full rollout.
Implementation in Kubernetes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-stable
spec:
replicas: 9 # 90% of traffic
selector:
matchLabels:
app: my-app
version: stable
template:
metadata:
labels:
app: my-app
version: stable
spec:
containers:
- name: my-app
image: my-app:1.0.0
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-canary
spec:
replicas: 1 # 10% of traffic
selector:
matchLabels:
app: my-app
version: canary
template:
metadata:
labels:
app: my-app
version: canary
spec:
containers:
- name: my-app
image: my-app:2.0.0
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
selector:
app: my-app # Both versions have this label
ports:
- port: 80
targetPort: 8080
A/B testing is similar to canary deployments but focuses on testing features rather than stability:
Symptoms: Pods remain in Pending, ImagePullBackOff, or CrashLoopBackOff state
Troubleshooting Steps:
kubectl get pods
kubectl describe pod <pod-name>
kubectl logs <pod-name>
kubectl logs <pod-name> --previous # For crashed containers
Symptoms: Deployment stuck at status “x out of y new replicas have been updated”
Troubleshooting Steps:
kubectl rollout status deployment/<deployment-name>
kubectl get rs
kubectl describe rs <new-replicaset-name>
kubectl get pods -l <deployment-selector>
kubectl describe pod <failing-pod>
kubectl rollout undo deployment/<deployment-name>
Symptoms: Deployment doesn’t scale to the desired number of replicas
Troubleshooting Steps:
kubectl describe quota -n <namespace>
kubectl describe nodes
Symptoms: Changes to Deployment don’t result in new Pods
Troubleshooting Steps:
kubectl describe deployment <deployment-name>
kubectl get deployment <deployment-name> -o yaml
Starting with Kubernetes 1.18, you can attach a debug container to a running Pod:
kubectl debug -it <pod-name> --image=busybox --target=<container-name>
kubectl run debug --rm -it --image=busybox -- /bin/sh
kubectl run curl --rm -it --image=curlimages/curl -- curl http://my-service
Add readiness and liveness probes to your Deployments to improve reliability:
spec:
containers:
- name: my-app
image: my-app:1.0.0
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 15
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
| Strategy | Use When | Avoid When |
|---|---|---|
| RollingUpdate | - Need zero downtime - Application supports N-1 compatibility |
- Cannot have multiple versions running - Limited cluster resources |
| Recreate | - Need complete version isolation - Downtime is acceptable |
- Zero downtime is required - User experience is critical |
| Blue/Green | - Need immediate rollback capability - Need to test the whole system before switching |
- Limited resources - High cost of running duplicate systems |
| Canary | - Need to test with real users - Want to gradually migrate |
- Application doesn’t work with multiple versions - Need to make quick updates |
kubectl get deployment <n>
kubectl describe deployment <n>
kubectl get pods -l <deployment-selector>
kubectl describe pod <pod-name>
kubectl logs <pod-name>
kubectl logs -l app=<app-name>
kubectl rollout history deployment/<n>
kubectl rollout undo deployment/<n>
Understanding advanced deployment strategies and troubleshooting techniques is crucial for the CKAD exam and real-world Kubernetes administration. By mastering these concepts, you’ll be able to deploy applications more effectively and solve issues when they arise.
Remember that Deployments are just one part of a larger application architecture. They typically work alongside Services, ConfigMaps, Secrets, and potentially Ingress controllers to create a complete application environment.