一、helm概述
1、helm是什么
Helm是Kubernetes的包管理器,類似於Python的pip centos的yum,主要用來管理 Charts。
Helm Chart是用來封裝Kubernetes原生應用程序的一系列YAML文件。可以在你部署應用的時候自定義應用程序的一些Metadata,以便於應用程序的分發。對於應用發布者而言,可以通過Helm打包應用、管理應用依賴關系、管理應用版本並發布應用到軟件倉庫。
官方文檔:https://helm.sh/zh/docs/topics/charts/
https://docs.helm.sh/zh/docs/helm/helm_repo_index/
2、helm解決了什么
在kubernetes中部署一個可以使用的應用,需要涉及到很多的kubernetes資源的共同協作。
部署一個服務,會一些kubernetes的資源對象,包括deployment用於部署應用,service提供服務發現,secret配置服務的密碼,ingress用於集群外部訪問,有可能需要pv和pvc來提供持久惡化服務。會涉及到一些依賴,數據庫或其他服務啟動。這些資源過於分散,不方便管理,直接提供kubectl來管理一個應用,讓人頭疼。
在部署一個應用,會遇到哪些問題:
- 如何統一管理、配置和更新這些分散的k8s的應用資源文件
- 如何分發和復用應用模板
- 如何將應用的一系列資源當做一個軟件包管理
3、helm2與helm3的區別
由於Helm v2 Tiller服務端在社區一直存在已知的安全問題,攻擊者可以通過tiller在集群內安裝未經授權的應用,因此推薦您升級至Helm v3版本。
helm3是直接與k8s api互通 無需再helm init 創建服務端,比較大的改動是,移除 Tiller(Helm 2 是一種 Client-Server 結構,客戶端稱為 Helm,服務器稱為 Tiller)。Helm 3 只有客戶端結構,客戶端仍稱為 Helm。如下圖所示,它的操作類似於 Helm 2 客戶端,但客戶端直接與 Kubernetes API 服務器交互
helm3的特性:
- 移除了tiller
- 支持分布式helm hub, 有了它就可以在很多時候不需要手動添加非官方repo了,例如helm3 search hub <package name>
- 為chart輸入值進行json schema驗證。
- 可以給helm charts添加test了,通過helm test <release>就能針對部署的應用跑一些tests。
- 部署的時候release name必須指定了,helm2的時候不指定會自動生成一個。
- Helm3中release的相關信息保存在應用自己對應的命名空間下,所以此時可以在不同的命名空間下使用相同的release名稱,而且不會相互干擾
- 刪除的時候不需要--purge了,刪了就是刪了。
3、基本概念
- Helm 是一個命令行下的客戶端工具。主要用於 Kubernetes 應用程序 Chart 的創建、打包、發布以及創建和管理本地和遠程的 Chart 倉庫。
- Chart:一個 Helm 包,,采用 TAR 格式。其中包含了運行一個應用所需要的鏡像、依賴和資源定義等,還可能包含 Kubernetes 集群中的服務定義,類似 Homebrew 中的 formula、APT 的 dpkg 或者 Yum 的 rpm 文件。
- Release:在 Kubernetes 集群上運行的 Chart 的一個實例。在同一個集群上,一個 Chart 可以安裝很多次。每次安裝都會創建一個新的 release。例如一個 MySQL Chart,如果想在服務器上運行兩個數據庫,就可以把這個 Chart 安裝兩次。每次安裝都會生成自己的 Release,會有自己的 Release 名稱。
- Repository:用於發布和存儲 Chart 的存儲庫。
4、helm3的架構(下面所有的都是helm3)
二、helm的安裝及使用
1、helm3的安裝
1)二進制安裝
wget https://get.helm.sh/helm-v3.1.2-linux-amd64.tar.gz tar -zxvf helm-v3.1.2-linux-amd64.tar.gz cd linux-amd64 mv helm /usr/local/bin/ helm --help
2)腳本安裝
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 chmod 700 get_helm.sh ./get_helm.sh
3)命令補全
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion、
# 在~/.bashrc追加 (根據實際需要添加環境變量)
source <(helm completion bash)
2、helm本地倉庫的搭建
helm chart 能夠很好的封裝和管理我們的 kubernetes 應用,可以實現中間件、數據庫、公共組件等快速發布。
1)什么場景下我們需要構建一個私有的helm倉庫
-
首先我們日常發布中肯定是經常使用到了helm
-
有較多自定義的或者調整過的helm模板,或者有多套k8s/ocp 集群要同時基於 helm 進行發布與管理
-
如果想要要更好的管理 charts 歷史版本,可以使用下面說的 github page 或者 gitlab page
https://hub.kubeapps.com/charts 國外helm倉庫
2)本地倉庫選擇
Helm chart對倉庫的要求並不高,需要你對外提供yaml文件和tar文件的web服務即可。helm2 原本是帶了本地倉庫功能,helm3 移除了這部分,將他變成了一個純粹的應用管理工具。
像 harbor鏡像倉庫,JFrog Artifactory(制品倉庫,鏡像倉庫) 都包含了 helm 商店的功能,如果同時對鏡像倉庫和制品管理有需求,可以選擇上面兩款產品,都提供免費社區版。
如果不需要上面兩者的鏡像倉庫功能,可以使用在線的github 或 gitlab
https://www.bookstack.cn/read/kubernetes-handbook-201910/practice-create-private-charts-repo.md
又或者是helm 開源得工具 chartmuseum https://github.com/helm/chartmuseum
3)chartmuseum 自檢倉庫的搭建
-
根據chart文件自動生成index.yaml(無須使用helm repo index手動生成)
-
helm push的插件,可以在helm命令之上實現將chart文件推送到chartmuseum上
-
相應的tls配置,Basic認證,JWT認證(Bearer token認證)
-
提供了Restful的api(可以使用curl命令操作)和可以使用的cli命令行工具
-
提供了各種后端存儲的支持(Amazon s3, Google Cloud Storage, 阿里、百度、騰訊,開源對象存儲等)
-
提供了Prometheus的集成,對外提供自己的監控信息。
-
沒有用戶的概念,但是基於目錄實現了一定程度上的多租戶的需求。
直接使用最簡單的 docker run 方式,使用local 本地存儲方式,通過 -v 映射到宿主機 /opt/charts
mkdir /opt/charts docker run -d \ -p 8080:8080 \ -e DEBUG=1 \ -e STORAGE=local \ -e STORAGE_LOCAL_ROOTDIR=/charts \ -v /opt/charts:/charts \ chartmuseum/chartmuseum:latest # 使用 curl 測試下接口,沒有報錯就行,當前倉庫內容還是空的 # curl localhost:8080/api/charts {}
倉庫可視化:Monocular UI 必須依賴ingress,如果環境里沒有,先安裝一個
安裝 ingress controller
helm search repo ingress helm install ng-ingress az-stable/nginx-ingress
安裝Monocular UI 界面,管理helm charts
官方站點 https://github.com/helm/monocular
通過 helm 方式來安裝
helm repo add monocular https://helm.github.io/monocular # helm install monocular/monocular # 下載chart,把mongodb 存儲改成持久化 helm pull monocular/monocular tar -zxvf monocular-1.4.15.tgz vim values.yaml 在 repos 注釋自帶的stable 和incubator 4行,添加 localrepo, 地址是 http://192.168.2.19:8080 修改 mongodb.persistence.enabled 為 true # 安裝 helm install helmui .
訪問通過 ingress controller 的nodeport, 不是直接通過 monocular 的 nodeport,可以看下ingress 的內容就了解了。
kubectl get svc |grep ingress
通過瀏覽器訪問 k8s節點ip:31537,標記 deprecated: true 的charts,即為棄用的,不會展示在頁面
默認是1小時同步一次repo,可以在helm install 的時候修改 values.yaml,比如這樣,5分鍾同步一次
- name: incubator url: https://kubernetes-charts-incubator.storage.googleapis.com schedule: "*/5 * * *
或者部署之后修改 cronjob,修改 schedule: "*/5 * * * *"
kubectl edit cronjob helmui-monocular-sync-scheduled-localrepo
另一款界面管理工具 kubeapps
kubeapps 和 monocular 類似,都是bitnami 公司維護的,多了已發布helm 應用的查看,以及可以通過頁面添加 repo,功能比上面的多,建議用這個。
注意訪問的時候是使用 kubeapps 這個svc 的nodeport, 不依賴於ingress
查看通過 helm 發布的應用,支持按照 namespace 區分
4)准備 helm 及離線 chart,推送到私有庫
為私有倉庫准備些chart,獲取方式:可以是自己生成的或者從公共倉庫獲取
# 添加官方源,網絡不好可以試試下面微軟的helm repo add stable https://kubernetes-charts.storage.googleapis.comhelm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com# 也可以換成微軟的源,速度快,內容和官方同步的helm repo add az-stable http://mirror.azure.cn/kubernetes/charts/helm repo add az-incubator http://mirror.azure.cn/kubernetes/charts-incubator/ ### 把 charts 文件直接下載到 chartmuseum 指定的本地目錄 cd /opt/charts
chmod 777 -R /opt/charts/ helm search repo mysql # 將charts 文件下載到本地 helm pull az-stable/mysql helm pull az-stable/tomcat
可以使用helm push插件上傳
# 安裝 helm push 插件 helm plugin install https://github.com/chartmuseum/helm-push.git # 從官網下載一個 consul helm search repo consul helm pull az-stable/consul # 添加本地倉庫到 helm repo,看下倉庫內容 helm repo add localrepo http://192.168.2.19:8080 helm search repo localrepo # helm push 推送 postgres helm push consul-7.1.3.tgz localrepo
3、helm倉庫的使用
1)倉庫查看
首次安裝 helm 3 是沒有指定默認倉庫的。需要手動瘋狂添加倉庫才可以獲取到程序包
helm repo list
2)helm倉庫添加
helm repo add stable https://kubernetes-charts.storage.googleapis.com/ helm repo add bitnami https://charts.bitnami.com/bitnami helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com/ helm repo update # Make sure we get the latest list of charts helm repo list
3)helm倉庫刪除
helm repo remove 倉庫名稱
4)helm查詢chart
helm search repo chart_name
4、helm常用命令
helm version //查看helm版本信息 helm list /查看當前安裝的Chart包 helm search mysql //查看與mysql相關的chart包 helm fetch stable/mysql //將mysql軟件包下載到本地 helm inspect stable/mysql //查看該軟件包的詳細信息 helm install stable/mysql -n mysql //安裝指定的mysql軟件包,並命名為mysql
helm install stable/mysql -n mysql //安裝指定的mysql軟件包,並命名為mysql
helm status mysql //查看mysql的狀態信息
helm delete --purge mysql //刪除mysql,並將本地的緩存也進行刪除
helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts //添加阿里雲的repo源
helm repo update //更新repo倉庫資源
helm create helm_charts //創建一個chart,名稱為helm_charts
cd helm_charts/ && helm lint //測試charts語法
helm package helm_charts //打包charts
helm template helm_charts-0.1.0.tgz //查看該軟件包生成的yaml文件
helm install的幫助信息
]# helm install -h This command installs a chart archive. The install argument must be a chart reference, a path to a packaged chart, a path to an unpacked chart directory or a URL. To override values in a chart, use either the '--values' flag and pass in a file or use the '--set' flag and pass configuration from the command line, to force a string value use '--set-string'. In case a value is large and therefore you want not to use neither '--values' nor '--set', use '--set-file' to read the single large value from file. $ helm install -f myvalues.yaml myredis ./redis or $ helm install --set name=prod myredis ./redis or $ helm install --set-string long_int=1234567890 myredis ./redis or $ helm install --set-file my_script=dothings.sh myredis ./redis You can specify the '--values'/'-f' flag multiple times. The priority will be given to the last (right-most) file specified. For example, if both myvalues.yaml and override.yaml contained a key called 'Test', the value set in override.yaml would take precedence: $ helm install -f myvalues.yaml -f override.yaml myredis ./redis You can specify the '--set' flag multiple times. The priority will be given to the last (right-most) set specified. For example, if both 'bar' and 'newbar' values are set for a key called 'foo', the 'newbar' value would take precedence: $ helm install --set foo=bar --set foo=newbar myredis ./redis To check the generated manifests of a release without installing the chart, the '--debug' and '--dry-run' flags can be combined. If --verify is set, the chart MUST have a provenance file, and the provenance file MUST pass all verification steps. There are five different ways you can express the chart you want to install: 1. By chart reference: helm install mymaria example/mariadb 2. By path to a packaged chart: helm install mynginx ./nginx-1.2.3.tgz 3. By path to an unpacked chart directory: helm install mynginx ./nginx 4. By absolute URL: helm install mynginx https://example.com/charts/nginx-1.2.3.tgz 5. By chart reference and repo url: helm install --repo https://example.com/charts/ mynginx nginx CHART REFERENCES A chart reference is a convenient way of referencing a chart in a chart repository. When you use a chart reference with a repo prefix ('example/mariadb'), Helm will look in the local configuration for a chart repository named 'example', and will then look for a chart in that repository whose name is 'mariadb'. It will install the latest stable version of that chart until you specify '--devel' flag to also include development version (alpha, beta, and release candidate releases), or supply a version number with the '--version' flag. To see the list of chart repositories, use 'helm repo list'. To search for charts in a repository, use 'helm search'. Usage: helm install [NAME] [CHART] [flags] Flags: --atomic if set, the installation process deletes the installation on failure. The --wait flag will be set automatically if --atomic is used --ca-file string verify certificates of HTTPS-enabled servers using this CA bundle --cert-file string identify HTTPS client using this SSL certificate file --create-namespace create the release namespace if not present --dependency-update run helm dependency update before installing the chart --description string add a custom description --devel use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored --disable-openapi-validation if set, the installation process will not validate rendered templates against the Kubernetes OpenAPI Schema --dry-run simulate an install -g, --generate-name generate the name (and omit the NAME parameter) -h, --help help for install --key-file string identify HTTPS client using this SSL key file --keyring string location of public keys used for verification (default "/root/.gnupg/pubring.gpg") --name-template string specify template used to name the release --no-hooks prevent hooks from running during install -o, --output format prints the output in the specified format. Allowed values: table, json, yaml (default table) --password string chart repository password where to locate the requested chart --post-renderer postrenderer the path to an executable to be used for post rendering. If it exists in $PATH, the binary will be used, otherwise it will try to look for the executable at the given path (default exec) --render-subchart-notes if set, render subchart notes along with the parent --replace re-use the given name, only if that name is a deleted release which remains in the history. This is unsafe in production --repo string chart repository url where to locate the requested chart --set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) --set-file stringArray set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2) --set-string stringArray set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) --skip-crds if set, no CRDs will be installed. By default, CRDs are installed if not already present --timeout duration time to wait for any individual Kubernetes operation (like Jobs for hooks) (default 5m0s) --username string chart repository username where to locate the requested chart -f, --values strings specify values in a YAML file or a URL (can specify multiple) --verify verify the package before installing it --version string specify the exact chart version to install. If this is not specified, the latest version is installed --wait if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment, StatefulSet, or ReplicaSet are in a ready state before marking the release as successful. It will wait for as long as --timeout Global Flags: --add-dir-header If true, adds the file directory to the header --alsologtostderr log to standard error as well as files --debug enable verbose output --kube-apiserver string the address and the port for the Kubernetes API server --kube-context string name of the kubeconfig context to use --kube-token string bearer token used for authentication --kubeconfig string path to the kubeconfig file --log-backtrace-at traceLocation when logging hits line file:N, emit a stack trace (default :0) --log-dir string If non-empty, write log files in this directory --log-file string If non-empty, use this log file --log-file-max-size uint Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800) --logtostderr log to standard error instead of files (default true) -n, --namespace string namespace scope for this request --registry-config string path to the registry config file (default "/root/.config/helm/registry.json") --repository-cache string path to the file containing cached repository indexes (default "/root/.cache/helm/repository") --repository-config string path to the file containing repository names and URLs (default "/root/.config/helm/repositories.yaml") --skip-headers If true, avoid header prefixes in the log messages --skip-log-headers If true, avoid headers when opening log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) -v, --v Level number for the log level verbosity --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
三、chart
kubernetes(k8s) 管理的對象叫做資源(比如 pod)。helm 作為 k8s 生態工具之一,將互相關聯的多個資源統一打包成一個整體,交給 k8s 去運行。這個被打包的整體就叫做 chart。可以把 chart 跟程序的源碼包做類比。
1、創建自己的chart
- 快速創建模板,
helm create my-charts
,修改對應內容 - 打包,然后拷貝至repository的目錄,然后執行更新index操作. helm package
- 安裝,
helm install . 或 helm install my-charts.tgz
- 驗證charts格式,helm lint
- 查看charts文件內容.
helm inspect chart my-charts
- 查看value文件內容.
helm inspect values my-charts
- 查看charts目錄下文件內容.
helm inspect my-charts
- 查看charts模板渲染后k8s的yaml,
helm template my-charts -f configfile --set a=b
2、創建完chart的目錄結構
myapp - chart 包目錄名 ├── charts - 依賴的子包目錄,里面可以包含多個依賴的chart包 ├── Chart.yaml - chart定義,可以定義chart的名字,版本號信息。 ├── templates - k8s配置模版目錄, 我們編寫的k8s配置都在這個目錄, 除了NOTES.txt和下划線開頭命名的文件,其他文件可以隨意命名。 │ ├── deployment.yaml │ ├── _helpers.tpl - 下划線開頭的文件,helm視為公共庫定義文件,主要用於定義通用的子模版、函數等,helm不會將這些公共庫文件的渲染結果提交給k8s處理。 │ ├── ingress.yaml │ ├── NOTES.txt - chart包的幫助信息文件,執行helm install命令安裝成功后會輸出這個文件的內容。 │ └── service.yaml └── values.yaml - chart包的參數配置文件,模版可以引用這里參數。
在k8s中部署一個應用,需要編寫deployment、service、ingress三個配置文件,剛才通過helm create命令已經創建好了。
Chart.yaml 文件是 chart 必需的。包含了以下字段
apiVersion:在 Helm3 中,apiVersion=v2;在 Helm3 之前的版本,apiVersion=v1
apiVersion: chart API 版本 (必需) name: chart名稱 (必需) version: 版本(必需) kubeVersion: 兼容Kubernetes版本的語義化版本(可選) description: 一句話對這個項目的描述(可選) type: chart類型 (可選) keywords: - 關於項目的一組關鍵字(可選) home: 項目home頁面的URL (可選) sources: - 項目源碼的URL列表(可選) dependencies: # chart 必要條件列表 (可選) - name: chart名稱 (nginx) version: chart版本 ("1.2.3") repository: 倉庫URL ("https://example.com/charts") 或別名 ("@repo-name") condition: (可選) 解析為布爾值的yaml路徑,用於啟用/禁用chart (e.g. subchart1.enabled ) tags: # (可選) - 用於一次啟用/禁用 一組chart的tag enabled: (可選) 決定是否加載chart的布爾值 import-values: # (可選) - ImportValue 保存源值到導入父鍵的映射。每項可以是字符串或者一對子/父列表項 alias: (可選) chart中使用的別名。當你要多次添加相同的chart時會很有用 maintainers: # (可選) - name: 維護者名字 (每個維護者都需要) email: 維護者郵箱 (每個維護者可選) url: 維護者URL (每個維護者可選) icon: 用做icon的SVG或PNG圖片URL (可選) appVersion: 包含的應用版本(可選)。不需要是語義化的 deprecated: 不被推薦的chart (可選,布爾值) annotations: example: 按名稱輸入的批注列表 (可選).
內置對象
(1) Values
Values 對象是為 Chart 模板提供值,這個對象的值有4個來源,后面的可以覆蓋前面的:
chart 包中的 values.yaml 文件 父 chart 包的 values.yaml 文件 通過 helm install 或者 helm upgrade 的 -f 或者 --values 參數傳入的自定義的 yaml 文件 通過 --set 參數傳入的值 在模板文件中,通過 {{.Values}} 對象來訪問設置的值。
3、chart使用
1)chart搜索
helm search repo nginx
2)chart下載
helm pull google/nginx-ingress
3)chart自建
除了從遠程 helm 倉庫中獲取第三方 chart 外,你也可以創建自己的 chart
helm create mychart
4)chart打包
如果你想上傳自建的 chart 到私有倉庫中去,需要先將自建的 chart 打包
helm package mychart
5)chart上傳
上傳 chart 需要 4 個步驟:
- 自建私有倉庫
- 生成或更新 chart 索引文件
- helm repo index /root/helm/repo
- 上傳 chart 和索引文件
- 登錄私有倉庫,上傳 chart 和索引文件 index.yaml
- 更新本地 chart 倉庫
- helm repo update
- 更新本地 chart 倉庫之后,就可以檢索到剛上傳的自定義 chart 了
四、release
release 理解起來則比較抽象,它是專屬於 helm 的一個邏輯概念,用來標識在 k8s 中運行的一組資源
1、release使用
1)release查看
helm ls
2)release安裝
在線安裝指定的 chart,比如 nginx-ingress。
helm search repo nginx-ingress helm install mynginx-ingress google/nginx-ingress
3)release更新
如果想修改運行時 release 的配置,可以使用 --set 或者 -f 選項進行修改。
基於命令行更新 release
## mynginx-ingress 是上面創建的 release 名;google/nginx-ingress 是在線 chart 名 # helm upgrade --set controller.hostNetwork=true \ mynginx-ingress google/nginx-ingress
基於文件更新 release
如果想基於文件來更新 release,則首先需要將 chart 下載到本地,然后手動修改 chart 的 values.yaml 文件
## 下載 chart # helm pull google/nginx-ingress ## 解壓縮 chart # tar -zxvf nginx-ingress-1.26.1.tgz ## 修改 values.yaml 內容。比如修改 hostNetwork 的值為 true # sed -i 's/hostNetwork: false/hostNetwork: true/g' nginx-ingress/values.yaml ## 針對文件使用 -f 選項更新 release # helm upgrade mynginx-ingress nginx-ingress -f nginx-ingress/values.yaml
查看 release 更新后的新值
helm get values mynginx-ingress
4)release 版本
helm history mynginx-ingress
5)release 回滾
helm rollback mynginx-ingress 4
6)release 卸載
helm uninstall mynginx-ingress