k8s学习笔记-Helm介绍1

网友投稿 231 2022-09-09

k8s学习笔记-Helm介绍1

helm类似linux包管理yum。

helm可以帮助管理k8s的应用--Helm Charts帮你定义,安装和升级应用。

Chart很容易创建、版本、共享和发布——所以完全可以不用以前的复制,粘贴等。

主要作用以下四个方面

管理复杂: chart甚至描述了最复杂的应用程序,提供了可重复的应用程序安装,并作为单一的权威中心点。简单更新: 使用就地升级和自定义挂钩来消除更新带来的痛苦。简单共享: chart很容易在公共或私有服务器上进行版本设置、共享和托管。回退: 可以使用helm rollback轻松回退到以前版本。

Helm的架构

Helm的作用

Helm是一个用于管理kubernetes包(称为图标)的工具。Helm可以做到以下几点

创建新的charts打包charts到charts的归档文件(tgz)和chart的仓库进行交互,仓库就是存储charts的地方从存在的k8s集群中安装卸载charts管理通过helm安装的charts生命周期

针对helm有三个概念

一个Chart就是一个Helm的包,它包含在Kubernetres集群中运行应用,工具或者服务的所有的包。可以把它和apt dpkg,yum rpm文件看作是一样的。而一个仓库就是存储charts和共享他们的release代表着kubernetes集群中运行的一个chart的实例

Helm架构和组件

Helm 2.x有两大组件

Helm Client

针对客户端的命令行工具,主要负责如下工作:

本地chart的开发仓库的管理和Tiller server的交互

发送Charts去安装

关于Charts的版本信息获取

版本升级或者卸载已安装的chart

Tiller Server

Tiller Server是一个集群内的服务器,它与Helm客户端交互,并与Kubernetes Api服务器接口进行通信,Tiller Server负责以下工作:

监听从Helm client发送的请求合并chart和配置文件,去构建一个release(可以说是某个版本的软件)安装charts到Kubernetes,然后跟踪后续的版本通过和Kubernets交互来升级或者卸载charts

Helm 2.xx版本架构

架构如下:

Helm 3.xx组件

Helm 3.1分为两个部分来执行

Helm client针对终端用户是一个命令行工具,主要负责如下:

本地chart的开发仓库的管理和 Helm库进行交互

发送charts去安装请求升级或卸载已存在的版本

Helm库提供了所有Helm的操作逻辑。它的接口和Kubernetes API进行交互,主要提供了以下能力:

合并chart和配置文件,去构建一个release(可以说是某个版本的软件)安装charts到Kubernetes,然后跟踪后续的版本通过和Kubernetes交互来升级或卸载charts.

独立的Helm库封装了Helm逻辑,因此不同的客户端可以使用它

Helm 3.1架构

Helm 2.x部署

helm client的安装

下载helm二进制文件$ wget tar xf /usr/local/helm-v2.15.2-linux-amd64.tar.gz $ cp linux-amd64/helm /usr/local/bin/

tiller server安装

由于Tiller负责和kubernetes进行打交道,用来创建release和跟踪它们的状态,需要创建一个帐户。

1 创建服务账户配置文件并应用

$ cat helm/tiller-rbac.ymlapiVersion: v1kind: ServiceAccountmetadata: name: tiller namespace: kube-system---apiVersion: rbac.authorization.k8s.io/v1beta1kind: ClusterRoleBindingmetadata: name: tillerroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-adminsubjects: - kind: ServiceAccount name: tiller namespace: kube-system$ kubectl create -f helm/tiller-rbac.yml --record --save-config

2 Helm的初始化

$ helm init --service-account tiller --history-max 200 --stable-repo-url kubectl get pods -n kube-system$ kubectl describe pod tiller-deploy-888888 -n kube-system

[warning] 由于官方仓库在国外,所以国内用户使用会非常慢。建议使用阿里或者微软的会好很多

官方chart仓库:​​server部署到指定节点上,使用-node-selectors选项,如helm init --node-selectors "beta.kubernetes.io/os"="linux"查看安装的TIler server是否准备就绪

$ kubectl -n kube-system rollout status deploy tiller-deploy // 有可能镜像无法下载,墙的原因,建议设置代理deployment "tiller-deploy" successfully rolled out$ kubectl -n kube-system get pods

Helm基本命令

必须掌握

1- helm search

[root@master helm]# helm search mysqlNAME CHART VERSION APP VERSION DESCRIPTION stable/mysql 1.6.9 5.7.30 DEPRECATED - Fast, reliable, scalable, and easy to use op...stable/mysqldump 2.6.2 2.4.1 DEPRECATED! - A Helm chart to help backup MySQL databases...stable/prometheus-mysql-exporter 0.7.1 v0.11.0 DEPRECATED A Helm chart for prometheus mysql exporter wit...stable/percona 1.2.3 5.7.26 DEPRECATED - free, fully compatible, enhanced, open sourc...stable/percona-xtradb-cluster 1.0.8 5.7.19 DEPRECATED - free, fully compatible, enhanced, open sourc...stable/phpmyadmin 4.3.5 5.0.1 DEPRECATED phpMyAdmin is an mysql administration frontendstable/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 to use open...

2 - helm fetch

[root@master helm]# helm fetch stable/mysql --untar 自动解压 [root@master helm]# ls mysql mysql-1.6.9.tgz tiller-rbac.yml[root@master helm]# helm fetch --helpRetrieve a package from a package repository, and download it locally.This is useful for fetching packages to inspect, modify, or repackage. It can also be used to perform cryptographic verification of a chart without installing the chart.There are options for unpacking the chart after download. This will create a directory for the chart and uncompress into that directory.If the --verify flag is specified, the requested chart MUST have a provenance file, and MUST pass the verification process. Failure in any part of this will result in an error, and the chart will not be saved locally.Usage: helm fetch [flags] [chart URL | repo/chartname] [...]Flags: --ca-file string Verify certificates of HTTPS-enabled servers using this CA bundle --cert-file string Identify HTTPS client using this SSL certificate file -d, --destination string Location to write the chart. If this and tardir are specified, tardir is appended to this (default ".") --devel Use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored. -h, --help help for fetch --key-file string Identify HTTPS client using this SSL key file --keyring string Keyring containing public keys (default "/root/.gnupg/pubring.gpg")

3- helm delete

[root@master helm]# helm delete mysqlrelease "mysql" deleted[root@master helm]# helm list[root@master helm]# helm install --name mysql stable/mysqlError: a release named mysql already exists.Run: helm ls --all mysql; to check the status of the releaseOr run: helm del --purge mysql; to delete it[root@master helm]# helm del --purge mysqlrelease "mysql" deleted

4- helm install

helm install --name mysql stable/mysql

5- helm repo

[root@master helm]# helm repo listNAME URLstable helm get

[root@master helm]# helm get --helpThis command shows the details of a named release.It can be used to get extended information about the release, including: - The values used to generate the release - The chart used to generate the release - The generated manifest fileBy default, this prints a human readable collection of information about thechart, the supplied values, and the generated manifest file.Usage: helm get [flags] RELEASE_NAME helm get [command]Available Commands: hooks Download all hooks for a named release manifest Download the manifest for a named release notes Displays the notes of the named release values Download the values file for a named release

5 helm history

[root@master helm]# helm history mysqlREVISION UPDATED STATUS CHART APP VERSION DESCRIPTION1 Sun Jan 3 09:53:04 2021 DEPLOYED mysql-1.6.9 5.7.30 Install complete

6 helm list

[root@master helm]# helm listNAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACEmysql 1 Sun Jan 3 09:48:14 2021 DEPLOYED mysql-1.6.9 5.7.30 default

helm reset

helm status

helm 安装mysql

直接在线安装

$ helm search stable/mysql$ helm install -n mysql57 stable/mysql# 或者执行如下:$ helm install -n mysql57 helm fetch stable/mysql$ helm install -n mysql57 mysql-1.6.2.tgz

指定目录的方式进行安装

$ tar xf mysql-1.6.2.tgz$ helm install -n mysql57 ./mysql

检查要安装应用的参数

$ helm inspect values stable/mysql

把关于参数的内容导出到某个文件中

helm inspect valuesstable/mysql > /tmp/mysql.values

修改/tmp/mysql.values的内容后,执行如下进行安装

$ helm install myjenkins stable/mysql --values /tmp/mysql.values

回滚

#1查看已经安装的应用[root@c720194 helm]# helm lsNAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACEmysql57 1 Wed Mar 18 17:26:43 2020 DEPLOYED mysql-1.6.2 5.7.28 default #2升级应用[root@c720194 helm]# helm upgrade --set mysqlRootPassword=yuanke mysql57 stable/mysql#3获取设置的值[root@c720194 helm]# helm get values mysql57mysqlRootPassword: yuanke#4查看部署的历史信息[root@c720194 helm]# helm history mysql57REVISION UPDATED STATUS CHART DESCRIPTION 1 Wed Mar 18 17:26:43 2020 SUPERSEDED mysql-1.6.2 Install complete2 Thu Mar 19 10:45:24 2020 DEPLOYED mysql-1.6.2 Upgrade complete#5针对先前部署的版本进行回退。[root@c720194 helm]# helm rollback mysql57 1Rollback was a success.[root@c720194 helm]# helm history mysql57REVISION UPDATED STATUS CHART DESCRIPTION 1 Wed Mar 18 17:26:43 2020 SUPERSEDED mysql-1.6.2 Install complete2 Thu Mar 19 10:45:24 2020 SUPERSEDED mysql-1.6.2 Upgrade complete3 Thu Mar 19 11:08:52 2020 DEPLOYED mysql-1.6.2 Rollback to 1 #6卸载安装的软件[root@c720194 helm]# helm delete mysql57release "mysql57" deleted[root@c720194 helm]# helm ls

