How to do variable substitution in Kubernetes manifests?

10/20/2018

I am learning Kubernetes and planning to do continuous deployment of my apps with Kubernetes manifests.

I'd like to have my app defined as a Deployment and a Service in a manifest, and have my CD system run kubectl apply -f on the manifest file.

However, our current setup is to tag our Docker images with the SHA of the git commit for that version of the app.

Is there a Kubernetes-native way to express the image tag as a variable, and have the CD system set that variable to the correct git SHA?

-- David Ham
kubernetes

1 Answer

10/21/2018

You should consider helm charts in this case, where you separate between the skeleton of templates (or what you called maniest) and its values which are changed from release to another.

In templates/deployment.yaml :

spec:
  containers:
    - name: {{ template "nginx.name" . }}
      image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"

And in values.yaml :

image:
  repository: nginx
  tag: 1.11.0

See the full example here

-- Abdennour TOUMI
Source: StackOverflow