Reader small image

You're reading from  Learn Helm

Product typeBook
Published inJun 2020
PublisherPackt
ISBN-139781839214295
Edition1st Edition
Right arrow
Authors (2):
Andrew Block
Andrew Block
author image
Andrew Block

Andrew Block is a core maintainer on the Helm project and a Distinguished Architect at Red Hat. He specializes in the use of continuous integration and continuous delivery methodologies to streamline the delivery process and incorporate security at each stage. He works with organizations to adopt and implement these technologies and concepts within their organization. As an open source enthusiast, Andrew not only has authored several publications, but he is also a contributor to several open source communities and a lead within the sigstore project, which aims at simplifying how software is signed and verified.
Read more about Andrew Block

Austin Dewey
Austin Dewey
author image
Austin Dewey

Austin Dewey is a DevOps engineer focused on delivering a streamlined developer experience on cloud and container technologies. Austin started his career with Red Hat's consulting organization, where he helped drive success at Fortune 500 companies by automating deployments on Red Hat's Kubernetes-based PaaS, OpenShift Container Platform. Currently, Austin works at fintech start-up Prime Trust, where he builds automation to scale financial infrastructure and supports developers on Kubernetes and AWS.
Read more about Austin Dewey

View More author details
Right arrow

Chapter 5: Building Your First Helm Chart

In the previous chapter, you learned about the various aspects that comprise a Helm chart. Now, it is time to put this knowledge to the test by building a Helm chart. Learning to build a Helm chart will allow you to package a complex Kubernetes application in a way that makes it simple to deploy.

In this chapter, you will learn how to build a Helm chart that deploys the guestbook application, a common quickstart application used throughout the Kubernetes community. This chart will be built by following best practices around Kubernetes and Helm chart development, to provide a well-written and easily maintainable piece of automation. Throughout the process of developing this chart, you will learn many different skills that you can apply toward building your own Helm charts. At the end of the chapter, you will learn how to package your Helm chart and deploy it to a chart repository, where it can be easily accessible to end users.

Here are...

Technical requirements

This chapter requires the following technologies:

  • minikube
  • kubectl
  • helm

In addition to the preceding tooling, you will find the GitHub repository for this book located at https://github.com/PacktPublishing/-Learn-Helm. We will reference the helm-charts/charts/guestbook folder contained in this chapter.

It is recommended that you have your own GitHub account in order to complete the final section of this chapter, Creating a chart repository. Instructions on how to create your own account will be provided in that section.

Understanding the Guestbook application

In this chapter, you will create a Helm chart to deploy the Guestbook tutorial application provided by the Kubernetes community. This application is introduced in the Kubernetes documentation at the following page: https://kubernetes.io/docs/tutorials/stateless-application/guestbook/

The Guestbook application is a simple PHP: Hypertext Preprocessor (PHP) frontend designed to persist messages to a Redis backend. The frontend consists of a dialog box and a Submit button, as illustrated in the following screenshot:

Figure 5.1: The Guestbook PHP frontend

To interact with this application, users can follow these next steps:

  1. Type a message in the Messages dialog box.
  2. Click the Submit button.
  3. When the Submit button is clicked, the message will be saved to a Redis database.

Redis is an in-memory, key-value data store that, in this chapter, will be clustered for data replication. The cluster will consist...

Setting up the environment

In order to see your chart in action, you'll need to create your minikube environment by following these steps:

  1. Start minikube by running the minikube start command, as follows:
    $ minikube start
  2. Create a new namespace called chapter5, like this:
    $ kubectl create namespace chapter5

We'll use this namespace when the Guestbook chart is deployed. Now that the environment is prepared, let's begin writing the chart.

Creating a Guestbook Helm chart

In this section, we will create a Helm chart to deploy the Guestbook application. The final chart has been published under the helm-charts/charts/guestbook folder of the Packt repository. Feel free to reference this location as you follow along with the examples.

We will begin development by first scaffolding the Guestbook Helm chart to create the chart's initial file structure.

Scaffolding the initial file structure

As you may recall from Chapter 4, Understanding Helm Charts, Helm charts must follow a particular file structure in order to be considered valid. Namely, a chart must contain the following required files:

  • Chart.yaml: Used to define chart metadata
  • values.yaml: Used to define default chart values
  • templates/: Used to define chart templates and Kubernetes resources to be created

