Kubernetes包管理器Helm使用指南
1. 查找图表
要使用Helm安装有用的应用程序和软件,首先需要找到它们的图表。
helm search
命令可用于此目的。默认情况下,Helm会搜索名为
stable
的官方Kubernetes图表仓库。
> helm search
NAME VERSION DESCRIPTION
stable/acs-engine-autoscaler 2.1.1 Scales worker nodes within agent pools
stable/aerospike 0.1.5 A Helm chart for Aerospike in Kubernetes
stable/artifactory 6.2.4 Universal Repository Manager supporting all maj...
官方仓库拥有丰富的图表库,涵盖了现代开源数据库、监控系统、Kubernetes特定的辅助工具等。你还可以搜索特定的图表,例如搜索名称或描述中包含
kube
的图表:
> helm search kube
NAME VERSION DESCRIPTION
stable/chaoskube 0.6.1 Chaoskube periodically kills random pods in you...
stable/kube-lego 0.3.0 Automatically requests certificates from Let's ...
再尝试搜索
mysql
:
> helm search mysql
NAME VERSION DESCRIPTION
stable/mysql 0.3.4 Fast, reliable, scalable, and easy to use open-...
stable/percona 0.3.0 free, fully compatible, enhanced, open source d...
stable/gcloud-sqlproxy 0.2.2 Google Cloud SQL Proxy
stable/mariadb 2.1.3 Fast, reliable, scalable, and easy to use open-...
mariadb
出现在结果中是因为它在描述中提到了
MySQL
。要获取完整描述,可使用
helm inspect
命令:
> helm inspect stable/mariadb
appVersion: 10.1.30
description: Fast, reliable, scalable, and easy to use open-source relational database
system. MariaDB Server is intended for mission-critical, heavy-load production systems
as well as for embedding into mass-deployed software.
engine: gotpl
home: https://mariadb.org
icon:
https://bitnami.com/assets/stacks/mariadb/img/mariadb-stack-220x234.png
keywords:
- mariadb
- mysql
- database
- sql
- prometheus
maintainers:
- email: containers@bitnami.com
name: bitnami-bot
name: mariadb
sources:
- https://github.com/bitnami/bitnami-docker-mariadb
- https://github.com/prometheus/mysqld_exporter
version: 2.1.3
2. 安装包
找到所需的包后,你可能想将其安装到Kubernetes集群中。使用
helm install
命令安装包时,Helm会创建一个版本,用于跟踪安装进度。以下是安装
MariaDB
的示例:
> helm install stable/mariadb
NAME: cranky-whippet
LAST DEPLOYED: Sat Mar 17 10:21:21 2018
NAMESPACE: default
STATUS: DEPLOYED
输出的第二部分列出了该图表创建的所有资源,资源名称都基于版本名称:
RESOURCES:
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
AGE
cranky-whippet-mariadb ClusterIP 10.106.206.108 <none> 3306/TCP
1s
==> v1beta1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
cranky-whippet-mariadb 1 1 1 0 1s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
cranky-whippet-mariadb-6c85fb4796-mttf7 0/1 Init:0/1 0 1s
==> v1/Secret
NAME TYPE DATA AGE
cranky-whippet-mariadb Opaque 2 1s
==> v1/ConfigMap
NAME DATA AGE
cranky-whippet-mariadb 1 1s
cranky-whippet-mariadb-tests 1 1s
==> v1/PersistentVolumeClaim
NAME STATUS VOLUME
CAPACITY ACCESS MODES STORAGECLASS AGE
cranky-whippet-mariadb Bound pvc-9cb7e176-2a07-11e8-9bd6-080027c94384
8Gi RWO standard 1s
最后一部分是使用说明,提供了在Kubernetes集群中使用
MariaDB
的简单易懂的指南:
NOTES:
MariaDB can be accessed via port 3306 on the following DNS name from within your cluster:
cranky-whippet-mariadb.default.svc.cluster.local
To get the root password run:
MARIADB_ROOT_PASSWORD=$(kubectl get secret --namespace default cranky-
whippet-mariadb -o jsonpath="{.data.mariadb-root-password}" | base64 --
decode)
To connect to your database:
1. Run a pod that you can use as a client:
kubectl run cranky-whippet-mariadb-client --rm --tty -i --env
MARIADB_ROOT_PASSWORD=$MARIADB_ROOT_PASSWORD --image bitnami/mariadb --
command -- bash
2. Connect using the mysql cli, then provide your password:
mysql -h cranky-whippet-mariadb -p$MARIADB_ROOT_PASSWORD
3. 检查安装状态
Helm不会等待安装完成,因为这可能需要一些时间。
helm status
命令可以显示版本的最新信息,格式与
helm install
命令的输出相同。例如,检查之前安装的
MariaDB
的
PersistentVolumeClaim
状态:
> helm status cranky-whippet | grep Persist -A 3
==> v1/PersistentVolumeClaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
cranky-whippet-mariadbBoundpvc-9cb7e176-2a07-11e8-9bd6-080027c943848Gi
RWO standard 5m
现在它已绑定,并且有一个8GB容量的卷。可以尝试连接并验证
mariadb
是否确实可以访问:
> kubectl run cranky-whippet-mariadb-client --rm --tty -i --image
bitnami/mariadb --command -- mysql -h cranky-whippet-mariadb
If you don't see a command prompt, try pressing Enter.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
4. 自定义图表
作为用户,你可能希望自定义或配置安装的图表。Helm通过配置文件完全支持自定义。使用
helm inspect values
命令可以了解可能的自定义选项:
> helm inspect values stable/mariadb
## Bitnami MariaDB image version
## ref: https://hub.docker.com/r/bitnami/mariadb/tags/
##
## Default: none
image: bitnami/mariadb:10.1.30-r1
## Specify an imagePullPolicy (Required)
## It's recommended to change this to 'Always' if the image tag is 'latest'
## ref: http://kubernetes.io/docs/user-guide/images/#updating-images
imagePullPolicy: IfNotPresent
## Use password authentication
usePassword: true
## Specify password for root user
## Defaults to a random 10-character alphanumeric string if not set and
usePassword is true
## ref:
https://github.com/bitnami/bitnami-docker-mariadb/blob/master/README.md#set
ting-the-root-password-on-first-run
##
# mariadbRootPassword:
## Create a database user
## Password defaults to a random 10-character alphanumeric string if not
set and usePassword is true
## ref:
https://github.com/bitnami/bitnami-docker-mariadb/blob/master/README.md#cre
ating-a-database-user-on-first-run
##
# mariadbUser:
# mariadbPassword:
## Create a database
## ref:
https://github.com/bitnami/bitnami-docker-mariadb/blob/master/README.md#cre
ating-a-database-on-first-run
##
# mariadbDatabase:
例如,如果你想在安装
mariadb
时设置根密码并创建一个数据库,可以创建一个YAML文件
mariadb-config.yaml
:
mariadbRootPassword: supersecret
mariadbDatabase: awesome_stuff
然后运行
helm
并传递YAML文件:
> helm install -f config.yaml stable/mariadb
你也可以在命令行上使用
--set
设置单个值。如果
--f
和
--set
尝试设置相同的值,
--set
优先。例如:
helm install -f config.yaml --set mariadbRootPassword=evenbettersecret
stable/mariadb
可以使用逗号分隔的列表指定多个值:
--set a=1,b=2
。
5. 额外的安装选项
helm install
命令可以从多个源进行安装:
- 图表仓库
- 本地图表存档(
helm install foo-0.1.1.tgz
)
- 未打包的图表目录(
helm install path/to/foo
)
- 完整URL(
helm install https://example.com/charts/foo-1.2.3.tgz
)
6. 升级和回滚版本
你可能希望将安装的包升级到最新版本。Helm提供了
upgrade
命令,它会智能地只更新发生变化的部分。例如,检查当前
mariadb
安装的值:
> helm get values cranky-whippet
mariadbDatabase: awesome_stuff
mariadbRootPassword: evenbettersecret
然后进行升级并更改数据库名称:
> helm upgrade cranky-whippet --set mariadbDatabase=awesome_sauce
stable/mariadb
$ helm get values cranky-whippet
mariadbDatabase: awesome_sauce
注意,升级时会丢失根密码。所有现有值都会被替换。可以使用
helm history
命令查看所有可用的版本,以便回滚:
> helm history cranky-whippet
REVISION STATUS CHART DESCRIPTION
1 SUPERSEDED mariadb-2.1.3 Install complete
2 SUPERSEDED mariadb-2.1.3 Upgrade complete
3 SUPERSEDED mariadb-2.1.3 Upgrade complete
4 DEPLOYED mariadb-2.1.3 Upgrade complete
回滚到版本3:
> helm rollback cranky-whippet 3
Rollback was a success! Happy Helming!
> helm history cranky-whippet
REVISION STATUS CHART DESCRIPTION
1 SUPERSEDED mariadb-2.1.3 Install complete
2 SUPERSEDED mariadb-2.1.3 Upgrade complete
3 SUPERSEDED mariadb-2.1.3 Upgrade complete
4 SUPERSEDED mariadb-2.1.3 Upgrade complete
5 DEPLOYED mariadb-2.1.3 Rollback to 3
验证更改是否已回滚:
> helm get values cranky-whippet
mariadbDatabase: awesome_stuff
mariadbRootPassword: evenbettersecret
7. 删除版本
可以使用
helm delete
命令删除版本。首先,查看版本列表:
> helm list
NAME REVISION STATUS CHART NAMESPACE
cranky-whippet 5 DEPLOYED mariadb-2.1.3 default
然后删除它:
> helm delete cranky-whippet
release "cranky-whippet" deleted
现在没有版本了:
> helm list
不过,Helm会跟踪已删除的版本。使用
--all
标志可以查看它们:
> helm list --all
NAME REVISION STATUS CHART NAMESPACE
cranky-whippet 5 DELETED mariadb-2.1.3 default
要完全删除版本,添加
--purge
标志:
> helm delete --purge cranky-whippet
8. 使用仓库
Helm将图表存储在简单的HTTP服务器仓库中。任何标准HTTP服务器都可以托管Helm仓库。在云端,AWS S3和Google Cloud存储都可以作为启用Web模式的Helm仓库。Helm还附带了一个本地包服务器,用于开发测试,但它运行在客户端机器上,不适合共享。在小团队中,可以在本地网络的共享机器上运行Helm包服务器,供所有团队成员访问。
使用本地包服务器,在单独的终端窗口中输入
helm serve
(它是阻塞的)。默认情况下,Helm将从
~/.helm/repository/local
提供图表。可以将图表放在那里,并使用
helm index
生成索引文件。生成的
index.yaml
文件列出了所有图表。
注意,Helm不提供将图表上传到远程仓库的工具,因为这需要远程服务器理解Helm,知道将图表放在哪里以及如何更新
index.yaml
文件。
在客户端,
helm repo
命令可以列出、添加、删除、索引和更新仓库:
> helm repo
This command consists of multiple subcommands to interact with chart repositories.
It can be used to add, remove, list, and index chart repositories:
Example usage:
$ helm repo add [NAME] [REPO_URL]
Usage:
helm repo [command]
Available commands:
add add a chart repository
index generate an index file for a given a directory
list list chart repositories
remove remove a chart repository
update update information on available charts
9. 使用Helm管理图表
Helm提供了几个命令来管理图表。可以创建新图表:
> helm create cool-chart
Creating cool-chart
Helm将在
cool-chart
下创建以下文件和目录:
-rw-r--r-- 1 gigi.sayfan gigi.sayfan 333B Mar 17 13:36 .helmignore
-rw-r--r-- 1 gigi.sayfan gigi.sayfan 88B Mar 17 13:36 Chart.yaml
drwxr-xr-x 2 gigi.sayfan gigi.sayfan 68B Mar 17 13:36 charts
drwxr-xr-x 7 gigi.sayfan gigi.sayfan 238B Mar 17 13:36 templates
-rw-r--r-- 1 gigi.sayfan gigi.sayfan 1.1K Mar 17 13:36 values.yaml
编辑图表后,可以将其打包成tar压缩存档:
> helm package cool-chart
Helm将创建一个名为
cool-chart-0.1.0.tgz
的存档,并将其存储在本地目录和本地仓库中。还可以使用
helm lint
命令查找图表格式或信息的问题:
> helm lint cool-chart
==> Linting cool-chart
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, no failures
10. 利用入门包
helm create
命令接受一个可选的
--starter
标志,用于指定入门图表。入门图表是位于
$HELM_HOME/starters
的常规图表。作为图表开发者,你可以创建专门用作入门图表的图表。这些图表的设计应考虑以下几点:
-
Chart.yaml
将被生成器覆盖
- 用户期望修改此类图表的内容,因此文档应说明用户如何进行修改
目前,没有办法将图表安装到
$HELM_HOME/starters
,用户必须手动复制。如果你开发入门包图表,请在图表文档中提及这一点。
11. 创建自己的图表
图表是描述一组相关Kubernetes资源的文件集合。一个图表可以用于部署简单的东西,如
memcached
容器,也可以用于部署复杂的东西,如包含HTTP服务器、数据库和缓存的完整Web应用程序栈。
图表以特定的目录树形式创建文件,然后可以打包成版本化的存档进行部署。关键文件是
Chart.yaml
。
12. Chart.yaml文件
Chart.yaml
是Helm图表的主要文件,它需要
name
和
version
字段:
-
name
:图表的名称(与目录名称相同)
-
version
:SemVer 2版本
它还可能包含各种可选字段:
| 字段 | 描述 |
| ---- | ---- |
|
kubeVersion
| 兼容的Kubernetes版本的SemVer范围 |
|
description
| 项目的单句描述 |
|
keywords
| 关于项目的关键字列表 |
|
home
| 项目主页的URL |
|
sources
| 项目源代码的URL列表 |
|
maintainers
| 维护者信息(
name
必需,
email
和
url
可选) |
|
engine
| 模板引擎的名称(默认为
gotpl
) |
|
icon
| 用作图标的SVG或PNG图像的URL |
|
appVersion
| 包含的应用程序的版本 |
|
deprecated
| 图表是否已弃用(布尔值) |
|
tillerVersion
| 此图表所需的Tiller版本 |
13. 图表版本控制
Chart.yaml
中的
version
字段由CLI和Tiller服务器使用。
helm package
命令在构建包名称时将使用
Chart.yaml
中找到的版本。图表包名称中的版本号必须与
Chart.yaml
中的版本号匹配。
14. appVersion字段
appVersion
字段与
version
字段无关。它不被Helm使用,仅作为用户了解部署内容的元数据或文档。Helm不强制其正确性。
15. 弃用图表
有时,你可能希望弃用一个图表。可以通过将
Chart.yaml
中的
deprecated
字段设置为
true
来标记图表已弃用。只需弃用图表的最新版本即可。之后可以重用图表名称并发布未弃用的新版本。
kubernetes/charts
项目的工作流程如下:
1. 更新图表的
Chart.yaml
,标记图表为已弃用并增加版本号
2. 发布新版本的图表
3. 从源仓库中删除图表
16. 图表元数据文件
图表可能包含各种元数据文件,如
README.md
、
LICENSE
和
NOTES.txt
,用于描述图表的安装、配置、使用和许可。
README.md
文件应采用Markdown格式,应提供以下信息:
- 图表提供的应用程序或服务的描述
- 运行图表的任何先决条件或要求
-
values.yaml
中选项的描述和默认值
- 与图表安装或配置相关的任何其他信息
templates/NOTES.txt
文件将在安装后或查看版本状态时显示。应保持说明简洁,并指向
README.md
以获取详细解释。通常会包含使用说明和下一步操作,如连接数据库或访问Web UI的信息。
17. 管理图表依赖项
在Helm中,一个图表可能依赖于任意数量的其他图表。这些依赖项可以通过在
requirements.yaml
文件中列出它们,或者在安装期间将依赖图表复制到
charts/
子目录中来明确表达。
依赖项可以是图表存档(
foo-1.2.3.tgz
)或未打包的图表目录,但名称不能以
_
或
.
开头,因为这些文件会被图表加载器忽略。
18. 使用requirements.yaml管理依赖项
最好在图表中使用
requirements.yaml
文件声明依赖项,而不是手动将图表放在
charts/
子目录中。
requirements.yaml
文件是一个简单的文件,用于列出图表依赖项:
dependencies:
- name: foo
version: 1.2.3
repository: http://example.com/charts
- name: bar
version: 4.5.6
repository: http://another.example.com/charts
总体而言,Helm为Kubernetes提供了强大的包管理功能,从查找和安装图表到自定义、升级和管理依赖项,都提供了丰富的工具和灵活的操作方式,帮助用户更高效地管理Kubernetes集群中的应用程序部署。
Kubernetes包管理器Helm使用指南(续)
19. 管理依赖项的流程图
下面是使用
requirements.yaml
管理图表依赖项的流程,使用mermaid格式的流程图展示:
graph LR
A[创建requirements.yaml文件] --> B[列出依赖图表信息]
B --> C[Helm安装时读取文件]
C --> D[从指定仓库下载依赖图表]
D --> E[部署主图表及依赖图表]
20. 依赖项管理的操作步骤
使用
requirements.yaml
管理依赖项的具体操作步骤如下:
1.
创建
requirements.yaml
文件
:在图表的根目录下创建该文件。
2.
列出依赖项
:按照以下格式列出依赖的图表信息:
dependencies:
- name: foo
version: 1.2.3
repository: http://example.com/charts
- name: bar
version: 4.5.6
repository: http://another.example.com/charts
-
更新依赖项
:运行
helm dependency update命令,Helm会根据requirements.yaml文件下载所需的依赖图表到charts/子目录。
> helm dependency update path/to/your/chart
-
安装图表
:使用
helm install命令安装包含依赖项的图表。
> helm install my-release path/to/your/chart
21. 版本管理的重要性
版本管理在Helm中非常重要,它确保了部署的可重复性和稳定性。以下是版本管理相关的要点总结:
| 管理方面 | 说明 |
| ---- | ---- |
|
Chart.yaml
版本 | 用于CLI和Tiller服务器识别图表版本,打包时包名称中的版本需与
Chart.yaml
一致。 |
|
appVersion
| 作为应用程序版本的元数据,不影响Helm操作,但可帮助用户了解部署内容。 |
| 版本升级 | 使用
helm upgrade
命令可智能更新包到最新版本,但会替换现有值。 |
| 版本回滚 | 通过
helm history
查看可用版本,使用
helm rollback
回滚到指定版本。 |
22. 版本管理操作示例
以下是版本管理操作的具体示例:
1.
查看当前版本值
:
> helm get values my-release
mariadbDatabase: awesome_stuff
mariadbRootPassword: evenbettersecret
- 升级版本并更改值 :
> helm upgrade my-release --set mariadbDatabase=awesome_sauce stable/mariadb
- 查看升级后的值 :
> helm get values my-release
mariadbDatabase: awesome_sauce
- 查看版本历史 :
> helm history my-release
REVISION STATUS CHART DESCRIPTION
1 SUPERSEDED mariadb-2.1.3 Install complete
2 SUPERSEDED mariadb-2.1.3 Upgrade complete
3 DEPLOYED mariadb-2.1.3 Upgrade complete
- 回滚到指定版本 :
> helm rollback my-release 2
23. 仓库管理的操作总结
Helm的仓库管理提供了丰富的功能,以下是使用
helm repo
命令进行仓库管理的操作总结:
| 操作 | 命令示例 | 说明 |
| ---- | ---- | ---- |
| 列出仓库 |
helm repo list
| 显示已添加的图表仓库列表。 |
| 添加仓库 |
helm repo add [NAME] [REPO_URL]
| 将指定URL的仓库添加到Helm。 |
| 删除仓库 |
helm repo remove [NAME]
| 从Helm中删除指定名称的仓库。 |
| 生成索引文件 |
helm repo index [DIR]
| 为指定目录生成索引文件
index.yaml
。 |
| 更新仓库信息 |
helm repo update
| 更新所有已添加仓库的图表信息。 |
24. 仓库管理的操作流程
下面是使用
helm repo
进行仓库管理的操作流程,使用mermaid格式的流程图展示:
graph LR
A[列出仓库] --> B{操作选择}
B -- 添加仓库 --> C[执行helm repo add]
B -- 删除仓库 --> D[执行helm repo remove]
B -- 生成索引 --> E[执行helm repo index]
B -- 更新信息 --> F[执行helm repo update]
C --> G[完成添加]
D --> H[完成删除]
E --> I[完成索引生成]
F --> J[完成信息更新]
25. 自定义图表的最佳实践
在自定义图表时,有一些最佳实践可以遵循,以确保图表的可维护性和易用性:
-
使用配置文件
:将自定义值存储在YAML文件中,通过
-f
标志传递,便于管理和复用。
-
合理使用
--set
:在需要临时覆盖某些值时,使用
--set
命令,但避免滥用,以免导致配置混乱。
-
遵循命名规范
:图表和资源的命名应具有描述性,便于识别和管理。
-
详细的文档
:在
README.md
中提供详细的说明,包括图表功能、使用方法、配置选项等,方便其他用户使用。
26. 总结
Helm作为Kubernetes的包管理器,为用户提供了强大而灵活的功能,涵盖了从图表查找、安装、自定义到版本管理、依赖项管理等多个方面。通过合理使用Helm的各种命令和工具,用户可以更高效地管理Kubernetes集群中的应用程序部署,提高开发和运维的效率。同时,遵循相关的最佳实践和规范,能够确保图表的质量和可维护性,为团队协作和项目的长期发展奠定良好的基础。
希望通过本文的介绍,你能够更好地掌握Helm的使用方法,在Kubernetes环境中更加轻松地管理应用程序。
超级会员免费看
2292

被折叠的 条评论
为什么被折叠?



