How to update deployment image on Azure Kubernetes Service with same image tag via an Azure pipeline?

11/1/2019

I am trying to update my deployment with latest image content on Azure Kubernetes Service every time some code is committed to github . I have made a stage in my build pipeline to build and push the image on docker hub which is working perfectly fine. however in my release pipeline the image is being used as an artifact and is being deployed to the Azure Kubernetes Service , but the problem is that the image on AKS in not updating according to the image pushed on Docker Hub with latest code.

Right now each time some commit happens i have to manually update the image on AKS via the Command

kubectl set image deployment/demo-microservice demo-microservice=customerandcontact:contact

My Yaml File

enter image description here

Can anyone tell the error/changes if any in my yaml file to automatically update the image on AKS.

-- Shubham Tiwari
azure
azure-kubernetes
continuous-deployment
docker
kubernetes

2 Answers

11/3/2019

When you relese a new image to the container registry under the same tag it does not mean anything to Kubernetes. If you run kubectl apply -f ... and the image name and tag remains the same, it still won't do anything as there is no configuration change. There are two options:

  1. Give a new tag on each build and change the :contact to the new tag in the yaml and run kubectl apply

  2. For dev environment only (do not do it in Stage or Prod) leave the same tag (usually a tag :latest is used) and after a new image is deployed to registry run kubectl delete pod demo-microservice. Since you've set image pull policy to Always, this will cause Kubernetes pull a new image from the registry and redeploy the pod.

The second approach is a workaround just for testing.

-- Piotr Gwiazda
Source: StackOverflow

11/3/2019

When you specify your image with the specific image tag Kubernetes will default container's imagePullPolicy to IfNotPresent, which means that image won't be pulled again, and previously pulled image will be deployed.

Kubernetes will change policy to Always only if tag is not present (which is effectively same as latest or if tag is set to latest explicitly.

Check what is actual imagePull policy on your Deployment template for particular container.

kubectl get pod demo-microservice -o yaml | grep imagePullPolicy -A 1

Try patching deployment

kubectl patch deployment demo-microservice -p 
 '{"spec": { "template" : 
  { "spec" : { "containers" : 
    [{"name" : "demo-microservice", 
    "image" : "repo/image:tag", 
    "imagePullPolicy": "Always" }]}}}}'

Make sure that imagePullPolicy for the container in question is set to Always.

-- fg78nc
Source: StackOverflow