How to pull environment variables with Helm charts - kubernetes

To pull environment variables with Helm charts in Kubernetes, we can use ConfigMaps and Secrets. ConfigMaps help us manage non-sensitive data. Secrets are for sensitive information. This way, we can keep our environment variables safe. By using these Kubernetes features in our Helm charts, we can make application setup easier and improve security.

In this article, we will look at different ways to pull environment variables using Helm charts in Kubernetes. We will talk about using ConfigMaps and Secrets for managing our environment variables. We will also see how to define them in the values.yaml file and use the env template function. Moreover, we will learn how to customize environment variables with Helm chart hooks. We will answer some common questions to help clear up any confusion.

  • How can we pull environment variables with Helm charts in Kubernetes?
  • How do we use ConfigMaps to manage environment variables in Helm charts?
  • How can we use Secrets for pulling sensitive environment variables with Helm charts?
  • How do we define environment variables in values.yaml for Helm charts?
  • How do we use the env template function in Helm charts to pull environment variables?
  • How can we customize environment variables using Helm chart hooks?
  • Frequently Asked Questions

How to use ConfigMaps to manage environment variables in Helm charts?

ConfigMaps in Kubernetes help us manage environment variables and settings apart from our application code. When we use Helm charts, we can easily add ConfigMaps to put environment variables into our pods.

To use ConfigMaps in our Helm charts, we can follow these steps:

  1. Create a ConfigMap Template: First, we need to create a configmap.yaml file in the templates folder of our Helm chart.
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "your-chart.fullname" . }}-config
data:
  MY_ENV_VAR: {{ .Values.myEnvVar | quote }}
  1. Define Values in values.yaml: Next, we write the values for our environment variables in the values.yaml file.
myEnvVar: "some_value"
  1. Update Deployment to Use ConfigMap: Then, we change our deployment template to use the ConfigMap as environment variables.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "your-chart.fullname" . }}
spec:
  template:
    spec:
      containers:
        - name: your-container
          image: "your-image:latest"
          env:
            - name: MY_ENV_VAR
              valueFrom:
                configMapKeyRef:
                  name: {{ include "your-chart.fullname" . }}-config
                  key: MY_ENV_VAR
  1. Deploy the Helm Chart: Finally, we use Helm to install or upgrade our chart.
helm install your-release-name ./your-chart

This will create a ConfigMap with our chosen environment variables. It will put them into our application container. This way, we can manage settings separately and update them without changing our application code. For more details on managing settings in Kubernetes, check this guide on ConfigMaps.

How to use Secrets for pulling sensitive environment variables with Helm charts?

In Kubernetes, we need to keep sensitive information like passwords, OAuth tokens, and ssh keys safe. We can do this by storing them in Secrets. Helm charts can get these Secrets to fill environment variables securely.

To use Secrets in a Helm chart, we can follow these steps:

  1. Create a Secret:

    We can create a Secret using a YAML file or the command line. Here is an example to create a Secret using a YAML file:

    apiVersion: v1
    kind: Secret
    metadata:
      name: my-secret
    type: Opaque
    data:
      my-password: bXlwYXNzd29yZA==  # This is the Base64 encoded value of 'mypassword'

    We apply this YAML file with:

    kubectl apply -f secret.yaml
  2. Access the Secret in Helm Chart:

    In our Helm chart’s deployment template, we can use the Secret to set environment variables. Here is an example:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: my-app
      template:
        metadata:
          labels:
            app: my-app
        spec:
          containers:
            - name: my-app-container
              image: my-app-image:latest
              env:
                - name: MY_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: my-secret
                      key: my-password
  3. Using the Secret in values.yaml:

    To make our Helm chart more flexible, we can define the Secret name and key in the values.yaml file:

    secret:
      name: my-secret
      key: my-password

    Then, we update our deployment template to use these values:

    env:
      - name: MY_PASSWORD
        valueFrom:
          secretKeyRef:
            name: {{ .Values.secret.name }}
            key: {{ .Values.secret.key }}
  4. Deploy the Helm Chart:

    We install or upgrade our Helm chart using:

    helm install my-release ./my-chart

    or

    helm upgrade my-release ./my-chart

Using Secrets in Helm charts helps us manage sensitive environment variables safely. This way, our applications can access the needed credentials without putting them directly in the source code. For more information on using Secrets in Kubernetes, we can check this link: how do I manage secrets in Kubernetes securely.

How to define environment variables in values.yaml for Helm charts?

To define environment variables in values.yaml for Helm charts, we can put them in a custom section. This section will be used in our deployment template. This way, we can manage the settings separately from the code. It makes it easier to change the variables without changing the chart itself.

Here is an example of how to set up your values.yaml file to add environment variables:

# values.yaml
env:
  MY_ENV_VAR: "value1"
  ANOTHER_ENV_VAR: "value2"

In our deployment template (like deployment.yaml), we can then use these environment variables like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mychart.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    spec:
      containers:
        - name: myapp
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          env:
            - name: MY_ENV_VAR
              value: {{ .Values.env.MY_ENV_VAR | quote }}
            - name: ANOTHER_ENV_VAR
              value: {{ .Values.env.ANOTHER_ENV_VAR | quote }}

