跳到主要内容

4、Helm命令

更新于:2024年3月19日

Helm命令

img

目录

[toc]

helm相关

查看helm版本

[root@master1 ~]#helm version
version.BuildInfo{Version:"v3.7.2", GitCommit:"663a896f4a815053445eec4153677ddc24a0a361", GitTreeState:"clean", GoVersion:"go1.16.10"}

helm帮助命令

[root@master1 ~]#helm
The Kubernetes package manager

Common actions for Helm:

- helm search: search for charts
- helm pull: download a chart to your local directory to view
- helm install: upload the chart to Kubernetes
- helm list: list releases of charts

Environment variables:

| Name | Description |
|------------------------------------|-----------------------------------------------------------------------------------|
| $HELM_CACHE_HOME | set an alternative location for storing cached files. |
| $HELM_CONFIG_HOME | set an alternative location for storing Helm configuration. |
| $HELM_DATA_HOME | set an alternative location for storing Helm data. |
| $HELM_DEBUG | indicate whether or not Helm is running in Debug mode |
| $HELM_DRIVER | set the backend storage driver. Values are: configmap, secret, memory, sql. |
| $HELM_DRIVER_SQL_CONNECTION_STRING | set the connection string the SQL storage driver should use. |
| $HELM_MAX_HISTORY | set the maximum number of helm release history. |
| $HELM_NAMESPACE | set the namespace used for the helm operations. |
| $HELM_NO_PLUGINS | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins. |
| $HELM_PLUGINS | set the path to the plugins directory |
| $HELM_REGISTRY_CONFIG | set the path to the registry config file. |
| $HELM_REPOSITORY_CACHE | set the path to the repository cache directory |
| $HELM_REPOSITORY_CONFIG | set the path to the repositories file. |
| $KUBECONFIG | set an alternative Kubernetes configuration file (default "~/.kube/config") |
| $HELM_KUBEAPISERVER | set the Kubernetes API Server Endpoint for authentication |
| $HELM_KUBECAFILE | set the Kubernetes certificate authority file. |
| $HELM_KUBEASGROUPS | set the Groups to use for impersonation using a comma-separated list. |
| $HELM_KUBEASUSER | set the Username to impersonate for the operation. |
| $HELM_KUBECONTEXT | set the name of the kubeconfig context. |
| $HELM_KUBETOKEN | set the Bearer KubeToken used for authentication. |

Helm stores cache, configuration, and data based on the following configuration order:

- If a HELM_*_HOME environment variable is set, it will be used
- Otherwise, on systems supporting the XDG base directory specification, the XDG variables will be used
- When no other location is set a default location will be used based on the operating system

By default, the default directories depend on the Operating System. The defaults are listed below:

| Operating System | Cache Path | Configuration Path | Data Path |
|------------------|---------------------------|--------------------------------|-------------------------|
| Linux | $HOME/.cache/helm | $HOME/.config/helm | $HOME/.local/share/helm |
| macOS | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm |
| Windows | %TEMP%\helm | %APPDATA%\helm | %APPDATA%\helm |

Usage:
helm [command]

Available Commands:
completion generate autocompletion scripts for the specified shell
create create a new chart with the given name
dependency manage a chart's dependencies
env helm client environment information
get download extended information of a named release
help Help about any command
history fetch release history
install install a chart
lint examine a chart for possible issues
list list releases
package package a chart directory into a chart archive
plugin install, list, or uninstall Helm plugins
pull download a chart from a repository and (optionally) unpack it in local directory
repo add, list, remove, update, and index chart repositories
rollback roll back a release to a previous revision
search search for a keyword in charts
show show information of a chart
status display the status of the named release
template locally render templates
test run tests for a release
uninstall uninstall a release
upgrade upgrade a release
verify verify that a chart at the given path has been signed and is valid
version print the client version information

Flags:
--debug enable verbose output
-h, --help help for helm
--kube-apiserver string the address and the port for the Kubernetes API server
--kube-as-group stringArray group to impersonate for the operation, this flag can be repeated to specify multiple groups.
--kube-as-user string username to impersonate for the operation
--kube-ca-file string the certificate authority file for the Kubernetes API server connection
--kube-context string name of the kubeconfig context to use
--kube-token string bearer token used for authentication
--kubeconfig string path to the kubeconfig file
-n, --namespace string namespace scope for this request
--registry-config string path to the registry config file (default "/root/.config/helm/registry.json")
--repository-cache string path to the file containing cached repository indexes (default "/root/.cache/helm/repository")
--repository-config string path to the file containing repository names and URLs (default "/root/.config/helm/repositories.yaml")

