目录
Kubernetes之(二十)Helm程序包管理器
概念
部署Helm
下载helm
* 部署Tiller
helm的使用
chart 目录结构
chart模板
定制安装 MySQL chart
chart安装准备
* 定制化安装 chart
* 升级和回滚release
自定义chart
创建chart
* 调试chart
* 安装chart
* 将chart添加到仓库
总结
Kubernetes之(二十)Helm程序包管理器
概念
每个成功的软件平台都有一个优秀的打包系统,比如 Debian、Ubuntu 的 apt,Redhat、Centos 的 yum。而 Helm 则是 Kubernetes 上的包管理器。
Kubernetes 能够很好地组织和编排容器,但它缺少一个更高层次的应用打包工具,而 Helm 就是来干这件事的。
举个例子,我们需要部署一个MySQL服务,Kubernetes则需要部署以下对象:
① 为了能够让外界访问到MySQL,需要部署一个mysql的service;
②需要进行定义MySQL的密码,则需要部署一个Secret;
③Mysql的运行需要持久化的数据存储,此时还需要部署PVC;
④保证后端mysql的运行,还需要部署一个Deployment,以支持以上的对象。
针对以上对象,我们可以使用YAML文件进行定义并部署,但是仅仅对于单个的服务支持,如果应用需要由一个甚至几十个这样的服务组成,并且还需要考虑各种服务的依赖问题,可想而知,这样的组织管理应用的方式就显得繁琐。为此就诞生了一个工具Helm,就是为了解决Kubernetes这种应用部署繁重的现象。
Helm的核心术语:
Chart:一个helm程序包,是创建一个应用的信息集合,包含各种Kubernetes对象的配置模板、参数定义、依赖关系、文档说明等。可以将Chart比喻为yum中的软件安装包;
Repository:Charts仓库,用于集中存储和分发Charts;
Config:应用程序实例化安装运行时所需要的配置信息;
Release:特定的Chart部署于目标集群上的一个实例,代表这一个正在运行的应用。当chart被安装到Kubernetes集群,就会生成一个release,chart可以多次安装到同一个集群,每次安装都是一个release。
Helm的程序架构:
Helm主要由Helm客户端、Tiller服务器和Charts仓库组成,如下图:
helm:客户端,GO语言编写,实现管理本地的Chart仓库,可管理Chart,与Tiller服务进行交互,用于发送Chart,实例安装、查询、卸载等操作。
Tiller:服务端,通常运行在K8S集群之上。用于接收helm发来的Charts和Conifg,合并生成release,完成部署。
简单的说:Helm 客户端负责管理 chart;Tiller 服务器负责管理 release。
部署Helm
下载helm
Helm的部署方式有两种:预编译的二进制程序和源码编译安装,这里使用二进制的方式进行安装
root@master manifestsroot@master manifests
root@master manifests
root@master linux-amd64
helm LICENSE README.md
root@master linux-amd64
root@master linux-amd64
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
部署Tiller
helm第一次init时,需要链接api-server并进行认证,所以在运行helm时,会去读取kube-config文件,所以必须确认当前用户存在kube-config文件。
Tiller运行在K8s集群之上,也必须拥有集群的管理权限,也就是需要一个serviceaccount,进行一个clusterrolebinding到cluster-admin。
Tiller的RBAC配置示例链接:
https://github.com/helm/helm/blob/master/docs/rbac.md
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
如果希望在安装时自定义一些参数,可以参考一下的一些参数:
--canary-image:安装canary分支,即项目的Master分支
--tiller-image:安装指定版本的镜像,默认和helm同版本
--kube-context:安装到指定的Kubernetes集群
--tiller-namespace:安装到指定的名称空间,默认为kube-system
Tiller将数据存储在ConfigMap资源当中,卸载或重装不会导致数据丢失,卸载Tiller的方法有以下两种:
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
helm的使用
官方可用的Chart列表:
https://hub.kubeapps.com
root@master helmThe Kubernetes package manager
To begin working with Helm, run the command:
$ helm init
This will Tiller to your running Kubernetes cluster.
It will also up any necessary configuration.
Common actions from this point include:
- helm search: search charts
- helm fetch: download a chart to your directory to view
- helm install: upload the chart to Kubernetes
- helm list: list releases of charts
Environment:
an alternative location Helm files. By default, these are stored ~/.helm
an alternative Tiller host. The is host:port
disable plugins. Set to disable plugins.
an alternative Tiller namespace default
an alternative Kubernetes configuration default
Usage:
helm command
Available Commands:
completion Generate autocompletions script the specified shell bash or
create create a new chart with the given name
delete given a release name, delete the release from Kubernetes
dependency manage a chart's dependencies
fetch download a chart from a repository and optionally unpack it directory
get download a named release
fetch release
home displays the location of HELM_HOME
init initialize Helm on both client and server
inspect inspect a chart
a chart archive
lint examines a chart possible issues
list list releases
package package a chart directory into a chart archive
plugin add, list, or remove Helm plugins
repo add, list, remove, update, and index chart repositories
reset uninstalls Tiller from a cluster
rollback roll back a release to a previous revision
search search a keyword charts
serve start a http web server
status displays the status of the named release
template locally render templates
a release
upgrade upgrade a release
verify verify that a chart at the given path has been signed and is valid
version print the client/server version information
Charts是Helm的程序包,它们都存在在Charts仓库当中。Kubernetes官方的仓库保存了一系列的Charts,仓库默认的名称为stable。安装Charts到集群时,Helm首先会到官方仓库获取相关的Charts,并创建release。可执行 helm search 查看当前可安装的 chart 。
Helm 可以像 yum 管理软件包一样管理 chart。 yum 的软件包存放在仓库中,同样的,Helm 也有仓库。
Helm 安装时已经默认配置好了两个仓库:stable 和 local。stable 是官方仓库,local 是用户存放自己开发的chart的本地仓库。可以通过helm repo list进行查看。由于网络原因,国内可能无法更新仓库源(网络不稳定偶尔出问题),这里可以更改为阿里云的仓库源,。
root@master helm has been removed from your repositories
root@master helm
root@master helm
has been added to your repositories
root@master helm
root@master helm
Hang tight we grab the latest from your chart repositories.
.Skip chart repository
.Successfully got an update from the chart repository
Update Complete. ⎈ Happy Helming⎈
root@master helm
NAME URL
http://127.0.0.1:8879/charts
stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
与 yum 一样,helm 也支持关键字搜索:
root@master helmNAME CHART VERSION APP VERSION DESCRIPTION
stable/mysql .0 .14 Fast, reliable, scalable, and easy to use open-.
stable/mysqldump .0 .0 A Helm chart to backup MySQL databases usi
使用helm inspect也可以查看详细信息
root@master helm
包括 DESCRIPTION 在内的所有信息,只要跟关键字匹配,都会显示在结果列表中。
安装 chart 也很简单,执行如下命令可以安装 MySQL。
root@master helmError: no available release name found
root@master helm
Error from server AlreadyExists: serviceaccounts already exists
root@master helm
clusterrolebinding.rbac.authorization.k8s.io/tiller-cluster-rule created
root@master helm
deployment.extensions/tiller-deploy patched no change
root@master helm
NAME: callous-zorse
LAST DEPLOYED: Thu Apr 09:31:41
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
v1/Secret
NAME TYPE DATA AGE
callous-zorse-mysql Opaque 0s
v1/ConfigMap
NAME DATA AGE
callous-zorse-mysql-test 0s
v1/PersistentVolumeClaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
callous-zorse-mysql Pending 0s
v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORTS AGE
callous-zorse-mysql ClusterIP .125.202 none /TCP 0s
v1beta1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
callous-zorse-mysql 0s
v1/Podrelated
NAME READY STATUS RESTARTS AGE
callous-zorse-mysql-f5c97689b-ch5cf /1 Pending 0s
NOTES:
MySQL can be accessed via port on the following DNS name from within your cluster:
callous-zorse-mysql.default.svc.cluster.local
To get your root password run:
To connect to your database:
. Run an Ubuntu pod that you can use as a client:
kubectl run -i --tty ubuntu --imageubuntu:16.04 --restartNever -- -il
. Install the mysql client:
$ update mysql-client -y
. Connect using the mysql cli, provide your password:
$ mysql -h callous-zorse-mysql -p
To connect to your database directly from outside the K8s cluster:
.0.1
kubectl port-forward svc/callous-zorse-mysql
mysql -h -P -u root -p
输出分为三部分:
chart 本次部署的描述信息:
NAME 是 release 的名字,因为我们没用 -n 参数指定,Helm 随机生成了一个,这里是 callous-zorse。
NAMESPACE 是 release 部署的 namespace,默认是 default,也可以通过 --namespace 指定。
STATUS 为 DEPLOYED,表示已经将 chart 部署到集群。
当前 release 包含的资源:Service、Deployment、Secret 和 PersistentVolumeClaim,其名字都是 callous-zorse-mysql,命名的格式为 ReleasName-ChartName。
NOTES 部分显示的是 release 的使用方法。比如如何访问 Service,如何获取数据库密码,以及如何连接数据库等。
通过 kubectl get 可以查看组成 release 的各个对象:
root@master helmNAME READY UP-TO-DATE AVAILABLE AGE
deployment.extensions/callous-zorse-mysql /1 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORTS AGE
service/callous-zorse-mysql ClusterIP .125.202 none /TCP 10m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/callous-zorse-mysql Pending 10m
NAME TYPE DATA AGE
secret/callous-zorse-mysql Opaque 10m
由于我们还没有准备 PersistentVolume,当前 release 还不可用。
helm list 显示已经部署的 release,helm delete 可以删除 release。
root@master helmNAME REVISION UPDATED STATUS CHART NAMESPACE
callous-zorse Thu Apr 09:31:41 DEPLOYED mysql-0.15.0 default
root@master helm
release deleted
chart 目录结构
chart 是 Helm 的应用打包格式。chart 由一系列文件组成,这些文件描述了 Kubernetes 部署应用时所需要的资源,比如 Service、Deployment、PersistentVolumeClaim、Secret、ConfigMap 等。
单个的 chart 可以非常简单,只用于部署一个服务,比如 Memcached;chart 也可以很复杂,部署整个应用,比如包含 HTTP Servers、 Database、消息中间件、cache 等。
chart 将这些文件放置在预定义的目录结构中,通常整个 chart 被打成 tar 包,而且标注上版本信息,便于 Helm 部署。
以前面 MySQL chart为例。一旦安装了某个 chart,我们就可以在 ~/.helm/cache/archive 中找到 chart 的 tar 包。
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
0
Chart.yaml:YAML 文件,描述 chart 的概要信息。
README.md:Markdown 格式的 README 文件,相当于 chart 的使用文档,此文件为可选。
LICENSE:文本文件,描述 chart 的许可信息,此文件为可选。
requirements.yaml :chart 可能依赖其他的 chart,这些依赖关系可通过 requirements.yaml 指定。
values.yaml:chart 支持在安装的时根据参数进行定制化配置,而 values.yaml 则提供了这些配置参数的默认值。
templates目录:各类 Kubernetes 资源的配置模板都放置在这里。Helm 会将 values.yaml 中的参数值注入到模板中生成标准的 YAML 配置文件。
templates/NOTES.txt:chart 的简易使用文档,chart 安装成功后会显示此文档内容。 与模板一样,可以在 NOTE.txt 中插入配置参数,Helm 会动态注入参数值。
chart模板
Helm 通过模板创建 Kubernetes 能够理解的 YAML 格式的资源配置文件,我们将通过例子来学习如何使用模板。
以 templates/secrets.yaml 为例:
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
1
从结构上看,文件的内容和我们在定义Secret的配置上大致相似,只是大部分的属性值变成了{{ xxx }}。这些{{ xx }}实际上是模板的语法。Helm采用了Go语言的模板来编写chart。
{{ template "mysql.fullname" . }} 定义 Secret 的 name
关键字 template 的作用是引用一个子模板 mysql.fullname。这个子模板是在 templates/_helpers.tpl 文件中定义的。
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
2
这个定义还是很复杂的,因为它用到了模板语言中的对象、函数、流控制等概念。现在看不懂没关系,这里我们学习的重点是:如果存在一些信息多个模板都会用到,则可在 templates/_helpers.tpl 中将其定义为子模板,然后通过 templates 函数引用。
这里 mysql.fullname 是由 release 与 chart 二者名字拼接组成。
根据 chart 的最佳实践,所有资源的名称都应该保持一致,对于我们这个 chart,无论 Secret 还是 Deployment、PersistentVolumeClaim、Service,它们的名字都是子模板 mysql.fullname 的值。
Chart 和 Release 是 Helm 预定义的对象,每个对象都有自己的属性,可以在模板中使用。如果使用下面命令安装 chart:
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
3
则
{{ .Chart.Name }} 的值为 mysql。
{{ .Chart.Version }} 的值为 0.15.0。
{{ .Release.Name }}的值为 my。
{{ .Release.Service }} 始终取值为 Tiller.
{{ template "mysql.fullname" . }} 计算结果为 my-mysql。
这里指定 mysql-root-password 的值,不过使用了 if-else 的流控制,其逻辑为:
如果 .Values.mysqlRootPassword有值,则对其进行 base64 编码;否则随机生成一个 10 位的字符串并编码。
Values 也是预定义的对象,代表的是values.yaml 文件。而 .Values.mysqlRootPassword 则是values.yaml中定义的 mysqlRootPassword参数:
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
4
因为 mysqlRootPassword 被注释掉了,没有赋值,所以逻辑判断会走 else,即随机生成密码。
randAlphaNum、b64enc、quote 都是 Go 模板语言支持的函数,函数之间可以通过管道 | 连接。
{{ randAlphaNum 10 | b64enc | quote }} 的作用是首先随机产生一个长度为 10 的字符串,然后将其 base64 编码,最后两边加上双引号。
templates/secrets.yaml 这个例子展示了chart 模板主要的功能,我们最大的收获应该是:模板将 chart参数化了,通过 values.yaml 可以灵活定制应用。
无论多复杂的应用,用户都可以用 Go 模板语言编写出 chart。无非是使用到更多的函数、对象和流控制
定制安装 MySQL chart
chart安装准备
作为准备工作,安装之前需要先清楚 chart 的使用方法。这些信息通常记录在 values.yaml 和 README.md 中。除了下载源文件查看,执行 helm inspect values 可能是更方便的方法。
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
5
输出的实际上是 values.yaml 的内容。阅读注释就可以知道 MySQL chart 支持哪些参数,安装之前需要做哪些准备。其中有一部分是关于存储的:
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
6
chart 定义了一个 PersistentVolumeClaim,申请 8G 的 PersistentVolume。由于我们的实验环境不支持动态供给,所以得预先创建好相应的 PV,其配置文件 mysql-pv.yml 内容为:
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
7
定制化安装 chart
除了接受 values.yaml 的默认值,我们还可以定制化 chart,比如设置 mysqlRootPassword
Helm有两种方式传递配置参数:
指定自己的values文件,通常是先通过helm inspect values mysql > myvalues.yaml生成values,然后设置mysqlRootPassword之后执行 helm install --values=myvalues.yaml mysql
通过 --set 直接传入参数值,比如:
root@master linux-amd64root@master manifests
root@master manifests
root@master helm
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
root@master helm
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
root@master helm
tiller 14s
root@master helm
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 repo with URL: http://127.0.0.1:8879/charts
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 policy.
For information on securing your installation see: https://docs.helm.sh/using_helm/
Happy Helming
root@master helm
root@master pki
root@master helm
NAME READY STATUS RESTARTS AGE
canal-nbspn /3 Running 30h
canal-pj6rx /3 Running 30h
canal-rgsnp /3 Running 30h
coredns-78d4cf999f-6cb69 /1 Running 14d
coredns-78d4cf999f-tflpn /1 Running 14d
etcd-master /1 Running 14d
kube-apiserver-master /1 Running 14d
kube-controller-manager-master /1 Running 14d
kube-flannel-ds-amd64-5zrk7 /1 Running 31h
kube-flannel-ds-amd64-pql5n /1 Running 31h
kube-flannel-ds-amd64-ssd29 /1 Running 31h
kube-proxy-ch4vp /1 Running 14d
kube-proxy-cz2rf /1 Running 14d
kube-proxy-kdp7d /1 Running 14d
kube-scheduler-master /1 Running 14d
kubernetes-dashboard-6f9998798-klf4t /1 Running 2d2h
metrics-server-v0.3.1-65bd5d59b9-xvmns /2 Running 5h31m
tiller-deploy-c4f47c598-gl6rp /1 Running 11m
root@master helm
Client: version.VersionSemVer:, GitCommit:, GitTreeState:
Server: version.VersionSemVer:, GitCommit:, GitTreeState:
8
mysqlRootPassword设置为 abc123。另外,-n设置elease 为 my-2,各类资源的名称即为my-2-mysql。
通过 helm list 和helm status 可以查看 chart 的最新状态。
升级和回滚release
release 发布后可以执行 helm upgrade 对其升级,通过 --values 或 --set应用新的配置。比如将当前的 MySQL 版本升级到 5.7.15:
root@master helm
helm history 可以查看 release 所有的版本。通过 helm rollback 可以回滚到任何版本。
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
0
自定义chart
Kubernetes 给我们提供了大量官方 chart,不过要部署微服务应用,还是需要开发自己的 chart。
创建chart
执行 helm create mychart 的命令创建 chart mychart:
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
1
Helm 会帮我们创建目录 mychart,并生成了各类 chart 文件。这样我们就可以在此基础上开发自己的 chart 了。
调试chart
elm 提供了 debug 的工具:helm lint 和 helm install --dry-run --debug。
helm lint会检测 chart 的语法,报告错误以及给出建议。 故意修改mychart中的value.yaml,进行检测:
helm lint mychart会指出这个语法错误。
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
2
helm install --dry-run --debug 会模拟安装 chart,并输出每个模板生成的 YAML 内容。
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
3
我们可以检视这些输出,判断是否与预期相符。
安装chart
Helm自持四种方法安装chart:
安装仓库中的 chart,例如:helm install stable/nginx
通过 tar 包安装,例如:helm install ./nginx-1.2.3.tgz
通过 chart 本地目录安装,例如:helm install ./nginx
通过 URL 安装,例如:helm install https://example.com/charts/nginx-1.2.3.tgz
本次使用本地目录安装
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
4
当 chart 部署到 Kubernetes 集群,便可以对其进行更为全面的测试。
将chart添加到仓库
chart 通过测试后可以将其添加到仓库,团队其他成员就能够使用。任何 HTTP Server 都可以用作 chart 仓库,下面演示在 nfs 10.0.0.14 上搭建仓库。
在nfs上启动nginx(容器也可以)并创建一个server标签(由于实验用nfs已经有nginx服务,新建个server标签监听8080端口)
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
5
通过 helm package 将 mychart 打包。
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
6
Helm 会扫描 myrepo 目录中的所有 tgz 包并生成 index.yaml。--url指定的是新仓库的访问路径。新生成的 index.yaml 记录了当前仓库中所有 chart 的信息:
当前只有 mychart 这一个 chart。
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
7
将 mychart-0.1.0.tgz 和 index.yaml 上传到 k8s-node1 的 /var/www/charts 目录。
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
8
通过 helm repo add 将新仓库添加到 Helm。
kubectl delete deployment tiller-deploy -n kube-systemhelm reset
9
现在已经可以 repo search 到 mychart 了。
[root@master helm]# helm search mychart NAME CHART VERSION APP VERSION DESCRIPTION local/mychart 0.1.0 1.0 A Helm chart for Kubernetes nfs_repo/mychart 0.1.0 1.0 A Helm chart for Kubernetes
除了 newrepo/mychart,这里还有一个 local/mychart。这是因为在执行打包操作的同时,mychart 也被同步到了 local 的仓库。
已经可以直接从新仓库安装 mychart 了。
root@master helmThe Kubernetes package manager
To begin working with Helm, run the command:
$ helm init
This will Tiller to your running Kubernetes cluster.
It will also up any necessary configuration.
Common actions from this point include:
- helm search: search charts
- helm fetch: download a chart to your directory to view
- helm install: upload the chart to Kubernetes
- helm list: list releases of charts
Environment:
an alternative location Helm files. By default, these are stored ~/.helm
an alternative Tiller host. The is host:port
disable plugins. Set to disable plugins.
an alternative Tiller namespace default
an alternative Kubernetes configuration default
Usage:
helm command
Available Commands:
completion Generate autocompletions script the specified shell bash or
create create a new chart with the given name
delete given a release name, delete the release from Kubernetes
dependency manage a chart's dependencies
fetch download a chart from a repository and optionally unpack it directory
get download a named release
fetch release
home displays the location of HELM_HOME
init initialize Helm on both client and server
inspect inspect a chart
a chart archive
lint examines a chart possible issues
list list releases
package package a chart directory into a chart archive
plugin add, list, or remove Helm plugins
repo add, list, remove, update, and index chart repositories
reset uninstalls Tiller from a cluster
rollback roll back a release to a previous revision
search search a keyword charts
serve start a http web server
status displays the status of the named release
template locally render templates
a release
upgrade upgrade a release
verify verify that a chart at the given path has been signed and is valid
version print the client/server version information
0
如果以后仓库添加了新的 chart,需要用 helm repo update 更新本地的 index。类似于yum update /apt-get update
root@master helmThe Kubernetes package manager
To begin working with Helm, run the command:
$ helm init
This will Tiller to your running Kubernetes cluster.
It will also up any necessary configuration.
Common actions from this point include:
- helm search: search charts
- helm fetch: download a chart to your directory to view
- helm install: upload the chart to Kubernetes
- helm list: list releases of charts
Environment:
an alternative location Helm files. By default, these are stored ~/.helm
an alternative Tiller host. The is host:port
disable plugins. Set to disable plugins.
an alternative Tiller namespace default
an alternative Kubernetes configuration default
Usage:
helm command
Available Commands:
completion Generate autocompletions script the specified shell bash or
create create a new chart with the given name
delete given a release name, delete the release from Kubernetes
dependency manage a chart's dependencies
fetch download a chart from a repository and optionally unpack it directory
get download a named release
fetch release
home displays the location of HELM_HOME
init initialize Helm on both client and server
inspect inspect a chart
a chart archive
lint examines a chart possible issues
list list releases
package package a chart directory into a chart archive
plugin add, list, or remove Helm plugins
repo add, list, remove, update, and index chart repositories
reset uninstalls Tiller from a cluster
rollback roll back a release to a previous revision
search search a keyword charts
serve start a http web server
status displays the status of the named release
template locally render templates
a release
upgrade upgrade a release
verify verify that a chart at the given path has been signed and is valid
version print the client/server version information
1
总结
Helm是Kubernetes的包管理器,Helm 让我们能够像 yum 管理 rpm 包那样安装、部署、升级和删除容器化应用。
Helm 由客户端和 Tiller 服务器组成。客户端负责管理 chart,服务器负责管理 release。
chart 是 Helm 的应用打包格式,它由一组文件和目录构成。其中最重要的是模板,模板中定义了 Kubernetes 各类资源的配置信息,Helm 在部署时通过 values.yaml 实例化模板。
Helm 允许用户开发自己的 chart,并为用户提供了调试工具。用户可以搭建自己的 chart 仓库,在团队中共享 chart。
还没有评论,来说两句吧...