0
点赞
收藏
分享

微信扫一扫

k8s学习笔记-helm操作2

毅会 2022-07-12 阅读 168

回滚

查看已经安装的应用

[root@c720194 helm]# helm ls
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
mysql57 1 Wed Mar 18 17:26:43 2020 DEPLOYED mysql-1.6.2 5.7.28 default

升级应用

[root@c720194 helm]# helm upgrade --set mysqlRootPassword=yuanke mysql57 stable/mysql

获取设置的值

[root@c720194 helm]# helm get values mysql57
mysqlRootPassword: yuanke

查看部署的历史信息

[root@c720194 helm]# helm history mysql57
REVISION UPDATED STATUS CHART DESCRIPTION
1 Wed Mar 18 17:26:43 2020 SUPERSEDED mysql-1.6.2 Install complete
2 Thu Mar 19 10:45:24 2020 DEPLOYED mysql-1.6.2 Upgrade complete

针对先前部署的版本进行回退

[root@c720194 helm]# helm rollback mysql57 1
Rollback was a success.

[root@c720194 helm]# helm history mysql57
REVISION UPDATED STATUS CHART DESCRIPTION
1 Wed Mar 18 17:26:43 2020 SUPERSEDED mysql-1.6.2 Install complete
2 Thu Mar 19 10:45:24 2020 SUPERSEDED mysql-1.6.2 Upgrade complete
3 Thu Mar 19 11:08:52 2020 DEPLOYED mysql-1.6.2 Rollback to 1

卸载安装的软件

[root@c720194 helm]# helm delete mysql57
release "mysql57" deleted
[root@c720194 helm]# helm ls

Helm charts文件结构

Charts介绍

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

chart是以文件的形式创建,放在特定的目录树中,然后可以将他们打包到版本化的归档中进行部署。

Chart文件结构

一个chart它是由一谢列的文件进行组装的,包含这些文件的目录名字往往就是chart的名字。比如描述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

k8s学习笔记-helm操作2_sed

创建chart

创建一个名为foo的chart

helm create foo

k8s学习笔记-helm操作2_sed_02

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 ("https://example.com/charts") 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 times
maintainers: # (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)


模板和值

Helm Chart模板是用go模板语言编写的,从Spring库中添加了大约50个附加模板函数和一些其他的专门函数。

模板下的几个常用文件:

  • NOTES.txt,当运行helm install的时候,会显示里面的内容给用户
  • deployment.yaml:针对kubernertes deployment的基本清单文件
  • service,yaml :针对部署创建service的清单文件
  • _helpers.tpl:放置模板帮助程序的地方,可以在整个chart中重用这些模板的帮助程序。

所有的模板文件存储在chart的templates文件夹下。当helm渲染charts的时候,将通过模板引擎传递该目标的每个文件。

针对模板的值,支持如下两种方式:

  • values.yaml:该文件包含默认的值
  • 命令行:在使用helm instal命令时提供(此值会覆盖values.yaml的值)

模板文件

模板文件遵循编写go模板的标准约定。

apiVersion: v1
kind: ReplicationController
metadata:
name: deis-database
namespace: deis
labels:
app.kubernetes.io/managed-by: deis
spec:
replicas: 1
selector:
app.kubernetes.io/name: deis-database
template:
metadata:
labels:
app.kubernetes.io/name: deis-database
spec:
serviceAccount: deis-database
containers:
- name: deis-database
image: {{ .Values.imageRegistry }}/postgres:{{ .Values.dockerTag }}
imagePullPolicy: {{ .Values.pullPolicy }}
ports:
- containerPort: 5432
env:
- name: DATABASE_STORAGE
value: {{ default "minio" .Values.storage }}

以上是一个基于kubernets replication controller的模板。它可以使用下面四个模板的值,

  • imageRegisty:针对Docker镜像的仓库源
  • dockerTag:针对Docker镜像的标签
  • pullPolicy:kubernetes拉取的策略
  • Storage:存储的后端,默认为“minio”

预先定义值

一般使用的值是存储在values.yaml文件中或者通过--set选项进行设定的。但是也有其他预先设定定义的数据,可以在模板中使用。