Use "helm [command] --help" for more information about a command.
[root@master1 ~]#

helm repo

查看当前helm chart仓库:list命令

helm repo list == helm repo ls

例子:
[root@Devops6 ~]#helm repo list
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /root/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /root/.kube/config
NAME URL
stable http://mirror.azure.cn/kubernetes/charts/
devops6repo http://172.29.9.120/chartrepo/devops6/

helm添加仓库并更新:add命令

# helm repo add 仓库名称 仓库地址

helm repo add apisix https://charts.apiseven.com
helm repo update

更新helm仓库:update命令

helm repo update

img

查找chart:search命令

[root@master ~]#helm search --help
Usage:
helm search [command]

Available Commands:
hub search for charts in the Artifact Hub or your own hub instance
repo search repositories for a keyword in charts

#说明:
helm search hub 从 artifact hub中搜索
helm search repo 从本地的仓库搜索

案例:

用 search 命令来搜索可以安装的 chart 包

[root@master ~]#helm search repo
或者
[root@master ~]#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.1 v4.5.0.5 A Helm chart for Aerospike in Kubernetes
stable/airflow 5.2.1 1.10.4 Airflow is a platform to programmatically autho...
stable/ambassador 5.1.0 0.85.0 A Helm chart for Datawire Ambassador
stable/anchore-engine 1.3.7 0.5.2 Anchore container analysis and policy evaluatio...
stable/apm-server 2.1.5 7.0.0 The server receives data from the Elastic APM a...
......

用 search 命令来搜索可以安装的 mysql chart 包:

虽然这里是一个废弃的版本,但是用来做测试是没任何问题的。

[root@master ~]#helm search repo mysql
NAME CHART VERSION APP VERSION DESCRIPTION
stable/mysql 1.6.9 5.7.30 DEPRECATED - Fast, reliable, scalable, and easy...
stable/mysqldump 2.6.2 2.4.1 DEPRECATED! - A Helm chart to help backup MySQL...
stable/prometheus-mysql-exporter 0.7.1 v0.11.0 DEPRECATED A Helm chart for prometheus mysql ex...
stable/percona 1.2.3 5.7.26 DEPRECATED - free, fully compatible, enhanced, ...
stable/percona-xtradb-cluster 1.0.8 5.7.19 DEPRECATED - free, fully compatible, enhanced, ...
stable/phpmyadmin 4.3.5 5.0.1 DEPRECATED phpMyAdmin is an mysql administratio...
stable/gcloud-sqlproxy 0.6.1 1.11 DEPRECATED Google Cloud SQL Proxy
stable/mariadb 7.3.14 10.3.22 DEPRECATED Fast, reliable, scalable, and easy t...
[root@master ~]#

推送包到helm chart仓库

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

helm 默认是不带 push 功能的,因此无法直接将本地的 chart 文件直接上传到 ChartMuseum 中。

1、安装插件

$ helm plugin install https://github.com/chartmuseum/helm-push
Downloading and installing helm-push v0.10.1 ...
https://github.com/chartmuseum/helm-push/releases/download/v0.10.1/helm-push_0.10.1_darwin_amd64.tar.gz
Installed plugin: cm-push

  • 直接安装二进制包
mv helm-cm-push /usr/bin/

[root@Devops6 devops6-npm-service_HELMCI]#helm-cm-push devops6-npm-service-1.1.1.tgz devops6repo
Pushing devops6-npm-service-1.1.1.tgz to devops6repo...
Done.

image-20230708223118001

image-20230708223127871

软件包:

链接:https://pan.baidu.com/s/1GvFSkMdRMBloKx8vwxKakA?pwd=0820 提取码:0820 2023.7.9-helm-push

image-20230709153806614

2、使用

先添加对应的 ChartMuseum 地址,

$ helm repo add chartmuseum http://your-cm-repo:8080

然后可以针对你需要 push 的文件,或者目录,甚至完整的 tgz 包,进行上传,

$ helm cm-push mychart-0.3.2.tgz chartmuseum
Pushing mychart-0.3.2.tgz to chartmuseum...
Done.

如果 CM 开启了验证,需要指定用户名和密码,

$ helm cm-push mychart-0.3.2.tgz chartmuseum -u username -p password
Pushing mychart-0.3.2.tgz to chartmuseum...
Done.

更多参数可以使用 help 命令查看,

$ helm cm-push --help
Helm plugin to push chart package to ChartMuseum

Examples:

