Kubernetes
Deployment
Update Image
DevOps
Container Management

Kubernetes how to make Deployment to update image

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding Kubernetes Deployments and Image Updates

Kubernetes is a powerful orchestrator for deploying, managing, and scaling containerized applications. One of the core components in Kubernetes is the Deployment object, which provides declarative updates to applications. This article explores how to update the container image of a Kubernetes Deployment, offering a technical understanding enhanced with practical examples.

What is a Kubernetes Deployment?

A Kubernetes Deployment is a resource object that defines the desired state for application scaling, updates, and rollbacks in a cluster environment. Deployments manage the scheduling of Pods that run application containers. By declaring a Deployment, you can:

  • Automatically roll out changes to configurations (such as image updates) at a controlled rate.
  • Roll back to an earlier deployment in case of failures.
  • Scale up or down the number of Pod replicas.
  • Pause or resume the deployment of changes.

Updating a Deployment's Image

Updating a container image in a Kubernetes Deployment can be done efficiently using the kubectl command-line tool or by modifying the deployment YAML file.

Using kubectl set image

  1. Get current Deployments: First, check your existing deployments with:
bash
   kubectl get deployments
  1. Update the image: Use the kubectl set image command to update the image. For example, to update the nginx image to version 1.19 in a deployment named my-deployment:
bash
   kubectl set image deployment/my-deployment nginx=nginx:1.19

This command updates the existing Pods with the new image, and Kubernetes' built-in mechanisms control the rollout process to maintain application availability.

  1. Verify the update: Check the status of your deployment to ensure the rollout has started and completed:
bash
   kubectl rollout status deployment/my-deployment

Rolling Back Changes

If something goes wrong, Kubernetes allows you to roll back to the previous version easily:

bash
kubectl rollout undo deployment/my-deployment

Modifying the Deployment YAML

Alternatively, you can directly edit the Deployment's YAML configuration:

  1. Edit the Deployment:
    Fetch and open the YAML file for editing:
bash
   kubectl edit deployment/my-deployment
  1. Update the image version: Locate the containers section and modify the image field:
yaml
1   spec:
2     containers:
3     - name: nginx
4       image: nginx:1.19
  1. Apply changes: Save the file. Kubernetes automatically detects changes and begins the update process.

Key Points to Remember

AspectDescriptionExample Command/Action
What is a Deployment?Manages scaling, updates, and rollbacks of a set of Podskubectl get deployments
Updating an imageModify the Deployment to use a new image versionkubectl set image deployment/my-deployment nginx=nginx:1.19
Rollout statusEnsures the update completes successfullykubectl rollout status deployment/my-deployment
Rolling backReverts to previous image if neededkubectl rollout undo deployment/my-deployment
Manual YAML changesEdit YAML directly for more complex updateskubectl edit deployment/my-deployment

Additional Considerations

  • Readiness Probes: Ensure you have readiness probes configured in your Pods. They help Kubernetes decide when a Pod is ready to receive traffic.
  • Rolling Updates: Kubernetes expands or shrinks the set of Pods according to the configuration. Define parameters like maxSurge and maxUnavailable for a smooth rollout.
  • Version Control: Always version-control your deployment manifests. This practice eases collaboration and rollback operations.
  • CI/CD Integration: Automating image updates with a Continuous Integration/Continuous Deployment (CI/CD) pipeline can accelerate and secure release processes. Tools like Jenkins, GitLab CI, and Argo CD work well with Kubernetes for this purpose.

Conclusion

Updating a container image within a Kubernetes Deployment is fundamental for maintaining and improving applications in production. With command-line tools, YAML editing, and rollbacks, Kubernetes provides powerful capabilities to manage changes safely and efficiently. A strategic approach, combined with Kubernetes resources and proper CI/CD integration, ensures your applications remain robust and up-to-date.


Course illustration
Course illustration

All Rights Reserved.