Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

We are working on this space so that IT professionals can grow together.

Create account Log in
Balkrishna Pandey
Balkrishna Pandey

Posted on

How to update a Kubernetes deployment?

Let's say you want to use the nginx:1.13 image instead of the nginx:latest image. You can update deployment using the following command:


kubectl set image deployments nginx nginx=nginx:1.13

Enter fullscreen mode Exit fullscreen mode

Verify it using the kubectl get pods -l app=frontend command. You will see some of the pods are in the Terminating state and some of them are in the ContainerCreating state.


kubectl get pods -l app=frontend

NAME           READY  STATUS       RESTARTS  AGE

nginx-54fd96d656-pkhc4  0/1   ContainerCreating  0     1s

nginx-54fd96d656-vgj7f  1/1   Running       0     9s

nginx-776567f984-sdrns  1/1   Running       0     11m

nginx-776567f984-vxdzw  1/1   Terminating     0     11m

Enter fullscreen mode Exit fullscreen mode

Sometimes later you will all the pods are updated,


kubectl get pods -l app=frontend  

Enter fullscreen mode Exit fullscreen mode

Output:


NAME           READY  STATUS  RESTARTS  AGE

nginx-54fd96d656-pkhc4  1/1   Running  0     118s

nginx-54fd96d656-vgj7f  1/1   Running  0     2m6s

Enter fullscreen mode Exit fullscreen mode

Let's confirm the image version as follows,


kubectl describe po nginx-54fd96d656-pkhc4 | grep -i image

Enter fullscreen mode Exit fullscreen mode

You can see image version is nginx:1.13.


  Image:     nginx:1.13

  Image ID:    docker-pullable://nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35

 Normal Pulling  92s  kubelet      Pulling image "nginx:1.13"

 Normal Pulled   91s  kubelet      Successfully pulled image "nginx:1.13" in 852.963655ms

Enter fullscreen mode Exit fullscreen mode

You can also update the nginx deployment using yaml file. In yaml file update your image: nginx:latest section with image: 1.13 and use the following command:


kubectl apply -f nginx-deployment.yaml 

Enter fullscreen mode Exit fullscreen mode

This will update the deployment based on the changes you made in the nginx-deployment.yaml file.

How to rollback a deployment update?

You can rollback a deployment update using the following command:


kubectl rollout undo deployments nginx

Enter fullscreen mode Exit fullscreen mode

Output:


deployment.apps/nginx rolled back

Enter fullscreen mode Exit fullscreen mode

Now, you should see all the pods are running with nginx:latest image.


kubectl get pods -l app=frontend    

Enter fullscreen mode Exit fullscreen mode

Output:


NAME           READY  STATUS       RESTARTS  AGE

nginx-54fd96d656-pkhc4  1/1   Running       0     5m5s

nginx-54fd96d656-vgj7f  1/1   Terminating     0     5m13s

nginx-776567f984-nkp9z  1/1   Running       0     2s

nginx-776567f984-w9bsl  0/1   ContainerCreating  0     0s

Enter fullscreen mode Exit fullscreen mode

Lets verify the image,


kubectl describe po nginx-776567f984-nkp9z | grep -i image

Enter fullscreen mode Exit fullscreen mode

Output:


  Image:     nginx:latest

  Image ID:    docker-pullable://nginx@sha256:2bcabc23b45489fb0885d69a06ba1d648aeda973fae7bb981bafbb884165e514

 Normal Pulling  94s  kubelet      Pulling image "nginx:latest"

 Normal Pulled   94s  kubelet      Successfully pulled image "nginx:latest" in 829.083943ms

Enter fullscreen mode Exit fullscreen mode

Discussion (0)