Helm charts文件结构

Charts介绍

Helm使用一种称为chart的打包格式,chart是描述一组相关kubernetes资源的文件集合。单个chart可以用于部署简单的东西,如memcached pod,也可以用于部署复杂的东西,如包含HTTP服务器,数据库,缓存的完整web应用程序堆栈。

chart是以为你教案的形似创建的,放在特定的目录树中,然后可以将他们打包到版本化的归档中进行部署

Chart文件结构

一个chart它是由一些列的文件进行组成的,包好这些文件的目录名字往往就是char的名字。比如描述wordpress的chart就存储在wordpress目录中。

wordpress/ 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

创建chart

创建chart

$ helm create foo

chart yaml文件

针对一个chart,Chart.yaml文件时必须的,它包含如下字段。

apiVersion: The chart API version (required)name: The name of the chart (required)version: A SemVer 2 version (required)kubeVersion: A SemVer range of compatible Kubernetes versions (optional)description: A single-sentence description of this project (optional)type: It is the type of chart (optional)keywords: - A list of keywords about this project (optional)home: The URL of this projects home page (optional)sources: - A list of URLs to source code for this project (optional)dependencies: # A list of the chart requirements (optional) - name: The name of the chart (nginx) version: The version of the chart ("1.2.3") repository: The repository URL ("or alias ("@repo-name") condition: (optional) A yaml path that resolves to a boolean, used for enabling/disabling charts (e.g. subchart1.enabled ) tags: # (optional) - Tags can be used to group charts for enabling/disabling together enabled: (optional) Enabled bool determines if chart should be loaded import-values: # (optional) - ImportValues holds the mapping of source values to parent key to be imported. Each item can be a string or pair of child/parent sublist items. alias: (optional) Alias usable alias to be used for the chart. Useful when you have to add the same chart multiple timesmaintainers: # (optional) - name: The maintainers name (required for each maintainer) email: The maintainers email (optional for each maintainer) url: A URL for the maintainer (optional for each maintainer)icon: A URL to an SVG or PNG image to be used as an icon (optional).appVersion: The version of the app that this contains (optional). This needn't be SemVer.deprecated: Whether this chart is deprecated (optional, boolean)

charts和Versioning

每个chart都必须有一个版本号,一个版本号必须遵循SemVer2标准。Helm使用版本号作为release的标志,而在仓库中的包是通过名字加版本号来进行标识的。

比如,一个名叫nginx的chart,版本号是version:1.2.3:那么整体的名字就是nginx-1.2.3.tgz

apiVersion字段

在Helm3中可以使用v2,也可以使用v1,但是在helm2中只能使用v1

把apiVersion的字段改成v2的话,有以下要求:

以前把以来关系定义在requirements.yaml文件中的,需要在Char.yaml中使用dependencies字段进行定义type字段,用来区分应用和库

helm 丰田车stable/wordporess

appVersion字段

这个appVersion字段和verison字段并不相关。它是指定应用版本的方式,比如drupal chart可能有一个appVersion:8.2.1的设置,那么表明在chart中的Drupa的版本是8.2.1,它会对chart版本计算没有任何影响。

kubeVersion字段(可选)

表明支持的kubeernetes版本

kubernetes的版本约束可以使用AND来进行约束,如>=1.13.0 < 1.15.0

也可以使用或的表达式来进行约束,如 >=1.13.0 <1.14.0 || >=1.14.1< 1.15.0

除了使用符号= !=><>= <=,还支持如下表达方式

1.1 - 2.3.4 等同行 >= 1.1 <= 2.3.4通配符x,X和*,比如1.2.x就等同行>= 1.2.0 < 1.3.0波浪符号,比如~1.2.3就等同于>= 1.2.3 < 1.3.0脱字符, 比如^1.2.3就等同于>=1.2.3 < 2.0.0

废弃的charts

当在chart仓库中管理charts的时候,有时候需要把某个chart标记启用的,那么就可以在chart.yaml中使用deprecated字段来表明该chart为弃用

当废弃一个chart的时候,需要遵循流程

更新char的yaml文件,标记为废弃在chart仓库中发布以一个chart的新版本然后从源仓库中删除废弃的chart

chart的类型

type字段定义图标的类型。有两种类型,应用程序和库,应程序是默认类型,他是可以完全操作的标准的chart。库或者helperchart针对chart构建提供使用程序或者函数,chart库和应用chart完全不同,因为它没有资源对象,因此无法安装。

chart依赖关系

chart LICENSE,README和Notes

LICENSE是一个普通的文本文件,主要包括该Chart的授权相关信息。而chart的README通常是以Mrakdown格式写的,通常应该包含如下:

chart提供的应用或服务描述。运行该chart的先决条件或需求。在values.yaml中的选项描述或者默认值。和chart的安装和配置相关的其它信息。

chart还可以包含一个简短的文本templates/NOTES.txt文件,它一般是在安装chart后,或者打印版本状态时会显示。此文件被评估为模板,并可用于显示使用说明、后续步骤或与图表发布相关的任何其他信息。例如,可以提供连接数据库或访问web UI的指令。因此在使用helm install或helm status时,会显示。建议用简短的话进行描述,详细信息可以指向README.

chart依赖

在Helm中,一个chart可能以来任何数量的charts,这些以来可以通过chart.yaml文件中使用dependencies字段来连接,或者把它放在charts目录中进行手工管理。

使用dependencies字段管理依赖:

dependencies: - name: apache version: 1.2.3 repository: - name: mysql version: 3.2.1 repository: - name: 依赖的chart名称。# - version: 依赖的chart版本号。# - repository: chart仓库的完整URL,还必须使用`helm repo add`添加repo到本地。# - 也可以使用repo的名字代替URL。$ helm repo add yuanke-charts - name: awesomeness version: 1.0.0 repository: "@yuanke-charts"

在定义好依赖关系之后,运行helm dependency update,它将会使用依赖文件去下载所有的指定的charts到chats/目录

$ helm dep up foochartHang tight while we grab the latest from your chart repositories......Successfully got an update from the "local" chart repository...Successfully got an update from the "stable" chart repository...Successfully got an update from the "example" chart repository...Successfully got an update from the "another" chart repositoryUpdate Complete. Happy Helming!Saving 2 chartsDownloading apache from repo mysql from repo dependency update检索图标的时候,它将把他们作为chart存储到charts/目录中。因此,对于上面的示例,可以看到如下文件

charts/ apache-1.2.3.tgz mysql-3.2.1.tgz

denpendencies中的Alias字段

除了上面的三个字段外,还可以包含一个alias的可选字段。

在需要访问具有其他名称的图表的情况下,可以使用alias

# parentchart/Chart.yamldependencies: - name: subchart repository: version: 0.1.0 alias: new-subchart-1 - name: subchart repository: version: 0.1.0 alias: new-subchart-2 - name: subchart repository: version: 0.1.0

在上面的案例中,针对parentchart,获取三个依赖

subchartnew-subchart-1new-subchart-2

实现此目的的手动方法是使用不同的名称在charts/目录中多次复制/粘贴相同的chart.

在依赖关系中的Tags和Condition字段(可选字段)

默认情况下,加载所有的charts. 如果tags和condition字段出现的话,那么在它用的时候会做个评估。

Conditon - 条件字段包含一个或多个YAML路径(用逗号分隔)。如果此路径存在于顶部父值中并解析为布尔值,则将根据该布尔值启用或禁用图表。只计算列表中找到的第一个有效路径,如果没有路径存在,则条件无效。

Tags - tags字段是与此图表关联的标签的YAML列表。在顶部的父值中,通过指定标记和布尔值,可以启用或禁用所有带有标记的图表。

# parentchart/Chart.yamldependencies: - name: subchart1 repository: version: 0.1.0 condition: subchart1.enabled, global.subchart1.enabled tags: - front-end - subchart1 - name: subchart2 repository: version: 0.1.0 condition: subchart2.enabled,global.subchart2.enabled tags: - back-end - subchart2

# parentchart/values.yamlsubchart1: enabled: truetags: front-end: false back-end: true

在上面的案例中,所有charts拥有tagfront-end​的将会关闭,但又由于subcchart1.enabled​的路径在它的父值中被评估为true​,因此该条件将会覆盖front-end​的tag,subchart1将会开启。

由于subchart2的标记back-end,而该标记又被评为true,那么subchart2将会开启。还注意到,虽然subchart2指定了一个条件,但是在父值中没有对应的路径和值,所以该条件没有影响。

Tags和Conditon解析

Conditions始终覆盖tags(当在condition中设置值的情况下)。存在的第一个条件路径将获胜,该chart的后续条件路径将被忽略。当chart的任何tag为true时,那么就会启用这个chart.在它的父chart中,tags和condition值必须设置。tags:键值必须是顶级键。全局和嵌套tags:当前不支持.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Operator-1初识Operator
下一篇:检测dom尺寸变化
相关文章

 发表评论

暂时没有评论,来抢沙发吧~