0

Kubernetes only seems to pull images when the pod is created, how can I make it re-pull the latest image when the container itself exits?

I'd want the same effect as kubectl rollout restart deployment, but when the container exits. For example, if the container is a shell script, and it does exit 1, Kubernetes should pull the latest image from registry before restarting.

My deployment has only 1 container if that matters.

  • Does this answer your question? [How do I force Kubernetes to re-pull an image?](https://stackoverflow.com/questions/33112789/how-do-i-force-kubernetes-to-re-pull-an-image) – loic.lopez Feb 27 '21 at 11:02

1 Answers1

1

You can use imagePullPolicy, read more over here.

Below is an example for your handy reference:

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx-container-image-repull
spec:
  containers:
  - name: nginx-alpine-container-1
    image: nginx:alpine
    ports:
      - containerPort: 80
    imagePullPolicy: Always

UPDATE 1:

When you use restartPolicy: Never then it means that you are telling Kubernetes that I do not want my container to restart in case it crashes. So, you are experiencing that there is no image re-pull in case container "restart" because there was actually no container restart.

I had quickly copied an example from my workspace so you saw restartPolicy: Never in my example which was not meant for you, I have updated my above example to remove it. But I hope you have understood one more concept with it.

Another thing worth noting is that restartPolicy applies to all containers in a Pod while imagePullPolicy can set at container level in Pod.

hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • With `restartPolicy: Never` the pod is stuck at the "Completed" status if the container dies and a new pod is never created. I already tried `imagePullPolicy: Always`, and it only works with `kubectl rollout restart deployment`, not if the container restarts. – throwaway91234 Feb 27 '21 at 16:33
  • @throwaway91234 I have updated my answer, please check and let me know in case of any question. – hagrawal7777 Feb 27 '21 at 17:01