helm3使用入门

helm3使用入门

概述

美国时间 2019 年 11 月 13 日,Helm 团队宣布 Helm 3 第一个稳定版本发布。

Helm 3 以 Helm 2 的核心功能为基础,对 Chart repo、发行版管理、安全性和 library Charts 进行了改进。

相比 Helm 2,Helm 3 最明显的变化是 Tiller 的删除,它新增丰富功能,某些功能已被弃用或重构,与 Helm 2 不再兼容。此外,Helm 3 还引入了一些新的实验功能,包括 OCI 支持。

Helm Go SDK 已重构为通用,目标是共享和重用 Go 社区开源代码。

什么是 Helm

Helm 为团队提供了在 Kubernetes 内部创建、安装和管理应用程序时需要协作的工具,有点类似于 Ubuntu 中的 APT 或 CentOS 中的 YUM。

有了 Helm,开发者可以:

  • 查找要安装和使用的预打包软件(Chart)
  • 轻松创建和托管自己的软件包
  • 将软件包安装到任何 K8s 集群中
  • 查询集群以查看已安装和正在运行的程序包
  • 更新、删除、回滚或查看已安装软件包的历史记录
  • ……

Helm 组件及相关术语

helm

Helm 是一个命令行下的客户端工具。主要用于 Kubernetes 应用程序 Chart 的创建、打包、发布以及创建和管理本地和远程的 Chart 仓库。

Chart

Helm 的软件包,采用 TAR 格式。类似于 APT 的 DEB 包或者 YUM 的 RPM 包,其包含了一组定义 Kubernetes 资源相关的 YAML 文件。

Repoistory

Helm 的软件仓库,Repository 本质上是一个 Web 服务器,该服务器保存了一系列的 Chart 软件包以供用户下载,并且提供了一个该 Repository 的 Chart 包的清单文件以供查询。Helm 可以同时管理多个不同的 Repository。

Release

使用 helm install 命令在 Kubernetes 集群中部署的 Chart 称为 Release。可以理解为 Helm 使用 Chart 包部署的一个应用实例。

安装 Helm3

二进制安装
$ wget https://get.helm.sh/helm-v3.1.2-linux-amd64.tar.gz
$ tar -zxvf helm-v3.1.2-linux-amd64.tar.gz
$ cd linux-amd64
$ mv helm /usr/local/bin/
$ helm --help
脚本安装
$ 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
命令补全
# 在~/.bashrc追加
source <(helm completion bash)

其他安装详情

快速开始

添加常用仓库

$ helm repo add stable https://kubernetes-charts.storage.googleapis.com/
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com/
$ helm repo update              # Make sure we get the latest list of charts

微软也提供了stable仓库的镜像:

  • stable: http://mirror.azure.cn/kubernetes/charts/

列出stable仓库可以安装的charts

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.2.8           v4.5.0.5                        A Helm chart for Aerospike in Kubernetes
stable/airflow                          4.1.0           1.10.4                          Airflow is a platform to programmatically autho...
stable/ambassador                       4.1.0           0.81.0                          A Helm chart for Datawire Ambassador
# ... and many more

安装stable仓库的一个mysql的chart

$ helm install stable/mysql --generate-name

查看详情

$ helm show all stable/mysql

查看chart怎么写的

$ helm pull stable/mysql

$ ls 
mysql-1.6.3.tgz

$ tar -zxvf mysql-1.6.3.tgz

$ tree mysql
mysql
├── Chart.yaml
├── README.md
├── templates
│   ├── configurationFiles-configmap.yaml
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── initializationFiles-configmap.yaml
│   ├── NOTES.txt
│   ├── pvc.yaml
│   ├── secrets.yaml
│   ├── serviceaccount.yaml
│   ├── servicemonitor.yaml
│   ├── svc.yaml
│   └── tests
│       ├── test-configmap.yaml
│       └── test.yaml
└── values.yaml

查看用helm安装的release

$ helm ls
NAME             VERSION   UPDATED                   STATUS    CHART
smiling-penguin  1         Wed Sep 28 12:59:46 2016  DEPLOYED  mysql-0.1.0

卸载release

$ helm uninstall smiling-penguin

查看status

$ helm status smiling-penguin
Status: UNINSTALLED

Chart 的使用

下面我们通过一个完整的示例来学习如何使用 Helm 创建、打包、分发、安装、升级及回退Kubernetes应用。

创建一个名为 mychart 的 Chart

官网的例子

$ helm create mychart

该命令在当前目录创建了一个 mychart 目录,该目录结构如下所示:

$ tree mychart/
mychart/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

