简介
很多人都使用过Ubuntu下的ap-get或者CentOS下的yum, 这两者都是Linux系统下的包管理工具。采用apt-get/yum,应用开发者可以管理应用包之间的依赖关系,发布应用;用户则可以以简单的方式查找、安装、升级、卸载应用程序。
我们可以将Helm看作Kubernetes下的apt-get/yum。Helm是Deis (https://deis.com/) 开发的一个用于kubernetes的包管理器。每个包称为一个Chart,一个Chart是一个目录(一般情况下会将目录进行打包压缩,形成name-version.tgz格式的单一文件,方便传输和存储)。
对于应用发布者而言,可以通过Helm打包应用,管理应用依赖关系,管理应用版本并发布应用到软件仓库。
对于使用者而言,使用Helm后不用需要了解Kubernetes的Yaml语法并编写应用部署文件,可以通过Helm下载并在kubernetes上安装需要的应用。
除此以外,Helm还提供了kubernetes上的软件部署,删除,升级,回滚应用的强大功能。
Helm 组件及相关术语
Helm
Helm 是一个命令行下的客户端工具。主要用于 Kubernetes 应用程序 Chart 的创建、打包、发布以及创建和管理本地和远程的 Chart 仓库。
Tiller
Tiller 是 Helm 的服务端,部署在 Kubernetes 集群中。Tiller 用于接收 Helm 的请求,并根据 Chart 生成 Kubernetes 的部署文件( Helm 称为 Release ),然后提交给 Kubernetes 创建应用。Tiller 还提供了 Release 的升级、删除、回滚等一系列功能。
Chart
Helm 的软件包,采用 TAR 格式。类似于 APT 的 DEB 包或者 YUM 的 RPM 包,其包含了一组定义 Kubernetes 资源相关的 YAML 文件。
Repoistory
Helm 的软件仓库,Repository 本质上是一个 Web 服务器,该服务器保存了一系列的 Chart 软件包以供用户下载,并且提供了一个该 Repository 的 Chart 包的清单文件以供查询。Helm 可以同时管理多个不同的 Repository。
Release
使用 helm install 命令在 Kubernetes 集群中部署的 Chart 称为 Release。
注:需要注意的是:Helm 中提到的 Release 和我们通常概念中的版本有所不同,这里的 Release 可以理解为 Helm 使用 Chart 包部署的一个应用实例。
Helm工作原理
Chart Install 过程:
- Helm从指定的目录或者tgz文件中解析出Chart结构信息
- Helm将指定的Chart结构和Values信息通过gRPC传递给Tiller
- Tiller根据Chart和Values生成一个Release
- Tiller将Release发送给Kubernetes用于生成Release
Chart Update过程:
- Helm从指定的目录或者tgz文件中解析出Chart结构信息
- Helm将要更新的Release的名称和Chart结构,Values信息传递给Tiller
- Tiller生成Release并更新指定名称的Release的History
- Tiller将Release发送给Kubernetes用于更新Release
Chart Rollback过程:
- Helm将要回滚的Release的名称传递给Tiller
- Tiller根据Release的名称查找History
- Tiller从History中获取上一个Release
- Tiller将上一个Release发送给Kubernetes用于替换当前Release
Helm的安装
helm 安装
Helm由客户端命helm令行工具和服务端tiller组成,Helm的安装十分简单。 下载helm命令行工具到master节点node1的/usr/local/bin下,这里下载的2.14.1版本:
- curl -O https://get.helm.sh/helm-v2.14.1-linux-amd64.tar.gz
- tar -zxvf helm-v2.14.1-linux-amd64.tar.gz
- cd linux-amd64/
- cp helm /usr/local/bin/
为了安装服务端tiller,还需要在这台机器上配置好kubectl工具和kubeconfig文件,确保kubectl工具可以在这台机器上访问apiserver且正常使用。 这里的master节点已经配置好了kubectl。
创建tiller账号
因为Kubernetes APIServer开启了RBAC访问控制,所以需要创建tiller使用的service account: tiller并分配合适的角色给它。 详细内容可以查看helm文档中的Role-based Access Control。 这里简单起见直接分配cluster-admin这个集群内置的ClusterRole给它。创建helm-rbac.yaml文件:
- apiVersion: v1
- kind: ServiceAccount
- metadata:
- name: tiller
- namespace: kube-system
- ---
- apiVersion: rbac.authorization.k8s.io/v1beta1
- kind: ClusterRoleBinding
- metadata:
- name: tiller
- roleRef:
- apiGroup: rbac.authorization.k8s.io
- kind: ClusterRole
- name: cluster-admin
- subjects:
- - kind: ServiceAccount
- name: tiller
- namespace: kube-system
创建账号:
- [root@master /]# kubectl create -f helm-rbac.yaml
- serviceaccount/tiller created
- clusterrolebinding.rbac.authorization.k8s.io/tiller created
部署tiller
接下来使用helm部署tiller:
- [root@master /]# helm init --service-account tiller --skip-refresh
- 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
由于 Helm 默认会去 kubernetes-charts.storage.googleapis.com
拉取镜像,如果你当前执行的机器不能访问该域名的话可以使用以下命令来安装:
- # 创建服务端
- helm init --service-account tiller --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
-
- # 创建TLS认证服务端,参考地址:https://github.com/gjmzj/kubeasz/blob/master/docs/guide/helm.md
- helm init --service-account tiller --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1 --tiller-tls-cert /etc/kubernetes/ssl/tiller001.pem --tiller-tls-key /etc/kubernetes/ssl/tiller001-key.pem --tls-ca-cert /etc/kubernetes/ssl/ca.pem --tiller-namespace kube-system --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
client:
- helm init --client-only --stable-repo-url https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/charts/
- helm repo add incubator https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/charts-incubator/
- helm repo update
tiller默认被部署在k8s集群中的kube-system这个namespace下:
- [root@master /]# kubectl get pod -n kube-system -l app=helm
- NAME READY STATUS RESTARTS AGE
- tiller-deploy-7bf78cdbf7-nclnr 0/1 ImagePullBackOff 0 40s
查看:
- [root@master /]# helm version
- Client: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", GitTreeState:"clean"}
- Error: could not find a ready tiller pod
异常修复
发现没有启动起来,查看错误:
- [root@master /]# kubectl describe pod tiller-deploy-7bf78cdbf7-nclnr -n kube-system
- 。。。。。。。
- Events:
- Type Reason Age From Message
- ---- ------ ---- ---- -------
- Normal Pulling 29m (x4 over 32m) kubelet, slaver1 Pulling image "gcr.io/kubernetes-helm/tiller:v2.14.1"
- Warning Failed 29m (x4 over 31m) kubelet, slaver1 Failed to pull image "gcr.io/kubernetes-helm/tiller:v2.14.1": rpc error: code = Unknown desc = Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
- Warning Failed 29m (x4 over 31m) kubelet, slaver1 Error: ErrImagePull
- Normal BackOff 29m (x6 over 31m) kubelet, slaver1 Back-off pulling image "gcr.io/kubernetes-helm/tiller:v2.14.1"
- Normal Scheduled 28m default-scheduler Successfully assigned kube-system/tiller-deploy-7bf78cdbf7-nclnr to slaver1
- Warning Failed 6m58s (x97 over 31m) kubelet, slaver1 Error: ImagePullBackOff
明显就是网络问题,镜像拉去不下来
- [root@master /]# helm init --service-account tiller --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/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
还是有问题
直接删除/root/.helm
安装socat
[root@master /]# yum install socat -y
再卸载helm
[root@master /]# helm reset -f
再次执行
- [root@master /]# helm init --service-account tiller --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
- 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.oss-cn-hangzhou.aliyuncs.com/charts
- 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 upgraded to the current version.
- [root@master /]# helm version
- Client: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", GitTreeState:"clean"}
- Server: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", GitTreeState:"clean"}
安装完成后查看
- [root@master /]# kubectl get pods -n kube-system -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- coredns-5c98db65d4-gts57 1/1 Running 0 3d22h 10.244.2.2 slaver2 <none> <none>
- coredns-5c98db65d4-qhwrw 1/1 Running 0 3d22h 10.244.1.2 slaver1 <none> <none>
- etcd-master 1/1 Running 2 3d22h 18.16.202.163 master <none> <none>
- kube-apiserver-master 1/1 Running 2 3d22h 18.16.202.163 master <none> <none>
- kube-controller-manager-master 1/1 Running 6 3d22h 18.16.202.163 master <none> <none>
- kube-flannel-ds-amd64-2lwl8 1/1 Running 0 3d18h 18.16.202.227 slaver1 <none> <none>
- kube-flannel-ds-amd64-9bjck 1/1 Running 0 3d18h 18.16.202.95 slaver2 <none> <none>
- kube-flannel-ds-amd64-gxxqg 1/1 Running 0 3d18h 18.16.202.163 master <none> <none>
- kube-proxy-8cwj4 1/1 Running 0 18h 18.16.202.163 master <none> <none>
- kube-proxy-j9zpz 1/1 Running 0 18h 18.16.202.227 slaver1 <none> <none>
- kube-proxy-vfgjv 1/1 Running 0 18h 18.16.202.95 slaver2 <none> <none>
- kube-scheduler-master 1/1 Running 6 3d22h 18.16.202.163 master <none> <none>
- tiller-deploy-6787c946f8-qm9cn 1/1 Running 0 15h 10.244.1.5 slaver1 <none> <none>
可以看到只在slaver1
节点上面才安装了tiller
helm 使用
更换仓库
- # 先移除原先的仓库
- helm repo remove stable
- # 添加新的仓库地址
- helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
- # 更新仓库
- helm repo update
阿里云的仓库 更新太慢,换成微软的
helm repo add stable http://mirror.azure.cn/kubernetes/charts/
查看仓库:
- [root@master /]# helm repo list
- NAME URL
- stable http://mirror.azure.cn/kubernetes/charts/
- local http://127.0.0.1:8879/charts
查看在存储库中可用的所有 Helm charts:
- [root@master /]# helm search
- NAME CHART VERSION APP VERSION DESCRIPTION
- stable/acs-engine-autoscaler 2.1.3 2.1.1 Scales worker nodes within agent pools
- stable/aerospike 0.1.7 v3.14.1.2 A Helm chart for Aerospike in Kubernetes
- stable/anchore-engine 0.1.3 0.1.6 Anchore container analysis and policy evaluation engine s...
- stable/artifactory 7.0.3 5.8.4 Universal Repository Manager supporting all major packagi...
- stable/artifactory-ha 0.1.0 5.8.4 Universal Repository Manager supporting all major packagi...
- stable/aws-cluster-autoscaler 0.3.2 Scales worker nodes within autoscaling groups.
- stable/bitcoind 0.1.0 0.15.1 Bitcoin is an innovative payment network and a new kind o...
- stable/buildkite 0.2.1 3 Agent for Buildkite
- stable/centrifugo 2.0.0 1.7.3 Centrifugo is a real-time messaging server.
- stable/cert-manager 0.2.2 0.2.3 A Helm chart for cert-manager
- stable/chaoskube 0.6.2 0.6.1 Chaoskube periodically kills random pods in your Kubernet...
- stable/chronograf 0.4.2 Open-source web application written in Go and React.js th...
- stable/cluster-autoscaler 0.4.2 1.1.0 Scales worker nodes within autoscaling groups.
- stable/cockroachdb 0.6.5 1.1.5 CockroachDB is a scalable, survivable, strongly-consisten...
- stable/concourse 1.0.2 3.9.0 Concourse is a simple and scalable CI system.
- stable/consul 1.3.1 1.0.0 Highly available and distributed service discovery and ke...
- stable/coredns 0.8.0 1.0.1 CoreDNS is a DNS server that chains plugins and provides ...
- stable/coscale 0.2.0 3.9.1 CoScale Agent
- stable/dask-distributed 2.0.0 Distributed computation in Python
- stable/datadog 0.10.9 DataDog Agent
- stable/docker-registry 1.0.3 2.6.2 A Helm chart for Docker Registry
- 。。。。。。
卸载helm
helm reset
Helm Chart 结构
Chart 目录结构
- examples/
- Chart.yaml # Yaml文件,用于描述Chart的基本信息,包括名称版本等
- LICENSE # [可选] 协议
- README.md # [可选] 当前Chart的介绍
- values.yaml # Chart的默认配置文件
- requirements.yaml # [可选] 用于存放当前Chart依赖的其它Chart的说明文件
- charts/ # [可选]: 该目录中放置当前Chart依赖的其它Chart
- templates/ # [可选]: 部署文件模版目录,模版使用的值来自values.yaml和由Tiller提供的值
- templates/NOTES.txt # [可选]: 放置Chart的使用指南
Chart.yaml 文件
- name: [必须] Chart的名称
- version: [必须] Chart的版本号,版本号必须符合 SemVer 2:http://semver.org/
- description: [可选] Chart的简要描述
- keywords:
- - [可选] 关键字列表
- home: [可选] 项目地址
- sources:
- - [可选] 当前Chart的下载地址列表
- maintainers: # [可选]
- - name: [必须] 名字
- email: [可选] 邮箱
- engine: gotpl # [可选] 模版引擎,默认值是gotpl
- icon: [可选] 一个SVG或PNG格式的图片地址
requirements.yaml 和 charts目录
requirements.yaml 文件内容:
- dependencies:
- - name: example
- version: 1.2.3
- repository: http://example.com/charts
- - name: Chart名称
- version: Chart版本
- repository: 该Chart所在的仓库地址
Chart支持两种方式表示依赖关系,可以使用requirements.yaml或者直接将依赖的Chart放置到charts目录中。
templates 目录
templates目录中存放了Kubernetes部署文件的模版。
- # db.yaml
- apiVersion: v1
- kind: ReplicationController
- metadata:
- name: deis-database
- namespace: deis
- labels:
- heritage: deis
- spec:
- replicas: 1
- selector:
- app: deis-database
- template:
- metadata:
- labels:
- app: deis-database
- spec:
- serviceAccount: deis-database
- containers:
- - name: deis-database
- image: {{.Values.imageRegistry}}/postgres:{{.Values.dockerTag}}
- imagePullPolicy: {{.Values.pullPolicy}}
- ports:
- - containerPort: 5432
- env:
- - name: DATABASE_STORAGE
- value: {{default "minio" .Values.storage}}
创建自己的chart
我们创建一个名为mongodb
的chart,看一看chart的文件结构。
- [root@master /]# helm create mongodb
- Creating mongodb
-
- [root@master /]# tree mongodb
- mongodb
- ├── charts #依赖的chart
- ├── Chart.yaml #Chart本身的版本和配置信息
- ├── templates #配置模板目录
- │ ├── deployment.yaml #kubernetes Deployment object
- │ ├── _helpers.tpl #用于修改kubernetes objcet配置的模板
- │ ├── ingress.yaml
- │ ├── NOTES.txt #helm提示信息
- │ ├── service.yaml #kubernetes Serivce
- │ └── tests
- │ └── test-connection.yaml
- └── values.yaml #kubernetes object configuration
-
- 3 directories, 8 files