How Kubernetes Replicasets help as a Version Control | Devops Junction

Do you know Kubernetes replica sets can be used as version control or revision control system?

If you have used the kubectl rollout command to check the revisions of the deployment.

or to undo the last deployment, you might already be aware that the deployments do track the last 10 revisions. Do you know it is all powered by the replica sets?

In this quick article, we are going to see how Replica sets help you in the rollout and revision control of deployment

kubernetes rollout

 

Replica set and Deployment

Whenever you patch/update the deployment like changing the Environment variable or deploying a new image, Kubernetes Deployment creates a new replica set.

Yes. Kubernetes Deployments do not manage the pods directly, it delegates the job to the replica set

Deployments create a new replica set whenever you do an update to the deployment

Even though the new replica set is created. the old replica sets are not removed they are retained for the versioning

By default, Kubernetes keep 10 replica sets for each deployment, which means you can go back 9 versions prior

If you have used the kubectl rollout history command you might have noticed that it displays only the last 10 revisions

 

Kubectl command to display revision with Images

By default, when you use the kubectl rollout history command,  you would see only the revisions and not the changes.

To see what are the last 10 images that have been deployed to a specific deployment

I use the following command to get the replica sets created for the deployment with the name nginx under the sre namespace

kubectl get replicaset -n sre 
-o jsonpath='{"NAME\tCREATIONTIME\tIMAGE"}{"\n"}{range .items[?(@.metadata.ownerReferences[0].name == "nginx")]}{.metadata.name}{"\t"}{.metadata.creationTimestamp}{"\t"}{range .spec}{.template.spec.containers[*].image}{"\n"}'

I parse through the data using jsonpath and print the following fields

✅ Name of the Replica set
✅ Creation time stamp ( to see when this replica was created)
✅ The Image used on that replica set

You can list these revisions with the kubectl rollout as well.

kubectl rollout history

this rollout history command helps you to associate the revisions with this replica set

Now If I want to go back to the specific version/revision from the past, I can use the following command

kubectl rollout undo deployment nginx – to-revision=9

Remember, the revision numbers always increase sequentially, it is not 1~10 always. with the previous history command, you can see the actual revision number

I hope this short article helps

Cheers

Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading