Helm k8s包管理

766 阅读3分钟

什么是helm?

简单来说helm就是k8s的包管理器,类似centos的yum和python的pip,比我在centos系统中,yum管理的包我们成为rpm包,在k8s中,使用helm管理的包,我们称之为charts,charts用来封装k8s应用程序一系列的yaml文件。

对于使用者而言,helm不需要再编写复杂的应用部署文件,可以简单地在k8s上查找、安装、升级、回滚、卸载应用程序。

目前(2021年)helm大版本是3,与helm 2最大的区别是移除了Tiller,helm2是CS架构,Tiller是服务端,现在没有tiller组件,而是由helm直接与k8s交互。

image.png

Install

方法1

$ 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

方法2

$ git clone https://github.com/helm/helm.git
$ cd helm
$ make

默认安装目录:/usr/local/bin/helm

如何使用

添加源

# 官方源
helm repo add stable https://charts.helm.sh/stable

# 阿里云源
helm repo add aliyuncs https://apphub.aliyuncs.com

查看哪些源

helm repo list

搜索包

查看源包含哪些charts

root@0 # helm  search repo stable
NAME                                 	CHART VERSION	APP VERSION            DESCRIPTION
stable/acs-engine-autoscaler         	2.2.2        	2.1.1                  DEPRECATED Scales worker nodes within agent pools
stable/aerospike                     	0.3.5        	v4.5.0.5               DEPRECATED A Helm chart for Aerospike in Kubern...
stable/airflow                       	7.13.3       	1.10.12                DEPRECATED - please use: https://github.com/air...
stable/ambassador                    	5.3.2        	0.86.1
......any more

搜索指定chart包

Helm 自带一个强大的搜索命令,可以用来从两种来源中进行搜索:

  • helm search hub 从 Artifact Hub 中查找并列出 helm charts。 Artifact Hub中存放了大量不同的仓库
  • helm search repo 从你添加(使用 helm repo add)到本地 helm 客户端中的仓库中进行查找。该命令基于本地数据进行搜索,无需连接互联网。
$ helm search hub wordpress
URL                                                 CHART VERSION APP VERSION DESCRIPTION
https://hub.helm.sh/charts/bitnami/wordpress        7.6.7         5.2.4       Web publishing platform for building blogs and ...
https://hub.helm.sh/charts/presslabs/wordpress-...  v0.6.3        v0.6.3      Presslabs WordPress Operator Helm Chart
https://hub.helm.sh/charts/presslabs/wordpress-...  v0.7.1        v0.7.1      A Helm chart for deploying a WordPress site on ...

模糊搜索 支持使用模糊字符串匹配算法,所以可以只输入名字的一部分。

安装chart

获取chart包

helm pull aliyuncs/nginx --untar
  • --untar : 解压包

执行命令后会在当前目录生成一个nginx文件夹

root@0 # tree
.
├── Chart.yaml
├── ci
│   └── values-with-ingress-metrics-and-serverblock.yaml
├── README.md
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── server-block-configmap.yaml
│   ├── servicemonitor.yaml
│   ├── svc.yaml
│   └── tls-secrets.yaml
├── values.schema.json
└── values.yaml

2 directories, 13 files

现在开始正式安装 使用 helm install 命令来安装一个新的 helm 包。最简单的使用方法只需要传入两个参数:你命名的release名字和你想安装的chart的名称

$ helm install my-nginx aliyuncs/nginx

你可以使用 helm status 来追踪 release 的状态,或是重新读取配置信息:

查看状态

 helm status my-nginx   

删除包

helm uninstall my-nginx

以上方法只是安装默认配置,很多时候,我们需要自定义 chart 来指定我们想要的配置。

使用 helm show values 可以查看 chart 中的可配置选项:

