Learnitweb

Updating the Image in a Kubernetes Deployment

When managing applications in Kubernetes, you may need to update the container image in a deployment to apply new changes or fixes. This tutorial covers different methods to update the image in a Kubernetes deployment.

Update the Image Using kubectl set image

Kubernetes provides a simple way to update the image of a running deployment without modifying the YAML file.

Command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl set image deployment/<deployment-name> <container-name>=<new-image>:<tag>
kubectl set image deployment/<deployment-name> <container-name>=<new-image>:<tag>
kubectl set image deployment/<deployment-name> <container-name>=<new-image>:<tag>

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl set image deployment/nginx-deployment nginx-container=nginx:1.21
kubectl set image deployment/nginx-deployment nginx-container=nginx:1.21
kubectl set image deployment/nginx-deployment nginx-container=nginx:1.21

This updates the nginx-container inside the nginx-deployment to use the nginx:1.21 image.

Check the rollout status:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl rollout status deployment/nginx-deployment
kubectl rollout status deployment/nginx-deployment
kubectl rollout status deployment/nginx-deployment

Rollback in case of failure:

If something goes wrong, you can roll back to the previous version:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl rollout undo deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment

Update the Image by Modifying the Deployment YAML File

If you prefer to update the image in the deployment configuration file, follow these steps:

  1. Edit the Deployment YAML File (deployment.yaml)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
template:
spec:
containers:
- name: nginx-container
image: nginx:1.21 # Update the image here
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: template: spec: containers: - name: nginx-container image: nginx:1.21 # Update the image here
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  template:
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.21  # Update the image here

2. Apply the Changes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl apply -f deployment.yaml
kubectl apply -f deployment.yaml
kubectl apply -f deployment.yaml

Force a Deployment Update Without Changing the YAML

If you’re using an image tag like latest and need Kubernetes to pull the new version, you can force a restart using the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl patch deployment <deployment-name> -p \
'{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"'$(date +%Y-%m-%dT%H:%M:%S%z)'"}}}}}'
kubectl patch deployment <deployment-name> -p \ '{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"'$(date +%Y-%m-%dT%H:%M:%S%z)'"}}}}}'
kubectl patch deployment <deployment-name> -p \
  '{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"'$(date +%Y-%m-%dT%H:%M:%S%z)'"}}}}}'

This triggers a new rollout without modifying the YAML file.

Verify the Update

After updating the image, ensure the pods are running the new version:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl get pods
kubectl describe pod <pod-name>
kubectl get pods kubectl describe pod <pod-name>
kubectl get pods
kubectl describe pod <pod-name>

You can also check the logs to confirm the updated container is working correctly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl logs -f <pod-name>
kubectl logs -f <pod-name>
kubectl logs -f <pod-name>