---
title: "k8s helm安装及使用"
author: "mukeliang728"
date: "2021-11-05T16:22:31Z"
tags: []
categories: ["开源软件"]
slug: "k8s-helm"
draft: false
lastmod: "2021-11-05T16:22:31Z"
id: 2521387
url: "https://mukeliang.iteye.com/blog/2521387"
---

## 安装 Helm

```
wget https://get.helm.sh/helm-v3.4.1-linux-amd64.tar.gz -O helm-v3.4.1-linux-amd64.tar.gz

tar -zxvf  helm-v3.4.1-linux-amd64.tar.gz

mv linux-amd64/helm /usr/local/bin/helm
```

## Helm 是什么

每个成功的软件平台都有一个优秀的打包系统，比如 Debian、Ubuntu 的 apt，Redhat、Centos 的 yum。而 Helm 则是 Kubernetes 上的包管理器。

## Helm 的架构

Helm 有两个重要的概念：`chart` 和 `release`。

chart 是创建一个应用的信息集合，包括各种 Kubernetes 对象的配置模板、参数定义、依赖关系、文档说明等。chart 是应用部署的自包含逻辑单元。可以将 chart 想象成 apt、yum 中的软件安装包。

release 是 chart 的运行实例，代表了一个正在运行的应用。当 chart 被安装到 Kubernetes 集群，就生成一个 release。chart 能够多次安装到同一个集群，每次安装都是一个 release。

Helm 是包管理工具，这里的包就是指的 chart。

Helm 包含两个组件：Helm 客户端 和 Tiller 服务器。

简单的讲：Helm 客户端负责管理 chart；Tiller 服务器负责管理 release。

## 安装和部署 Helm 客户端和 Tiller 服务器

将 Helm 客户端安装在能够执行 `kubectl` 命令的节点上

> ```shell
> curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get | bash
> ```

```shell
root@roy-eas-vm-k8s-1:~# curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7230  100  7230    0     0  20792      0 --:--:-- --:--:-- --:--:-- 20775
Downloading https://kubernetes-helm.storage.googleapis.com/helm-v2.11.0-linux-amd64.tar.gz
Preparing to install helm and tiller into /usr/local/bin
helm installed into /usr/local/bin/helm
tiller installed into /usr/local/bin/tiller
Run 'helm init' to configure helm.
```

Tiller 服务器安装非常简单，只需要执行 `helm init`：

```shell
root@roy-eas-vm-k8s-1:~# helm init
Creating /root/.helm
Creating /root/.helm/repository
Creating /root/.helm/repository/cache
Creating /root/.helm/repository/local
Creating /root/.helm/plugins
Creating /root/.helm/starters
Creating /root/.helm/cache/archive
Creating /root/.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 /root/.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
Happy Helming!
```

现在，`helm version` 已经能够查看到客户端和服务器的版本信息了。

```shell
root@roy-eas-vm-k8s-1:~# helm version
Client: &version.Version{SemVer:"v2.11.0", GitCommit:"2e55dbe1fdb5fdb96b75ff144a339489417b146b", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.11.0", GitCommit:"2e55dbe1fdb5fdb96b75ff144a339489417b146b", GitTreeState:"clean”}
```

## 使用 Helm

Helm 安装成功后，可执行 `helm search` 查看当前可安装的 chart。例如找mysql

```shell
root@roy-eas-vm-k8s-1:~# helm search  mysql
NAME                                CHART VERSION   APP VERSION DESCRIPTION
incubator/mysqlha                   0.4.0           5.7.13      MySQL cluster with a single master and zero or more slave...
stable/mysql                        0.10.1          5.7.14      Fast, reliable, scalable, and easy to use open-source rel...
stable/mysqldump                    1.0.0           5.7.21      A Helm chart to help backup MySQL databases using mysqldump
stable/prometheus-mysql-exporter    0.1.0           v0.10.0     A Helm chart for prometheus mysql exporter with cloudsqlp...
stable/percona                      0.3.2           5.7.17      free, fully compatible, enhanced, open source drop-in rep...
stable/percona-xtradb-cluster       0.1.5           5.7.19      free, fully compatible, enhanced, open source drop-in rep...
```

Helm 仓库

```shell
root@roy-eas-vm-k8s-1:~# helm repo list
NAME        URL
stable      https://kubernetes-charts.storage.googleapis.com
local       http://127.0.0.1:8879/charts
```

Helm 安装时已经默认配置好了两个仓库：`stable` 和 `local`。`stable` 是官方仓库，`local` 是用户存放自己开发的 chart 的本地仓库。

用户可以通过 `helm repo add` 添加更多的仓库，比如企业的私有仓库，仓库的管理和维护方法请参考官网文档 [https://docs.helm.sh](https://docs.helm.sh)

添加仓库：

```shell
root@roy-eas-vm-k8s-1:~# helm repo add incubator http://storage.googleapis.com/kubernetes-charts-incubator
root@roy-eas-vm-k8s-1:~# helm repo list
NAME        URL
stable      https://kubernetes-charts.storage.googleapis.com
local       http://127.0.0.1:8879/charts
incubator   http://storage.googleapis.com/kubernetes-charts-incubator
```

安装 chart 也很简单，执行如下命令可以安装 MySQL。

```undefined
helm install stable/mysql
```

如果看到报错，通常是因为 Tiller 服务器的权限不足。

```shell
# helm install --name roy-kafka  incubator/kafka
Error: release roy-kafka failed: namespaces "default" is forbidden: User "system:serviceaccount:kube-system:default" cannot get resource "namespaces" in API group "" in the namespace "default"
```

执行如下命名添加权限：

```shell
$ 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"}}}}'
```

然后再次执行

```shell
helm install stable/mysql
```

。