1. Overview

Nowadays, Kubernetes is the most popular container orchestration tool. One of the prime reasons for its popularity is it provides an efficient way to manage the deployments of containerized applications at scale.

Kubernetes has various built-in objects that allow us to run containerized applications reliably. The most commonly used objects for such use cases are Pod, Deployment, StatefulSet, CronJob, etc.

In this tutorial, we’ll discuss various methods that allow us to update the container image of the Deployment object without modifying the declarative configuration file.

2. Setting up an Example

In this section, we’ll create an NGINX Deployment object to use as an example.

Kubernetes namespaces allow us to isolate resources within a cluster. So, first, let’s create a new namespace to deploy an nginx pod:

$ kubectl create ns deployment-demo
namespace/deployment-demo created

Next, let’s save the following declarative configuration in a deployment-demo.yaml file. We’ll use it to create a pod that uses the 1.22 tag of the nginx image:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: deployment-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.22
        name: nginx
        ports:
        - containerPort: 80
          name: nginx

It’s important to note that the name of the Deployment and Container is nginx.

Now, let’s use the apply command to create the nginx pod:

$ kubectl apply -f deployment-demo.yaml
deployment.apps/nginx created

Finally, let’s verify that the pod has been created in a deployment-demo namespace and it’s in a running state:

$ kubectl get pods -n deployment-demo      
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7c8cbb65f7-jwjqg   1/1     Running   0          118s

Now, the required setup is ready. So, let’s see how to update the container image of this Deployment object.

3. Using the set Operation

We can use the set operation with the kubectl command to update the image of the nginx deployment.

To understand this, let’s modify the Deployment object to use the 1.23 tag of the nginx image:

$ kubectl set image deployment/nginx nginx=nginx:1.23 -n deployment-demo
deployment.apps/nginx image updated

In this example, the nginx represents the container name, whereas the nginx:1.23 represents the container image and its tag.

Now, let’s verify that the pod is in a healthy state and that it’s using the 1.23 tag of the nginx image:

$ kubectl get pods -n deployment-demo
NAME                     READY   STATUS    RESTARTS   AGE
nginx-578b68d5d6-dtt72   1/1     Running   0          23s

$ kubectl get deploy -o wide -n deployment-demo
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES       SELECTOR
nginx   1/1     1            1           2d20h   nginx        nginx:1.23   app=nginx

Here, we’ve used the wide output format to show the additional details of the deployment. In the above output, the second to last column shows the image used by the container.

4. Using the patch Operation

In addition to this, we can also use the patch operation with the kubectl command to update the image of the Deployment object.

We can specify the patch configuration from the command line or via the patch file. The patch file is somewhat similar to the declarative configuration, but it contains only partial configuration.

For example, in the declarative configuration, the container details are present under the spec.template.spec.containers section. So, let’s create a YAML file with this section and save it as patch-demo.yaml file:

spec:
  template:
    spec:
      containers:
      - image: nginx:1.24
        name: nginx

In this configuration, we are using the nginx image with a 1.24 tag.

Next, let’s use the –-patch-file option to update the Deployment object:

$ kubectl patch deployment/nginx --patch-file patch-demo.yaml -n deployment-demo
deployment.apps/nginx patched

Finally, let’s verify the state of the pod and the image used by the container:

$ kubectl get pods -n deployment-demo
NAME                     READY   STATUS    RESTARTS   AGE
nginx-56df444f8c-gbxc8   1/1     Running   0          22s

$ kubectl get deploy -o wide -n deployment-demo                                       
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES       SELECTOR
nginx   1/1     1            1           2d20h   nginx        nginx:1.24   app=nginx

Here, we can observe that the container is using the 1.24 tag of the nginx image.

5. Using the edit Operation

In the previous example, we discussed how to use the patch file to update the Deployment object. It’s a best practice to use the configuration files to update the Kubernetes objects. One of the main reasons for this is we can always version control these files to track the changes.

However, sometimes, we want to make throwaway changes to try something out. In such cases, we can use the edit operation with the kubectl command:

$ kubectl edit deployment nginx -n deployment-demo

This command opens the YAML configuration of the nginx deployment in the default editor. After this point, we can make the required changes, save the file, and exit from the editor. In our case, we’ll modify the spec.template.spec.containers section to use the 1.25 tag of the nginx image.

Now, let’s exit from the editor after saving the file and verify that the container is using the specified image:

$ kubectl get pods -n deployment-demo
NAME                     READY   STATUS    RESTARTS   AGE
nginx-74d46ddcd8-qxrlc   1/1     Running   0          26s

$ kubectl get deploy -o wide -n deployment-demo         
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES       SELECTOR
nginx   1/1     1            1           2d21h   nginx        nginx:1.25   app=nginx

6. Cleaning Up

It’s always recommended to remove the temporarily created objects. This helps us to get rid of unwanted objects and allows the better utilization of the hardware resources.

So, let’s perform the delete operation to remove the Deployment and Namespace objects that we created while setting up our example:

$ kubectl delete deploy nginx -n deployment-demo
deployment.apps "nginx" deleted

$ kubectl delete namespace deployment-demo 
namespace "deployment-demo" deleted

7. Conclusion

In this article, we discussed how to edit a Kubernetes Deployment object on the fly without using the original declarative configuration file.

First, we discussed how to specify the container image inline using the set operation.

Next, we saw how to perform the same operation using the patch file.

Finally, we discussed the usage of the edit operation that provides the fastest and easiest way to modify multiple fields at once.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.