What are Helm Charts?

What are Helm Charts?

An in-depth guide to deploying Java applications using Helm charts on Kubernetes

Introduction

Helm charts are a great way to define, install, and upgrade Kubernetes applications. Helm is a package manager for Kubernetes that helps to automate the process of creating, deploying, and managing applications.

In this article, we will dive deep into Helm charts and explore their usage in a Java application. We will discuss what Helm charts are, how they work, and how to create a Helm chart for a Java application.

What are Helm Charts? Helm Charts is a collection of YAML files that define and describe a Kubernetes application. A Helm chart can be thought of as a bundle of files that defines the application in a declarative way.

Helm charts consist of three parts:

  1. Chart.yaml: This file contains information like the name of the chart, version, description, and other metadata.

  2. Values.yaml: This file contains user-defined values for the chart that can be overridden at deployment time.

  3. Templates: These are the actual Kubernetes manifests for the application which are generated from the chart using the values provided in the values.yaml file.

How do Helm Charts work? Helm charts work by taking the user-specified values from the values.yaml file and using them to generate Kubernetes manifests. The generated manifests are then passed to the Kubernetes API server, where they are used to create and manage the application.

The values.yaml file provides a way for users to configure a Helm chart without modifying the underlying templates. This separation of configuration and templates allows for greater flexibility and ease of use.

Creating a Helm Chart for a Java Application Let's take a Java application as an example and walk through the process of creating a Helm Chart for it.

Step 1: Create a Java application The first step is to create a simple Java application that we can use for our example. For this, we will create a "Hello World" Java application that prints "Hello, world!" to the console.

Step 2: Define a Helm Chart for the Java Application The next step is to define a Helm Chart for our Java application. Let's create a new directory called "myapp" and create a file called Chart.yaml in it. The content of Chart.yaml file should look like this:

apiVersion: v2
name: myapp
version: 1.0.0
description: A Helm chart for my Java application.

This file specifies the basic metadata for our Helm chart. The next step is to define the values that our Helm chart will use. Let's create a file called values.yaml and define the following values:

# Default values for myapp.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
  repository: myapp
  tag: latest
  pullPolicy: IfNotPresent
service:
  name: myapp
  type: ClusterIP
  port: 8080

This file defines several variables that our templates will use when generating Kubernetes manifests. We can also specify default values for these variables.

Step 3: Define Templates The next step is to define the Kubernetes manifests that our Helm chart will generate. Let's create a directory called "templates" and create a file called deployment.yaml in it with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "myapp.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "myapp.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "myapp.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "myapp.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /actuator/health
              port: http
            initialDelaySeconds: 10
            periodSeconds: 10

This file defines a Kubernetes deployment object that our Helm chart will generate.

Step 4: Package the Helm Chart The final step is to package our Helm chart into a distributable format. We can do this by running the following command from the root directory of our Helm chart:

helm package .

This will create a file called myapp-1.0.0.tgz, which contains our Helm chart.

Step 5: Deploy the Helm Chart Now, we are ready to deploy our Helm chart. We can do this by running the following command:

helm install myapp ./myapp-1.0.0.tgz

This will create a Kubernetes deployment and a service for our Java application.

Conclusion

In this article, we discussed the concept of Helm charts, their usage with Kubernetes, and explained how to create a Helm chart for a Java application. We demonstrated how Helm charts can be used to automate the deployment of Kubernetes applications, making it easier for developers to manage their applications. By following the example provided in this article, you can create and use Helm charts to manage your Java applications on Kubernetes.

End Note

I hope you found this article helpful in understanding the power and simplicity of Helm charts for deploying Java applications on Kubernetes. If you enjoyed reading this article, please consider following me on my social media channels(GitHub, LinkedIn, Twitter), where I share more information about programming languages and technologies that I learn about. Your support and feedback inspire me to continue sharing my knowledge and experience with others. Thank you for reading and happy coding!

Did you find this article valuable?

Support Anant Singh Raghuvanshi by becoming a sponsor. Any amount is appreciated!