How to make my kubernetes to take my latest docker image?

2/3/2020

Problem

A frequent question that comes up on Slack and Stack Overflow is how to trigger an update to a Deployment/RS/RC when the image tag hasn't changed but the underlying image has.

Consider:

  1. There is an existing Deployment with image foo: latest

  2. The user builds a new image foo: latest

  3. The user pushes foo: latest to their registry

  4. The user wants to do something here to tell the Deployment to pull the new image and do a rolling-update of existing pods

For Example:

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: worker-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: worker
  template:
    metadata:
      labels:
        component: worker
    spec:
      containers:
        - name: worker
          image: stephengrider/multi-worker
          env:
            - name: REDIS_HOST
              value: redis-cluster-ip-service
            - name: REDIS_PORT
              value: "6379"

And now I have made changes in my multiWorker image

and redeploys the deployment, but it doesn't fetch the latest image?

--
kubernetes

1 Answer

2/3/2020

The default pull policy is IfNotPresent which causes the Kubelet to skip pulling an image if it already exists. Refer Link

If you would like to always force a pull, you can do one of the following:

1: set the imagePullPolicy of the container to Always.

2: omit the imagePullPolicy and use :latest as the tag for the image to use.

3: omit the imagePullPolicy and the tag for the image to use.

4: enable the AlwaysPullImages admission controller.

Note that you should avoid using :latest tag, we should avoid using the :latest tag when deploying containers in production as it is harder to track which version of the image is running and more difficult to roll back properly.

Various imagePullPolicy and the tag of the image affect when the kubelet attempts to pull the specified image listed below. Refer Link for Details

1) imagePullPolicy: IfNotPresent: the image is pulled only if it is not already present locally.

2) imagePullPolicy: Always: the image is pulled every time the pod is started.

3) imagePullPolicy is omitted and either the image tag is :latest or it is omitted: Always is applied.

4) imagePullPolicy is omitted and the image tag is present but not :latest: IfNotPresent is applied.

5) imagePullPolicy: Never: the image is assumed to exist locally. No attempt is made to pull the image.

-- DT.
Source: StackOverflow