How can I make Kubernetes do an image pull every time a container restarts?

2/26/2021

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.

-- throwaway91234
kubernetes

1 Answer

2/27/2021

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.

-- hagrawal
Source: StackOverflow