Skip to content
Kubernetes

Deploying Applications in Kubernetes Using Helm and Kustomize

k8shelmkustomize
On this page

How to Use Helm and Kustomize

Introduction to Helm

What Is Helm?

Helm is a package manager used in Kubernetes that simplifies the definition, installation, and upgrading of applications.

Basic Components of Helm

Chart: The Structure of a Helm Package

A Helm package is a set of files containing all the resources and configurations needed to run an application within a Kubernetes cluster. A chart consists of templates—composed of YAML files—and metadata.

Repository: Storing and Sharing Charts

A space for storing and sharing charts. Users can create their own charts and upload them to a repository, and can search for and use other users’ charts as needed.

Releases: Deployment Management

An instance of a chart once it has been installed in a cluster. A single chart can be installed multiple times, and each installation is managed as an independent release.

Helm Installation and Configuration

Installing Helm

Link to the Helm Installation Guide

Using Charts

Searching for and Adding Charts

Adding a Repo

    helm repo add [repo_name] [repo_url]
    helm repo update

Searching for Charts

helm search repo [keyword]

Installing, Upgrading, and Rolling Back Charts

Installation

   helm install [release_name] [chart_name]

Configuring Settings

   helm install [release_name] [chart_name] --values custom_values.yaml
   helm install [release_name] [chart_name] --set key=value

Upgrade and Rollback

   helm upgrade [release_name] [chart_name]
   helm rollback [release_name] [revision]

Deleting a Chart

   helm uninstall [release_name]

Customizing Chart Values

helm install [release_name] [chart_name] --values custom_values.yaml
helm install [release_name] [chart_name] --set key=value

Creating a Chart

Chart Generation and Packaging

helm create [chart_name] 

Chart Configuration:

  1. Chart.yaml Basic Information File: Contains the chart’s metadata, including the chart’s name, version, and description. This file defines the chart’s identity and is used to distinguish it from other charts.

  2. values.yaml Default Configuration File: Defines the default configuration values used when installing the chart. Users can override the values defined in this file to apply custom settings.

  3. templates/ Kubernetes Resource Templates: This directory contains the template files used to create the chart’s resources. Each file defines a Kubernetes API object and allows values to be dynamically injected using Helm’s template language.

  4. charts/ Dependent Charts: This directory contains charts that depend on other charts. It is primarily used to modularize and manage complex applications. For example, a web application chart might depend on a database chart.

  5. crds/ Custom Resource Definitions (CRDs): Contains the custom resource definitions used by the chart. The CRD files in this directory define resources that must be installed in the cluster before the chart is installed.

  6. .helmignore List of files to exclude from chart packaging: This works similarly to a .gitignore file and specifies files or directories that should be ignored when packaging the chart.

Dependency Management

List them in the `dependencies` field of the `Chart.yaml` file as shown below.

dependencies:
  - name: apache
    version: 1.2.3
    repository: https://example.com/charts
  - name: mysql
    version: 3.2.1
    repository: https://another.example.com/charts

Dependency management commands:

helm dependency list

helm dependency update

helm dependency build

Advanced Helm Features

Helm Hooks: Deployment Flow Control

Helm hooks provide the ability to execute custom tasks at specific points during the Helm chart lifecycle.

Annotation ValueDescriptionpre-installExecutes after templates are rendered, but before any resources are created in Kubernetespost-installExecutes after all resources are loaded into Kubernetespre-deleteExecutes on a deletion request before any resources are deleted from Kubernetespost-delete: Executes on a deletion request after all of the release’s resources have been deleted. pre-upgrade: Executes on an upgrade request after templates are rendered, but before any resources are updated. post-upgrade: Executes on an upgrade request after all resources have been upgraded. pre-rollback: Executes on a rollback request after templates are rendered, but before any resources are rolled back. post-rollback: Executes on a rollback request after all resources have been modified. test: Executes when the Helm test subcommand is invoked (view test docs)

Example:

apiVersion: batch/v1
kind: Job
metadata:
  name: ""
  labels:
    app.kubernetes.io/managed-by: 
    app.kubernetes.io/instance: 
    app.kubernetes.io/version: 
    helm.sh/chart: "-"
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-install
    "helm.sh/hook-weight": "-5"
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  template:
    metadata:
      name: ""
      labels:
        app.kubernetes.io/managed-by: 
        app.kubernetes.io/instance: 
        helm.sh/chart: "-"
    spec:
      restartPolicy: Never
      containers:
      - name: post-install-job
        image: "alpine:3.3"
        command: ["/bin/sleep",""]

Kustomize

The Concept of Kustomize

  • Kustomize is a tool for managing manifest files for Kubernetes resources.

  • It generates new or customized manifests by applying user-defined changes to the original manifest.

  • It allows you to configure the same application differently across multiple environments (such as development, staging, and production) while minimizing code duplication.

Deploying Applications in Kubernetes Using Helm and Kustomize article image 1

Key Components of Kustomize

kustomization.yaml file

  • The `kustomization.yaml` file instructs Kustomize on what to do. This file defines the resources to use, the patches to apply, and other configurations (e.g., changing namespaces, adding labels).

Definition of a transformation:

  • Transformer

    • Performs batch operations on multiple resources (namespace, label, annotation, prefix, etc.)

  • Patch

    • Specifies patch files to modify specific parts of a resource. Patches can be in strategic, JSON, or merge formats.

  • Generator

    • Enables the dynamic creation of ConfigMaps and Secrets.

