1. Helm 简介
Helm是Deis (https://deis.com/) 开发的一个用于kubernetes的包管理器,相当于kubernetes环境下的yum包管理工具。
1) 重要概念
Helm 有三个重要概念:
- chart:包含了创建Kubernetes的一个应用实例的必要信息
- config:包含了应用发布配置信息
- release:是一个 chart 及其配置的一个运行实例
2) Helm组件
Helm有以下两个组成部分:
Helm Client 是用户命令行工具,其主要负责如下:
- 本地 chart 开发
- 仓库管理
- 与 Tiller sever 交互
- 发送预安装的 chart
- 查询 release 信息
- 要求升级或卸载已存在的 release
Tiller Server是一个部署在Kubernetes集群内部的 server,其与 Helm client、Kubernetes API server 进行交互。Tiller server 主要负责如下:
- 监听来自 Helm client 的请求
- 通过 chart 及其配置构建一次发布
- 安装 chart 到Kubernetes集群,并跟踪随后的发布
- 通过与Kubernetes交互升级或卸载 chart
- client 管理 charts,而 server 管理发布 release
3) 用途
做为 Kubernetes 的一个包管理工具,Helm具有如下功能:
- 创建新的 chart
- chart 打包成 tgz 格式
- 上传 chart 到 chart 仓库或从仓库中下载 chart
- 在Kubernetes集群中安装或卸载 chart
- 管理用Helm安装的 chart 的发布周期
2. 安装Helm
在 Helm 下载 v2.10.0 版本 二进制安装包后解压。
1) 部署Tiller Server
安装 Helm 的服务端程序,需要使用到kubectl工具,所以先确保kubectl工具能够正常的访问 kubernetes 集群的apiserver。
$ yum install -y socat
$ helm init --upgrade --tiller-image cnych/tiller:v2.10.0 --stable-repo-url https://cnych.github.io/kube-charts-mirror/
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 服务端正常安装完成后,Tiller默认被部署在kubernetes集群的kube-system命名空间下:
$ kubectl get pod -n kube-system -l app=helm
NAME READY STATUS RESTARTS AGE
tiller-deploy-5bc5cf785c-pvgv4 1/1 Running 0 2m
2) 安装Helm Client
将helm
可执行文件复制到/usr/local/bin目录下即可。
# helm version
Client: &version.Version{SemVer:"v2.10.0", GitCommit:"9ad53aac42165a5fadc6c87be0dea6b115f93090", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.10.0", GitCommit:"9ad53aac42165a5fadc6c87be0dea6b115f93090", GitTreeState:"clean"}
kubernetes 集群是1.10.0版本的,默认开启了RBAC
访问控制,所以我们需要为Tiller
创建一个ServiceAccount
,让他拥有执行的权限,详细内容可以查看 Helm 文档中的Role-based Access Control。 创建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
$ kubectl create -f rbac-config.yaml
serviceaccount "tiller" created
clusterrolebinding.rbac.authorization.k8s.io "tiller" created
因为我们的 Tiller 之前已经就部署成功了,而且是没有指定 ServiceAccount 的,所以我们需要给 Tiller 打上一个 ServiceAccount 的补丁:
$ kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
Helm客户端和服务端配置完成。
3. 基本使用
- Helm 的基本操作
创建一个 Chart:
$ helm create hello-helm
Creating hello-helm
$ tree hello-helm
hello-helm
├── charts #Chart本身的版本和配置信息
├── Chart.yaml #依赖的chart
├── templates #配置模板目录
│ ├── deployment.yaml
│ ├── _helpers.tpl #用于修改kubernetes objcet配置的模板
│ ├── ingress.yaml
│ ├── NOTES.txt #helm提示信息
│ └── service.yaml
└── values.yaml
2 directories, 7 files
具体文件的作用,我们可以前往 Helm 官方文档进行查看。
查看deployment.yaml 文件可以看到都是去取的一些值,如.Valuesxxxx
,这些值都是定义在values.yaml
文件中。
apiVersion: apps/v1beta2
kind: Deployment
metadata:
---------
-------
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 80
------
比如这里我们来安装 1.7.9 这个版本的 nginx,则我们更改 value.yaml 文件下面的 image tag 即可,将默认的 stable 更改为 1.7.9,为了测试方便,我们把 Service 的类型也改成 NodePort。
# Default values for hello-helm.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
repository: nginx
tag: 1.7.9
pullPolicy: IfNotPresent
nameOverride: ""
fullnameOverride: ""
service:
type: NodePort
port: 80
ingress:
enabled: false
安装这个 Chart
$ helm install ./hello-helm
NAME: tailored-hedgehog
LAST DEPLOYED: Fri Jul 12 14:16:03 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
tailored-hedgehog-hello-helm NodePort 10.1.71.46 80:30860/TCP 0s
==> v1beta2/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
tailored-hedgehog-hello-helm 1 0 0 0 0s
NOTES:
1. Get the application URL by running these commands:
export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services tailored-hedgehog-hello-helm)
export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
安装完成后查看状态:
$ kubectl get pods -l app=hello-helm
NAME READY STATUS RESTARTS AGE
tailored-hedgehog-hello-helm-55fbdb76cc-nwkw7 1/1 Running 0 56m
$ kubectl get svc -l app=hello-helm
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
tailored-hedgehog-hello-helm NodePort 10.1.71.46 80:30860/TCP 56m
访问这个刚才创建nginx服务
2) 仓库
Helm 的 Repo 仓库和 Docker Registry 比较类似,Chart 库可以用来存储和共享打包 Chart 的位置,Chart 仓库其实就是一个带有index.yaml
索引文件和任意个打包的 Chart 的 HTTP 服务器而已,比如我们想要分享一个 Chart 包的时候,将我们本地的 Chart 包上传到该服务器上面,别人就可以使用了,所以其实我们自己托管一个 Chart 仓库也是非常简单的,比如阿里云的 OSS、Github Pages,甚至自己创建的一个简单服务器都可以。
我们在安装了 Helm 后,默认的仓库地址是 google 的一个地址,没办法访问到官方提供的 Chart 仓库,可以用helm repo list
来查看当前的仓库配置:
helm repo list
NAME URL
stable https://kubernetes-charts.storage.googleapis.com/
local http://127.0.0.1:8879/charts
更改Helm 仓库为 Github Pages 仓库,每天会自动和官方的仓库进行同步,地址是:https://github.com/cnych/kube-charts-mirror,这样我们就可以将我们的 Helm 默认仓库地址更改成我们自己的仓库地址了:
$ helm repo remove stable
"stable" has been removed from your repositories
$ helm repo add stable https://cnych.github.io/kube-charts-mirror/
"stable" has been added to your repositories
$ helm repo list
NAME URL
stable https://cnych.github.io/kube-charts-mirror/
local http://127.0.0.1:8879/charts
$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Skip local chart repository
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈ Happy Helming!⎈
仓库添加完成后,可以使用 update 命令进行仓库更新。当然如果要我们自己来创建一个 web 服务器来服务 Helm Chart 的话,只需要实现下面几个功能点就可以提供服务了:
- 将索引和
Chart
置于服务器目录中 - 确保索引文件
index.yaml
可以在没有认证要求的情况下访问 - 确保 yaml 文件的正确内容类型(text/yaml 或 text/x-yaml)
如果你的 web 服务提供了上面几个功能,那么也就可以当做 Helm Chart 仓库来使用了。
Helm 的基础使用
查看helm仓库
$ helm repo list
删除helm仓库
$ helm repo remove stable
添加helm仓库
$ helm repo add stable https://cnych.github.io/kube-charts-mirror/(仓库地址)
更新helm 仓库
$ helm repo update
查找chart
$ helm search mysql
查看chart 的详细信息
$ helm inspect stable/mysql
创建chart
$ helm create hello-helm
打包chart
$ helm package hello-helm(文件夹)
安装chart
$ helm install stable/mysql
安装chart,添加release名称
$ helm install stable/mysql --name mydb
跟踪release状态
$ helm status mydb
查看 chart 上可配置的选项
$ helm inspect values stable/mysql
自定义chart
新建config.yaml
mysqlUser: haimaxyUser
mysqlDatabase: haimaxyDB
service:
type: NodePort
persistence:
enabled: false
安装指定config.yaml
$ helm install -f config.yaml stable/mysql --name mydb
或者安装过程中使用--set来覆盖对应的 value 值.比如禁用持久化
$ helm install stable/mysql --set persistence.enabled=false --name mydb
升级release
$ helm upgrade -f config.yaml mydb stable/mysql
查看当前release
$ helm ls
查看release历史版本
$ helm history mydb
回滚release版本
$ helm rollback mydb 1
删除release(会保留记录,可以回滚重新激活)
$ helm delete mydb
查看被删除掉 release
$ helm list --deleted
查看所有release(包含被删除掉release )
$ helm list --all
彻底删除release
$ helm delete mydb --purge
查看最终生成清单文件
$ helm get manifest mydb(release)
调试模板,执行helm install打印最终资源清单,不会部署
$ helm install --dry-run --debug ./mychart