k8s_pkg_manager

Introduction

Helm is the best way to find, share, and use software built Kubernetes, a software in k8s may be one separate deployment or several deployments that works together to provides service to user, Helm manages these yaml files with concept Chart, Chart is a bundle of yaml files and other files related to the software, Chart helps you define, install, and upgrade, rollback even the most complex Kubernetes application, Chart likes deb package which creates package with xx.deb, while to create an application for k8s by Helm, you need to create Chart files with fixed layout and follow its syntax.

For Helm, there are three important concepts:

  • The chart is a bundle of information necessary to create an instance of a Kubernetes application. mostly for complex application, but for deployment only, no auto scale, no monitor with Chart.
  • The config contains configuration information that can be merged into a packaged chart to create a releasable object.
  • 🟢 A release is a running instance of a chart, combined with a specific config. with release version, we can update/rollback a chart

Command

In order to use helm, we need to install it firstly with below command

1
2
3
4
5
6
# Centos7 to install snap
$ yum install -y snapd
$ systemctl enable --now snapd.socket
$ ln -s /var/lib/snapd/snap /snap

$ sudo snap install helm --classic

🔴 As helm installs application in k8s(create deployment), hence in order to use it, you MUST have kubernetes and kubectl installed before. there are required!!!

Helm and K8s compatibility

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Helm Version	Supported Kubernetes Versions
3.5.x 1.20.x - 1.17.x
3.4.x 1.19.x - 1.16.x
3.3.x 1.18.x - 1.15.x
3.2.x 1.18.x - 1.15.x
3.1.x 1.17.x - 1.14.x
3.0.x 1.16.x - 1.13.x
2.16.x 1.16.x - 1.15.x
2.15.x 1.15.x - 1.14.x
2.14.x 1.14.x - 1.13.x
2.13.x 1.13.x - 1.12.x
2.12.x 1.12.x - 1.11.x
2.11.x 1.11.x - 1.10.x
2.10.x 1.10.x - 1.9.x
2.9.x 1.10.x - 1.9.x
...

k8s version supported by Helm

Command used for helm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
$ sudo snap install helm --classic

#------------------------------------repo related----------------------------------
# Hub is groups of package from many repos!!!
# Official Hub: https://artifacthub.io/

$ helm search hub mysql
# search from official hub which will tell you which repo has such package!!!
# then add the right repo with below command

# add a chart repo, hence you can search, download, install charts from there
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm repo list
NAME URL
bitnami https://charts.bitnami.com/bitnami

$ helm repo update # Make sure we get the latest list of charts
$ helm search repo mysql # search from user added repo


#-----------------------------------install/uninstall package-----------------------
$ helm show chart bitnami/mysql
$ helm show values bitnami/mysql

# get all chart info
$ helm show all bitnami/mysql
# get deps of this chart
$ helm dep list bitnami/mysql

$ helm install bitnami/mysql --generate-name
# Whenever you install a chart, a new release is created.
# So one chart can be installed multiple times into the same cluster.
# And each can be independently managed and upgraded.

# install with custom value, note myvals.yaml is merged with default!!!!
$ helm show values bitnami/mysql
$ helm install --generate-name --values=./myvals.yaml bitnami/mysql

# if only change few values, you can pass it from commandline
$ helm install --generate-name --set auth.database="my_db" bitnami/mysql

OR
# another way to install a package
# 1. first only download(default path: ~/.cache/helm/repository/mysql.tgz)
# 2. start package from chart file manually
$ helm install ~/.cache/helm/repository/mysql.tgz --generate-name


# show installed chart
$ helm ls
# uninstall a chart
$ helm uninstall mysql-1612624192

# check chart status
$ helm status mysql-1612624192

# get yaml for this application
$ helm get manifest mysql-1612624192

Build a pkg with Helm

A chart is a collection of files that describe a related set of Kubernetes resources. A single chart might be used to deploy something simple, like a memcached pod, or something complex, like a full web app stack with HTTP servers, databases, caches, and so on.

Charts are created as files laid out in a particular directory tree. They can be packaged into versioned archives to be deployed.

1
2
3
4
5
6
7
8
9
10
11
wordpress/
Chart.yaml # A YAML file containing information about the chart
LICENSE # OPTIONAL: A plain text file containing the license for the chart
README.md # OPTIONAL: A human-readable README file
values.yaml # The default configuration values for this chart
values.schema.json # OPTIONAL: A JSON Schema for imposing a structure on the values.yaml file
charts/ # A directory containing any charts upon which this chart depends.
crds/ # Custom Resource Definitions
templates/ # A directory of templates that, when combined with values,
# will generate valid Kubernetes manifest files.
templates/NOTES.txt # OPTIONAL: A plain text file containing short usage notes

Details about charts, refer to charts guidline

Helm Chart templates are written in the Go template language, with the addition of 50 or so add-on template functions from the Sprig library and a few other specialized functions.

All template files are stored in a chart’s templates/ folder. When Helm renders the charts, it will pass every file in that directory through the template engine

Predefined Values

Values that are supplied via a values.yaml file (or via the –set flag) are accessible from the .Values object in a template. But there are other pre-defined pieces of data you can access in your templates.

The following values are pre-defined, are available to every template, and cannot be overridden. As with all values, the names are case sensitive.

  • Release.Name: The name of the release (not the chart)
  • Release.Namespace: The namespace the chart was released to.
  • Release.Service: The service that conducted the release.
  • Release.IsUpgrade: This is set to true if the current operation is an upgrade or rollback.
  • Release.IsInstall: This is set to true if the current operation is an install.
  • Chart: The contents of the Chart.yaml. Thus, the chart version is obtainable as Chart.Version and the maintainers are in Chart.Maintainers.
  • Files: A map-like object containing all non-special files in the chart. This will not give you access to templates, but will give you access to additional files that are present (unless they are excluded using .helmignore). Files can be accessed using { { index .Files “file.name” } } or using the { {.Files.Get name } } function. You can also access the contents of the file as byte using { { .Files.GetBytes } }
  • Capabilities: A map-like object that contains information about the versions of Kubernetes ({ { .Capabilities.KubeVersion } }) and the supported Kubernetes API versions ({ { .Capabilities.APIVersions.Has “batch/v1” } })

NOTE: Any unknown Chart.yaml fields will be dropped. They will not be accessible inside of the Chart object. Thus, Chart.yaml cannot be used to pass arbitrarily structured data into the template. The values file can be used for that, though.

Steps to create your own chart

1
2
3
4
5
6
7
8
9
$ helm create mychart

# modify template and values below

# syntax check
$ helm lint mychart

# package it with xx.tgz
$ helm package mychart

Ref