How to Update the Image in Kubernetes Deployment

Update the Image in Kubernetes Deployment

We can update the image of a Kubernetes deployment by simply running the “kubectl set image” command with the updated image

In this article, We will see the 2 easy ways to update an image in a deployment that is running currently,

  • Using Imperative command
  • Using the “kubectl edit” option

Before starting, Let’s create a deployment using the below YAML file:

Check here to learn more about deployment and ways to record the changes

Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
  • Save the above definition as a file “deployment.yaml”

Now, We can create the deployment using the below command

Kubectl create -f deployment.yaml

Output:

deployment.apps/nginx-deployment created

Currently, We have a running deployment with 3 replica

To Update the Image in Kubernetes Deployment

Due to the new version release of “nginx”, We wanted to update the image to the latest version, and below are the ways to update it

Image

nginx:1.14.2 => nginx:1.16.1

Using Imperative command

With an imperative command, We can directly update the image of the deployment, Based on the deployment strategy, It will bring down the pod and update the image, and will make it available to the user

kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

Output:

deployment.apps/nginx-deployment image updated

We can check the rollout (deployment procedure ) with the below command

➜  kubectl rollout status deployment/nginx-deployment

Output:

Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out

Now, you can able to notice the pod got respawned and will have the latest image

Kubectl describe deployment/nginx-deployment
Containers:
   nginx:
    Image:        nginx:1.16.1

Using the “kubectl edit” option

Using the “kubectl edit” basically allows you to edit the deployment definition file directly, and while saving the file, It will update the running deployment definition, and based on the deployment strategy, it will respawn the pod with the latest image

kubectl edit deployment/nginx-deployment

Save the file using “wq!”


deployment.apps/nginx-deployment edited

We can check the rollout (deployment procedure ) with the below command

➜  kubectl rollout status deployment/nginx-deployment

Output:

Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out

Now, Deployment has been updated with the latest image

Kubectl describe deployment/nginx-deployment
Containers:
   nginx:
    Image:        nginx:1.16.1

Conclusion:

In this article, We have learned the ways to update the images in the deployment and to track the changes

Check here for more tips to pass CKA and CKAD

Good Luck with your learning !!

Similar Posts