James111
James111

Reputation: 15903

How can we setup kubernetes to automatically change containers when a new one is pushed?

I'm using google cloud to store my Docker images & host my kubernetes cluster. I'm wondering how I can have kubernetes pull down the image which has the latest tag each time a new one is pushed.

I thought imagePullPolicy was the way to go, but it doesn't seem to be doing the job (I may be missing something). Here is my container spec:

"name": "blah",
"image": "gcr.io/project-id/container-name:latest",
"imagePullPolicy": "Always",
"env": [...]

At the moment I'm having to delete and recreate the deployments when I upload a new docker image.

Upvotes: 2

Views: 542

Answers (2)

hakik ayoub
hakik ayoub

Reputation: 65

Actually, you can patch your deployment so it can re-pull the spec part of your manifest (put your deployment name there)

kubectl patch deployment YOUR-DEPLOYMENT-NAME -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}"

Now, every time you push a new version in your container registry (DockerHub,ECR...), go to your kubernetes CLI and :

kubectl rollout restart deployment/YOUR-DEPLOYMENT-NAME

Upvotes: 0

Kubernetes it self will never trigger on container image update in repository. You need some sort of CI/CD pipeline in your tooling. Furthermore, I do strongly advise to avoid using :latest as it makes your container change over time. It is much better in my opinion to use some sort of versioning. Be it semantic like image:1.4.3 commit based image:<gitsha> or as I use image:<gitsha>-<pushid> where push is a sequentially updated value for each push to repo (so that label changes even if I reupload from the same build).

With such versioning, if you change image in your manifest, the deployment will get a rolling update as expected.

If you want to stick to image:latest, you can add a label with version to your pod template, so if you bump it, it will roll. You can also just kill pods manually one by one, or (if you can afford downtime) you can scale deployment to 0 replicas and back to N

Upvotes: 3

Related Questions