The Concepts of Base and Overlay

Allows you to configure the same application differently for various environments (development, staging, production, etc.).

Deploying Applications in Kubernetes Using Helm and Kustomize article image 2

How to Use Kustomize

Output:

kubectl kustomize <kustomization_directory>

Applying the Results:

kubectl apply -k <kustomization_directory>

Kustomization Files

  • Resources: This section lists the base resource files that Kustomize will process or the paths to other kustomization directories. Resources are typically Kubernetes YAML definition files.

  • Patches: Specifies patch files to modify specific parts of a resource. Patches can be in the form of strategic, JSON, or merge patches. This allows you to modify existing resources to tailor them to a specific environment.

  • Generators: Enables the dynamic creation of ConfigMaps and Secrets. It retrieves data from literal values, file contents, or other sources and converts it into ConfigMap or Secret resources that can be used in Kubernetes.

  • Namespace: Specifies the namespace for all resources to which the configuration applies. This setting is applied uniformly to all resources and can override the namespace.

  • NamePrefix and NameSuffix: Adds a specific prefix or suffix to the beginning or end of all resource names, enabling more specific and organized resource management.

  • CommonLabels and CommonAnnotations: Add labels or annotations that apply to all resources. This is useful for grouping and managing resources, and helps identify and filter resources, especially in large-scale systems.

  • Images: You can change the name, tags, or digest of an image used within a resource. This allows you to use different versions of an image in different environments or easily switch image repositories.

  • Replicas: You can adjust the number of replicas for a specific resource. This is useful, for example, when configuring more replicas for a production environment and fewer replicas for a development environment.

# This is the root kustomization file, which ties everything together

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

# Resources to include
resources:
  - deployment.yaml
  - service.yaml

# Prefix added to all resource names
namePrefix: dev-

# Namespace in which resources will be deployed
namespace: my-app-namespace

# Common labels applied to all resources
commonLabels:
  app: my-app
  env: development

# Patches for environment-specific adjustments
patchesStrategicMerge:
  - patch_deployment.yaml
  - patch_service.yaml

# ConfigMap generator with literals
configMapGenerator:
  - name: my-app-config
    literals:
      - key1=value1
      - key2=value2

# Secret generator using literal values (secrets should be encrypted in real use)
secretGenerator:
  - name: my-app-secret
    literals:
      - username=admin
      - password=s3cr3t

Advanced Features

Transformers

Transformers allow you to apply changes across multiple fields or settings at once. This is useful for applying consistent updates or configurations to all resources in a Kustomization.

  • NamePrefix and NameSuffix Transformers: Add prefixes or suffixes to resource names to help distinguish between multiple instances of the same application within the same namespace.

  • LabelTransformer: Adds or updates labels on all resources in Kustomization. This is essential for organizing resources, enforcing policies, and controlling access.

  • AnnotationsTransformer: Similar to LabelTransformer but designed for annotations; it is used to attach metadata to resources that Kubernetes itself does not directly use.

  • ImageTransformer: Changes the image name, tag, or digest of any resource; this is particularly useful during the rollout of new image versions.

kustomization.yaml

resources:
  - deployment.yaml
  - service.yaml

transformers:
  - label_transformer.yaml

label_transformer.yaml

apiVersion: builtin
kind: LabelTransformer
metadata:
  name: add-env-label
labels:
  environment: dev
fieldSpecs:
  - path: metadata/labels
    create: true

Generators

ConfigMapGenerator

kustomization.yaml

generatorOptions:
  labels:
    fruit: apple

configMapGenerator:
- name: my-java-server-props
  behavior: merge
  files:
  - application.properties
  - more.properties
- name: my-java-server-env-vars
  literals: 
  - JAVA_HOME=/opt/java/jdk
  - JAVA_TOOL_OPTIONS=-agentlib:hprof
  options:
    disableNameSuffixHash: true
    labels:
      pet: dog
- name: dashboards
  files:
  - mydashboard.json
  options:
    annotations:
      dashboard: "1"
    labels:
      app.kubernetes.io/name: "app1"

SecretsGenerator

A Secret is generated for each item defined in the list.

secretGenerator:
- name: app-tls
  files:
  - secret/tls.cert
  - secret/tls.key
  type: "kubernetes.io/tls"
- name: app-tls-namespaced
  # you can define a namespace to generate
  # a secret in, defaults to: "default"
  namespace: apps
  files:
  - tls.crt=catsecret/tls.cert
  - tls.key=secret/tls.key
  type: "kubernetes.io/tls"
- name: env_file_secret
  envs:
  - env.txt
  type: Opaque
- name: secret-with-annotation
  files:
  - app-config.yaml
  type: Opaque
  options:
    annotations:
      app_config: "true"
    labels:
      app.kubernetes.io/name: "app2"

HelmChartInflationGenerator

  • helmCharts

  • helmGlobals

helmCharts:
- name: minecraft
  repo: https://kubernetes-charts.storage.googleapis.com
  version: v1.2.0
  releaseName: test
  namespace: testNamespace
  valuesFile: values.yaml
  additionalValuesFiles:
  - values-file-1.yml
  - values-file-2.yml
helmGlobals:
  chartHome: my-charts-dir

Differences Between Kustomize and Helm

  • Presence or Absence of Templating

  • Complexity

  • Upgrades and Rollbacks

References