kubernetes helm example

Can I use multiple values.yaml files for Helm Chart ?

This tutorial guides you on how to use multiple values.yaml files for helm chart. In the previous tutorial we have learnt about values.yaml which is one of the built-in object used to access values passed in to the chart. In this tutorial let’s see how to create multiple values.yaml file and use it for helm chart.

Can I use multiple values.yaml files for Helm Chart ?

Yes, we can create multiple values.yaml file for: let’s say different environments as shown in the example below. Helm by default will use the values.yaml file only in the root directory of your chart.

sneppets@cloudshell:~/demo/hello-helm/k8s/demo $ ls

Chart.yaml  production-values.yaml  staging-values.yaml  templates  values.yaml

A values file can be passed in to helm install or helm upgrade command using -f flag as shown below.

$ helm install -f values.yaml ./mychart

For instance, if you wanted to have different settings based on the different environments (production / staging), then you can ask helm to load values from the specific values file as shown below.

$ helm install -f production-values.yaml ./mychart

Example : Use different values.yaml files

The below helm install commands will use the values from default values.yaml file if not specified. Otherwise will use the specified one.

For example,

values.yaml

username: user
password: password
environment: development
container:
  name: demo
  port: 8888
  image: cloudnatived/demo
  tag: hello
replicas: 1

Set the environment variables with the desired values using export as shown below.

$ export USERNAME=normal-user

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.

sneppets@cloudshell:~/demo/hello-helm/k8s/demo/templates (sne21)$ ls

deployment.yaml  secret.yaml  service.yaml

secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-auth
data:
  password: {{ .Values.password | b64enc }}
  username: {{ .Values.username | b64enc }}

Below is the contents of the deployment YAML file.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.container.name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.container.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.container.name }}
        environment: {{ .Values.environment }}
    spec:
      containers:
        - name: {{ .Values.container.name }}
          image: {{ .Values.container.image }}:{{ .Values.container.tag }}
          ports:
            - containerPort: {{ .Values.container.port }}
          env:
            - name: environment
              value: {{ .Values.environment }}
            - name: "USERNAME"
              valueFrom:
                secretKeyRef:
                  key:  username
                  name: {{ .Release.Name }}-auth
            - name: "PASSWORD"
              valueFrom:
                secretKeyRef:
                  key:  password
                  name: {{ .Release.Name }}-auth

helm install command : using values.yaml

To run in dry-run mode with values.yaml without actually installing the helm resource, you can run the following command. Note, you no need to mention the default Values using -f flag in the command.

$ 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 12:15:14 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

Therefore, from the above response (environment: development) it is evident that it is using default values.yaml.

helm install command : using production-values.yaml

Similarly, you can try to use different yaml file (i.e., production-values.yaml) to use different settings as shown below.

$ helm install --dry-run --set username=$USERNAME -f production-values.yaml --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 12:33:07 2021 NAMESPACE: default STATUS: pending-install REVISION: 1 TEST SUITE: None USER-SUPPLIED VALUES: environment: production username: normal-user COMPUTED VALUES: container: image: cloudnatived/demo name: demo port: 8888 tag: hello environment: production password: password replicas: 1 username: normal-user ----- ----- -------

From the above response, it is evident that the helm install command is using values from the production-values.yaml file for the helm chart resource.

That’s it. You had learnt how to use multiple values.yaml file for helm charts.

Hope it helped 🙂

You’ll also like:

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments