Pull environment variables to the Helm charts
This tutorial guides you on how to pull environment variables to the Helm charts. Helm is a kubernetes package manager and helm charts are the best way to find, share and use software built for kubernetes.
Pull environment variables to the Helm charts
Let’s learn how to include environment variables in your kubernetes deployment YAML configurations.
Before you begin, make sure that you have
- Access to the terminal/ command line
- Created kubernetes cluster
- Installed and configured helm.
Let’s say you wanted to pull the value of username and password from the local environment variables when you run helm install command. Follow the step by step instructions below to use environment variables with helm charts.
1: Value Files / Values is one of the built-in objects in Helm. This object can be used to provide access to environment values passed in to the chart. To pass this values file in to helm install you need to use -f flag and the command should look like helm install -f values.yaml ./mychart
To pass the individual parameters you need to use –set flag ( helm install –set username=$USERNAME ./mychart)
Let’s add the following lines for username and password to the values.yaml file in your helm chart.
values.yaml
username: user password: password
2: If you don’t wanted to expose the username and password data, then create a new file called secret.yaml under templates folder and add the following contents in that YAML file.
secret.yaml
apiVersion: v1 kind: Secret metadata: name: {{ .Release.Name }}-auth data: password: {{ .Values.password | b64enc }} username: {{ .Values.username | b64enc }}
3: After that, modify your deployment YAML configuration file in the env section as shown below.
deployment.yaml
... ... containers: - name: my-app image: "my-app:latest" imagePullPolicy: Always env: - name: "USERNAME" valueFrom: secretKeyRef: key: username name: {{ .Release.Name }}-auth - name: "PASSWORD" valueFrom: secretKeyRef: key: password name: {{ .Release.Name }}-auth ... ...
4: After modifying the templates, you need to set the environment variables with the desired values using export as shown below.
export USERNAME=normal-user
5: Finally, to use or pull the environment variable to the helm chart, you need to run helm install command with –set flag so that you can pass individual parameter to the helm chart.
$ helm install --set username=$USERNAME ./mychart
If you wanted to run this in dry-run mode without actually installing the helm resource, then run the following command.
$ helm install --dry-run --set username=$USERNAME --debug ./mychart
Example: Use environment variables for helm chart
For example,
$ helm install --dry-run --set username=$USERNAME --debug demo /home/sneppets/demo/hello-helm/k8s/demo/ install.go:173: [debug] Original chart version: "" install.go:190: [debug] CHART PATH: /home/nithip/demo/hello-helm/k8s/demo NAME: demo LAST DEPLOYED: Thu Apr 8 06:22:00 2021 NAMESPACE: default STATUS: pending-install REVISION: 1 TEST SUITE: None USER-SUPPLIED VALUES: username: normal-user COMPUTED VALUES: container: image: cloudnatived/demo name: demo port: 8888 tag: hello environment: development password: password replicas: 1 username: normal-user HOOKS: MANIFEST: --- # Source: demo/templates/secret.yaml apiVersion: v1 kind: Secret metadata: name: demo-auth data: password: cGFzc3dvcmQ= username: bm9ybWFsLXVzZXI= --- # Source: demo/templates/service.yaml apiVersion: v1 kind: Service metadata: name: demo-service labels: app: demo spec: ports: - port: 80 protocol: TCP targetPort: 8888 selector: app: demo type: LoadBalancer --- # Source: demo/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: demo spec: replicas: 1 selector: matchLabels: app: demo template: metadata: labels: app: demo environment: development spec: containers: - name: demo image: cloudnatived/demo:hello ports: - containerPort: 8888 env: - name: environment value: development - name: "USERNAME" valueFrom: secretKeyRef: key: username name: demo-auth - name: "PASSWORD" valueFrom: secretKeyRef: key: password name: demo-auth
From the above response you can see that, username has been set to “normal-user” under USER–SUPPLIED VALUES and COMPUTED VALUES. Also check the username and password values being pulled from Values file to the helm chart.
That’s it you had learnt how to use or pull environment variables forhelm chart.
Hope it helped 🙂
You’ll also like:
- List all versions of helm chart available using helm search repo
- Can I use multiple values.yaml files for Helm Chart ?
- Get Docker Container’s IP Address from the Host
- Copy Files between Host and Docker Container
- What is ImagePullBackOff status on a Kubernetes pod ?
- Run ionic app in android studio emulator
- List all Containers running in Kubernetes Pod
- How to find which users belongs to a specific group in linux
- Delete or uninstall specific helm chart resource
- Error: helm install unknown flag: –name
- Build a Docker Image with a Dockerfile and Cloud Build in GCP?
- How to create GCP project on Google Cloud Platform
- no matches for kind “Deployment” in version “extensions/v1beta1”
- Helm 3 – Not a valid chart repository or cannot be reached: failed to fetch : 404 Not Found
- Is it possible to change Google Cloud Platform Project ID ?
- Purpose of –purge flag in helm delete command ?
- Make manual payment or pay early for your Google Cloud Platform charges
- Open specific port such as 8082 in Google Compute Engine
- Delete or unset clusters contexts and users entries from kubectl config