Feedback

Chat Icon

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Introduction to Helm
94%

Understanding Helm Templating

After deploying your chart, you might want to understand how Helm templating works to customize your charts further. Let's take this example. Imagine we have the following templates/deployment.yaml file in our Helm chart:

apiVersion: apps/v1
kind: Deployment
metadata:
  # Uses a predefined Helm function to get
  # a full name composed of the release name
  # and the chart name
  name: {{ include "hello-world.fullname" . }}

  labels:
    # Inserts labels defined in the
    # "hello-world.labels" template
    {{- include "hello-world.labels" . | nindent 4 }}
spec:
  # Checks if autoscaling is not enabled
  # in the values file
  {{- if not .Values.autoscaling.enabled }}
  # Sets the replica count based on the values file
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      # Inserts selector labels defined in the
      # "hello-world.selectorLabels" template
      {{- include "hello-world.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      # Checks if podAnnotations are provided in the values file
      {{- with .Values.podAnnotations }}
      annotations:
        # Converts the podAnnotations to YAML
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        # Inserts selector labels defined in the
        # "hello-world.selectorLabels" template
        {{- include "hello-world.selectorLabels" . | nindent 8 }}
    spec:
      # Checks if imagePullSecrets are provided
      # in the values file
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        # Converts the imagePullSecrets to YAML
        {{- toYaml . | nindent 8 }}
      {{- end }}
      # Uses a predefined Helm function to get
      # the service account name
      serviceAccountName: {{ include "hello-world.serviceAccountName" . }}
      securityContext:
        # Converts the podSecurityContext
        # from the values file to YAML
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            # Converts the securityContext from
            # the values file to YAML
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Enroll now to unlock all content and receive all future updates for free.