Question on rollout upgrade and deployments -->kubernetes

Hello Team ,

I need help with one of the question specific to roll out ,upgrades and deployments

I first created a deployment names nginx and set the image to version 1.9.1

kubectl create deployment nginx --image=nginx:1.9.1

That means that it would create a roll-out and version. AM i correct here ?

PS C:\Users\kkaushal> kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 9s
PS C:\Users\kkaushal> kubectl get pods
NAME READY STATUS RESTARTS AGE
event-simulator 2/2 Running 0 137m
nginx-5dfdd6897-lplbq 1/1 Running 0 14s

Then i basically went ahead of the deployment to new version of nginx image

PS C:\Users\kkaushal> kubectl set image deployment/nginx nginx=nginx:1.10.1
deployment.apps/nginx image updated
PS C:\Users\kkaushal> kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 91s
PS C:\Users\kkaushal> kubectl rollout status deployment/nginx
deployment “nginx” successfully rolled out
PS C:\Users\kkaushal> kubectl describe deployments.apps nginx
Name: nginx
Namespace: default
CreationTimestamp: Sun, 19 Jan 2025 17:50:37 +0530
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 2
Selector: app=nginx
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.10.1
Port:
Host Port:
Environment:
Mounts:
Volumes:
Node-Selectors:
Tolerations:
Conditions:
Type Status Reason


Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: nginx-5dfdd6897 (0/0 replicas created)
NewReplicaSet: nginx-69d8b87bd9 (1/1 replicas created)
Events:
Type Reason Age From Message


Normal ScalingReplicaSet 2m7s deployment-controller Scaled up replica set nginx-5dfdd6897 to 1
Normal ScalingReplicaSet 46s deployment-controller Scaled up replica set nginx-69d8b87bd9 to 1
Normal ScalingReplicaSet 41s deployment-controller Scaled down replica set nginx-5dfdd6897 to 0 from 1

PS C:\Users\kkaushal> kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5dfdd6897 0 0 0 2m43s
nginx-69d8b87bd9 1 1 1 82s

The question i have here is then i executed rollout undo deployment/nginx and it was successfull.

PS C:\Users\kkaushal> kubectl rollout undo deployment/nginx
deployment.apps/nginx rolled back
PS C:\Users\kkaushal> kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5dfdd6897 1 1 1 3m33s
nginx-69d8b87bd9 0 0 0 2m12s

I am not able to understand how this roll back undo was successfull . Ideally i have not create the deployment by using any yaml file . I only set the image to new version of nginx that is 1.10.1 and once the roll out was successful , i just did undo of the rollout .

Is that any deployment that we do . Kubernetes preserves the state of the deployment meaning the rollout and version and any point we can undo the roll-out and go back to the previous version ?

It is expected to be able to undo the image change.

Note that every time you do something to a deployment that alters its pod spec (like changing the image), a new revision is created.

After the image update…

controlplane $ k rollout history deployment nginx 
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

Each revision corresponds to a new replicaset owned by the deployment

controlplane $ k get rs
NAME               DESIRED   CURRENT   READY   AGE
nginx-5559c848db   1         1         1       8m14s
nginx-768667b68b   0         0         0       7m38s

up to a total number specified by the deployment’s revisionHistoryLimit (default 10)
The active revision has its replicaset scaled up to the replicas of the deployment. All other repliasets are at zero.

In your case, if you inspect the two replicasets, one will have image 1.9.1 and the other image 1.10.1