How to read environment variables in kubernetes yaml file? for example, I want to change the docker image tag but do not want to rewrite the file, like this
apiVersion: v1
kind: ReplicationController
...
spec:
containers:
- name: myapp
image: myapp:${VERSION}
...
With this I can do kubectl rolling-update
without updating the yaml file.
thanks
Helm should solve your config issues - https://github.com/kubernetes/helm
If you want a simple, lightweight approch you might try using envsubst. So assuming your example is in a file called example.yaml
in a bash shell you'd execute:
export VERSION=69
envsubst < example.yaml | kubectl apply -f -
I would highly recommend using HELM. https://github.com/kubernetes/helm
You can install HELM using the information contained in the above link. That will make the helm
command available to you.
By running helm create YOUR_APP_NAME
it will create a directory structure like the following.
YOUR_APP_NAME/
Chart.yaml # A YAML file containing information about the chart
LICENSE # OPTIONAL: A plain text file containing the license for the chart
README.md # OPTIONAL: A human-readable README file
values.yaml # The default configuration values for this chart
charts/ # OPTIONAL: A directory containing any charts upon which this chart depends.
templates/ # OPTIONAL: A directory of templates that, when combined with values,
# will generate valid Kubernetes manifest files.
templates/NOTES.txt # OPTIONAL: A plain text file containing short usage notes
In the values.yaml
file you can set some ENV variables like:
container:
name: "nginx"
version: "latest"
In your ReplicationController
file you can reference the variables using:
apiVersion: v1
kind: ReplicationController
...
spec:
containers:
- name: myapp
image: {{.Values.container.name}}:{{.Values.container.version}}
...
The YAML file for your replication controller should be placed in the templates directory.
You can then run the command helm package YOUR_PACKAGE_NAME
. To install the package on your K8S cluster you can then run helm install PACKAGE_NAME
NOTE: I would suggest you switch to using Deployments
instead of ReplicationController
. See: https://kubernetes.io/docs/user-guide/deployments/
You should use a Deployment
coupled with kubectl set image
like this:
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
Maybe you mean this?
- name: PUBLIC_URL
value: "http://gitserver.$(POD_NAMESPACE):$(SERVICE_PORT)"
This is something what their docs specified.. but it doesn't work for me anymore.