Helm部署k8s应用

打印 上一主题 下一主题

主题 1004|帖子 1004|积分 3012

目次

一、概述

1、什么是Helm

Helm是Kubernetes的一个包管理工具,它为Kubernetes提供了一种管理和标准化的部署方式。在Kubernetes生态系统中,Helm饰演着雷同于我们在 Ubuntu 中使用的apt、Centos中使用的 yum 大概Python中的 pip 一样,它使用户能够轻松地查找、共享和重复使用复杂的容器化应用步伐部署配置。
Helm的核心概念是图表(Chart),这是一个封装了完整Kubernetes应用的软件包。一个Helm图表通常包含一组Kubernetes资源模板,比如Deployment、Service、ConfigMap等,以及用于定制这些资源的参数文件。通过这种方式,Helm图表可以作为可重用的应用步伐模板,答应用户在不同的情况和需求下机动地部署和管理复杂的应用步伐。
2、特点


  • 简化部署 :Helm答应使用单个下令轻松部署和管理应用步伐,从而简化了整个部署过程;
  • 高度可配置:Helm Charts提供了高度可配置的选项,可以轻松自定义和修改应用步伐的部署配置;
  • 版本控制 :Helm答应管理应用步伐的多个版本,从而轻松实现版本控制和回滚;
  • 模板化:Helm Charts使用YAML模板来定义Kubernetes对象的配置,从而简化了配置过程,并提高了可重复性和可扩展性;
  • 应用步伐库:Helm具有应用步伐库的概念,可以轻松地共享和重用Helm Charts,从而简化了多个应用步伐的部署和管理;
  • 插件系统:Helm拥有一个强大的插件系统,答应您扩展和定制Helm的功能,以满意特定的需求和要求。
3、工作流程

这里使用的是Helm的v3版本,该版本没有了tiller并并使用更加简单和机动的架构,直接通过kubeconfig毗连apiserver,简化安全模块


  • 开发者首先创建并编辑chart的配置;
  • 接着打包并发布至Helm的仓库(Repository);
  • 当管理员使用helm下令安装时,相关的依赖会从仓库下载;
  • 接着helm会根据下载的配置部署资源至k8s;
4、核心概念

概念描述ChartChart是Helm的根本单位,它是一个打包好的Kubernetes应用模板聚集,包含了一组Kubernetes资源的描述文件(如Deployment、Service、ConfigMap等)。Chart还包罗一个Chart.yaml文件,用于描述Chart的元数据,如版本、描述、维护者等。Chart还包含一个values.yaml文件,用于存储默认的可配置参数。RepositoryRepository是一个存储和分发Helm Charts的地方。Charts可以被上传到仓库,供用户搜索、下载和安装。Helm支持多种类型的仓库,包罗当地文件系统、HTTP服务器或专用的Helm仓库服务。仓库通常包含一个索引文件,列出全部可用的Charts及其版本。ReleaseRelease是指使用Helm安装Chart到Kubernetes集群上的一次实例化操作。每次使用helm install下令安装Chart时,都会创建一个新的Release。Release有本身的名称,且在同一个命名空间内必须唯一。通过Release,用户可以追踪和管理部署的应用实例。ValuesValues是Helm用来参数化Chart的方式。Chart中可能包含多个values.yaml文件,它们用于存储可以由用户修改的变量。在安装Chart时,用户可以通过传递额外的值或覆盖默认值来定制部署。Values的合并遵照一定的规则,以确定终极的配置。TemplateTemplates是Chart中用于天生Kubernetes资源定义的文件。它们使用Go模板语言编写,可以包含变量、条件和循环等结构。在安装Chart时,Helm会渲染这些模板,并根据提供的值天生具体的Kubernetes资源定义。TillerTiller是Helm的服务器端组件,负责接收来自Helm客户端的请求,并在Kubernetes集群上执行相应的操作。在早期版本的Helm中,Tiller作为一个守护进程运行在Kubernetes集群中。但在Helm 3中,Tiller已被移除,Helm 3采用了无守护进程的计划,全部操作都在客户端执行,与Kubernetes API Server直接通讯。rollback回滚,每一次发布会更新chart大概配置。当天生发布汗青后,一次发布也可以被 rolled back 之前的发布版本号。 回滚使用 helm rollback 下令实现。二、安装Helm

本篇只介绍二进制安装,其他的安装教程:
Helm | 安装Helm
1、二进制版本安装

1.1、下载必要的版本

Helm-GitHub
1.2、解压
  1. tar -zxvf helm-v3.15.2-linux-amd64.tar
复制代码
1.3、将helm移动到指定路径
  1. mv linux-amd64/helm /usr/local/bin/helm
复制代码
1.4、验证
  1. helm version
复制代码

三、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
四、--set 的格式和限定

--set 选项使用0或多个 name/value 对
1、最简单的name/value对
  1. --set name=value
  2. 等价于
  3. name: value
复制代码
2、多个name/value对
  1. --set a=b,c=d
  2. 等价于
  3. a: b
  4. c: d
复制代码
3、更复杂的表达式
  1. --set outer.inner=value
  2. 等价于
  3. outer:
  4.   inner: value
复制代码
4、列表的表达
  1. --set name={a, b, c}
  2. 等价于
  3. name:
  4.   - a
  5.   - b
  6.   - c
复制代码
5、name/key可以设置为null大概空数组
  1. --set name=[],a=null
  2. name:
  3.   - a
  4.   - b
  5.   - c
  6. a: b
  7. 变为了
  8. name: []
  9. a: null
复制代码
6、使用数组下标的语法来访问列表中的元素

从 2.5.0 版本开始支持
  1. --set servers[0].port=80
  2. 等价于
  3. servers:
  4.   - port: 80
复制代码
7、多个值得数组下标语法
  1. --set servers[0].port=80,servers[0].host=example
  2. 等价于
  3. servers:
  4.   - port: 80
  5.     host: example
复制代码
8、特殊字符转义
  1. --set name=value1\,value2
  2. 等价于
  3. name: "value1,value2"
复制代码
9、转义.
  1. --set nodeSelector."kubernetes\.io/role"=master
  2. 等价于
  3. nodeSelector:
  4.   kubernetes.io/role: master
复制代码
五、values文件

优先级为values.yaml最低,--set参数最高。
对应的官网链接
六、实例

1、创建Chart

1.1、在当地创建一个wordpress的Chart
  1. helm create wordpress
复制代码

1.2、查看目次结构
  1. cd wordpress
  2. tree
复制代码

2、编辑Chart配置

2.1、Chart.yaml

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


  • 各字段解释
  1. apiVersion: chart API 版本 (必需)  #必须有
  2. name: chart名称 (必需)     # 必须有
  3. version: 语义化2 版本(必需) # 必须有
  4. kubeVersion: 兼容Kubernetes版本的语义化版本(可选)
  5. description: 一句话对这个项目的描述(可选)
  6. type: chart类型 (可选)
  7. keywords:
  8.   - 关于项目的一组关键字(可选)
  9. home: 项目home页面的URL (可选)
  10. sources:
  11.   - 项目源码的URL列表(可选)
  12. dependencies: # chart 必要条件列表 (可选)
  13.   - name: chart名称 (nginx)
  14.     version: chart版本 ("1.2.3")
  15.     repository: (可选)仓库URL ("https://example.com/charts") 或别名 ("@repo-name")
  16.     condition: (可选) 解析为布尔值的yaml路径,用于启用/禁用chart (e.g. subchart1.enabled )
  17.     tags: # (可选)
  18.       - 用于一次启用/禁用 一组chart的tag
  19.     import-values: # (可选)
  20.       - ImportValue 保存源值到导入父键的映射。每项可以是字符串或者一对子/父列表项
  21.     alias: (可选) chart中使用的别名。当你要多次添加相同的chart时会很有用
  22. maintainers: # (可选) # 可能用到
  23.   - name: 维护者名字 (每个维护者都需要)
  24.     email: 维护者邮箱 (每个维护者可选)
  25.     url: 维护者URL (每个维护者可选)
  26. icon: 用做icon的SVG或PNG图片URL (可选)
  27. appVersion: 包含的应用版本(可选)。不需要是语义化,建议使用引号
  28. deprecated: 不被推荐的chart (可选,布尔值)
  29. annotations:
  30.   example: 按名称输入的批注列表 (可选)
复制代码
v3.3.2,不再答应额外的字段。保举的方法是在 annotations 中添加自定义元数据。

  • 编写我们本身的Chart.yaml
  1. name: nginx-helm
  2. apiVersion: v1
  3. version: 1.0.0
复制代码
2.2、values.yaml

values文件被定义为YAML格式。chart会包含一个默认的values.yaml文件。 Helm安装下令答应用户使用附加的YAML values覆盖这个values:
  1. helm install --generate-name --values=myvals.yaml wordpress
复制代码

  • 各字段解释
  1. # Default values for wordpress.
  2. # This is a YAML-formatted file.
  3. # Declare variables to be passed into your templates.
  4. replicaCount: 1        #控制部署的Pod副本数量,默认为1。这适用于StatefulSet或Deployment类型的Kubernetes资源。
  5. image:        #用于配置容器镜像的相关信息
  6.   repository: nginx        #镜像仓库地址。
  7.   pullPolicy: IfNotPresent        #拉取镜像的策略,可以是Always, IfNotPresent, 或 Never。
  8.   # Overrides the image tag whose default is the chart appVersion.
  9.   tag: ""        #镜像的标签,如果没有指定,则默认使用Chart的版本。
  10. imagePullSecrets: []        #列出拉取私有镜像所需的密钥名称。
  11. nameOverride: ""
  12. fullnameOverride: ""        #用于覆盖生成的资源名称的部分或全部。
  13. serviceAccount:        #配置服务账户的选项。
  14.   # Specifies whether a service account should be created
  15.   create: true        #是否自动创建服务账户。
  16.   # Automatically mount a ServiceAccount's API credentials?
  17.   automount: true        #是否自动挂载服务账户的API凭证。
  18.   # Annotations to add to the service account
  19.   annotations: {}        #附加到服务账户的注解。
  20.   # The name of the service account to use.
  21.   # If not set and create is true, a name is generated using the fullname template
  22.   name: ""        #使用的服务账户名称。
  23. podAnnotations: {}        #附加到Pod的注解。
  24. podLabels: {}        #附加到Pod的标签。
  25. podSecurityContext: {}       
  26.   # fsGroup: 2000
  27. securityContext: {}        #定义Pod和容器的安全上下文。
  28.   # capabilities:
  29.   #   drop:
  30.   #   - ALL
  31.   # readOnlyRootFilesystem: true
  32.   # runAsNonRoot: true
  33.   # runAsUser: 1000
  34. service:        #配置Service资源
  35.   type: ClusterIP        #Service的类型,例如ClusterIP, NodePort, LoadBalancer等。
  36.   port: 80        #Service监听的端口。
  37. ingress:        #Ingress资源的配置。
  38.   enabled: false        #是否启用Ingress。
  39.   className: ""        #Ingress控制器的类名。
  40.   annotations: {}        #附加到Ingress的注解。
  41.     # kubernetes.io/ingress.class: nginx
  42.     # kubernetes.io/tls-acme: "true"
  43.   hosts:        #定义Ingress的主机名和路径。
  44.     - host: chart-example.local
  45.       paths:
  46.         - path: /
  47.           pathType: ImplementationSpecific
  48.   tls: []        # TLS配置。
  49.   #  - secretName: chart-example-tls
  50.   #    hosts:
  51.   #      - chart-example.local
  52. resources: {}        #Pod的资源限制和请求。
  53.   # We usually recommend not to specify default resources and to leave this as a conscious
  54.   # choice for the user. This also increases chances charts run on environments with little
  55.   # resources, such as Minikube. If you do want to specify resources, uncomment the following
  56.   # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  57.   # limits:
  58.   #   cpu: 100m
  59.   #   memory: 128Mi
  60.   # requests:
  61.   #   cpu: 100m
  62.   #   memory: 128Mi
  63. livenessProbe:       
  64.   httpGet:
  65.     path: /
  66.     port: http
  67. readinessProbe:        #定义容器的存活探针和就绪探针,用于健康检查。
  68.   httpGet:
  69.     path: /
  70.     port: http
  71. autoscaling:         #水平自动扩缩容的配置。
  72.   enabled: false
  73.   minReplicas: 1
  74.   maxReplicas: 100
  75.   targetCPUUtilizationPercentage: 80
  76.   # targetMemoryUtilizationPercentage: 80
  77. # Additional volumes on the output Deployment definition.
  78. volumes: []
  79. # - name: foo
  80. #   secret:
  81. #     secretName: mysecret
  82. #     optional: false
  83. # Additional volumeMounts on the output Deployment definition.
  84. volumeMounts: []        #Pod的卷和卷挂载配置。
  85. # - name: foo
  86. #   mountPath: "/etc/foo"
  87. #   readOnly: true
  88. nodeSelector: {}        #选择运行Pod的节点的标签。
  89.        
  90. tolerations: []        #允许Pod容忍节点的污点。
  91. affinity: {}        #节点亲和性和反亲和性配置,用于控制Pod在哪些节点上运行。
复制代码

  • 编写我们本身的values.yaml
  1. image:
  2.   repository: nginx
  3.   tag: latest
复制代码
2.3、templates

在模板中引入values.yaml里的配置,在模板文件中可以通过 .VAlues对象访问到
  1. deployment.yaml
复制代码
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4.   name: nginx-helm-{{ .Values.image.repository }}
  5. spec:
  6.   replicas: 1
  7.   selector:
  8.     matchLabels:
  9.       app: nginx-helm
  10.   template:
  11.     metadata:
  12.       labels:
  13.         app: nginx-helm
  14.     spec:
  15.       containers:
  16.       - name: nginx-helm
  17.         image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
  18.         ports:
  19.         - containerPort: 80
  20.           protocol: TCP
复制代码
3、打包Chart

使用helm package下令将Chart打包为一个tarball文件
  1. helm package wordpress/
复制代码

这将天生一个名为nginx-helm-1.0.0.tgz的tarball文件。
4、发布Chart

将打包好的Chart发布到一个Helm Repository中。可以使用helm repo add下令添加一个Repository,然后使用helm push下令将Chart推送到Repository中
  1. helm repo add myrepo https://example.com/charts
  2. helm push nginx-helm-1.0.0.tgz myrepo
复制代码
5、安装Release

使用helm install下令安装Chart的Release,可以通过下令行选项或指定values.yaml文件来配置Release
  1. helm install my-nginx ./nginx-helm-1.0.0.tgz
复制代码

6、查看当前运行的Release列表

helm list 与 helm ls 下令执行的效果是一致的
  1. helm list
复制代码
  1. helm ls
复制代码

7、升级版本

7.1、格式
  1. helm upgrade [RELEASE] [CHART] [flags]
复制代码
7.2、指定'--values'/'-f'参数

可以多次指定'--values'/'-f'参数,最后(最右边)指定的文件优先级最高。比如如果myvalues.yaml和override.yaml同时包含了名为 'Test'的key,override.yaml中的设置会优先使用:
  1. helm upgrade -f myvalues.yaml -f override.yaml redis ./redis
复制代码
7.3、指定'--set'参数

可以多次指定'--set'参数,最后(最右边)指定的优先级最高。比如'bar' 和 'newbar'都设置了一个名为'foo'的可以, 'newbar'的值会优先使用:
  1. helm upgrade --set foo=bar --set foo=newbar redis ./redis
复制代码
7.4、修改nginx镜像版本为1.27.0
  1. helm upgrade my-nginx nginx-helm-1.0.0.tgz --set image.tag=1.27.0
复制代码
  1. kubectl get pods
  2. kubectl get pods nginx-helm-nginx-5b97c9f87d-ggbgw -o yaml | grep image
复制代码

8、回滚

8.1、查看发布汗青
  1. helm history my-nginx
复制代码

8.2、回滚到第一个版本
  1. helm rollback my-nginx 1
复制代码
  1. kubectl get pods
  2. kubectl get pods nginx-helm-nginx-868dbb969-p9k5t -o yaml | grep image
复制代码

9、删除Chart
  1. helm uninstall my-nginx
复制代码

七、查询Helm下令的帮助信息
  1. helm --help
复制代码
  1.                         
  2. The Kubernetes package manager
  3. Common actions for Helm:
  4. - helm search:    search for charts
  5. - helm pull:      download a chart to your local directory to view
  6. - helm install:   upload the chart to Kubernetes
  7. - helm list:      list releases of charts
  8. Environment variables:
  9. | Name                               | Description                                                                                                |
  10. |------------------------------------|------------------------------------------------------------------------------------------------------------|
  11. | $HELM_CACHE_HOME                   | set an alternative location for storing cached files.                                                      |
  12. | $HELM_CONFIG_HOME                  | set an alternative location for storing Helm configuration.                                                |
  13. | $HELM_DATA_HOME                    | set an alternative location for storing Helm data.                                                         |
  14. | $HELM_DEBUG                        | indicate whether or not Helm is running in Debug mode                                                      |
  15. | $HELM_DRIVER                       | set the backend storage driver. Values are: configmap, secret, memory, sql.                                |
  16. | $HELM_DRIVER_SQL_CONNECTION_STRING | set the connection string the SQL storage driver should use.                                               |
  17. | $HELM_MAX_HISTORY                  | set the maximum number of helm release history.                                                            |
  18. | $HELM_NAMESPACE                    | set the namespace used for the helm operations.                                                            |
  19. | $HELM_NO_PLUGINS                   | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.                                                 |
  20. | $HELM_PLUGINS                      | set the path to the plugins directory                                                                      |
  21. | $HELM_REGISTRY_CONFIG              | set the path to the registry config file.                                                                  |
  22. | $HELM_REPOSITORY_CACHE             | set the path to the repository cache directory                                                             |
  23. | $HELM_REPOSITORY_CONFIG            | set the path to the repositories file.                                                                     |
  24. | $KUBECONFIG                        | set an alternative Kubernetes configuration file (default "~/.kube/config")                                |
  25. | $HELM_KUBEAPISERVER                | set the Kubernetes API Server Endpoint for authentication                                                  |
  26. | $HELM_KUBECAFILE                   | set the Kubernetes certificate authority file.                                                             |
  27. | $HELM_KUBEASGROUPS                 | set the Groups to use for impersonation using a comma-separated list.                                      |
  28. | $HELM_KUBEASUSER                   | set the Username to impersonate for the operation.                                                         |
  29. | $HELM_KUBECONTEXT                  | set the name of the kubeconfig context.                                                                    |
  30. | $HELM_KUBETOKEN                    | set the Bearer KubeToken used for authentication.                                                          |
  31. | $HELM_KUBEINSECURE_SKIP_TLS_VERIFY | indicate if the Kubernetes API server's certificate validation should be skipped (insecure)                |
  32. | $HELM_KUBETLS_SERVER_NAME          | set the server name used to validate the Kubernetes API server certificate                                 |
  33. | $HELM_BURST_LIMIT                  | set the default burst limit in the case the server contains many CRDs (default 100, -1 to disable)         |
  34. | $HELM_QPS                          | set the Queries Per Second in cases where a high number of calls exceed the option for higher burst values |
  35. Helm stores cache, configuration, and data based on the following configuration order:
  36. - If a HELM_*_HOME environment variable is set, it will be used
  37. - Otherwise, on systems supporting the XDG base directory specification, the XDG variables will be used
  38. - When no other location is set a default location will be used based on the operating system
  39. By default, the default directories depend on the Operating System. The defaults are listed below:
  40. | Operating System | Cache Path                | Configuration Path             | Data Path               |
  41. |------------------|---------------------------|--------------------------------|-------------------------|
  42. | Linux            | $HOME/.cache/helm         \| $HOME/.config/helm             | $HOME/.local/share/helm |
  43. | macOS            | $HOME/Library/Caches/helm \| $HOME/Library/Preferences/helm | $HOME/Library/helm      |
  44. | Windows          | %TEMP%\helm               | %APPDATA%\helm                 | %APPDATA%\helm          |
  45. Usage:
  46.   helm [command]
  47. Available Commands:
  48.   completion  generate autocompletion scripts for the specified shell
  49.   create      create a new chart with the given name
  50.   dependency  manage a chart's dependencies
  51.   env         helm client environment information
  52.   get         download extended information of a named release
  53.   help        Help about any command
  54.   history     fetch release history
  55.   install     install a chart
  56.   lint        examine a chart for possible issues
  57.   list        list releases
  58.   package     package a chart directory into a chart archive
  59.   plugin      install, list, or uninstall Helm plugins
  60.   pull        download a chart from a repository and (optionally) unpack it in local directory
  61.   push        push a chart to remote
  62.   registry    login to or logout from a registry
  63.   repo        add, list, remove, update, and index chart repositories
  64.   rollback    roll back a release to a previous revision
  65.   search      search for a keyword in charts
  66.   show        show information of a chart
  67.   status      display the status of the named release
  68.   template    locally render templates
  69.   test        run tests for a release
  70.   uninstall   uninstall a release
  71.   upgrade     upgrade a release
  72.   verify      verify that a chart at the given path has been signed and is valid
  73.   version     print the client version information
  74. Flags:
  75.       --burst-limit int                 client-side default throttling limit (default 100)
  76.       --debug                           enable verbose output
  77.   -h, --help                            help for helm
  78.       --kube-apiserver string           the address and the port for the Kubernetes API server
  79.       --kube-as-group stringArray       group to impersonate for the operation, this flag can be repeated to specify multiple groups.
  80.       --kube-as-user string             username to impersonate for the operation
  81.       --kube-ca-file string             the certificate authority file for the Kubernetes API server connection
  82.       --kube-context string             name of the kubeconfig context to use
  83.       --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
  84.       --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
  85.       --kube-token string               bearer token used for authentication
  86.       --kubeconfig string               path to the kubeconfig file
  87.   -n, --namespace string                namespace scope for this request
  88.       --qps float32                     queries per second used when communicating with the Kubernetes API, not including bursting
  89.       --registry-config string          path to the registry config file (default "/root/.config/helm/registry/config.json")
  90.       --repository-cache string         path to the file containing cached repository indexes (default "/root/.cache/helm/repository")
  91.       --repository-config string        path to the file containing repository names and URLs (default "/root/.config/helm/repositories.yaml")
  92. Use "helm [command] --help" for more information about a command.
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天空闲话

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表