Updating Container Images
Image Update Methods
1. Using kubectl set image
The simplest way to update a container image in a Deployment is using the kubectl set image
command:
## Basic syntax
kubectl set image deployment/[deployment-name] [container-name]=[new-image]
## Example
kubectl set image deployment/nginx-deployment nginx=nginx:1.19.10
2. Editing Deployment Directly
Update the Deployment configuration using the kubectl edit
command:
## Open deployment in default editor
kubectl edit deployment nginx-deployment
3. Applying Updated YAML File
Modify the YAML file and apply changes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19.10 ## Updated image version
ports:
- containerPort: 80
## Apply updated configuration
kubectl apply -f nginx-deployment.yaml
Image Update Strategies
graph TD
A[Image Update Strategies] --> B[Rolling Update]
A --> C[Recreate]
A --> D[Blue-Green Deployment]
A --> E[Canary Deployment]
Rolling Update Strategy
Strategy Characteristic |
Description |
Zero Downtime |
Gradually replaces pods |
Controlled Rollout |
Manages pod replacement |
Rollback Capability |
Easy to revert changes |
Configuration Example
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
Verification Commands
## Check rollout status
kubectl rollout status deployment/nginx-deployment
## View deployment history
kubectl rollout history deployment/nginx-deployment
## Rollback to previous version
kubectl rollout undo deployment/nginx-deployment
Best Practices
- Use specific image tags
- Implement health checks
- Monitor deployment progress
- Use consistent naming conventions
Common Pitfalls
- Using
latest
tag unpredictably
- Not specifying resource limits
- Ignoring image pull policies
Image Pull Policies
containers:
- name: nginx
image: nginx:1.19.10
imagePullPolicy: Always ## IfNotPresent, Never
Practical Considerations
- Ensure image availability
- Consider network bandwidth
- Validate image compatibility
- Test thoroughly before production deployment
LabEx recommends practicing image update techniques in controlled environments to build confidence and expertise in Kubernetes deployment management.