This page looks best with JavaScript enabled

Kubernetes' Package Manager -- Helm

1. Why Helm Is Needed

One important design philosophy in Kubernetes is declarative operation. Users change the system by setting the system’s expected state. For example, the current replica count is 2 and it needs to be adjusted to 3. The declarative way is to modify the replica count in the configuration file to 3; the imperative way is to send a command that adds one replica, +1.

A system that uses declarative configuration focuses more on the outcome and places higher demands on system design. In a distributed system, no component is 100% reliable; for users, a declaratively configured system is friendlier.

Kubernetes uses yaml as its configuration file format. Deploying a simple Jenkins service in Kubernetes means writing two yaml files: jenkins-deployment.yaml and jenkins-service.yaml. See Deploying Jenkins with Kubectl. Add other services, and take into account multiple environments, and the number of yaml files that must be maintained becomes very large.

Maintaining yaml directly for deployment is bad both for project organization and for maintenance and updates. We need a tool that simplifies the application deployment and management process.

2. What Helm Is

Helm is a Kubernetes package manager started by Deis, similar to the apt and yum tools in Linux. Deis has been acquired by Microsoft.

2.1 Basic Concepts

  • Chart

A Chart is used to package yaml files; it contains the images, dependencies, resources, service definitions, and so on required to run an application.

  • Release

An instance of a Chart on a Kubernetes cluster. Every installation creates a new Release, and a single Chart can correspond to many instances.

  • Repository

A repository used to publish and store Charts.

2.2 Basic Components

Helm uses a C/S architecture. The components are:

  • Helm CLI

Helm CLI is the Helm client. It runs locally and is responsible for interacting with the other components.

  • Tiller

Tiller is the server-side component. It runs on the Kubernetes cluster and manages the applications deployed by Helm.

  • Repository

Repository is the Chart repository.

2.3 Features Helm Provides

  • Application packaging
  • Application distribution
  • Version management
  • Dependency checking

3. Chart

By creating a folder with a specific directory structure, a chart describes a set of application resources and guides application deployment.

The chart package structure for Wordpress:

1
2
3
4
5
6
7
8
9
Wordpress/
  Chart.yaml          # Yaml文件,用于描述 Chart 的基本信息,包括名称版本等
  LICENSE             # [可选] 文本格式的协议
  README.md           # [可选] 应用介绍、使用说明
  requirements.yaml   # [可选] 用于存放当前 Chart 依赖的其它 Chart 的说明文件
  values.yaml         # Chart 的默认值配置文件
  charts/             # [可选] 该目录中放置当前 Chart 依赖的其它 Chart
  templates/          # [可选] 部署文件模版目录,模版填入 values.yaml 中相应值,生成最终的 kubernetes 配置文件
  templates/NOTES.txt # [可选] 使用指南

