Dependencies

In this chapter we’ll look at bundling charts together with subcharts and dependencies. This allows us to create a single release bundling all applications (frontend, backend and the database).

Exercise - Deploying the application in separate releases

In the last section we looked at how we can create a Helm chart from scratch to deploy our database. Now we are going to deploy the remaining components todobackend and todoui as well. Fortunately there are already helm charts prepared so we don’t have to create those now. When you look at those charts with

tree todobackend/helmchart/

and

tree todoui/helmchart

you will see that they are pretty similar to the one we just created.

todobackend/helmchart
todobackend/helmchart
├── charts
├── Chart.yaml
├── templates
│   ├── todobackend-service.yaml
│   └── todobackend.yaml
└── values.yaml

2 directories, 4 files
todoui/helmchart
todoui/helmchart
├── charts
├── Chart.yaml
├── templates
│   ├── todoui-service.yaml
│   └── todoui.yaml
└── values.yaml

2 directories, 4 files

We can now deploy the whole application with three helm installs:

helm install postgres ./postgresdb/helmchart

helm install todobackend ./todobackend/helmchart

helm install todoui ./todoui/helmchart

This works well and may just be enough for our simple scenario but you can probably imagine how tedious this would get if you had more than just three components to your application.

Luckily Helm comes with the capability of bundling multiple charts into a single release by using dependencies. But before we dive into the next section we should clean up again:

helm uninstall postgres todobackend todoui

Subcharts vs Dependencies

Helm offers two ways of organizing the dependencies of a chart:

  1. Dependencies in Chart.yaml
  2. Subcharts

In this section we’re going to walk you through both of these options.

Exercise - Creating the parent chart

