Rolling Updates And Rolling Back Deployments in Kubernetes
Task :
There is a production deployment planned for next week. The Nautilus DevOps team wants to test the deployment update and rollback on Dev environment first so that they can identify the risks in advance. Below you can find more details about the plan they want to execute.
Create a namespace devops. Create a deployment called httpd-deploy under this new namespace, It should have one container called httpd, use httpd:2.4.27 image and 4 replicas. The deployment should use RollingUpdate strategy with maxSurge=1, and maxUnavailable=2.
Next upgrade the deployment to version httpd:2.4.43 using a rolling update.
Finally, once all pods are updated undo the update and roll back to the previous/original version.
Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.
Solution :
thor@jump_host /$ kubectl create ns devops namespace/devops created thor@jump_host /$ cd /home/thor/ thor@jump_host ~$ vi deployment.yml apiVersion: apps/v1 kind: Deployment metadata: name: httpd-deploy namespace: devops spec: replicas: 4 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 2 selector: matchLabels: app: httpd template: metadata: labels: app: httpd spec: containers: - name: httpd image: httpd:2.4.27
thor@jump_host ~$ kubectl apply -f deployment.yml deployment.apps/httpd-deploy created thor@jump_host ~$ kubectl get deployment -n devops NAME READY UP-TO-DATE AVAILABLE AGE httpd-deploy 0/4 4 0 6s
thor@jump_host ~$ kubectl set image deployment httpd-deploy --namespace devops httpd=httpd:2.4.43 --record=true deployment.apps/httpd-deploy image updated
thor@jump_host ~$ kubectl get pods -n devops NAME READY STATUS RESTARTS AGE httpd-deploy-7bb4d96457-b5bbn 1/1 Running 0 34s httpd-deploy-7bb4d96457-b87f2 1/1 Running 0 34s httpd-deploy-7bb4d96457-wrfvk 1/1 Running 0 34s httpd-deploy-7bb4d96457-zvc8r 1/1 Running 0 14s thor@jump_host ~$ kubectl rollout status deployment httpd-deploy -n devops deployment "httpd-deploy" successfully rolled out thor@jump_host ~$ kubectl get pods -n devops NAME READY STATUS RESTARTS AGE httpd-deploy-7bb4d96457-b5bbn 1/1 Running 0 76s httpd-deploy-7bb4d96457-b87f2 1/1 Running 0 76s httpd-deploy-7bb4d96457-wrfvk 1/1 Running 0 76s httpd-deploy-7bb4d96457-zvc8r 1/1 Running 0 56s thor@jump_host ~$ kubectl get deployment -n devops NAME READY UP-TO-DATE AVAILABLE AGE httpd-deploy 4/4 4 4 2m50s thor@jump_host ~$ kubectl get deployment httpd-deploy -n devops NAME READY UP-TO-DATE AVAILABLE AGE httpd-deploy 4/4 4 4 3m31s thor@jump_host ~$ kubectl rollout undo deployment httpd-deploy -n devops deployment.apps/httpd-deploy rolled back thor@jump_host ~$ kubectl get deployment httpd-deploy -n devops NAME READY UP-TO-DATE AVAILABLE AGE httpd-deploy 2/4 3 2 3m49s thor@jump_host ~$ kubectl get deployment httpd-deploy -n devops NAME READY UP-TO-DATE AVAILABLE AGE httpd-deploy 4/4 4 4 3m54s thor@jump_host ~$ kubectl describe deployment httpd-deploy -n devops Name: httpd-deploy Namespace: devops CreationTimestamp: Thu, 22 Apr 2021 12:59:51 +0000 Labels: <none> Annotations: deployment.kubernetes.io/revision: 3 Selector: app=httpd Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 2 max unavailable, 1 max surge Pod Template: Labels: app=httpd Containers: httpd: Image: httpd:2.4.27 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: httpd-deploy-5c68f9f7b7 (4/4 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 4m13s deployment-controller Scaled up replica set httpd-deploy-5c68f9f7b7 to 4 Normal ScalingReplicaSet 2m52s deployment-controller Scaled up replica set httpd-deploy-7bb4d96457 to 1 Normal ScalingReplicaSet 2m52s deployment-controller Scaled down replica set httpd-deploy-5c68f9f7b7 to 2 Normal ScalingReplicaSet 2m52s deployment-controller Scaled up replica set httpd-deploy-7bb4d96457 to 3 Normal ScalingReplicaSet 2m32s deployment-controller Scaled down replica set httpd-deploy-5c68f9f7b7 to 1 Normal ScalingReplicaSet 2m32s deployment-controller Scaled up replica set httpd-deploy-7bb4d96457 to 4 Normal ScalingReplicaSet 2m32s deployment-controller Scaled down replica set httpd-deploy-5c68f9f7b7 to 0 Normal ScalingReplicaSet 32s deployment-controller Scaled up replica set httpd-deploy-5c68f9f7b7 to 1 Normal ScalingReplicaSet 32s deployment-controller Scaled down replica set httpd-deploy-7bb4d96457 to 2 Normal ScalingReplicaSet 23s (x4 over 32s) deployment-controller (combined from similar events): Scaled down replica set httpd-deploy-7bb4d96457 to 0 thor@jump_host ~$