Generate Kubernetes Manifests with Helm Charts using Kustomize

Tharuka Mannapperuma
4 min readSep 10, 2023

In the world of Kubernetes, there’s a common question: “Should I pick Helm or Kustomize?” 🤔 It’s like choosing between two powerful tools. Sometimes, Helm is your best buddy, and at other times, Kustomize is your go-to pal. But not many folks talk about using them together. 😮

So, let’s keep things simple. We’ll explore Helm and Kustomize separately and then look at when it’s a smart move to use both of them at the same time. It’s all about making Kubernetes a breeze, one step at a time. 🌟

Prerequisite

  1. Install Helm 3: Ensure that Helm 3 is installed on your system. You can find installation instructions for Helm 3 on the official Helm website: Helm 3 Installation.
  2. Install Kustomize: Make sure Kustomize is installed. You can obtain Kustomize installation instructions from the official GitHub repository: Kustomize Installation.
  3. Chart URL: Identify or provide the URL of the Helm chart you intend to use. In this example, I will use MergeStat Helm Chart https://helm.mergestat.com/

Having these prerequisites in place will set the stage for effectively managing your Kubernetes configurations using Helm, Kustomize, or a combination of both.

🔮 Harnessing the Power of Helm Charts

📦 Helm Charts provide an efficient way to package, share, and deploy Kubernetes applications. However, they often come with default configurations that may not perfectly match your requirements. 🧩 This is where Kustomize steps in, offering the flexibility to fine-tune Helm Charts to fit your specific needs. 🛠️

🧰 Customizing Helm Charts with Kustomize

Now, let’s dive into the process of using Kustomize to augment your Helm Charts. You can provide a values.yaml file to Helm, which contains configuration settings tailored to your environment. This allows you to override default values defined in the Helm Chart templates. First let's see how we can build a kustomization file to generate Kubernetes manifests for our Helm chart

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

helmCharts:
- name: mergestat
repo: https://helm.mergestat.com/
releaseName: mergestat
namespace: admin
version: 0.1.0
valuesFile: values.yaml
# Example values.yaml file
worker:
replicaCount: 1
image:
repository: mergestat/worker
pullPolicy: Always
env:
- name: POSTGRES_CONNECTION
valueFrom:
secretKeyRef:
name: mergestat-postgresql
key: postgresql-uri
- name: CONCURRENCY
value: "3"
- name: ENCRYPTION_SECRET
valueFrom:
secretKeyRef:
name: mergestat-secrets
key: encryption-secret

graphql:
replicaCount: 1
image:
repository: mergestat/graphql
pullPolicy: Always
env:
- name: POSTGRES_CONNECTION
valueFrom:
secretKeyRef:
name: mergestat-postgresql
key: postgresql-uri
- name: JWT_SECRET
valueFrom:
secretKeyRef:
name: mergestat-secrets
key: jwt-secret
- name: ENCRYPTION_SECRET
valueFrom:
secretKeyRef:
name: mergestat-secrets
key: encryption-secret

ui:
replicaCount: 1
image:
repository: mergestat/console
pullPolicy: Always
env:
- name: POSTGRAPHILE_API
value: http://mergestat-graphql:5433/graphql
- name: POSTGRES_CONNECTION
valueFrom:
secretKeyRef:
name: mergestat-postgresql
key: postgresql-uri
- name: JWT_SECRET
valueFrom:
secretKeyRef:
name: mergestat-secrets
key: jwt-secret

postgresql:
# https://github.com/bitnami/charts/tree/master/bitnami/postgresql
enabled: false

You can use the following link as a reference to see what are the supported fields in the helmCharts field: Link

Incorporating Kustomize patches further enhances your customization capabilities. You can apply Kustomize patches to modify Helm Chart templates selectively. These patches can target specific resources within the Helm Chart and make adjustments without altering the original Chart files.

Let's add a nodeAffinity patch to the kustomization file as well.

# Patch File for nodeAffinity
apiVersion: apps/v1
kind: Deployment
metadata:
name: node_affinity_deployment
spec:
template:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: <key from node labels>
operator: In
values:
- <value to match>
# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

helmCharts:
- name: mergestat
repo: https://helm.mergestat.com/
releaseName: mergestat
namespace: admin
version: 0.1.0
valuesFile: values.yaml

patches:
- path: patches/node_affinity_deployment.yaml
target:
kind: Deployment

After configuring your needs you can use the below commands to inflate your helm charts to Kubernetes manifest files

mkdir build
kustomize build -o build --enable-helm

In the above commands, you can see the --enable-helm flag which tells kustomize to use the helm client. This command will download the Helm chart to your local machine before generating Kubernetes manifests.

build manifest files

🚀Conclusion

As we conclude this exploration, we encourage you to embrace this powerful synergy. Dive into your Kubernetes configuration journey with the confidence that Helm and Kustomize, when used together, can help you navigate the complexities of orchestration, ensuring your applications run seamlessly in your Kubernetes cluster. 💪

Stay tuned for more insights, tips, and best practices! 🚀📚

Reference

  1. https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/helmcharts/
  2. https://kubectl.docs.kubernetes.io/references/kustomize/builtins/#_helmchartinflationgenerator_
  3. https://helm.sh/docs/intro/quickstart/
  4. https://kubectl.docs.kubernetes.io/installation/kustomize/

--

--

Tharuka Mannapperuma

DevOps Engineer at MyBudget, Cloud and Web development enthusiast. Studied at ENTC , University of Moratuwa