This example shows how we can define environment variables in values.yaml and use them in our Helm chart templates. Using this method, we can easily change the values in values.yaml without changing the deployment settings directly.

For more information on managing application settings in Kubernetes using ConfigMaps, you can check how to use ConfigMaps to manage application configuration in Kubernetes.

How to use the env template function in Helm charts to pull environment variables?

In Helm charts, we use the env template function to get environment variables from the Kubernetes environment. This helps our application to set itself up based on the environment it runs in.

We can use the env function in our templates like this:

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
    - name: example-container
      image: your-image:latest
      env:
        - name: EXAMPLE_ENV
          value: {{ .Values.exampleEnv | quote }}

In this example, we define an environment variable called EXAMPLE_ENV. It gets its value from the values.yaml file by using {{ .Values.exampleEnv }}. The quote function makes sure the value has quotes around it.

To set this variable in our values.yaml, we add:

exampleEnv: "Hello, World!"

When we deploy our Helm chart, this environment variable will be ready for our application.

We can also use the lookup function with env to access existing Kubernetes environment variables. For example, if we want to get the namespace of the pod:

env:
  - name: POD_NAMESPACE
    value: {{ .Release.Namespace | quote }}

This helps our application know what namespace it runs in. This can be very important for applications that serve multiple users.

For more details on using environment variables in Helm, like getting values from ConfigMaps or Secrets, we can check out other articles. These include how to use ConfigMaps to manage application configuration in Kubernetes and how to use Kubernetes secrets to store database credentials.

How to customize environment variables using Helm chart hooks?

Helm chart hooks help us run actions at certain times during the release process. We can change environment variables when these events happen using hooks. Here is how we can do it well.

Defining Hooks in Helm Charts

To change environment variables, we need to define a hook in our Helm chart. We define hooks in the metadata part of our Kubernetes resources. Here is an example of using a hook to set environment variables during the pre-install or post-install phase.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  annotations:
    "helm.sh/hook": post-install
data:
  ENV_VAR_ONE: "value1"
  ENV_VAR_TWO: "value2"

Using Hooks to Set Environment Variables

  1. Pre-install Hook: This runs before we install our release.
  2. Post-install Hook: This runs after we finish the installation.
  3. Delete Hook: This runs when we delete a release.

Example with Deployment

To change environment variables in a deployment using a hook, we can create a ConfigMap and use it in our Deployment manifest. Here is an example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-image:latest
        env:
        - name: ENV_VAR_ONE
          valueFrom:
            configMapKeyRef:
              name: my-config
              key: ENV_VAR_ONE
        - name: ENV_VAR_TWO
          valueFrom:
            configMapKeyRef:
              name: my-config
              key: ENV_VAR_TWO

Deploying with Hooks

When we deploy our Helm chart, the hooks run in the order we set. This sets our environment variables based on the stage of the lifecycle:

helm install my-release ./my-chart

By using Helm chart hooks, we can manage environment variables that fit our deployment needs. This gives us flexibility and control in our Kubernetes apps. For more information on managing application settings, we can read how to use ConfigMaps to manage application configuration in Kubernetes.

Frequently Asked Questions

1. What are environment variables in Kubernetes, and why are they important?

Environment variables in Kubernetes are key-value pairs. We use them to set up applications that run in containers. They let us pass important info like database connection strings or API keys when the app is running. Using environment variables helps us manage app settings in different environments like development, testing, and production. This way, we do not have to hardcode values in the app code. It makes our apps more flexible and safe.

2. How do I create and use ConfigMaps in Helm charts?

ConfigMaps in Helm charts help us manage environment variables and configuration settings. We can create a ConfigMap by writing it in our values.yaml file. Then we can reference it in our deployment templates. This way, we can separate configuration from our app code. It makes it easier to update without needing to redeploy. For more details, check our article on how to use ConfigMaps to manage application configuration in Kubernetes.

3. How can I store sensitive information with Kubernetes Secrets?

Kubernetes Secrets help us store sensitive info like passwords and API tokens safely. We can create Secrets in our Helm chart and reference them in our pod specs. This keeps sensitive data away from our app code and logs. For good practices on managing Secrets, see our guide on how do I manage secrets in Kubernetes securely.

4. How can I pass environment variables from values.yaml in Helm charts?

We can define environment variables in our values.yaml file by making a special section for them. Each variable can be referenced in our deployment templates using the Helm templating syntax. This makes it easy for us to customize and manage configuration data. For a step-by-step guide, see our article on how do I create and manage Helm charts.

5. What is the purpose of the env template function in Helm charts?

The env template function in Helm charts helps us pull environment variables into our Kubernetes manifests. This function lets us reference values from values.yaml or environment variables from our CI/CD pipeline. This way, our application can adjust to different environments without problems. For more info on using Helm well, read our article on what is Helm and how does it help with Kubernetes deployments.

These FAQs give us basic knowledge on using environment variables with Helm charts in Kubernetes. This can help us make our deployments more efficient and safe.