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

503 阅读4分钟

翻译于Helm Tutorial: How To Install and Configure Helm

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

前置条件

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

  1. 正在运行的kubernetes集群

  2. kubernetes集群API Endpoint应该可以从运行Helm的机器上访问。

  3. 使用kubectl对集群进行身份验证,它应该具有集群管理员权限。

Helm架构

在这里插入图片描述

安装Helm[客户端]

在命令行执行以下命令。

curl -L https://git.io/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. 创建名称为tillerService Account
  2. 创建tillerService Account具有集群管理员权限的ClusterRoleBinding

我们将在一个yaml文件中添加Service AccountclusterRoleBinding

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

apiVersion: v1
kind: ServiceAccount
metadata:
  name: tiller
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: tiller
roleRef:
  apiGroup: rbac.authorization.k8s.io
  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参数指定tillerimage地址。可以在公共Google GCR注册中心找到所有tillerdocker映像。由于国内网络原因,可以从阿里镜像仓库拉取。即把gcr.io/kubernetes-helm/tiller:v2.14.1替换为registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1

helm init --service-account=tiller --tiller-image=registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1   --history-max 300

执行后如下:

[root@master helm]# helm init --service-account=tiller --tiller-image=registry.cn-hangzhou.aliyuncs.com/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

作者简介

作者:小碗汤,一位热爱、认真写作的小伙,目前维护原创公众号:『我的小碗汤』,专注于写golang、docker、kubernetes等知识等提升硬实力的文章,期待你的关注。 转载说明:务必注明来源(注明:来源于公众号:我的小碗汤, 作者:小碗汤)

作者简洁

作者:小碗汤,一位热爱、认真写作的小伙,目前维护原创公众号:『我的小碗汤』,专注于写go语言、docker、kubernetes、java等开发、运维知识等提升硬实力的文章,期待你的关注。转载说明:务必注明来源(注明:来源于公众号:我的小碗汤,作者:小碗汤)