目录
一、介绍
Helm
是一个用于管理和部署 Kubernetes 应用程序的包管理工具。它可以帮助简化和自动化 Kubernetes 应用程序的部署、升级和管理过程。
官网:https://helm.sh/
官方文档:https://helm.sh/zh/docs/
Helm 通过使用 Charts的概念来组织和管理 Kubernetes 应用程序。Chart 是一个预定义的包含 Kubernetes 资源定义和相关配置的文件集合。它可以包含 Deployment、Service、ConfigMap、Ingress 等 Kubernetes 资源的定义,以及用于自定义配置值的模板文件。
Helm 三大概念
Chart 代表着 Helm 包。它包含在 Kubernetes 集群内部运行应用程序,工具或服务所需的所有资源定义。你可以把它看作是 Homebrew formula,Apt dpkg,或 Yum RPM 在Kubernetes 中的等价物。
Repository(仓库) 是用来存放和共享 charts 的地方。它就像 Perl 的 CPAN 档案库网络 或是 Fedora 的 软件包仓库,只不过它是供 Kubernetes 包所使用的。
Release 是运行在 Kubernetes 集群中的 chart 的实例。一个 chart 通常可以在同一个集群中安装多次。每一次安装都会创建一个新的 release。以 MySQL chart为例,如果你想在你的集群中运行两个数据库,你可以安装该chart两次。每一个数据库都会拥有它自己的 release 和 release name。
在了解了上述这些概念以后,我们就可以这样来解释 Helm:
Helm 安装 charts 到 Kubernetes 集群中,每次安装都会创建一个新的 release。你可以在 Helm 的 chart repositories 中寻找新的 chart。
二、安装
首先需要准备一个k8s集群
官方安装文档:https://helm.sh/zh/docs/intro/install/
可以选择下载二进制文件手动安装:https://github.com/helm/helm/releases
或者选择脚本安装:
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
# 查看helm版本
helm version
添加一个chart 仓库
helm repo add bitnami https://charts.bitnami.com/bitnami
三、通过部署Nginx了解Helm基本命令
安装chart
通过helm install
命令安装chart。
helm install bitnami/nginx --generate-name
可以通过执行 helm show chart bitnami/nginx
命令了解这个chart的基本信息。每执行 helm install
的时候,都会创建一个新的发布版本。 所以一个chart在同一个集群里面可以被安装多次,每一个都可以被独立的管理和升级。
helm list
(或 helm ls
) 命令会列出所有可被部署的版本。
卸载Chart
#mysql-1612624192 表示ChartName
helm uninstall nginx-1688310640
四、部署自定义 chart
# 创建一个Helm Chart Demo
helm create mychart
通过这个Demo我们可以了解Helm Chart的基本目录结构:
- chart : 包含其他的chart(称之为 子chart)。
- Chart.yaml :包含了该chart的描述。
- templates :模板文件,当Helm评估chart时,会通过模板渲染引擎将所有文件发送到templates/目录中。 然后收集模板的结果并发送给Kubernetes。
- values.yaml : 配置文件。
现在我们将templates目录清空,创建一个自己Chart:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
我们用{{ .Release.Name }}
在模板中插入版本名称。Release是你可以在模板中访问的顶层对象之一。
Helm提供了一种简单命令执行安装或升级的方法。使用helm upgrade
和--install
命令,Helm会查看是否已经安装版本, 如果没有,会执行安装;如果版本存在,会进行升级
helm upgrade --install <release name> --values <values file> <chart directory>
helm ls
helm upgrade --install mychart ./mychart/
helm get manifest mychart
# 查看k8s集群默认空间下的Configmaps
kubectl get cm
helm get manifest
命令后跟一个发布名称(mychart)然后打印出了所有已经上传到server的Kubernetes资源。 每个文件以—开头表示YAML文件的开头,然后是自动生成的注释行,表示哪个模板文件生成了这个YAML文档。
当templates目录下的资源文件更新了以后,同样可以使用helm upgrade --install
命令进行升级部署:
ok, Configmaps只是一个简单的示例,我们可以将我们项目创建的资源deployment.yaml、service.yaml等都放在helm的templates目录下,创建一个chart包,使用helm部署。
五、Helm 模版函数 和 管道符 |
Helm 有超过60个可用函数。
从一个最佳实践开始:可以通过调用模板指令中的quote函数
把.Values
对象中的字符串属性用引号引起来,然后放到模板中。
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ quote .Values.favorite.drink }}
food: {{ quote .Values.favorite.food }}
模板语言其中一个强大功能是 管道 概念。借鉴UNIX中的概念,管道符是将一系列的模板语言紧凑地将多个流式处理结果合并的工具。换句话说,管道符是按顺序完成一系列任务的方式。
例如将 .Values.favorite.food
的值先大写,然后加上双引号:
food: {{ .Values.favorite.food | upper | quote }}
required
方法可以让你声明模板渲染所需的特定值。如果这个值是空的,模板渲染会出错并打印用户提交的错误信息。
value: {{ required "A valid .Values.who entry required!" .Values.who }}
还有更多的函数就不一一举例了,大家如果有不清楚的可以去官方文档查阅:https://helm.sh/zh/docs/chart_template_guide/function_list/
附录 Helm 命令
# Chart 管理
helm create <name> # Creates a chart directory along with the common files and directories used in a chart.
helm package <chart-path> # Packages a chart into a versioned chart archive file.
helm lint <chart> # Run tests to examine a chart and identify possible issues:
helm show all <chart> # Inspect a chart and list its contents:
helm show values <chart> # Displays the contents of the values.yaml file
helm pull <chart> # Download/pull chart
helm pull <chart> --untar=true # If set to true, will untar the chart after downloading it
helm pull <chart> --verify # Verify the package before using it
helm pull <chart> --version <number> # Default-latest is used, specify a version constraint for the chart version to use
helm dependency list <chart> # Display a list of a chart’s dependencies:
# 安装和卸载应用
helm install <name> <chart> # Install the chart with a name
helm install <name> <chart> --namespace <namespace> # Install the chart in a specific namespace
helm install <name> <chart> --set key1=val1,key2=val2 # Set values on the command line (can specify multiple or separate values with commas)
helm install <name> <chart> --values <yaml-file/url> # Install the chart with your specified values
helm install <name> <chart> --dry-run --debug # Run a test installation to validate chart (p)
helm install <name> <chart> --verify # Verify the package before using it
helm install <name> <chart> --dependency-update # update dependencies if they are missing before installing the chart
helm uninstall <name> # Uninstall a release
# 应用升级和回滚
helm upgrade <release> <chart> # Upgrade a release
helm upgrade <release> <chart> --atomic # If set, upgrade process rolls back changes made in case of failed upgrade.
helm upgrade <release> <chart> --dependency-update # update dependencies if they are missing before installing the chart
helm upgrade <release> <chart> --version <version_number> # specify a version constraint for the chart version to use
helm upgrade <release> <chart> --values # specify values in a YAML file or a URL (can specify multiple)
helm upgrade <release> <chart> --set key1=val1,key2=val2 # Set values on the command line (can specify multiple or separate valuese)
helm upgrade <release> <chart> --force # Force resource updates through a replacement strategy
helm rollback <release> <revision> # Roll back a release to a specific revision
helm rollback <release> <revision> --cleanup-on-fail # Allow deletion of new resources created in this rollback when rollback fails
# 列出,添加,移除和升级仓库
helm repo add <repo-name> <url> # Add a repository from the internet:
helm repo list # List added chart repositories
helm repo update # Update information of available charts locally from chart repositories
helm repo remove <repo_name> # Remove one or more chart repositories
helm repo index <DIR> # Read the current directory and generate an index file based on the charts found.
helm repo index <DIR> --merge # Merge the generated index with an existing index file
helm search repo <keyword> # Search repositories for a keyword in charts
helm search hub <keyword> # Search for charts in the Artifact Hub or your own hub instance
# Helm 发布检测
helm list # Lists all of the releases for a specified namespace, uses current namespace context if namespace not specified
helm list --all # Show all releases without any filter applied, can use -a
helm list --all-namespaces # List releases across all namespaces, we can use -A
helm -l key1=value1,key2=value2 # Selector (label query) to filter on, supports '=', '==', and '!='
helm list --date # Sort by release date
helm list --deployed # Show deployed releases. If no other is specified, this will be automatically enabled
helm list --pending # Show pending releases
helm list --failed # Show failed releases
helm list --uninstalled # Show uninstalled releases (if 'helm uninstall --keep-history' was used)
helm list --superseded # Show superseded releases
helm list -o yaml # Prints the output in the specified format. Allowed values: table, json, yaml (default table)
helm status <release> # This command shows the status of a named release.
helm status <release> --revision <number> # if set, display the status of the named release with revision
helm history <release> # Historical revisions for a given release.
helm env # Env prints out all the environment information in use by Helm.
#下载发布信息
helm get all <release> # A human readable collection of information about the notes, hooks, supplied values, and generated manifest file of the given release.
helm get hooks <release> # This command downloads hooks for a given release. Hooks are formatted in YAML and separated by the YAML '---\n' separator.
helm get manifest <release> # A manifest is a YAML-encoded representation of the Kubernetes resources that were generated from this release's chart(s). If a chart is dependent on other charts, those resources will also be included in the manifest.
helm get notes <release> # Shows notes provided by the chart of a named release.
helm get values <release> # Downloads a values file for a given release. use -o to format output
# 插件管理
helm plugin install <path/url1> # Install plugins
helm plugin list # View a list of all installed plugins
helm plugin update <plugin> # Update plugins
helm plugin uninstall <plugin> # Uninstall a plugin
如果您有什么问题或者经验,欢迎评论区交流。
如果您觉得本文对您有帮助,欢迎点赞、评论、分享。
您的支持是我创作的最大动力。