Helm Chart Use Environment Variables In Values.Yaml
When deploying applications using Helm, environment variables play a crucial role in customizing the deployment for different environments without changing the underlying code. In this article, we will discuss how to use environment variables in the values.yaml file of a Helm chart.
What is a Helm Chart?
A Helm Chart is a package of pre-configured Kubernetes resources that can be used to deploy an application. It contains a set of templates for Kubernetes manifests, along with default values for the configuration parameters. Helm Charts simplify the deployment process by enabling developers to package all the required resources in a single file.
What are Environment Variables?
Environment variables are dynamic values that can be passed to an application at runtime. They enable developers to customize the application for different environments without changing the code. For example, a developer can set a different database URL for the application in the development, staging, and production environments using environment variables.
Using Environment Variables in values.yaml
The values.yaml file in a Helm Chart contains default values for the configuration parameters. To use environment variables in the values.yaml file, we need to define them in the format {{ getenv "ENV_VAR_NAME" }}
.
For example, consider a values.yaml file that contains the following configuration:
replicaCount: 1image:repository: myapptag: latest
To use an environment variable for the image tag, we can modify the values.yaml file as follows:
replicaCount: 1image:repository: myapptag: {{ getenv "IMAGE_TAG" }}
Here, the {{ getenv "IMAGE_TAG" }}
expression will resolve to the value of the environment variable IMAGE_TAG at runtime.
Defining Environment Variables in Kubernetes
To define environment variables in Kubernetes, we can use the env field in the container specification of a Pod or Deployment. For example, consider the following Pod definition:
apiVersion: v1kind: Podmetadata:name: mypodspec:containers:- name: mycontainerimage: myapp:latestenv:- name: DB_URLvalue: mysql://localhost/mydb
Here, we have defined an environment variable DB_URL with the value mysql://localhost/mydb for the container named mycontainer.
Using Environment Variables in Helm Templates
In addition to the values.yaml file, we can also use environment variables in Helm templates to customize the deployment. To use an environment variable in a Helm template, we can use the {{ getenv "ENV_VAR_NAME" }}
expression.
For example, consider a Helm template that deploys a ConfigMap:
apiVersion: v1kind: ConfigMapmetadata:name: myconfigmapdata:config.ini: |[database]url={{ getenv "DB_URL" }}
Here, the {{ getenv "DB_URL" }}
expression will resolve to the value of the environment variable DB_URL at runtime.
Conclusion
Using environment variables in a Helm chart enables developers to customize the deployment for different environments without changing the underlying code. By defining environment variables in Kubernetes and using them in the values.yaml file and Helm templates, developers can easily deploy applications in a consistent and scalable manner.