Kubernetes Deployment Image Update Strategies and Practical Guide

Nov 20, 2025 · Programming · 29 views · 7.8

Keywords: Kubernetes | Image_Update | Deployment | kubectl | Rolling_Update

Abstract: This article provides an in-depth exploration of various methods for updating container images in Kubernetes Deployments, focusing on kubectl set image command, imagePullPolicy configuration, and techniques for triggering rolling updates through environment variables and labels. With detailed code examples, it covers best practices for seamless image updates in both development and production environments, including Jenkins automation integration and manual update techniques.

Introduction

Managing containerized applications in Kubernetes clusters often involves frequent image updates, particularly during development cycles. In continuous integration and deployment (CI/CD) pipelines, efficiently and reliably updating Deployment images while maintaining service availability presents significant challenges for developers. This article systematically examines multiple image update strategies based on community practices and official documentation, helping readers select the most appropriate approach for their specific scenarios.

Core Concepts and Basic Configuration

Kubernetes Deployment, as the primary resource for managing application replicas, supports declarative update strategies. By default, Kubernetes does not pull new images when image tags remain unchanged. Therefore, proper container specification configuration is essential for implementing image updates.

The following basic Deployment configuration example demonstrates key parameter settings:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo
  template:
    metadata:
      labels:
        app: demo
    spec:
      terminationGracePeriodSeconds: 30
      containers:
      - name: mycontainer
        image: myimage:latest
        imagePullPolicy: Always

In this configuration, imagePullPolicy: Always ensures that the latest image version is pulled from the registry every time a Pod is created. Combined with terminationGracePeriodSeconds: 30, this provides sufficient graceful termination time for containers, preventing service interruptions.

Updating Images with kubectl set image Command

The kubectl set image command is Kubernetes' officially recommended method for image updates, supporting various resource types including Deployment, ReplicaSet, and StatefulSet. This command directly modifies running resource objects, triggering the rolling update process.

Basic syntax:

kubectl set image deployment/<deployment-name> <container-name>=<image>:<tag>

For example, to update the image for container mycontainer in Deployment my-deployment to myimage:1.9.1:

kubectl set image deployment/my-deployment mycontainer=myimage:1.9.1

For images using the latest tag, execute the following command sequence to force an update:

kubectl set image deployment/my-deployment mycontainer=myimage:latest
kubectl set image deployment/my-deployment mycontainer=myimage

This operation triggers Kubernetes' rolling update mechanism, gradually replacing old Pods while ensuring service continuity.

Automated Build and Image Tagging Strategies

In CI/CD pipelines, automated builds and image tagging are crucial for efficiency improvements. For example, when using Jenkins, unique image tags can be generated using build numbers and Git commit hashes:

kubectl set image deployment/my-deployment mycontainer=myimage:"${BUILD_NUMBER}-${SHORT_GIT_COMMIT}"

This approach ensures each build has a unique image version, facilitating tracking and rollback capabilities. Combined with imagePullPolicy: Always, it guarantees the use of the most recently built image.

Triggering Updates Through Environment Variables

Another common update strategy involves modifying environment variables to trigger Deployment rolling updates. Kubernetes detects changes in Pod templates and automatically creates new Pods.

Example configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: demo
    spec:
      containers:
      - name: demo
        image: registry.example.com/apps/demo:master
        imagePullPolicy: Always
        env:
        - name: FOR_GODS_SAKE_PLEASE_REDEPLOY
          value: 'THIS_STRING_IS_REPLACED_DURING_BUILD'

During the build process, use scripts to replace environment variable values:

sed -ie "s/THIS_STRING_IS_REPLACED_DURING_BUILD/$(date)/g" deployment.yml
kubectl apply -f deployment.yml

Generating different timestamps during each build ensures Pod template changes, thereby triggering updates.

Modifying Deployment Parameters with kubectl patch

For scenarios requiring fine-grained control over the update process, the kubectl patch command provides more flexible solutions. By modifying specific Deployment fields, rolling updates can be indirectly triggered.

For example, modifying terminationGracePeriodSeconds:

kubectl patch deployment your_deployment -p \
  '{"spec":{"template":{"spec":{"terminationGracePeriodSeconds":31}}}}'

Or triggering updates through label modifications:

kubectl patch deployment web -p \
  "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"`date +'%s'`\"}}}}}"

These methods maintain the same image name while triggering updates through modifications to other fields.

kubectl rollout restart Command

Starting from Kubernetes 1.15, the kubectl rollout restart command was introduced specifically for restarting Deployments and triggering rolling updates:

kubectl rollout restart deployment/demo

This command gradually restarts all Pods using the latest configured images, making it an ideal choice for updating images with latest tags.

Best Practices and Considerations

In practical applications, it's recommended to select appropriate update strategies based on specific requirements:

Additionally, pay attention to image registry access permissions and network policies to ensure Kubernetes clusters can successfully pull new images.

Conclusion

Kubernetes offers multiple flexible image update mechanisms, ranging from simple kubectl set image commands to complex automated pipeline integrations. Understanding the principles and applicable scenarios of these methods helps build stable and efficient containerized application deployment workflows. By appropriately combining these techniques, developers can achieve seamless image updates across different environments, enhancing both development and operational efficiency.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.