1. Helm 簡介
Helm是Deis (https://deis.com/) 開發(fā)的一個用于kubernetes的包管理器阴绢,相當(dāng)于kubernetes環(huán)境下的yum包管理工具毅往。
1) 重要概念
Helm 有三個重要概念:
- chart:包含了創(chuàng)建Kubernetes的一個應(yīng)用實例的必要信息
- config:包含了應(yīng)用發(fā)布配置信息
- release:是一個 chart 及其配置的一個運行實例
2) Helm組件
Helm有以下兩個組成部分:
Helm Client 是用戶命令行工具娇豫,其主要負(fù)責(zé)如下:
- 本地 chart 開發(fā)
- 倉庫管理
- 與 Tiller sever 交互
- 發(fā)送預(yù)安裝的 chart
- 查詢 release 信息
- 要求升級或卸載已存在的 release
Tiller Server是一個部署在Kubernetes集群內(nèi)部的 server魏烫,其與 Helm client、Kubernetes API server 進(jìn)行交互患亿。Tiller server 主要負(fù)責(zé)如下:
- 監(jiān)聽來自 Helm client 的請求
- 通過 chart 及其配置構(gòu)建一次發(fā)布
- 安裝 chart 到Kubernetes集群慌核,并跟蹤隨后的發(fā)布
- 通過與Kubernetes交互升級或卸載 chart
- client 管理 charts,而 server 管理發(fā)布 release
3) 用途
做為 Kubernetes 的一個包管理工具央拖,Helm具有如下功能:
- 創(chuàng)建新的 chart
- chart 打包成 tgz 格式
- 上傳 chart 到 chart 倉庫或從倉庫中下載 chart
- 在Kubernetes集群中安裝或卸載 chart
- 管理用Helm安裝的 chart 的發(fā)布周期
2. 安裝Helm
在 Helm 下載 v2.10.0 版本 二進(jìn)制安裝包后解壓祭阀。
1) 部署Tiller Server
安裝 Helm 的服務(wù)端程序,需要使用到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 服務(wù)端正常安裝完成后,Tiller默認(rèn)被部署在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
可執(zhí)行文件復(fù)制到/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版本的伦腐,默認(rèn)開啟了RBAC
訪問控制,所以我們需要為Tiller
創(chuàng)建一個ServiceAccount
失都,讓他擁有執(zhí)行的權(quán)限柏蘑,詳細(xì)內(nèi)容可以查看 Helm 文檔中的Role-based Access Control幸冻。 創(chuàng)建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 之前已經(jīng)就部署成功了,而且是沒有指定 ServiceAccount 的咳焚,所以我們需要給 Tiller 打上一個 ServiceAccount 的補肚⑺稹:
$ kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
Helm客戶端和服務(wù)端配置完成。
3. 基本使用
- Helm 的基本操作
創(chuàng)建一個 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 官方文檔進(jìn)行查看趁啸。
查看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 即可赏胚,將默認(rèn)的 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 <none> 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
安裝完成后查看狀態(tài):
$ 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 <none> 80:30860/TCP 56m
訪問這個剛才創(chuàng)建nginx服務(wù)
2) 倉庫
Helm 的 Repo 倉庫和 Docker Registry 比較類似崖疤,Chart 庫可以用來存儲和共享打包 Chart 的位置,Chart 倉庫其實就是一個帶有index.yaml
索引文件和任意個打包的 Chart 的 HTTP 服務(wù)器而已典勇,比如我們想要分享一個 Chart 包的時候劫哼,將我們本地的 Chart 包上傳到該服務(wù)器上面,別人就可以使用了割笙,所以其實我們自己托管一個 Chart 倉庫也是非常簡單的权烧,比如阿里云的 OSS、Github Pages伤溉,甚至自己創(chuàng)建的一個簡單服務(wù)器都可以般码。
我們在安裝了 Helm 后,默認(rèn)的倉庫地址是 google 的一個地址乱顾,沒辦法訪問到官方提供的 Chart 倉庫板祝,可以用helm repo list
來查看當(dāng)前的倉庫配置:
helm repo list
NAME URL
stable https://kubernetes-charts.storage.googleapis.com/
local http://127.0.0.1:8879/charts
更改Helm 倉庫為 Github Pages 倉庫,每天會自動和官方的倉庫進(jìn)行同步走净,地址是:https://github.com/cnych/kube-charts-mirror券时,這樣我們就可以將我們的 Helm 默認(rèn)倉庫地址更改成我們自己的倉庫地址了:
$ 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 命令進(jìn)行倉庫更新伏伯。當(dāng)然如果要我們自己來創(chuàng)建一個 web 服務(wù)器來服務(wù) Helm Chart 的話革为,只需要實現(xiàn)下面幾個功能點就可以提供服務(wù)了:
- 將索引和
Chart
置于服務(wù)器目錄中 - 確保索引文件
index.yaml
可以在沒有認(rèn)證要求的情況下訪問 - 確保 yaml 文件的正確內(nèi)容類型(text/yaml 或 text/x-yaml)
如果你的 web 服務(wù)提供了上面幾個功能,那么也就可以當(dāng)做 Helm Chart 倉庫來使用了舵鳞。
Helm 的基礎(chǔ)使用
查看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 的詳細(xì)信息
$ helm inspect stable/mysql
創(chuàng)建chart
$ helm create hello-helm
打包chart
$ helm package hello-helm(文件夾)
安裝chart
$ helm install stable/mysql
安裝chart,添加release名稱
$ helm install stable/mysql --name mydb
跟蹤release狀態(tài)
$ 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來覆蓋對應(yīng)的 value 值.比如禁用持久化
$ helm install stable/mysql --set persistence.enabled=false --name mydb
升級release
$ helm upgrade -f config.yaml mydb stable/mysql
查看當(dāng)前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)
調(diào)試模板蜓堕,執(zhí)行helm install打印最終資源清單抛虏,不會部署
$ helm install --dry-run --debug ./mychart