Let’s start by creating a new chart which we are going to use as our parent.

  1. Create the parentchart with helm create parentchart while you are in the root directory of the exercises.
  2. Clean the templates directory by running rm -r parentchart/templates/*.
  3. Delete all contents of values.yaml
  4. Verify your helmchart by running tree parentchart. It should look like this:
parentchart
├── charts
├── Chart.yaml
├── templates
└── values.yaml

2 directories, 2 files

Exercise - Adding the three dependencies to Chart.yaml

We can simply specify dependencies in Chart.yaml by using the dependencies: property. Open Chart.yaml in a text editor and insert the following code at the bottom of the file:

dependencies:
  - name: postgresdb
    version: "0.1.0"
    repository: file://../postgresdb/helmchart/
  - name: todobackend
    version: "0.1.0"
    repository: file://../todobackend/helmchart/
  - name: todoui
    version: "0.1.0"
    repository: file://../todoui/helmchart/

Before we can install the parentchart we need to do one more thing. The command helm dependency update parentchart/ will load the dependencies into the parents charts/ directory so they can be deployed when you run helm install.

Exercise - Installing the parent chart

Try running helm install todo ./parentchart/ now and you should see the whole application being deployed!

Solution
  1. helm dependency update parentchart/

    Saving 3 charts
    Deleting outdated charts
  2. helm install todo ./parentchart/

    NAME: todo
    LAST DEPLOYED: Fri Dec 01 13:43:55 2023
    NAMESPACE: <your_namespace>
    STATUS: deployed
    REVISION: 1
    TEST SUITE: None
  3. Verify by running kubectl get svc,deploy,po and you should see (among other resources)

    NAME                       TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
    service/helm-postgresdb    ClusterIP      10.103.197.166   <none>        5432/TCP         12s
    service/helm-todobackend   ClusterIP      10.96.248.255    <none>        8080/TCP         12s
    service/helm-todoui        LoadBalancer   10.100.170.88    <pending>     8090:30798/TCP   12s
    
    NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/helm-postgresdb    0/1     1            0           12s
    deployment.apps/helm-todobackend   0/1     1            0           12s
    deployment.apps/helm-todoui        0/1     1            0           12s
    
    NAME                                    READY   STATUS              RESTARTS   AGE
    pod/helm-postgresdb-5567b699b6-qtzch    0/1     ContainerCreating   0          12s
    pod/helm-todobackend-586c49db6d-vx7gg   0/1     ContainerCreating   0          12s
    pod/helm-todoui-7b7c74fcd6-zxx4c        0/1     ContainerCreating   0          12s
Note

You may have noticed that the charts in the charts/ directory are .tgz files. Helm uses packaging to reduce the size of charts. They can still be installed with helm install todobackend-0.1.0.tgz and you can manually package charts with the helm package <chart directory> command.

Exercise - Parentchart cleanup

Before we start the next exercise please run the following commands to clean up:

helm uninstall todo to uninstall the helm release

rm parentchart/charts/* to remove the dependencies charts

nano parentchart/Chart.yaml to remove the dependency declarations

Chart.yaml should look like this:

apiVersion: v2
name: parentchart
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: 1.16.0

Exercise - Subcharts

The second option for bundling helm charts would be subcharts. It’s the same principle as listing dependencies in Chart.yaml but we’re simply skipping the step of declaring the dependencies in the chart file by directly placing them as subcharts in the parents charts/ directory.

As a consequence our subcharts can inject values from the parent or override them.

All we have to do to turn our standalone charts into subcharts, is copy them in the parents charts/ directory with the following commands:

cp -r postgresdb/helmchart parentchart/charts/postgresdb

cp -r todobackend/helmchart parentchart/charts/todobackend

cp -r todoui/helmchart parentchart/charts/todoui

Running tree parentchart/ should output

parentchart
├── charts
│   ├── postgresdb
│   │   ├── charts
│   │   ├── Chart.yaml
│   │   ├── templates
│   │   │   ├── postgres-service.yaml
│   │   │   └── postgres.yaml
│   │   └── values.yaml
│   ├── todobackend
│   │   ├── charts
│   │   ├── Chart.yaml
│   │   ├── templates
│   │   │   ├── todobackend-service.yaml
│   │   │   └── todobackend.yaml
│   │   └── values.yaml
│   └── todoui
│       ├── charts
│       ├── Chart.yaml
│       ├── templates
│       │   ├── todoui-service.yaml
│       │   └── todoui.yaml
│       └── values.yaml
├── Chart.yaml
├── templates
└── values.yaml

Exercise - Installing the parentchart with subcharts

After we put the subcharts in the right directory all that’s left to do is installing the parentchart again. Run

helm install todo ./parentchart/

again and see what’s happening now!

Solution

The steps for

  1. helm install todo ./parentchart/

    NAME: todo
    LAST DEPLOYED: Fri Dec 01 13:44:27 2023
    NAMESPACE: <your_namespace>
    STATUS: deployed
    REVISION: 1
    TEST SUITE: None
  2. Verify by running kubectl get svc,deploy,po and you should see

    NAME                               READY   STATUS              RESTARTS   AGE
    pod/postgresdb-5567b699b6-s7p4t    0/1     ContainerCreating   0          7s
    pod/todobackend-586c49db6d-f9mvr   0/1     ContainerCreating   0          7s
    pod/todoui-7b7c74fcd6-gthg5        0/1     ContainerCreating   0          7s
    
    NAME                  TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
    service/kubernetes    ClusterIP      10.96.0.1        <none>        443/TCP          35d
    service/postgresdb    ClusterIP      10.104.125.201   <none>        5432/TCP         7s
    service/todobackend   ClusterIP      10.107.164.204   <none>        8080/TCP         7s
    service/todoui        LoadBalancer   10.100.70.129    <pending>     8090:30796/TCP   7s
    
    NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/postgresdb    0/1     1            0           7s
    deployment.apps/todobackend   0/1     1            0           7s
    deployment.apps/todoui        0/1     1            0           7s
    
    NAME                                     DESIRED   CURRENT   READY   AGE
    replicaset.apps/postgresdb-5567b699b6    1         1         0       7s
    replicaset.apps/todobackend-586c49db6d   1         1         0       7s
    replicaset.apps/todoui-7b7c74fcd6        1         1         0       7s

Exercise - Subchart cleanup

To clean up, please run helm uninstall todo once more.

Summary

In this chapter we learned how we can bundle and deploy multiple helmcharts in one release using either dependency declarations or subcharts. These two approaches practically accomplish the same thing, yet there is still a point to be made for preferring dependencies over subcharts.

Placing the tarballs of dependencies directly into the parent chart’s directory structure forces you to manually update those once your dependencies receive a version bump.

Declaring a subchart as a dependency allows you to separately version it. It will have its own repository with its own, independent version history that your parent chart can choose from.