下面这些值是预先定义的,针对每个模板都有效,且不能覆盖,并且他们的名称也是却分大小写的。

  • Release.Name: 发布版本的名称。
  • Release.Namespace: 发布chart所属的名称空间。
  • Release.Service: 发布的服务。
  • Release.IsUpgrade: 如果当前的操作是升级或者回退,那么设置为true.
  • Release.IsInstall: 假如当前的操作是一个安装,那么设置为true.
  • Chart: 这是Chart.yaml的内容。 因此,chart的版本从Chart.Version中获得,而维护者是Chart.Maintainers.
  • Files: A map-like object containing all non-special files in the chart. This will not give you access to templates, but will give you access to additional files that are present (unless they are excluded using .helmignore). Files can be accessed using {{ index .Files "file.name" }} or using the {{ .Files.Get name }} or {{ .Files.GetString name }} functions. You can also access the contents of the file as []byte using {{ .Files.GetBytes }}
  • Capabilities: A map-like object that contains information about the versions of Kubernetes ({{ .Capabilities.KubeVersion }} and the supported Kubernetes API versions ({{ .Capabilities.APIVersions.Has "batch/v1" }})

注意:任何在chart.yaml中未知的字段将丢失。chart对象内部也不能访问。

Values files

一个values.yaml文件所提供的所需的值如下所示:

imageRegistry: "quay.io/deis"
dockerTag: "latest"
pullPolicy: "Always"
storage: "s3"

可以用以下的方式进行覆盖默认值

helm install --values=myvals.yaml wordpress

如果使用 --values=myvalues.yaml中的值,而默认的值里面没有的话,那么就是合并的操作。

imageRegistry: "quay.io/deis"
dockerTag: "latest"
pullPolicy: "Always"
storage: "gcs"

chart中默认的值文件必须是values.yaml文件。而且命令行指定的时候,可以不限制名称。

可以在模板内部使用.Values对象访问values.yaml中的任何值。

apiVersion: v1
kind: ReplicationController
metadata:
name: deis-database
namespace: deis
labels:
app.kubernetes.io/managed-by: deis
spec:
replicas: 1
selector:
app.kubernetes.io/name: deis-database
template:
metadata:
labels:
app.kubernetes.io/name: deis-database
spec:
serviceAccount: deis-database
containers:
- name: deis-database
image: {{ .Values.imageRegistry }}/postgres:{{ .Values.dockerTag }}
imagePullPolicy: {{ .Values.pullPolicy }}
ports:
- containerPort: 5432
env:
- name: DATABASE_STORAGE
value: {{ default "minio" .Values.storage }}

范围,依赖和值

values文件可以针对定级的chart声明值,也可以声明包含在该chart/目录中任何chart的值,或者一个的值可以向chart极其任何依赖项提供值,值文件可以为所有的这些组件提供值。

title: "My WordPress Site" # Sent to the WordPress template

mysql:
max_connections: 100 # Sent to MySQL
password: "secret"

apache:
port: 8080 # Passed to Apache

WordPress chart可以访问Mysq密码:.Values.mysql.password,但是低级别的chart不能访问它的父chart的值,比如mysql不能访问title的属性,也不不能访问apache.port

Global值

从Helm 2.0.0版本开始,Helm支持指定“global”值。

title: "My WordPress Site" # Sent to the WordPress template

global:
app: MyWordPress

mysql:
max_connections: 100 # Sent to MySQL
password: "secret"

apache:
port: 8080 # Passed to Apache

所有的chart都可以使用.Values.global.app来访问它的值

比如,mysql模板可以使用{{ .Values.gloabl.app }}来访问app,apcahe也是一样的。实际上面的值文件就像下面定义一样。

title: "My WordPress Site" # Sent to the WordPress template

global:
app: MyWordPress

mysql:
global:
app: MyWordPress
max_connections: 100 # Sent to MySQL
password: "secret"

apache:
global:
app: MyWordPress
port: 8080 # Passed to Apache

如果chart声明了一个全局变量,那么全局变量将向下传递,传递到子chart的子chart,而不是向上传递到父chart,子chart不能影响到父chart的值。

此外,父chart的全局变量优先于子chart的全局变量。

使用helm对charts的管理

创建新的chart

$ helm create mychart
Created mychart/

对于helm chart的文件进行打包

$ helm package mychart
Archived mychart-0.1.-.tgz

针对自建的helm chart进行检查

$ helm lint mychart
No issues found

内置对象的引入

创建一个名为mychart的chart

 helm create mychart

创建mychart/templates/configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: mychart-configmap
data:
myvalue: "Hello World"

模板的文件名称没有严格的限制。但是推荐针对YAML文件建议使用.yaml后缀,针对helpers建议使用.tpl后缀。

安装chart

helm install -n full-coral ./mychart

检索release和查看实际加载的模板

helm get manifest full-coral

卸载安装的release

helm delete full-coral

修改configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"

此使用如下的命令查看情况,Release.Name是内置的名称

$ helm install -n full-coral ./mychart
$ helm get manifest full-coral

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: full-coral-configmap
data:
myvalue: "Hello World"

当然,如果只是想查看以下安装的结果,而不安装它,可以使用如下:

$ helm install --debug --dry-run -n full-coral ./mychart
[debug] Created tunnel using local port: '39663'

[debug] SERVER: "127.0.0.1:39663"

[debug] Original chart version: ""
[debug] CHART PATH: /root/helm/mychart

NAME: goodly-guppy
REVISION: 1
RELEASED: Fri Mar 20 10:53:00 2020
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
affinity: {}
fullnameOverride: ""
image:
pullPolicy: IfNotPresent
repository: nginx
tag: stable
imagePullSecrets: []
ingress:
annotations: {}
enabled: false
hosts:
- host: chart-example.local
paths: []
tls: []
nameOverride: ""
nodeSelector: {}
replicaCount: 1
resources: {}
service:
port: 80
type: ClusterIP
tolerations: []

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-guppy-configmap
data:
myvalue: "Hello World"



举报

相关推荐

0 条评论