We provided a list of each of the possible files a chart can contain in Chapter 4, Understanding Helm Charts, but the three preceding...

Improving the Guestbook Helm chart

The chart created in the previous section was able to successfully deploy the Guestbook application. However, as is the case with any type of software, the Helm chart can always be improved. In this section, we will focus on the following two features that will improve the guestbook chart:

  • Life cycle hooks to back up and restore the Redis database
  • Input validation to ensure only valid values are provided

Let's focus first on adding life cycle hooks.

Creating pre-upgrade and pre-rollback life cycle hooks

In this section, we will create two life cycle hooks, as follows:

  1. The first hook will occur in the pre-upgrade life cycle phase. This phase takes place immediately after the helm upgrade command is run, but before any Kubernetes resources become modified. This hook will be used to take a data snapshot of the Redis database before performing the upgrade, ensuring that the database is backed up in case the upgrade...

Publishing the Guestbook chart to a chart repository

Now that you have completed the development of the Guestbook chart, the chart can be published to a repository so that it is easily accessible for other users. Let's begin by first creating the chart repository.

Creating a chart repository

Chart repositories are servers containing two different components, as follows:

  • Helm charts, packaged as tgz archives
  • An index.yaml file, containing metadata about the charts contained in the repository

Basic chart repositories require maintainers to generate their own index.yaml files, while more complex solutions such as the Helm community's ChartMuseum tool dynamically generate the index.yaml file when new charts are pushed to the repository. In this example, we will create a simple chart repository using GitHub Pages. GitHub Pages allows maintainers to create a simple static hosting site out of a GitHub repository, which can be used to create a basic chart...

Cleaning up

You can clean up your environment by deleting the chapter5 namespace, as follows:

$ kubectl delete namespace chapter5

If you have finished working, you can also stop your minikube cluster with the minikube stop command.

Summary

In this chapter, you learned how to build a Helm chart from scratch by writing a chart to deploy the Guestbook application. You began by creating a chart that deployed the Guestbook frontend and a Redis dependency chart, and you later improved upon this chart by writing life cycle hooks and adding input validation. You concluded this chapter by building your own chart repository with GitHub Pages and publishing your guestbook chart to this location.

In the next chapter, you will learn strategies around testing and debugging a Helm chart, to enable you to further strengthen your chart development skills.

Further reading

For additional information on the Guestbook application, please refer to the Deploying PHP Guestbook application with Redis tutorial from the Kubernetes documentation, at https://kubernetes.io/docs/tutorials/stateless-application/guestbook/.

To learn more about developing Helm chart templates, please refer to the following links:

Questions

  1. Which command can be used to scaffold a new Helm chart?
  2. Which key advantages did declaring a Redis chart dependency provide when developing the guestbook chart?
  3. What annotation can be used to set the execution order of hooks for a given life cycle phase?
  4. What are the common use cases for using the fail function? What about the required function?
  5. Which Helm commands are involved in order to publish a Helm chart to a GitHub Pages chart repository?
  6. What is the purpose of the index.yaml file in a chart repository?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Helm
Published in: Jun 2020Publisher: PacktISBN-13: 9781839214295
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Andrew Block

Andrew Block is a core maintainer on the Helm project and a Distinguished Architect at Red Hat. He specializes in the use of continuous integration and continuous delivery methodologies to streamline the delivery process and incorporate security at each stage. He works with organizations to adopt and implement these technologies and concepts within their organization. As an open source enthusiast, Andrew not only has authored several publications, but he is also a contributor to several open source communities and a lead within the sigstore project, which aims at simplifying how software is signed and verified.
Read more about Andrew Block

author image
Austin Dewey

Austin Dewey is a DevOps engineer focused on delivering a streamlined developer experience on cloud and container technologies. Austin started his career with Red Hat's consulting organization, where he helped drive success at Fortune 500 companies by automating deployments on Red Hat's Kubernetes-based PaaS, OpenShift Container Platform. Currently, Austin works at fintech start-up Prime Trust, where he builds automation to scale financial infrastructure and supports developers on Kubernetes and AWS.
Read more about Austin Dewey