The contents of the Chart.yaml file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
apiVersion: [必须] Chart API 版本,可用值 v1
name: [必须] Chart 名称
version: [必须] 版本,遵循 [SemVer 2 标准](https://semver.org/)
kubeVersion: [可选] 兼容的 Kubernetes 版本,遵循 [SemVer 2 标准](https://semver.org/)
description: [可选] 一句话的应用描述
keywords:
  - [可选] 应用关键字列表
home: [可选] 应用主页 URL
sources:
  - [可选] 当前应用下载地址列表
maintainers: [可选]
  - name: [必须] name
    email: [可选] email
    url: [可选] url
engine: [可选] 模板引擎,默认值是 gotpl
icon: [可选] SVG 或者 PNG 格式的图片地址
appVersion: [可选] 应用版本
deprecated: [可选] boolean 类型,是否不建议使用
tillerVersion: [可选] Chart 需要的 Tiller 版本,遵循 [SemVer 2 标准](https://semver.org/),需要 ">2.0.0"

4. Installation and Usage

Because Helm uses a C/S architecture, installation has two parts: the client and the server. The exact commands differ by operating system; see the official Helm documentation. Here we use an OS X client and a CentOS server running minikube as the example.

4.1 Installing the Client

On OS X:

1
brew install kubernetes-helm

On CentOS:

1
2
3
wget https://storage.googleapis.com/kubernetes-helm/helm-v2.12.2-linux-amd64.tar.gz
tar -zxvf helm-v2.12.2-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/helm

View the repository sources:

1
2
3
4
helm repo list
NAME  	URL
stable	https://kubernetes-charts.storage.googleapis.com
local 	http://127.0.0.1:8879/charts

It is recommended to update the repository source:

1
2
helm repo add stable https://charts.helm.sh/stable
"stable" has been added to your repositories

4.2 Installing the Server

  • Install tiller

Since a remote development environment is configured (see Building a Remote Kubernetes Development Environment), run the following on OS X:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
helm init --history-max 200
Creating /Users/username/.helm/repository/repositories.yaml
Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com
Adding local repo with URL: http://127.0.0.1:8879/charts
$HELM_HOME has been configured at /Users/username/.helm.

Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.

Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
To prevent this, run `helm init` with the --tiller-tls-verify flag.
For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation

On Kubernetes 1.16.0 and later you may hit Error: error installing: the server could not find the requested resource. This is because extensions/v1beta1 has been replaced by apps/v1. Run the following command to install:

1
helm init --service-account tiller --override spec.selector.matchLabels.'name'='tiller',spec.selector.matchLabels.'app'='helm' --output yaml | sed 's@apiVersion: extensions/v1beta1@apiVersion: apps/v1@' | kubectl apply -f -

tiller will be installed into the kube-system namespace; you can check it with the kubectl get pods --namespace kube-system command.

  • Create an access role

Run the commands:

1
2
3
kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'

4.3 Checking Whether Helm Works

Running the helm version command checks whether both the local and the server installation are ready.

1
2
3
4
helm version
helm  version
Client: &version.Version{SemVer:"v2.14.2", GitCommit:"a8b13cc5ab6a7dbef0a58f5061bcc7c0c61598e7", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.2", GitCommit:"a8b13cc5ab6a7dbef0a58f5061bcc7c0c61598e7", GitTreeState:"clean"}

If the Server side reports an error, it may be caused by a missing package on the Node, for example socat. Log in to the server and run yum install -y socat.

4.4 Creating a Chart Deployment

  • Create a new chart
1
2
helm create hello-chart
Creating hello-chart

In values.yaml you can see that a Nginx application is created by default. To make it easier to access from outside for testing, change the service properties in values.yaml to:

1
2
3
service:
  type: NodePort
  port: 30003
  • Deploy to the server
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
helm install ./hello-chart
NAME:   alert-koala
LAST DEPLOYED: Wed Jul 24 14:21:58 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Deployment
NAME                     READY  UP-TO-DATE  AVAILABLE  AGE
alert-koala-hello-chart  0/1    1           0          1s

==> v1/Pod(related)
NAME                                      READY  STATUS   RESTARTS  AGE
alert-koala-hello-chart-86cdd48bfc-b9dqv  0/1    Running  0         1s

==> v1/Service
NAME                     TYPE      CLUSTER-IP     EXTERNAL-IP  PORT(S)          AGE
alert-koala-hello-chart  NodePort  10.0.0.10  <none>       30003:32046/TCP  1s
  • View the deployed application

After a few seconds the application is running. Open the address http://10.10.10:32046 and you will see the Nginx page.

  • View the release
1
2
3
4
helm list
helm list
NAME       	REVISION	UPDATED                 	STATUS  	CHART            	APP VERSION	NAMESPACE
alert-koala	1       	Wed Jul 24 14:21:58 2019	DEPLOYED	hello-chart-0.1.0	1.0        	default
  • Package the chart
1
2
3
helm package hello-chart
helm package hello-chart
Successfully packaged chart and saved it to: /Users/username/Code/Kubernetes/hello-chart-0.1.0.tgz

Note that this must be run from the parent directory of hello-chart. Packaging simply compresses the hello-chart folder into a tgz file.

  • Delete the release:
1
2
helm delete alert-koala --purge
release "alert-koala" deleted

After deletion, the application is removed as well.

5. References


微信公众号
WRITTEN BY
微信公众号