How to deploy new Docker images to your Kubernetes cluster? Of course, there are many ways and one of the most common ways is to use kubectl set image, for example:
However, what happens if you have more containers in your deployment pod spec? And what if you also have initContainers? Fortunately, kubectl patch can help. Let’s assume we have two containers and one init container. Here is the patch file (patch.yaml) we can use:
We can simply apply this by the following command:
And voilà, all images in the deployment are updated at once.
Of course, you may ask what happens when image tags are variable? Of course, this information may differ in each deployment. You can use git tags or maybe git hashes for this, but how to pass it to your patch file. We can simply replace tags with variables and then use sed to replace these dummy values with the real ones. Let’s consider this template:
Now let’s use sed to replace these variables with the real values:
And we are done, now just apply kubectl patch the same way as above.