=== Kubernetes Deployment Strategies: Rolling Updates and Rollbacks === Kubernetes offers robust deployment strategies, allowing for smooth updates and quick rollbacks in case of issues. The most common strategy is the Rolling Update, which incrementally updates Pods with new ones. Let's dive into how to implement Rolling Updates and Rollbacks. ==== Part 1: Rolling Updates ==== '''Step 1: Create a Deployment''' 1. Create a Deployment YAML File: myappdeployment2.yaml {{{ apiVersion: apps/v1 kind: Deployment metadata: name: myapp-deployment2 spec: replicas: 3 selector: matchLabels: app: myapp2 template: metadata: labels: app: myapp2 spec: containers: - name: myapp-container2 image: nginx:1.16 ports: - containerPort: 80 }}} • Deploy it: `kubectl apply -f myappdeployment2.yaml` '''Step 2: Update the Deployment''' 1. Update the Image: Change the image in your deployment to a new version. For example, update nginx:1.16 to nginx:1.17. 2. Apply the Updated Deployment: `kubectl apply -f deployment.yaml` • Monitor the Rollout: `kubectl rollout status deployment/myapp-deployment2` Kubernetes will stop the old Pods and start new ones with the updated configuration. '''Step 3: Verify the Update''' 1. Check the Deployed Pods: `kubectl get pods` You should see the Pods with the new image running. ==== Part 2: Rollbacks ==== ''' Step 1: Trigger a Faulty Update''' 1. Update the Deployment with a Faulty Image: Change the image to a non-existent or faulty one, like nginx:1.18-faulty. 2. Apply the Faulty Deployment: `kubectl apply -f deployment.yaml` ==== Step 2: Rollback the Update ==== 1. Check the Rollout History: `kubectl rollout history deployment/myapp-deployment` • Rollback to a Previous Revision: Find the revision number you want to rollback to (e.g., 2) and execute: `kubectl rollout undo deployment/myapp-deployment --to-revision=2` • Verify the Rollback: `kubectl get pods` The Pods should be running with the image from the previous stable revision. ==== Additional Tips ==== • Automated Rollbacks: Set up readiness and liveness probes in your deployment to automatically rollback if the new version fails to start properly. • Canary Deployments: For more controlled updates, consider using canary deployments where only a subset of Pods is updated initially. • Blue/Green Deployments: Another strategy is blue/green deployment, where you deploy a new version alongside the old version and switch traffic once the new version is verified. By mastering these deployment strategies, you can ensure smooth updates and quick recovery in your Kubernetes environment.