Manage Deployment in multiple Kubernetes environments with HELM

Manage Deployment in multiple Kubernetes environments with HELM

What is HELM?

Helm is a package manager for Kubernetes that helps you deploy Kubernetes applications.

For more details check out the HELM website - https://helm.sh/

In this article, I am trying to cover the strategy to manage Helm Charts for deploying applications in multiple environments (i.e. Kubernetes Clusters - DEV , QA and so on.)

Each kubernetes environment requires handling the below aspects.

  • Environment specific constants like urls etc. - mostly non secret values (Kubernetes Configmap)
  • Environment specific secret values (base64 encoded) like db username and passwords etc. (Kubernetes Secrets)
  • Updating kubernetes deployments (pods) when configmap / secrets gets updated.

Here’s , the project structure at a glance

No alt text provided for this image

FYI: This is a sample project that’s meant to give you an understanding of how this works. Feel free to make changes as needed for your application. To learn more about Helm please refer the official Helm documentation

The sub folders ( dev and qa ) under environment folder is where we’ll need to put all our respective environment specific files.

Mainly, we will have 2 types of files inside these folder. These are the configmap files and secrets files.

Helm requires values YAML file to be supplied for the helm install/upgrade command.

We will maintain the values YAML files at the root folder; namely - values-<env>.yaml (For instance: values-dev.yaml and values-qa.yaml in the above case)

All environment specific values other than configmap and secrets should reside in these values-<env>.yaml. For instance: Image name, tag, namespace etc. or any other user defined value. 

Once all the configmap, secrets are updated in the environment specific folders, What’s Next?

They won’t be directly applied to the cluster. So you need to carry out the following steps -

Configmap: Create the configmap.yaml file under the templates directory and paste the below content.

apiVersion: v1
kind: ConfigMap
metadata:
 name: helm-multi-env-configmap
 namespace: {{ .Values.namespace }}
data:
{{ printf "environment/%s/configmap.yaml" .Values.global.env | .Files.Get | indent 2 }}        

Note:

  • %s will be replaced by the value which is defined in the values YAML file.

No alt text provided for this image

  • Make sure the value matches with the folder name corresponding to the environment in which you want to deploy. Example: dev or qa in this case. 

Secrets: Create the secret.yaml file under the templates directory and paste the below content.

apiVersion: v1
kind: Secret
metadata:
  name: helm-multi-env-secrets
  namespace: {{ .Values.namespace }}
type: Opaque
data:
{{ printf "environment/%s/secret.yaml" .Values.global.env | .Files.Get | indent 2 }}        

Once you’ve created both these files under templates, both the kubernetes configmap and secrets files will be deployed on the cluster when you execute helm install/upgrade command.

How you can update all your corresponding deployments?

Add both the annotations mentioned below to your deployments and whenever the configmap or secrets files are updated, your pods will be restarted.

If any file changes, kubernetes will try to redeploy all respective deployments which are configured with below annotations at once.

apiVersion: apps/v1
kind: Deployment
...
spec:
  template:
    metadata:
      annotations:
        checksum/configmap1: {{ printf "environment/%s/configmap.yaml" .Values.global.env | .Files.Get | sha256sum }}
        checksum/secret1: {{ printf "environment/%s/secret.yaml" .Values.global.env | .Files.Get | sha256sum }}        

Some useful Helm Commands

helm lint

helm lint <your_chart_path> -f values-<env>.yaml

Example: helm lint . -f values-dev.yaml 
        

Note: . indicates command is executed from the root directory of my helm chart

helm template

helm template <your_release_name> <your_chart_path> -f values-<env>.yaml

Example: helm template myapp . -f values-dev.yaml
        

helm install

helm install <your_release_name> <your_chart_path> -f values-<env>.yaml -n <namespace>

Example: helm install myapp . -f values-dev.yaml -n dev        

Sample GitHub Repository structure

No alt text provided for this image

Hope this article, helps you understand how this Helm strategy can be used to work with multiple environment deployments.

Cheers,

Rohan Shetty

To view or add a comment, sign in

More articles by Rohan Shetty

Insights from the community

Others also viewed

Explore topics