Helm Syntax – Chart Development

If you don’t know about Helm, check out the What is Helm blog first!

In this tutorial, we are going to prepare a basic helm template. What will the Helm template that we are going to prepare to do is generate Kubernetes manifest files;

What is the expected output;

Basic Nginx Deployment with;

  • using 1.14.2 version
  • 3 replicas
  • opening 80 port
  • nginx labels

But what if we want to change the image or replica or the version of the image, even the images itself.

So we have to decide what to parameterize or not.

Let’s start with the upper properties first.

  • The name of the deployment
  • Labels
  • Replica count
  • Image & Version
  • Port
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Let’s start with creating the helm chart first;

mkdir -p deployment/templates
touch deployment/Chart.yaml
touch deployment/values.yaml
touch deployment/templates/deployment.yaml

Put below content into the Chart.yaml;

apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: nginx
version: 0.1.0

And fill the values.yaml;

name: nginx

image:
  name: nginx
  repository: nginx
  version: 1.14.2

replicaCount: 3

port: 80

To use these properties we have a syntax;

{{ .Values.name }}

You should put the properties map after .Values to use it;

In our case the deployment/templates/deployment.yaml file should be;

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.name }}-deployment
  labels:
    app: {{ .Values.name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Values.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.name }}
    spec:
      containers:
      - name: {{ .Values.image.name }}
        image: {{ .Values.image.repository }}:{{ .Values.image.version }}
        ports:
        - containerPort: {{ .Values.port }}

For example in the image section, as you can imagine {{ .Values.image.repository }} part gets the name under the image section on values.yaml.

For final testing, you can use the below command to see what you have prepared;

helm template deployment

This should give you the exact same output as the manifest at the top of this blog.

Leave a Comment

Your email address will not be published. Required fields are marked *