Skip to content

Instantly share code, notes, and snippets.

@pratikmallya
Last active January 24, 2018 06:28
Show Gist options
  • Save pratikmallya/7041c770575385848668c5c054b1fb45 to your computer and use it in GitHub Desktop.
Save pratikmallya/7041c770575385848668c5c054b1fb45 to your computer and use it in GitHub Desktop.
Using BASH environment variables in k8s templates

When deploying k8s resources from task runners (like Jenkins) its often convenient to use environment variables directly in the template, and then pre-process the template using envsubst before feeding it to kubectl apply.

That can be accomplished by the following one liner (assuming you have gettext GNU package installed, which provides envsubst):

envsubst < k8s_template.yml | kubectl apply -f -

example k8s_template.yml:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: $NAME
    image: nginx:1.7.9
    ports:
    - containerPort: 80

In the above template, the NAME environment variable can be exported beforehand, and will get replaced.

Acknowledgements:

Thanks to Chris and John for helping out with bash!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment