1

I have a kubernetes deployment that looks like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: datacollector
spec:
  replicas: 1
  selector:
    matchLabels:
      pod-type: datacollector
  template:
    metadata:
      labels:
        pod-type: datacollector
    spec:
      imagePullSecrets:
      - name: secret
      containers:
        - name: datacollector
          image: "build.dorangg.dev/doranggdatacollector:latest"
          imagePullPolicy: Always

Currently I run the following command whenever I want to update the deployment to use the newest version of the image from the docker registry (build.dorangg.dev) :

kubectl rollout restart deployment/datacollector

I want to initiate a rolling update of the deployment (what the above command does) AUTOMATICALLY whenever a new version of the image appears in the docker registry.

Can this be accomplished using the kubernetes configuration? If not, is this automation possible to do with a script?

domisum
  • 111
  • 2

3 Answers3

0

The better way to do this is to use kubectl set image to tell the deployment there is a new image, but it won't work if you are using :latest because the deployment won't trigger if the tag doesn't change. It's bad idea using :latest in general anyway, so you would have to move to semversioning of the tags for your containers.

Juan Jimenez
  • 717
  • 1
  • 6
  • 12
0

Can this be accomplished using the kubernetes configuration?

I don't know.

If not, is this automation possible to do with a script?

Yes. You do a docker pull on the image. If docker pulls a new image then you delete the POD with kubectl and then it gets redeployed.

0

Honestly I don't think it can be configured inside the Kubernetes. It will need additional scripts/triggers.

Options comes to my mind, but you need to "catch" new of the new image with script (for example name of new image after successfull push to repository).

- Patch deployment

$ kubectl patch deployment [your-deployment] -p \
'{"spec":{"template":{"spec":{"image":"[new-image-name:tag]"}}}}'

Kubernetes if will see some changes it should redeploy whole deployment.

- Create script with set new image

$ kubectl set image deployment/[deployment-name] [image-name]=[new-image-name:tag]

After this change kubernetes should also redeploy all pods.

- Use HELM

As helm using Charts and Templates you can create script which will change image name in values.yaml and redeploy it.

- Patch GracePeriod

If you want to keep the same image name all the time, you can try to patch GracePeriod. It was described here.

Hope it will help.

PjoterS
  • 615
  • 3
  • 11