root@0 # helm search repo nginx
NAME                             	CHART VERSION	APP VERSION         	DESCRIPTION
aliyuncs/nginx                   	5.1.5        	1.16.1              	Chart for the nginx server
aliyuncs/nginx-ingress           	1.30.3       	0.28.0              	An nginx Ingress controller that uses ConfigMap...
aliyuncs/nginx-ingress-controller	5.3.4        	0.29.0              	Chart for the nginx Ingress controller
aliyuncs/nginx-lego              	0.3.1        	                    	Chart for nginx-ingress-controller and kube-lego
aliyuncs/nginx-php               	1.0.0        	nginx-1.10.3_php-7.0	Chart for the nginx php server
stable/nginx-ingress             	1.41.3       	v0.34.1             	DEPRECATED! An nginx Ingress controller that us...
stable/nginx-ldapauth-proxy      	0.1.6        	1.13.5              	DEPRECATED - nginx proxy with ldapauth
stable/nginx-lego                	0.3.1        	                    	Chart for nginx-ingress-controller and kube-lego
stable/gcloud-endpoints          	0.1.2        	1                   	DEPRECATED Develop, deploy, protect and monitor...
YinHeOffice20 [~/nginx/templates] 03/26/21 18:45:57
root@0 # helm show values aliyuncs/nginx
## Global Docker image parameters
## Please, note that this will override the image parameters, including dependencies, configured to use the global value
## Current available global Docker image parameters: imageRegistry and imagePullSecrets
##
# global:
#   imageRegistry: myRegistryName
#   imagePullSecrets:
#     - myRegistryKeySecretName

## Bitnami NGINX image version
## ref: https://hub.docker.com/r/bitnami/nginx/tags/
##
image:
  registry: docker.io
  repository: bitnami/nginx
  tag: 1.16.1-debian-10-r0
  ## Specify a imagePullPolicy
  ## Defaults to 'Always' if image tag is 'latest', else set to 'IfNotPresent'
  ## ref: http://kubernetes.io/docs/user-guide/images/#pre-pulling-images
  ##
  pullPolicy: IfNotPresent
  ## Optionally specify an array of imagePullSecrets.
  ## Secrets must be manually created in the namespace.
  ## ref: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
  ##
  # pullSecrets:
  #   - myRegistryKeySecretName

## String to partially override nginx.fullname template (will maintain the release name)
##
# nameOverride:

## String to fully override nginx.fullname template
##
# fullnameOverride:

## Custom server block to be added to NGINX configuration
## PHP-FPM example server block:
## serverBlock: |-
##   server {
##     listen 0.0.0.0:8080;
##     root /app;
##     location / {
##       index index.html index.php;
##     }
##     location ~ \.php$ {
##       fastcgi_pass phpfpm-server:9000;
##       fastcgi_index index.php;
##       include fastcgi.conf;
##     }
##   }
##
# serverBlock:

## Number of replicas to deploy
##
replicaCount: 1

然后,你可以使用 YAML 格式的文件覆盖上述任意配置项,并在安装过程中使用该文件。

$ echo '{mariadb.auth.database: user0db, mariadb.auth.username: user0}' > values.yaml
$ helm install -f values.yaml bitnami/wordpress --generate-name

上述命令将为 MariaDB 创建一个名称为 user0 的默认用户,并且授予该用户访问新建的 user0db 数据库的权限。chart 中的其他默认配置保持不变。

安装过程中有两种方式传递配置数据:

  • --values (或 -f):使用 YAML 文件覆盖配置。可以指定多次,优先使用最右边的文件。
  • --set:通过命令行的方式对指定项进行覆盖。

如果同时使用两种方式,则 --set 中的值会被合并到 --values 中,但是 --set 中的值优先级更高。在--set 中覆盖的内容会被被保存在 ConfigMap 中。可以通过 helm get values 来查看指定 release 中 --set 设置的值。也可以通过运行 helm upgrade 并指定 --reset-values 字段来清除 --set 中设置的值。

更多详细内容参考:helm.sh/zh/docs/int…

自定义Chart

chart 开发指南 介绍了如何开发你自己的chart。 但是你也可以通过使用 helm create 命令来快速开始:

$ helm create deis-workflow
Creating deis-workflow

现在,./deis-workflow 目录下已经有一个 chart 了。你可以编辑它并创建你自己的模版。

在编辑 chart 时,可以通过 helm lint 验证格式是否正确。

当准备将 chart 打包分发时,你可以运行 helm package 命令:

$ helm package deis-workflow
deis-workflow-0.1.0.tgz

然后这个 chart 就可以很轻松的通过 helm install 命令安装:

$ helm install deis-workflow ./deis-workflow-0.1.0.tgz
...

打包好的 chart 可以上传到 chart 仓库中。查看 Helm chart 仓库获取更多信息。