在rancher中自定義應用商店_Kubernetes中文社群
1、應用商店概述
在Rancher中提供基於helm的應用商店,通過應用商店能夠快速和容易的重複部署應用。應用商店的charts來源即可以是helm倉庫或者git倉庫,其中包含現成的用於部署的應用程式。應用程式捆綁在稱為charts的物件中。charts是Helm推廣的封裝格式。可以將它們視為部署模板,對於每個Helm,圖表是:描述相關Kubernetes資源集的檔案集合。使用單個charts即可以部署簡單的應用,例如memcached pod;也可以部署複雜的應用,例如具有HTTP伺服器,資料庫,快取等的完整Web應用程式堆疊。
Rancher自身改進了Helm目錄和charts。所有原生的Helm charts都可以在Rancher中執行,但為了提升使用者體驗,Rancher增加了一些功能。在Rancher中提供了由Rancher官方維護的模板倉庫、由Kubernetes社群維護的模板倉庫和未經測試驗證的應用模板倉庫這三種內建的應用商店。另外,也支援新增自定義的應用商店。
2、自定義應用商店
自定義的應用商店是一個git倉庫,URL需要是一個git clone能夠處理的,以及以.git結尾的地址。
2.1 Chart型別
Rancher支援兩種charts型別:
- Helm Charts:原生的Helm charts包含執行應用的所有內容。當部署一個原生的Helm charts時,需要通過Answers配置鍵值對形式的引數。Helm chart有Helm Stable和Helm Incubators這兩種。
- Rancher Charts:Rancher charts在原生的helm charts基礎上,新增量app-readme.md和questions.yaml這兩個檔案,以提升使用者體驗。 Rancher charts的優勢:
- 增強版本追蹤:
- 簡化應用程式啟動:Rancher chart增加簡化的charts描述和配置表單,使應用商店中的應用程式部署更加容易。Rancher使用者不需要閱讀Helm整個列表中的變數來理解如何啟動應用程式。
- 應用資源管理:Rancher追蹤所建立的所有資源。使用者能夠跟蹤所有的資源。使用者能在一個頁面上檢視被用來支援應用的工作負載物件。
- 增強版本追蹤:
2.2 Chart目錄結構
下表展示了chart的目錄結構,在目錄中可以看到:charts/%application%/%app version%/。當為應用商店定製charts時,此資訊很有用。用Rancher Specific表示的檔案專門用於於Rancher chart,但對於chart自定義是可選的。
子目錄 | 檔案 | 描述 |
app-readme.md |
Rancher需要:在Rancher頁面中app描述資訊。 | |
charts/ |
包含依賴charts的目錄 | |
Chart.yml |
helm chart資訊檔案 | |
questions.yml |
Rancher需要: 包含在Rancher介面中顯示的請求。 | |
README.md |
可選:在Rancher UI中顯示的Helm說明檔案。 | |
requirements.yml |
可選:列出chart依賴關係的YAML檔案。 | |
templates/ |
模板目錄,當與values.yml結合使用時,會生成Kubernetes YAML。 | |
values.yml |
chart的預設配置檔案。 |
2.3 Rancher Chart額外的檔案
在建立自己的定製化應用商店之前,需要對Rancher chart和原生helm chart有一個基本的瞭解。Rancher chart與helm chart在目錄結構上有細微的差別,即Rancher chart比helm chart多兩個檔案。
- app-readme.md:此檔案提供chart的描述文字。下面兩張圖顯示了Rancher chart(包含app-readme.md檔案)和原生的Helm chart 之間的區別。左邊為Rancher Chart
(帶有app-readme.md
)右邊的為Helm Chart。 - questions.yml:此檔案包含表單的請求資訊。這些表單請求用於簡化chart的部署,當然,也可以使用鍵值對來配置部署,但使用questions.yml能夠簡化這個工作。下面顯示了Rancher chart(包含app-readme.md檔案)和原生的Helm chart 之間的區別。左邊為Rancher Chart
(帶有app-readme.md
)右邊的為Helm Chart。
2.3.1 查詢變數參考
此參考包含的變數可以在questions.yml中進行使用。
變數 | 型別 | 是否必需 | 描述 |
---|---|---|---|
variable | string | true | Define the variable name specified in the values.yml file, using foo.bar for nested objects. |
label | string | true | 定義UI標籤 |
description | string | false | 指定變數的描述 |
type | string | false | Default to string if not specified (current supported types are string, boolean, int, enum, password, storageclass and hostname). |
required | bool | false | Define if the variable is required or not (true | false) |
default | string | false | 指定預設值 |
group | string | false | Group questions by input value. |
min_length | int | false | Min character length. |
max_length | int | false | Max character length. |
min | int | false | Min integer length. |
max | int | false | Max integer length. |
options | []string | false | Specify the options when the variable type is enum , for example: options:– “ClusterIP” – “NodePort” – “LoadBalancer” |
valid_chars | string | false | Regular expression for input chars validation. |
invalid_chars | string | false | Regular expression for invalid input chars validation. |
subquestions | []subquestion | false | Add an array of subquestions. |
show_if | string | false | Show current variable if conditional variable is true. For example show_if: "serviceType=Nodeport" |
show_subquestion_if | string | false | Show subquestions if is true or equal to one of the options. for example show_subquestion_if: "true" |
2.4 建立定製chart的例項
可以使用Helm Charts或Rancher Charts填充您的自定義商店,建議使用Rancher Charts。
- 在github倉庫中,構建符合要求的chart目錄結構。提示:可以通過拷貝一個來自於Helm Stable或Rancher Library中chart來定製自己的chart。
- 推薦:建立一個app-readme.md檔案。在Rancher UI中,使用此檔案為chart標題建立自定義文字。可以使用此文字告知使用者如何使用該chart。示例:
$ cat ./app-readme.md # WordPress ROCKS!
- 推薦: 建立一個questions.yml檔案。此檔案用於定義部署的引數,沒有此檔案的話,使用者必須通過鍵值對手工指定引數。下面的例子建立了一個表單,提示使用者設定持久化容量大小和儲存類。
categories: - Blog - CMS questions: - variable: persistence.enabled default: "false" description: "Enable persistent volume for WordPress" type: boolean required: true label: WordPress Persistent Volume Enabled show_subquestion_if: true group: "WordPress Settings" subquestions: - variable: persistence.size default: "10Gi" description: "WordPress Persistent Volume Size" type: string label: WordPress Volume Size - variable: persistence.storageClass default: "" description: "If undefined or null, uses the default StorageClass. Default to null" type: storageclass label: Default StorageClass for WordPress
- 檢入定製的chart到github倉庫
3、定製應用商店示例
在本例中,將以elasticsearch為例,構建自定義的應用商店:
1)啟動本地helm倉庫
通過執行如下的命令,在本地起一個helm倉庫。
$ helm serve --addresss 0.0.0.0:8879
2)新增自己的應用商店
在rancher中的全域性範圍中新增應用商店。
3)獲取elasticsearch chart
從helm公共庫中獲取mysql chart。
$ helm fetch stable/mysql
4)部署mysql到helm倉庫
將獲取到的mysql-0.4.4.tgz拷貝至“~\.helm\repository\local”目錄下,重啟本地helm倉庫。
5)進入特定的專案檢視應用商店的應用
參考資料
1.《Custom Catalogs and Charts》地址:https://rancher.com/docs/rancher/v2.x/en/catalog/custom/
作者簡介:
季向遠,北京神舟航天軟體技術有限公司。本文版權歸原作者所有。