In Kubernetes, the image pull policy is a configuration option that determines how the system should handle pulling container images. There are three possible values for the image pull policy:
Always: The system will always pull the latest version of the image, regardless of whether or not an image with the same tag is already present in the local cache.
Never: The system will never pull the image, and will only use the version that is already present in the local cache.
IfNotPresent: The system will only pull the image if it is not already present in the local cache.
The default value for the image pull policy is
IfNotPresent. However, it can be set to
Never by specifying the
imagePullPolicy field in the pod or deployment yaml file.
For example, to set the image pull policy to Always for a deployment, you would add the following to your deployment yaml file:
spec: template: spec: containers: - name: my-container imagePullPolicy: Always
Please note that the
Always policy can create unnecessary traffic and delay on your network, so it’s not recommended to use it unless you have a specific use case.
To force Kubernetes to re-pull an image, you can update the image tag in the deployment or pod definition, or set
Always, and then running a rolling update. You can use the
kubectl set image command to update the image in a deployment or pod, and then use the
kubectl rollout restart command to perform a rolling update.
For example, if you want to update the image for a deployment named “my-deployment” to version “2.0”, you can use the following commands:
kubectl set image deployment/my-deployment my-container=my-image:2.0
kubectl rollout restart deployment my-deployment
Alternatively, if you want to update image for a pod named “my-pod” you can use the following command:
kubectl set image pod/my-pod my-container=my-image:2.0
Please note that you can use the
kubectl edit command to edit a pod or deployment yaml file, but this option would be less recommendable since it could be prone to errors.