Kubernetes-在rancher中自定义应用商店

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的优势:
    • 增强版本追踪:Helm支持版本化的部署,Rancher增加了追踪和版本历史管理,以显示不同版本之间的变化。
    • 简化应用程序启动: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。app-readme.md
  • questions.yml:此文件包含表单的请求信息。这些表单请求用于简化chart的部署,当然,也可以使用键值对来配置部署,但使用questions.yml能够简化这个工作。下面显示了Rancher chart(包含app-readme.md文件)和原生的Helm chart 之间的区别。左边为Rancher Chart(带有app-readme.md)右边的为Helm Chart。questions.yml

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。

  1. 在github仓库中,构建符合要求的chart目录结构。提示:可以通过拷贝一个来自于Helm Stable或Rancher Library中chart来定制自己的chart。
  2. 推荐:创建一个app-readme.md文件。在Rancher UI中,使用此文件为chart标题创建自定义文本。可以使用此文本告知用户如何使用该chart。示例:
    $ cat ./app-readme.md
    # WordPress ROCKS!
  3. 推荐: 创建一个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
    
  4. 检入定制的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/

作者简介:
季向远,北京神舟航天软件技术有限公司。本文版权归原作者所有。

K8S中文社区微信公众号

评论 抢沙发

登录后评论

立即登录