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
- Get current Deployments: First, check your existing deployments with:
- Update the image: Use the
kubectl set imagecommand to update the image. For example, to update thenginximage to version1.19in a deployment namedmy-deployment:
This command updates the existing Pods with the new image, and Kubernetes' built-in mechanisms control the rollout process to maintain application availability.
- Verify the update: Check the status of your deployment to ensure the rollout has started and completed:
Rolling Back Changes
If something goes wrong, Kubernetes allows you to roll back to the previous version easily:
Modifying the Deployment YAML
Alternatively, you can directly edit the Deployment's YAML configuration:
- Edit the Deployment:Fetch and open the YAML file for editing:
- Update the image version: Locate the
containerssection and modify theimagefield:
- Apply changes: Save the file. Kubernetes automatically detects changes and begins the update process.
Key Points to Remember
| Aspect | Description | Example Command/Action |
| What is a Deployment? | Manages scaling, updates, and rollbacks of a set of Pods | kubectl get deployments |
| Updating an image | Modify the Deployment to use a new image version | kubectl set image deployment/my-deployment nginx=nginx:1.19 |
| Rollout status | Ensures the update completes successfully | kubectl rollout status deployment/my-deployment |
| Rolling back | Reverts to previous image if needed | kubectl rollout undo deployment/my-deployment |
| Manual YAML changes | Edit YAML directly for more complex updates | kubectl 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
maxSurgeandmaxUnavailablefor 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.

