In our project, which also uses Kustomize, our base deployment.yaml
file looks like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:IMAGE_TAG # <------------------------------
ports:
- containerPort: 80
Then we use sed
to replace IMAGE_TAG
with the version of the image we want to deploy.
Is there a more sophisticated way to do this, rather than editing the text yaml file using sed
?
3
Answers
There is a specific transformer for this called the images transformer.
You can keep your deployment as it is, with or without tag:
and then in your kustomization file:
Do keep in mind that this will replace the tag of all the nginx images of all the resources included in your kustomization file. If you need to run multiple versions of nginx you can replace the image name in your deployment by a placeholder and have different entries in the transformer.
It is possible to use an image tag from an environment variable, without having to edit files for each different tag. This is useful if your image tag needs to vary without changing version-controlled files.
Standard
kubectl
is enough for this purpose. In short, use aconfigMapGenerator
with data populated from environment variables. Then addreplacements
that refer to this ConfigMap data to replace relevant image tags.Example
Continuing with your example
deployment.yaml
, you could have akustomization.yaml
file in the same folder that looks like so:In the same folder, you need a file
.env
with the environment variable name only (note: just the name, no value assigned):Now
MY_IMAGE_TAG
from the local environment is integrated as the image tag when runningkubectl kustomize
,kubectl apply --kustomize
, etc.Demo:
This prints the generated image tag, which is
foobar
as desired:Alternatives
Keep the following in mind from the
configMapGenerator
documentation:If you are simply looking to share a fixed image tag between multiple files, see the already suggested
images
transformer.@evolutics answer is probably "proper" way to do it but if you are customizing only image tag every deployment you could consider putting $IMAGE_TAG variable in
deployment.yaml
and usingenvsubst
command.Example: