0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Helm的一些概念及用法

马哥Linux运维 来源:马哥Linux运维 2023-05-30 09:51 次阅读

一、前言

在k8s中,我们很多时候需要部署很多个应用,特别是微服务的项目,如果每个服务部署都需要使用kubectl apply依次执行,这将是一件很痛苦的事。

这个时候,如果一键部署所有应用,使用Helm(https://helm.sh)是一个很不错的选择,它具备如下的能力:

简化部署:Helm允许使用单个命令轻松部署和管理应用程序,从而简化了整个部署过程;

高度可配置:Helm Charts提供了高度可配置的选项,可以轻松自定义和修改应用程序的部署配置;

版本控制:Helm允许管理应用程序的多个版本,从而轻松实现版本控制和回滚;

模板化:Helm Charts使用YAML模板来定义Kubernetes对象的配置,从而简化了配置过程,并提高了可重复性和可扩展性;

应用程序库:Helm具有应用程序库的概念,可以轻松地共享和重用Helm Charts,从而简化了多个应用程序的部署和管理;

插件系统:Helm拥有一个强大的插件系统,允许您扩展和定制Helm的功能,以满足特定的需求和要求。

Helm本质就是一个k8s的包管理器。

二、Helm工作流程

以下是Helm的工作流程(注意:这里使用的是Helm的v3版本,该版本没有了tiller而是用更加简单和灵活的架构,直接通过kubeconfig连接apiserver,简化安全模块,降低了用户的使用壁垒):

21a4de64-fe68-11ed-90ce-dac502259ad0.png

如上图所示,Helm的工作流程总结如下:

开发者首先创建并编辑chart的配置;

接着打包并发布至Helm的仓库(Repository);

当管理员使用helm命令安装时,相关的依赖会从仓库下载

接着helm会根据下载的配置部署资源至k8s。

三、Helm概念

在使用Helm的过程中,需要理解如下的几个核心的概念:

概念 描述
Chart 一个Helm包,其中包含了运行一个应用所需要的镜像、依赖和资源定义等,还可能包含Kubernetes集群中的服务定义,类似Homebrew中的formula、APT的dpkg或者Yum的rpm文件
Repository 存储Helm Charts的地方
Release Chart在k8s上运行的Chart的一个实例,例如,如果一个MySQL Chart想在服务器上运行两个数据库,可以将这个Chart安装两次,并在每次安装中生成自己的Release以及Release名称
Value Helm Chart的参数,用于配置Kubernetes对象
Template 使用Go模板语言生成Kubernetes对象的定义文件
Namespace Kubernetes中用于隔离资源的逻辑分区

四、Helm的使用

下面简单讲下Helm的使用。

4.1 安装Helm

首先需要在本地机器或Kubernetes集群上安装Helm。可以从Helm官方网站下载适合自己平台的二进制文件,或使用包管理器安装Helm,安装教程参考https://helm.sh。

4.2 创建Chart

使用helm create命令创建一个新的Chart,Chart目录包含描述应用程序的文件和目录,包括Chart.yaml、values.yaml、templates目录等;

例如:在本地机器上使用helm create命令创建一个名为wordpress的Chart:21cc67ae-fe68-11ed-90ce-dac502259ad0.png

在当前文件夹,可以看到创建了一个wordpress的目录,且里面的内容如下:21d89dee-fe68-11ed-90ce-dac502259ad0.png

4.3配置Chart

使用编辑器编辑Chart配置文件,包括Chart.yaml和values.yaml。

Chart.yaml:

Chart.yaml包含Chart的元数据和依赖项

Chart.yaml的模板及注释如下:

apiVersion: chart API 版本 (必需)  #必须有
name: chart名称 (必需)     # 必须有 
version: 语义化2 版本(必需) # 必须有


kubeVersion: 兼容Kubernetes版本的语义化版本(可选)
description: 一句话对这个项目的描述(可选)
type: chart类型 (可选)
keywords:
  - 关于项目的一组关键字(可选)
home: 项目home页面的URL (可选)
sources:
  - 项目源码的URL列表(可选)
dependencies: # chart 必要条件列表 (可选)
  - name: chart名称 (nginx)
    version: chart版本 ("1.2.3")
    repository: (可选)仓库URL ("https://example.com/charts") 或别名 ("@repo-name")
    condition: (可选) 解析为布尔值的yaml路径,用于启用/禁用chart (e.g. subchart1.enabled )
    tags: # (可选)
      - 用于一次启用/禁用 一组chart的tag
    import-values: # (可选)
      - ImportValue 保存源值到导入父键的映射。每项可以是字符串或者一对子/父列表项
    alias: (可选) chart中使用的别名。当你要多次添加相同的chart时会很有用


maintainers: # (可选) # 可能用到
  - name: 维护者名字 (每个维护者都需要)
    email: 维护者邮箱 (每个维护者可选)
    url: 维护者URL (每个维护者可选)


icon: 用做icon的SVG或PNG图片URL (可选)
appVersion: 包含的应用版本(可选)。不需要是语义化,建议使用引号
deprecated: 不被推荐的chart (可选,布尔值)
annotations:
  example: 按名称输入的批注列表 (可选).

举例:

name: nginx-helm
apiVersion: v1
version: 1.0.0

Values.yaml:

values.yaml包含应用程序的默认配置值,举例:

image:
  repository: nginx
  tag: '1.19.8'

templates:

在模板中引入values.yaml里的配置,在模板文件中可以通过.VAlues对象访问到,例如:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-helm-{{ .Values.image.repository }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-helm
  template:
    metadata:
      labels:
        app: nginx-helm
    spec:
      containers:
      - name: nginx-helm
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        ports:
        - containerPort: 80
          protocol: TCP

4.4打包Chart

使用helm package命令将Chart打包为一个tarball文件,例如在wordpress目录中使用helm package命令将Chart打包为一个tarball文件,这将生成一个名为wordpress-0.1.0.tgz的tarball文件:

helm package wordpress/

4.5发布Chart

将打包好的Chart发布到一个Helm Repository中。可以使用helm repo add命令添加一个Repository,然后使用helm push命令将Chart推送到Repository中,例如:

helm repo add myrepo https://example.com/charts
helm push wordpress-0.1.0.tgz myrepo

4.6安装Release

使用helm install命令安装Chart的Release,可以通过命令行选项或指定values.yaml文件来配置Release,例如:

helm install mywordpress myrepo/wordpress

这将在Kubernetes集群中创建一个名为mywordpress的Release,包含WordPress应用程序和MySQL数据库。

4.7管理Release

使用helm ls命令查看当前运行的Release列表,例如:

helm upgrade mywordpress myrepo/wordpress --set image.tag=5.7.3-php8.0-fpm-alpine

这将升级mywordpress的WordPress应用程序镜像版本为5.7.3-php8.0-fpm-alpine。

可以使用helm rollback命令回滚到先前版本,例如:

helm rollback mywordpress 1

这将回滚mywordpress的版本到1。

4.8更新Chart

在应用程序更新时,可以更新Chart配置文件和模板,并使用helm package命令重新打包Chart。然后可以使用helm upgrade命令升级已安装的Release,可以按照以下步骤更新Chart:

在本地编辑Chart配置或添加新的依赖项;

使用helm package命令打包新的Chart版本;

使用helm push命令将新的Chart版本推送到Repository中;

使用helm repo update命令更新本地或远程的Helm Repository;

使用helm upgrade命令升级现有Release到新的Chart版本。

例如,可以使用以下命令更新WordPress的Chart版本:

helm upgrade mywordpress myrepo/wordpress --version 0.2.0

这将升级mywordpress的Chart版本到0.2.0,其中包括新的配置和依赖项。

如果需要删除一个Release,可以使用helm uninstall命令。例如:

helm uninstall mywordpress

这将删除名为mywordpress的Release,同时删除WordPress应用程序和MySQL数据库。

如果需要删除与Release相关的PersistentVolumeClaim,可以使用helm uninstall命令的--delete-data选项,例如:

helmuninstallmywordpress--delete-data

这将删除名为mywordpress的Release,并删除与之相关的所有PersistentVolumeClaim。

五、Helm的执行安装顺序

Helm按照以下顺序安装资源:

Namespace

NetworkPolicy

ResourceQuota

LimitRange

PodSecurityPolicy

PodDisruptionBudget

ServiceAccount

Secret

SecretList

ConfigMap

StorageClass

PersistentVolume

PersistentVolumeClaim

CustomResourceDefinition

ClusterRole

ClusterRoleList

ClusterRoleBinding

ClusterRoleBindingList

Role

RoleList

RoleBinding

RoleBindingList

Service

DaemonSet

Pod

ReplicationController

ReplicaSet

Deployment

HorizontalPodAutoscaler

StatefulSet

Job

CronJob

Ingress

APIService

Helm 客户端不会等到所有资源都运行才退出,可以使用 helm status 来追踪 release 的状态,或是重新读取配置信息

#$helmstatusmynginx
NAME: mynginx
LAST DEPLOYED: Fri Oct 29 1432 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

六、Helm命令汇总

在最后,附上所有helm的命令,直接控制台使用helm --help即可查看:

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_KUBEINSECURE_SKIP_TLS_VERIFY | indicate if the Kubernetes API server's certificate validation should be skipped (insecure)       |
| $HELM_KUBETLS_SERVER_NAME          | set the server name used to validate the Kubernetes API server certificate                        |
| $HELM_BURST_LIMIT                  | set the default burst limit in the case the server contains many CRDs (default 100, -1 to disable)|


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
  push        push a chart to remote
  registry    login to or logout from a registry
  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:
      --burst-limit int                 client-side default throttling limit (default 100)
      --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-insecure-skip-tls-verify   if true, the Kubernetes API server's certificate will not be checked for validity. This will make your HTTPS connections insecure
      --kube-tls-server-name string     server name to use for Kubernetes API server certificate validation. If it is not provided, the hostname used to contact the server is used
      --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 "/Users/yanglinwei/Library/Preferences/helm/registry/config.json")
      --repository-cache string         path to the file containing cached repository indexes (default "/Users/yanglinwei/Library/Caches/helm/repository")
      --repository-config string        path to the file containing repository names and URLs (default "/Users/yanglinwei/Library/Preferences/helm/repositories.yaml")


Use "helm [command] --help" for more information about a command.

七、文末

本文主要讲解了Helm的一些概念及用法,希望能帮助到大家,谢谢大家的阅读,本文完!

审核编辑:彭静
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 应用程序
    +关注

    关注

    37

    文章

    3136

    浏览量

    56405
  • 配置
    +关注

    关注

    1

    文章

    184

    浏览量

    18171

原文标题:Helm入门(一篇就够了)

文章出处:【微信号:magedu-Linux,微信公众号:马哥Linux运维】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    Allegro正负片的概念及相关设置说明

     Allegro正负片的概念及相关设置说明
    发表于 05-12 21:22

    中断的概念及51单片机的中断系统

    中断的概念及51单片机的中断系统13-1. 演示范例——声控小车13-2. 中断的概念13-3. P89V51RD2单片中断系统的构成 
    发表于 03-29 10:27

    使用Helm 在容器服务k8s集群键部署wordpress

    的kubernets集群默认集成了helm并初始化提供了一些常用charts,下面我们就以安装wordpress示例来演示使用流程。以上为容器服务默认提供的一些安装charts,下面我们来安装wordpress
    发表于 03-29 13:38

    FPGA与CPLD的概念及其区别PDF

    FPGA与CPLD的概念及其区别
    发表于 08-15 15:46

    CAD命令:CAD软件中from命令的相关概念及应用

    ,最后输入半径值,就把圆画好了。如下图所示:以上就是在浩辰CAD软件中from命令(基点捕捉命令)的相关概念及应用,感兴趣的小伙伴可以参考本CAD教程来了解下CAD命令——from命令的一些使用技巧。
    发表于 05-25 17:27

    阻抗控制相关的基本概念

    阻抗控制部分包括两部分内容:基本概念及阻抗匹配。本篇主要介绍阻抗控制相关的一些基本概念
    发表于 02-25 08:11

    串口通讯的概念及接口电路解析,不看肯定后悔

    串口通讯的概念及接口电路解析,不看肯定后悔
    发表于 05-27 06:01

    有关一些电路常识性概念须知

    有关一些电路常识性概念须知
    发表于 06-08 06:50

    介绍蓝牙的一些概念

    . 声明本专栏文章我们会以连载的方式持续更新,本专栏计划更新内容如下:第篇:蓝牙综合介绍 ,主要介绍蓝牙的一些概念,产生背景,发展轨迹,市面蓝牙介绍,以及蓝牙开发板介绍。第二篇
    发表于 07-22 06:03

    USB基本概念及从机编程方法介绍

    慕课苏州大学.嵌入式开发及应用.第四章.较复杂通信模块.USB基本概念及从机编程方法0 目录4 较复杂通信模块4.4 USB基本概念及从机编程方法4.4.1 课堂重点4.4.2 测试与作业5 下
    发表于 11-08 09:14

    嵌入式系统的概念及特点

    文章目录嵌入式系统概要嵌入式系统的概念及特点嵌入式系统硬件嵌入式系统软件嵌入式系统的编程模式微控制器的程序开发方式嵌入式系统概要嵌入式系统的概念及特点1. 概念国外的定义:用于控制、监视或者辅助操作
    发表于 12-22 06:36

    嵌入式系统的概念及特点

    嵌入式系统概要嵌入式系统概要1.嵌入式系统的概念及特点2.嵌入式硬件3.嵌入式系统软件4.嵌入式系统编程模式5.微控制器的程序开发方式嵌入式系统概要1.嵌入式系统的概念及特点2.嵌入式硬件3.嵌入式系统软件4.嵌入式系统编程模式5.微控制器的程序开发方式...
    发表于 12-22 07:21

    地和接地的概念及区别

    地和接地的概念及区别 1.地 (1)电气地 大地是一个电阻非常低、电容量非常大的物体,拥有吸收无限电荷的
    发表于 12-31 11:09 3062次阅读

    INCA的一些用法

    INCA的一些用法
    的头像 发表于 11-10 15:32 6772次阅读

    如何开发一个helm chart

    使用helmfile时,我们首先得了解helm的使用,以及如何开发一个helm chart。
    的头像 发表于 05-16 09:28 440次阅读