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?
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 -
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With