$ helm cm-push mychart-0.1.0.tgz chartmuseum # push .tgz from "helm package"
$ helm cm-push . chartmuseum # package and push chart directory
$ helm cm-push . --version="1.2.3" chartmuseum # override version in Chart.yaml
$ helm cm-push . https://my.chart.repo.com # push directly to chart repo URL
...

参考文档:

  1. https://github.com/chartmuseum/helm-push

helm下载charts包:fetch命令

这里我们可以使用helm fetch stable/mysql命令将包下载下来。

[root@master1 ~]#helm fetch stable/mysql
[root@master1 ~]#ll -h mysql-1.6.9.tgz
-rw-r--r-- 1 root root 12K Apr 4 12:25 mysql-1.6.9.tgz

helm install

部署helm实例:install命令

## 安装chart
helm install RELEASE_NAME CHART_NAME
helm install happy-panda bitnami/wordpress

### 本地 chart 压缩包
helm install foo foo-0.1.1.tgz

### 解压后的 chart 目录
helm install foo path/to/foo

### 完整的 URL
helm install foo https://example.com/charts/foo-1.2.3.tgz

==工作里比较常用的一条命令==【荐】

为了安装一个 chart 包,我们可以使用 helm install 命令,Helm 有多种方法来找到和安装 chart 包,但是最简单的方法当然是使用官方的 stable 这个仓库直接安装:

首先从仓库中将可用的 charts 信息同步到本地,可以确保我们获取到最新的 charts 列表:(一般每次安装/更新时都建议先update下仓库)

☸ ➜ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈ Happy Helming!

一条命令部署应用:

案例1:
[root@master1 ~]#helm upgrade --install longhorn longhorn/longhorn --create-namespace --namespace longhorn-system -f values.yaml
#说明
longhorn 应用的名称
longhorn/longhorn chart包
upgrade --install 第一次是安装,后面就是更新了
--create-namespace 如果是本次需要用到的命名空间,可以加上这个参数

案例2:
[root@master1 ~]#helm upgrade --install traefik ./traefik -f ./traefik/ci/deployment-prod.yaml --create-namespace --namespace kube-system
Release "traefik" has been upgraded. Happy Helming!
NAME: traefik
LAST DEPLOYED: Sun Jan 16 11:55:13 2022
NAMESPACE: kube-system
STATUS: deployed
REVISION: 2
TEST SUITE: None

helm install 命令可以从多个源进行安装

  • chart 仓库(类似于上面我们提到的)
  • 本地 chart 压缩包(helm install foo-0.1.1.tgz)
  • 本地解压缩的 chart 目录(helm install foo path/to/foo)
  • 在线的 URL(helm install fool https://example.com/charts/foo-1.2.3.tgz)

注意:有时helm upgrade --install命令可能下载不下来

有时helm upgrade --install命令可能下载不下来,可以先用helm fetch longhorn/longhorn命令将helm软件包下载下来,然后修改好values.yaml文件,再使用如下命令安装就好:

helm upgrade --install longhorn .  --create-namespace --namespace longhorn-system -f values.yaml

特别注意:做实验时一定要记得当时对values.yaml文件做了哪方面的改动!

不然可能导致helm安装软件失败

-generate-name选项:生成一个随机名称

[root@master1 ~]#helm install stable/mysql --generate-name
WARNING: This chart is deprecated
NAME: mysql-1649031212
LAST DEPLOYED: Mon Apr 4 08:13:37 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1649031212.default.svc.cluster.local

To get your root password run:

MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-1649031212 -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

$ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
$ mysql -h mysql-1649031212 -p

To connect to your database directly from outside the K8s cluster:
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306

# Execute the following command to route the connection:
kubectl port-forward svc/mysql-1649031212 3306

mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}

helm升级:upgrade命令

[root@master1 ~]#helm upgrade mysql stable/mysql -f 01-config.yaml

helm rollback

rollback命令

## 回滚
helm history [RELEASE] 命令来查看一个特定 release 的修订版本号。
helm rollback
helm rollback [RELEASE] [REVISION]

例子:

[root@master1 ~]#helm rollback mysql 1
Rollback was a success! Happy Helming!

helm release

查看与卸载helm实例

1.查看已经安装的release
[root@master1 ~]#helm list -n apisix #helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
apisix apisix 1 2022-02-14 07:30:08.608328227 +0800 CST deployed apisix-0.7.2 2.10.0
或者:
helm status mysql-1649031212 #查看release的状态

2.卸载helm实例
[root@master1 ~]#helm uninstall apisix -n apisix
release "apisix" uninstalled
或者
➜ helm uninstall mysql-1575619811 --keep-history
release "mysql-1575619811" uninstalled

