Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass environment variable when use `kubectl apply -f file.yaml`

Tags:

kubernetes

Can I pass environment variable when use kubectl apply -f file.yaml like kubectl apply -f file.yaml -env HOST=dev.api.host.com?

Because I have a file yaml, and I need to run in two pipelines, in one case to use host for production and in another case to use host for developlemnt.

I don't want to use two different files, I want to use one file, where I'll replace the host.

Is it possible?

like image 325
Rui Martins Avatar asked Dec 10 '22 02:12

Rui Martins


2 Answers

This is how you should update environment variables in deployment yaml.

export TAG="1.11.1"
export NAME=my-nginx
envsubst < deployment.yaml | kubectl apply -f -
like image 137
P Ekambaram Avatar answered Jan 04 '23 02:01

P Ekambaram


I'd recommend a different solution:

In Kubernetes you can use ConfigMaps and Secrets to store your configuration.

There are multiple ways how to do it. You could for example split development and production in 2 namespaces, if they should run in one cluster. Then you can put a ConfigMap with your host configuration in each namespace. When you deploy the same deployment to different namespaces, they'll find their appropriate ConfigMap and load it.


If you want to go the way of replacing it, a simple search-and-replace in your file would work.

In your file.yml specify an environment variable like this

    env:
    - name: HOST
      value: %%HOST%%

And in your pipeline replace it with the appropriate value

sed -i -e "s#%%HOST%%#http://whatever#" file.yml;
like image 27
Pampy Avatar answered Jan 04 '23 01:01

Pampy