介绍:

  Chart.yaml          # A YAML file containing information about the chart
  LICENSE             # OPTIONAL: A plain text file containing the license for the chart
  README.md           # OPTIONAL: A human-readable README file
  values.yaml         # The default configuration values for this chart
  values.schema.json  # OPTIONAL: A JSON Schema for imposing a structure on the values.yaml file
  charts/             # A directory containing any charts upon which this chart depends.
  crds/               # Custom Resource Definitions
  templates/          # A directory of templates that, when combined with values,
                      # will generate valid Kubernetes manifest files.
  templates/NOTES.txt # OPTIONAL: A plain text file containing short usage notes

Templates 目录下 YAML 文件模板(go template语法)填充的值默认都是在 values.yaml 里定义的,比如在 deployment.yaml 中定义的容器镜像:

image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
$ cat mychart/values.yaml|grep repository
  repository: nginx

以上变量值是在 create chart 的时候就自动生成的默认值,你可以根据实际情况进行修改。

编写应用的介绍信息

打开 mychart/Chart.yaml:

apiVersion: v2
name: mychart
description: A Helm chart for Kubernetes
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: 1.16.0 
编写应用具体部署信息

编辑 mychart/values.yaml,它默认会在 Kubernetes 部署一个 Nginx。下面是 mychart 应用的 values.yaml 文件的内容:

# Default values for mychart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: nginx
  pullPolicy: IfNotPresent

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name:

podSecurityContext: {}
  # fsGroup: 2000

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  hosts:
    - host: chart-example.local
      paths: []
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #   cpu: 100m
  #   memory: 128Mi
  # requests:
  #   cpu: 100m
  #   memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}
检查依赖和模板配置是否正确
$ helm lint mychart/
==> Linting mychart/
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, 0 chart(s) failed

如果文件格式错误,可以根据提示进行修改。

将应用打包
$ helm package mychart
Successfully packaged chart and saved it to: /root/mychart-0.1.0.tgz

mychart 目录会被打包为一个 mychart-0.1.0.tgz 格式的压缩包,该压缩包会被放到当前目录下。

如果你想看到更详细的输出,可以加上 --debug 参数来查看打包的输出。

离线部署

注意: ~/.kube/config不存在的情况下要用 helm --kubeconfig 指定配置文件

# 方式一
$ helm install demo-test ./mychart

# 可根据不同的配置来install,默认是values.yaml
# helm install demo-test ./mychart -f ./mychart/values-prod.yaml

# 方式二
$ helm install demo-test ./mychart-0.1.0.tgz

$ helm list

# 升级
# $ helm upgrade demo-test ./mychart-0.2.0.tgz

$ helm uninstall demo-test
将应用发布到 Repository

harbor1.6+ 支持存储 helm charts,这里使用 helm 安装 harbor

这里为了简化测试操作,我关闭了数据卷的挂载并使用的是 NodePort 方式进行访问。

$ helm repo add goharbor https://helm.goharbor.io

$ helm repo update

# 查看harbor chart的各个版本
$ helm search repo harbor -l

# --version选择chart的版本
$ helm install harbor goharbor/harbor --set persistence.enabled=false --set expose.type=nodePort --set expose.tls.enabled=false --set externalURL=http://10.13.84.187:30002

参数说明:

  • persistence.enabled=false 关闭存储,为了方便操作,真实使用时需要挂在存储
  • expose.type=nodePort 使用 NodePort 访问
  • expose.tls.enabled=false 关闭tls
  • externalURL=http://10.13.84.187:30002 设置登录 harbor 的外部链接,ip是某个node的ip

harbor 装好之后,我们访问 http://10.13.84.187:30002 进行登录 harbor, harbor 的默认账号密码是 admin/Harbor12345

新建一个项目 chart_repo

新建一个用户test,密码Test1234,加入到项目的成员里面,并赋予权限

安装使用 helm-push 插件

helm plugin install https://github.com/chartmuseum/helm-push

超时没装成功,直接下载:

$ mkdir ~/.local/share/helm/plugins/helm-push

$ wget https://github.com/chartmuseum/helm-push/releases/download/v0.8.1/helm-push_0.8.1_linux_amd64.tar.gz

$ tar -zxvf helm-push_0.8.1_linux_amd64.tar.gz -C ~/.local/share/helm/plugins/helm-push

$ helm push --help

添加repo

# helm repo add --ca-file <ca file> --cert-file <cert file> --key-file <key file>     --username <username> --password <password> <repo name> http://10.13.84.187:30002/chartrepo/chart_repo

$ helm repo add myrepo http://10.13.84.187:30002/chartrepo/chart_repo

push

$ helm push mychart/ myrepo -u test -p Test1234
Pushing mychart-0.1.0.tgz to myrepo...
Done.

查找chart

$ helm repo update

$ helm search repo mychart

