Rohith
Rohith

Reputation: 356

Kubernates pass env variable to "kubectl create"

I need to pass dynamic env variable to kubectl create. Something like this

kubectl create -f app.yaml --Target=prod

Based on Target code deploys on different servers.

Upvotes: 6

Views: 5378

Answers (4)

Chakradar Raju
Chakradar Raju

Reputation: 2811

I've published a command-line tool ysed that also performs what you need.

Upvotes: 0

Rohith
Rohith

Reputation: 356

If you want to avoid installing 3rd party plugin then you can replace the text using sed "s/orginal/change/". It worked. I used this in Jenkins shell.

cat app.yaml | sed "s/l3-apps/l2-apps/" | kubectl create -f -

Upvotes: 8

Alex Lurye
Alex Lurye

Reputation: 571

kubectl config set-context allows you to configure cluster, namespace, user credentials and more and save it as a "context" in your ~/.kube/config.

The you can use --context option of kubectl exactly in a way that you used --Target in your example.

Upvotes: 1

embik
embik

Reputation: 1070

You can achieve this in two ways:

  1. Use Helm. It is a "package manager" for Kubernetes and is built exactly for your use case (dynamic variables to configure behaviour of your resources). If it is only a single variable, "converting" your deployment is as simple as creating a new Helm chart, copy your files into templates/, modify values.yaml and use {{ .Values.target }} in your templates. See the quickstart guide for a more in-depth introduction to Helm.

  2. If you consider Helm to be over the top for a single variable, use kubectl's capability to read from standard input. You'll need an additional templating tool (for example mustache). Rewrite your deployment to fit your templating tool. Create a dynamic data.yml in your deployment process (e.g. a simple bash script that reads from environment variables) and run something like mustache data.yml deployment.mustache | kubectl apply -f -.

Upvotes: 4

Related Questions