Learn Kubernetes Deployments and how to record changes
There are two ways to create a deployment in Kubernetes
– Imperative way
– Declarative way
This has been explained in detail here. Let’s just go with the imperative approach, which is easy and less time-consuming (Helpful in case of the Kubernetes Certification Exam)
Create a deployment with 4 replica
kubectl create deployment nginx --image=nginx --replicas=4
This would take some time to bring the 4 replicas to a running state. Check using the below command and make sure all of them are in running state
kubectl get pods
NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 18s nginx 1/1 Running 0 18s ngin 1/1 Running 0 18s nginx 1/1 Running 0 18s
Update the replica
Increasing the replica count to 5 would bring a new pod to the same deployment
kubectl scale deployment nginx --replicas=5
NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 18s nginx 1/1 Running 0 18s nginx 1/1 Running 0 18s nginx 1/1 Running 0 18s nginx 1/1 Running 0 18s
Let’s check the list of changes made in this deployment.
kubectl rollout history deployment nginx
deployment.extensions/nginx REVISION CHANGE-CAUSE 1 <none> 2 <none>
You can see the ”CHANGE-CAUSE” is marked as “none”, and there is no information about the command that ran to do these changes, We can use the –record flag to save the command used to create/update a deployment against the revision number.
Now let’s update the container image to a different nginx version with “—record flag” and check the revision history
kubectl set image deployment nginx nginx=nginx:1.17 --record
kubectl rollout history deployment nginx
REVISION CHANGE-CAUSE 1 <none> 2 <none> 3 kubectl set image deployment nginx nginx=nginx:1.17 --record=true
As we can see the “CHANGE-CAUSE” has been updated with the command used while updating the deployment.
To roll back to the previous version, We can use the below command and this would revert back to the previous version where the nginx image is 1.16
kubectl rollout undo deployment nginx
Check here for more useful commands
Yes, That’s it, Let us know if this blog helps, and comment or chat with us if you have any questions related to it.