NAME          	CHART VERSION	APP VERSION	DESCRIPTION                
myrepo/mychart	0.1.0        	1.16.0      	A Helm chart for Kubernetes
部署应用

注意: ~/.kube/config不存在的情况下要用 helm --kubeconfig 指定配置文件

# helm install --ca-file <ca file> --cert-file <cert file> --key-file <key file>     --username=<username> --password=<password> --version 0.2.0 <repo name>/mychart

# 在部署前使用 --dry-run 参数验证 Chart 的配置
$ helm install demo-test  --dry-run --debug myrepo/mychart

# 没问题就install
$ helm install demo-test  myrepo/mychart

NAME: demo-test
LAST DEPLOYED: Tue Apr 21 11:06:27 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=demo-test" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80

测试:

export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=demo-test" -o jsonpath="{.items[0].metadata.name}")

echo "Visit http://127.0.0.1:8080 to use your application"

kubectl --namespace default port-forward $POD_NAME 8080:80

访问nginx

curl http://127.0.0.1:8080

使用下面的命令列出的所有已部署的 Release 以及其对应的 Chart。

$ helm list

NAME                  	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART                  	APP VERSION
demo-test             	default  	1       	2020-04-21 11:06:27.672714497 +0800 CST	deployed	mychart-0.1.0          	1.16.0  

从上面 helm list 输出的结果中我们可以看到有一个 REVISION(更改历史)字段,该字段用于表示某一个 Release 被更新的次数,我们可以用该特性对已部署的 Release 进行回滚。

查看状态

$ helm status demo-test 

NAME: demo-test
LAST DEPLOYED: Tue Apr 21 11:06:27 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=demo-test" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80
升级应用

修改 mychart/Chart.yaml 文件

apiVersion: v2
name: mychart
description: A Helm chart for Kubernetes
type: application

# 改了下面两行
version: 0.2.0
appVersion: 1.17.0

上传chart

$ helm push mychart/ myrepo -u test -p Test1234
Pushing mychart-0.2.0.tgz to myrepo...
Done.

$ helm repo udpate

$ helm search repo mychart -l

NAME          	CHART VERSION	APP VERSION	DESCRIPTION                
myrepo/mychart	0.2.0        	1.17.0      	A Helm chart for Kubernetes
myrepo/mychart	0.1.0        	1.16.0      	A Helm chart for Kubernetes

现在用 helm upgrade 命令将已部署的 demo-test 升级到新版本。你可以通过 --version 参数指定需要升级的版本号,如果没有指定版本号,则缺省使用最新版本。

$ helm upgrade demo-test myrepo/mychart

Release "demo-test" has been upgraded. Happy Helming!
NAME: demo-test
LAST DEPLOYED: Tue Apr 21 11:33:12 2020
NAMESPACE: default
STATUS: deployed
REVISION: 2
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=demo-test" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80

$ helm list

NAME                  	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART                  	APP VERSION
demo-test             	default  	2       	2020-04-21 11:33:12.376727722 +0800 CST	deployed	mychart-0.2.0          	1.17.0

完成后,可以看到已部署的 demo-test 被升级到chart的 0.2.0 版本。

回退应用

如果更新后的程序由于某些原因运行有问题,需要回退到旧版本的应用。可以使用 helm history 命令查看一个 Release 的所有变更记录

$ helm history demo-test 

REVISION	UPDATED                 	STATUS    	CHART        	APP VERSION	DESCRIPTION     
1       	Tue Apr 21 11:06:27 2020	superseded	mychart-0.1.0	1.16.0      	Install complete
2       	Tue Apr 21 11:33:12 2020	deployed  	mychart-0.2.0	1.17.0      	Upgrade complete

其次,我们可以使用下面的命令对指定的应用进行回退。

$ helm rollback demo-test 1

Rollback was a success! Happy Helming!

注:其中的参数 1 是 helm history 查看到 Release 的历史记录中 REVISION 对应的值。
查看

$ helm list

NAME     	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART        	APP VERSION
demo-test	default  	3       	2020-04-21 11:40:50.782129072 +0800 CST	deployed	mychart-0.1.0	1.16.0

$ helm history demo-test 

REVISION	UPDATED                 	STATUS    	CHART        	APP VERSION	DESCRIPTION     
1       	Tue Apr 21 11:06:27 2020	superseded	mychart-0.1.0	1.16.0      	Install complete
2       	Tue Apr 21 11:33:12 2020	superseded	mychart-0.2.0	1.17.0      	Upgrade complete
3       	Tue Apr 21 11:40:50 2020	deployed  	mychart-0.1.0	1.16.0      	Rollback to 1   
删除应用
helm uninstall demo-test

参考

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值