也可以在删除的时候使用 --keep-history 参数,则会保留 release 的历史记录,可以获取该 release 的状态就是 UNINSTALLED,而不是找不到 release了:

➜ helm uninstall mysql-1575619811 --keep-history
release "mysql-1575619811" uninstalled

➜ helm status mysql-1575619811
helm status mysql-1575619811
NAME: mysql-1575619811
LAST DEPLOYED: Fri Dec 6 16:47:14 2019
NAMESPACE: default
STATUS: uninstalled
...

➜ helm ls -a
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mysql-1575619811 default 1 2019-12-06 16:47:14.415214 +0800 CST uninstalled mysql-1.5.0 5.7.27

因为 Helm 会在删除 release 后跟踪你的 release,所以你可以审查历史甚至取消删除 release(使用 helm rollback 命令)。

⚠️ 注意:也可以使用helm delete来卸载release

[root@master ~]#helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mysql-1711286279 default 1 2024-03-24 21:18:00.924308284 +0800 CST deployed mysql-1.6.9 5.7.30
[root@master ~]#helm delete mysql-1711286279
release "mysql-1711286279" uninstalled
[root@master ~]#helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
[root@master ~]#

查看某个release通过-f或者--自定义的值

[root@master ~]#helm get values mysql
USER-SUPPLIED VALUES:
mysqlDatabase: user0db
mysqlPassword: user0pwd
mysqlUser: user0
persistence:
enabled: false
[root@master ~]#

追踪release的状态

## 追踪release的状态
helm status happy-panda

查看某个release的历史

[root@master1 ~]#helm history mysql

查看release安装后对应的k8s资源状态

[root@master1 ~]#kubectl get all -l release=mysql-1649031212
NAME READY STATUS RESTARTS AGE
pod/mysql-1649031212-9df8c986b-8tj8z 1/1 Running 0 2m3s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql-1649031212 ClusterIP 10.105.125.254 <none> 3306/TCP 2m4s

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql-1649031212 1/1 1 1 2m4s

NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-1649031212-9df8c986b 1 1 1 2m3s

helm chart

查看一个 chart 包的所有可配置的参数选项

☸ ➜ helm show values stable/mysql
## mysql image version
## ref: https://hub.docker.com/r/library/mysql/tags/
##
image: "mysql"
imageTag: "5.7.30"

strategy:
type: Recreate

busybox:
image: "busybox"
tag: "1.32"

testFramework:
enabled: true
image: "bats/bats"
tag: "1.2.1"
imagePullPolicy: IfNotPresent
securityContext: {}

## Specify password for root user
##
## Default: random 10 character string
# mysqlRootPassword: testing

## Create a database user
##
# mysqlUser:
## Default: random 10 character string
# mysqlPassword:

## Allow unauthenticated access, uncomment to enable
##
# mysqlAllowEmptyPassword: true

## Create a database
##
# mysqlDatabase:

## 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
......

查看chart包的特性

[root@master1 ~]#helm show chart stable/mysql
apiVersion: v1
appVersion: 5.7.30
deprecated: true
description: DEPRECATED - Fast, reliable, scalable, and easy to use open-source relational
database system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 1.6.9
  • 如果想要了解更多信息,可以用 helm show all 命令:
➜ helm show all stable/mysql #可以看一些定制特性!
......

查看一个 chart 包的所有可配置的参数选项

## 查看chart中的可配置选项
helm show values CHART_NAME
helm install -f values.yaml bitnami/wordpress --generate-name

数据传递:
--values /-f 指定yaml文件
--set 命令行方式对值进行覆盖

helm get values 来查看新设置是否生效

[root@master1 ~]#helm get values mysql

helm debug命令

[root@master1 ~]#helm ls --debug

helm template命令 只渲染,不部署

语法:
➜ ~ helm template -h
Usage:
helm template [NAME] [CHART] [flags]

Render(渲染) chart templates locally and display the output.
……

不部署,但可以将最终yaml文件给渲染出来:

例子:
$ helm template mysql stable/mysql > mysql.yaml
$ cat mysql.yaml

img

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 微信二维码 x2675263825 (舍得), qq:2675263825。

image-20230107215114763

🍀 微信公众号 《云原生架构师实战》

image-20230107215126971

🍀 语雀

https://www.yuque.com/xyy-onlyone

image-20230624093747671

🍀 csdn https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

image-20230107215149885

🍀 知乎 https://www.zhihu.com/people/foryouone

image-20230107215203185

最后

好了,关于本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!