22

I have a Kubernetes deployment which uses image: test:latest (not real image name but it's the latest tag). This image is on docker hub. I have just pushed a new version of test:latest to dockerhub. I was expecting a new deployment of my pod in Kubernetes but nothing happends.

I've created my deployment like this:

kubectl run sample-app --image=`test:latest` --namespace=sample-app --image-pull-policy Always

Why isn't there a new deployment triggered after the push of a new image?

DenCowboy
  • 13,884
  • 38
  • 114
  • 210

3 Answers3

27

Kubernetes is not watching for a new version of the image. The image pull policy specifies how to acquire the image to run the container. Always means it will try to pull a new version each time it's starting a container. To see the update you'd need to delete the Pod (not the Deployment) - the newly created Pod will run the new image.

There is no direct way to have Kubernetes automatically update running containers with new images. This would be part of a continuous delivery system (perhaps using kubectl set image with the new sha256sum or an image tag - but not latest).

Janos Lenart
  • 25,074
  • 5
  • 73
  • 75
  • Okay, I didn't work a lot with Kubernetes but I did with OpenShift where it was possible. – DenCowboy Aug 27 '17 at 15:44
  • Yes, Kubernetes is missing some of the bells and whistles OpenShift has. – Janos Lenart Aug 27 '17 at 16:11
  • 1
    @JanosLenart, if you have an image with two tags : v1(old tag), and v2(new tag) and in the kubernetes config file it is set to pull v1, Does "image-pull-policy: Always" forces the kubernetes to take v2 instead of v1 image? – Benjamin Apr 26 '18 at 11:07
  • If you change the image name in a podtemplate (in a Deployment or a StatefulSet) that will always trigger a rollout regardless of the `imagePullPolicy` setting. If you set `imagePullPolicy: Always` that forces the Node to pull the image even if it already has an image by that name. By *name* I mean the complete image name, including the repository and the tag (:v1, :v2). A good reading on the topic is https://stackoverflow.com/questions/33112789/how-do-i-force-kubernetes-to-re-pull-an-image and https://kubernetes.io/docs/concepts/configuration/overview/#container-images – Janos Lenart Apr 26 '18 at 13:34
9

One way to force the update to happen is to run this in your CI script (after pushing the new image and with image-pull-policy set to Always in the applied yaml):

kubectl rollout restart deployment/<name> --namespace=<namespace>

In Azure Devops enter "rollout" as the command, use the namespace feature above and put "restart ..." in the parameters field.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • this is the correct way I think , some case we need trigering kubectl to do rollback cause change on image example on automate and CI CD pipeline. – viyancs Jan 06 '22 at 13:48
3

If you are working with yml files, executing deployment with

kubectl apply -f myfile.yml

and

imagePullPolicy: Always

on your file, k8s will not pull a new image. You will first need to delete the pod, and the K8s deployment will automatically pull the image.

yaach
  • 386
  • 2
  • 3