Introduction

When deploying applications using Helm, it's common to need environment variables for configuration. However, hardcoding sensitive information like usernames and passwords in your deployment.yaml file can pose security risks. This guide explains how to pull these values from your local environment when running Helm commands.

Step-by-Step Guide

1. Modify Your Helm Chart

First, ensure your Helm chart is set up to accept environment variables. You can do this by defining them in your values.yaml file. Here’s an example:

# values.yaml
env:
  USERNAME: ""
  PASSWORD: ""

2. Update the Deployment Template

Next, you need to modify your deployment.yaml file located in the templates directory of your Helm chart. Replace the hardcoded values with references to the values defined in values.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
        - name: sample-app
          image: "sample-app:latest"
          imagePullPolicy: Always
          env:
            - name: USERNAME
              value: {{ .Values.env.USERNAME }}
            - name: PASSWORD
              value: {{ .Values.env.PASSWORD }}

3. Set Local Environment Variables

Before running your Helm command, export the environment variables in your terminal. This allows Helm to access them during deployment:

export USERNAME=your-username
export PASSWORD=your-password

4. Install the Helm Chart

Now, you can install your Helm chart while passing the environment variables. Use the --set flag to specify the values:

helm install my-release ./my-chart --set env.USERNAME=$USERNAME --set env.PASSWORD=$PASSWORD

Conclusion

By following these steps, you can securely manage sensitive information in your Helm deployments without exposing it in your configuration files. This approach enhances the security of your applications while leveraging the flexibility of Helm.