这篇文章将一步步教你如何在Kubernetes集群安装和配置​​helm​​,并用其部署和管理应用程序。

前置条件

开始使用​​helm​​之前,应具备以下条件。


  1. 正在运行的kubernetes集群
  2. kubernetes集群API Endpoint应该可以从运行​​Helm​​的机器上访问。
  3. 使用kubectl对集群进行身份验证,它应该具有集群管理员权限。

Helm架构

5分钟教你在kubernetes集群上安装Helm,并部署应用_nginx

安装Helm[客户端]

在命令行执行以下命令。

curl -L https:///get_helm.sh | bash

由于国内网络原因,下载helm包时会失败。我已经将get_helm.sh脚本和helm的安装包打包:提取码:​​jrko​

helm-v2.16.1-linux-amd64.tar.gz

[root@master helm]# bash get_helm.sh 
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 创建具有集群管理员权限的Service Account

​Tiller​​​是Helm的服务端组件。​​Tiller​​​将被安装在kubernetes集群中,​​Helm​​​客户端会与其交互,从而使用​​Helm charts​​部署应用程序。

​Helm​​​将管理k8s集群资源。因此,我们需要向安装在集群​​kube-system​​​命令空间中的​​tiller​​组件添加必要的权限。

所以需要做以下操作:


  1. 创建名称为​​tiller​​​的​​Service Account​
  2. 创建​​tiller​​​对​​Service Account​​​具有集群管理员权限的​​ClusterRoleBinding​​。

我们将在一个yaml文件中添加​​Service Account​​​和​​clusterRoleBinding​​。

创建一个名为​​helm-rbac.yaml​​的文件,并将以下内容复制到该文件中。

apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: /v1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup:
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system

使用kubectl创建:

kubectl apply -f helm-rbac.yam

初始化Helm:部署Tiller

下一步是初始化Helm。当初始化​​Helm​​​时,将在kube-system名称空间中部署一个名为​​tiller-deploy​​的deploy。

使用以下命令初始化Helm。

helm init --service-account=tiller --history-max 300

如果要安装指定的​​tiller​​​版本,则可以在​​init​​​命令中使用​​--tiller-image​​​参数指定​​tiller​​​image地址。可以在公共Google GCR注册中心找到所有​​tiller​​​docker映像。由于国内网络原因,可以从阿里镜像仓库拉取。即把​​/kubernetes-helm/tiller:v2.14.1​​​替换为​​/google_containers/tiller:v2.14.1​

helm init --service-account=tiller --tiller-image=/google_containers/tiller:v2.14.1   --history-max 300

执行后如下:

[root@master helm]# helm init --service-account=tiller --tiller-image=/google_containers/tiller:v2.14.1   --history-max 300
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

如果不加“ --service-account = tiller”参数,则会出现以下错误。

Error: no available release name found

可以使用kubectl在kube-system名称空间中查看​​tiller​​部署。

kubectl get deployment tiller-deploy -n kube-system

使用Helm部署应用

现在,让我们使用Helm部署Nginx应用。

执行以下​​helm install​​​命令,在kubernetes集群中部署​​ingress nginx​​​。它将从github​仓库中下载​​nginx-ingress helm chart​​。

helm install stable/nginx-ingress --name nginx-ingress

可以使用以下命令检查​​helm chart​​是否安装。

helm ls

可以使用​​delete​​命令删除刚才的部署。例如:

helm delete nginx-ingress

从kubernetes集群中删除Helm(Tiller)

如果要从kubernetes集群中删除​​Tiller​​,请使用以下命令:

helm reset

由于某种原因,如果引发错误,请使用以下命令强制将其删除。

helm reset --force

也可以使用​​kubectl​​命令将其删除。

kubectl delete deployment tiller-deploy --namespace kube-system

作者简介


作者:小碗汤