helm的go模板语法学习

 1、helm chart

1.0、什么是helm?

介绍:就是个包管理器。理解为java的maven、linux的yum就好。
安装方法也可参见官网: https://helm.sh/docs/intro/install

通过前面的演示我们知道,有了helm之后应用的安装、升级、查看、停止都变得非常简单。
 

1.1、helm chart 及 相关介绍

1、什么是chart?

        一个chart就可以管理一个服务,一个chart就应该包括一个服务所需要的所有配置文件,例如 deployment、service、ingress、role、serviceaccount等。

        例如对于如下截图所示的服务有很多资源。没有chart的时候我们需要 kubectl apply -f a.yaml b.yaml …… 等进行启动,有了helm之后启动、升级、查看、停止等管理都变的很简单了。

2、关于chart的几个概念

①chart:就代表helm包,包含在k8s集群内部运行的程序、服务所需要的所有定义。

②repository(仓库):用来存放和共享charts的地方。

③release:运行在k8s集群中的chart的实例。一个chart可以在同一个集群中安装多次,每次都会创建一个新的release。

3、chart包中文件的组成形式及作用

各文件作用及解释如下:

(1)templates: 这个就是k8s资源声明的模板文件。

其中包括了一个服务所需要的deployment、service、ingress、configmap等所有需要用到的资源的模板。

注:“模板文件”的意思是文件的规范就是k8s资源描述规范,但是里面部分值信息是被模板化了的意思。

注:在执行helm install 安装时helm会把这里的模板文件和values.yaml中配置项的值结合起来,得到k8s的实际资源定义并应用到k8s上。

(2)values.yaml:主要用来保存一些可变参数(配置项),例如 版本、副本数等。

注:所以通过查看values.yaml我们就知道这个chart暴露了哪些参数给我们可以调。

(3)chart.yaml:描述chart信息的元数据,名称、描述、类型、版本号等基础信息。

(4)charts:里面包含的是子charts,一般用不到。

(5)NOTE.txt:安装成功的提示信息。注:里面也可以引用可变参数。

(6)_helpers.tpl:定义了一些函数供模板通过include的方式使用。

4、helmfile又是什么东西?

类比docker: helm 与 helmfile的关系 就是 docker 与 docker composer 的关系。

一个chart就可以对应成一个服务,helm就是负责部署单个chart的;—— 单兵。

helmfile则是用一份配置文件批量部署多个helm chart; —— 指挥多个单兵的长官。

什么情况需要用 helmfile?

  • ✅ 需要同时管理多个 Helm Chart

  • ✅ 环境配置复杂(开发/测试/生产/多集群)

  • ✅ 要求部署过程可重复、可版本化

如果只是部署单个应用,直接用 Helm 就够了!

一句话总结

Helmfile = Helm + 批量处理 + 环境管理 + 依赖控制


它让 Helm Chart 的复杂部署变得像点外卖一样简单——你只需要下一份订单(helmfile.yaml),剩下的交给它搞定!

helmfile 常用命令:

# 查看将要执行的操作(干跑测试)
helmfile diff# 部署所有 Chart
helmfile apply# 仅部署某个 Chart(如 frontend)
helmfile sync -l name=frontend# 卸载所有 Chart
helmfile destroy

1.2、helm 常用命令

1.2.1、安装与部署
#helm install 将chart安装到k8s集群,创建release
helm install my-nginx bitnami/nginx -n default
注:my-nginx 是 Release 名称,bitnami/nginx 是 Chart 名称,-n 指定命名空间#helm upgrade 升级已安装的 Release,应用新配置或新版本 Chart。
helm upgrade my-nginx bitnami/nginx --set replicaCount=2
注:修改副本数为 2,并更新 Release
1.2.2、仓库管理
#helm repo add 添加第三方 Chart 仓库。
helm repo add bitnami https://charts.bitnami.com/bitnami
注:添加 Bitnami 官方仓库#​​helm repo update​​ 更新本地缓存的仓库索引,获取最新 Chart 列表。
helm repo update
注:同步所有已添加仓库的最新信息
1.2.3、版本管理
#helm history 查看release的版本历史记录
helm history my-nginx
注:显示所有修订版本的部署时间和状态#helm rollback  回滚 Release 到指定历史版本。
helm rollback my-nginx 1
注:回滚到版本 1(通过 helm history my-nginx 查看版本号)

1.2.4、调试与开发

(1)helm template 

作用:本地渲染 Chart 的模板生成yaml文件,不实际部署。

helm template [RELEASE_NAME] [CHART] [flags]
helm template my-nginx ./mychart

注:预览 Chart 生成的 Kubernetes 资源清单

(2)helm lint

作用​​:检查 Chart 的语法和格式是否符合规范。

helm lint ./my-chart

(3)​​helm install --dry-run

作用​​:模拟安装过程,验证配置是否合法。

helm install my-nginx bitnami/nginx --dry-run --debug

注:输出渲染后的 YAML 文件但不实际部署

1.2.5、其他常用命令

(1)helm list 

作用​​:列出当前命名空间下的所有 Release。

helm list -n default

注:显示 default 命名空间已部署的 Release。

(2)helm show values

​作用​​:查看 Chart 的默认配置值。

helm show values bitnami/nginx

输出 values.yaml 的默认配置,供自定义覆盖

(3)helm uinstall

​作用​​:卸载并删除指定 Release。

helm uninstall my-nginx -n default

注:理 Release 相关资源

1.2.6、chart开发相关

(1)helm create

​作用​​:生成 Chart 模板目录结构。

helm create mychart

(2)helm package

​作用​​:将 Chart 目录打包为 .tgz 文件。

helm package ./my-chart

注:生成可分发或上传到仓库的 Chart 包。

2、go模板语法学习 —— 解读文件

 文件组织形式如下。

2.1、ingress.yaml及对应解释

先看一个ingress.yaml的case。 

#检查 values.yaml 中的 ingress.enabled 是否为true。只有为true才会渲染后续内容。
# "-"表示取出模板渲染是多余的空白符,避免生成多余的空白行。
{{- if .Values.ingress.enabled -}}
#使用的 api 版本
apiVersion: networking.k8s.io/v1
#资源类型是 ingress.
kind: Ingress
#接下来是元数据
metadata:# 使用helm助手函数 mychart.fullname 生成名称,通常在 _helpers.tpl 中定义,生成资源的名称。# include 表示引入标签模板???# . 表示传递当前作用域的上下文。name: {{ include "mychart.fullname" . }}labels:# 引入Chart中定义的通用标签,nindent 4 表示缩进4个空格(就是缩进4个空格的意思){{- include "mychart.labels" . | nindent 4 }}# 注解配置    晚点研究下什么叫注解配置??#如果 values.yaml 中定义了 ingress.annotations,则进入 with 块;此时,. 指向 annotations 对象。{{- with .Values.ingress.annotations }}annotations:# "toYaml .":将注解转换为YAML格式; "nindent 4":表示缩进4个空格。{{- toYaml . | nindent 4 }}{{- end }}
# 接下来才是 ingress 的规范
spec:# 如果配置了 ingress.className ,则设置 Ingress 类。{{- with .Values.ingress.className }}ingressClassName: {{ . }}{{- end }}# 然后是 tls 配置。如果配置了 TLS {{- if .Values.ingress.tls }}tls:#遍历 tls 配置列表{{- range .Values.ingress.tls }}- hosts:# 遍历每个 tls 中的 hosts 列表。 # quote 函数自动为域名添加引号(避免yaml解析问题){{- range .hosts }}- {{ . | quote }}{{- end }}#指定对应的 secretNamesecretName: {{ .secretName }}{{- end }}{{- end }}#接下来是路由规则rules:# 首先遍历所有的hosts配置{{- range .Values.ingress.hosts }}- host: {{ .host | quote }}http:paths:# 这里在遍历所有的paths{{- range .paths }}- path: {{ .path }}     #这个就是路径匹配规则了# "with .pathType":如果定义了 pathType(如 Prefix 或 Exact),则渲染该字段。{{- with .pathType }} #可选的路径类型pathType: {{ . }}{{- end }}#后端的指向。肯定是指向chart的主 Servicebackend:service:# $ 表示顶层作用域的上下文,避免别range或with覆盖name: {{ include "mychart.fullname" $ }}port:# "$.Values.service.port" 显示地指定从顶层作用域获取 service.port 值number: {{ $.Values.service.port }}{{- end }}{{- end }}
{{- end }}

2.2、deployment.yaml

#固定内容
apiVersion: apps/v1
#表示资源类型是deployment
kind: Deployment
#元数据
metadata:# 调用模板函数 mychart.fullname (通常在 _helpers.tpl中定义),生成deployment名称# . 表示传递当前上下文 ????????????????????????????????没懂什么意思??name: {{ include "mychart.fullname" . }}labels:# "mychart.labels" 也是在 _helpers.tpl 中定义的模板函数。注:定义了通用标签。{{- include "mychart.labels" . | nindent 4 }}
spec:# 如果 values.yaml 中的 autoscaling.enabled 为false就进入 if 块。# 这句话的意思是如果为启用自动扩缩容,则设置固定副本数为 values.yaml 中的 .replicaCount 值。{{- if not .Values.autoscaling.enabled }}replicas: {{ .Values.replicaCount }}{{- end }} #if块结束#pod选择器selector:matchLabels:# 同样这里也是使用 _helpers.tpl中定义 的模板函数 mychart.selectorLabels 获取选择器标签。{{- include "mychart.selectorLabels" . | nindent 6 }}#接下来是pod模板template:metadata:# with 表示如果对应字段存在就进入该字段的作用域{{- with .Values.podAnnotations }}annotations:# "toYaml ."表示将注解转换为yaml,并缩进8空格{{- toYaml . | nindent 8 }}{{- end }}#labels:# 使用 _helpers.tpl中定义的 "mychart.labels"函数;其定义的是通用标签。{{- include "mychart.labels" . | nindent 8 }}#然后是一个 with 块,引的是 values.yaml 中 .podLabels 字段定义的标签{{- with .Values.podLabels }}{{- toYaml . | nindent 8 }}{{- end }}# pod规则spec:# 如果配置了 values.yaml 中配置了 imagePullSecrets,则将其值转换成yaml放置过来,并缩进8空格。{{- with .Values.imagePullSecrets }}imagePullSecrets:{{- toYaml . | nindent 8 }}{{- end }} #最近的那个with块结束#使用 _helpers.tpl中定义 的模板函数 mychart.serviceAccountName 生成服务账号名称。serviceAccountName: {{ include "mychart.serviceAccountName" . }}# 如果values.yaml 中定义了 podSecurityContext(pod级安全上下文)则渲染{{- with .Values.podSecurityContext }}securityContext:{{- toYaml . | nindent 8 }}{{- end }}#接下来就是容器配置了containers:# .Chart.Name: 使用 Chart 名称作为容器名。Chart.yaml 中的name字段。- name: {{ .Chart.Name }}{{- with .Values.securityContext }}securityContext:{{- toYaml . | nindent 12 }}{{- end }}# 镜像名: values.yaml 中定义的 "image.repository"# 镜像标签: 优先使用 values.yaml 中定义的 image.tag; 如果没有就使用 Chart.yaml 中的 .AppVersion 兜底。image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"#镜像拉取策略: 采用 values.yaml 中定义的 .image.pullPolicyimagePullPolicy: {{ .Values.image.pullPolicy }}ports:- name: http#容器端口采用 values.yaml 中定义的 .service.port containerPort: {{ .Values.service.port }}protocol: TCP#下面就是配置健康检查#如果values.yaml中配置了 livenessProbe 就转换为yaml。{{- with .Values.livenessProbe }}livenessProbe:{{- toYaml . | nindent 12 }}{{- end }}#转换为yaml{{- with .Values.readinessProbe }}readinessProbe:{{- toYaml . | nindent 12 }}{{- end }}#转换为yaml 换成了资源{{- with .Values.resources }}resources:{{- toYaml . | nindent 12 }}{{- end }}#转换为yaml 换成了挂载{{- with .Values.volumeMounts }}volumeMounts:{{- toYaml . | nindent 12 }}{{- end }}# 转换为yaml 数据卷{{- with .Values.volumes }}volumes:{{- toYaml . | nindent 8 }}{{- end }}# 转换为yaml 节点选择器 指定pod调度到特定的节点{{- with .Values.nodeSelector }}nodeSelector:{{- toYaml . | nindent 8 }}{{- end }}#转换为yaml 亲和性{{- with .Values.affinity }}affinity:{{- toYaml . | nindent 8 }}{{- end }}#转换为yaml 容忍度{{- with .Values.tolerations }}tolerations:{{- toYaml . | nindent 8 }}{{- end }}

2.3、service.yaml及对应解释

#固定内容
apiVersion: v1
#类型是 service 表示声名一个 service 类型的资源
kind: Service
metadata:# 调用模板函数 mychart.fullname(通常在 _helpers.tpl 中定义),生成 Service 名称。name: {{ include "mychart.fullname" . }}labels:# 调用模板函数 mychart.labels _helpers.tpl 中定义),获取定义的通用标签。{{- include "mychart.labels" . | nindent 4 }}
spec:# 从values.yaml中读取 service.type 字段(如 ClusterIP、NodePort 等)。type: {{ .Values.service.type }}ports:# Service 暴露的端口,值来自 values.yaml 的 service.port。- port: {{ .Values.service.port }}# 固定值 http,表示将流量转发到 Pod 中名为 http 的容器端口(需与 Pod 定义一致)。targetPort: httpprotocol: TCPname: httpselector:# 调用模板生成选择器标签(需匹配 Pod 的标签)。{{- include "mychart.selectorLabels" . | nindent 4 }}

2.4、hpa.yaml

# 这是一个条件判断语句,检查 values.yaml 中 autoscaling.enabled 是否为 true。
# - 表示去除渲染时左侧的空白符(避免生成多余空行)。  只有当条件为真时,才会渲染后续内容。
{{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2
# 固定内容,声明这是一个 Kubernetes HPA 资源,使用 autoscaling/v2 API 版本。
kind: HorizontalPodAutoscaler
metadata:name: {{ include "mychart.fullname" . }}labels:{{- include "mychart.labels" . | nindent 4 }}
spec:# 指定 HPA 管理的目标资源(这里是一个 Deployment)。scaleTargetRef:apiVersion: apps/v1kind: Deployment# nclude "mychart.fullname" . 再次调用模板函数,确保名称与 Deployment 一致。name: {{ include "mychart.fullname" . }}# 从 values.yaml 中读取最小和最大副本数配置。minReplicas: {{ .Values.autoscaling.minReplicas }}maxReplicas: {{ .Values.autoscaling.maxReplicas }}# 扩缩容指标metrics:# 检查是否配置了 CPU 目标使用率(targetCPUUtilizationPercentage)。如果配置了,则渲染 CPU 指标部分。{{- if .Values.autoscaling.targetCPUUtilizationPercentage }}- type: Resourceresource:name: cputarget:type: UtilizationaverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }}{{- end }}# 同理,检查并渲染内存指标配置(如果存在)。{{- if .Values.autoscaling.targetMemoryUtilizationPercentage }}- type: Resourceresource:name: memorytarget:type: UtilizationaverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }}{{- end }}
{{- end }}

2.6、_helper.tpl (定义模板函数的地方)

作用:其实就是定义了一些函数,可以再模板中通过 include 的方式调用。

{{/*
Expand the name of the chart.
*/}}
/*
作用: 生成 Chart 的基础名称。
如果 values.yaml 中定义了 nameOverride,则优先使用它,否则用 Chart.Name。
trunc 63:限制名称不超过 63 个字符(Kubernetes 命名规范)。
trimSuffix "-":删除末尾的短横线(避免无效命名)。
*/
{{- define "mychart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
/*
作用:生成唯一的资源名称(通常用于 Deployment/Service 等)。
如果定义了 fullnameOverride,直接使用它。
否则,检查 Release 名是否已包含 Chart 名:如果包含(如 Release 名为 myapp-prod),直接使用 Release 名。否则拼接两者(如 prod-myapp)。
*/
{{- define "mychart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}{{/*
Create chart name and version as used by the chart label.
*/}}
/*
作用:生成 Chart 的版本标签(用于元数据)。
拼接 Chart 名称和版本(如 myapp-1.0.0)。
replace "+" "_":替换版本中的 + 为 _(避免非法字符)。
*/
{{- define "mychart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}{{/*
Common labels
*/}}
/*
作用:定义资源的通用标签(Labels)。
Chart 版本信息(helm.sh/chart)。
选择器标签(selectorLabels,见下文)。
应用版本(app.kubernetes.io/version,如果 Chart.AppVersion 存在)。
管理工具标识(通常是 Helm)。
*/
{{- define "mychart.labels" -}}
helm.sh/chart: {{ include "mychart.chart" . }}
{{ include "mychart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}{{/*
Selector labels
*/}}
{{- define "mychart.selectorLabels" -}}
app.kubernetes.io/name: {{ include "mychart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}{{/*
Create the name of the service account to use
*/}}
{{- define "mychart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "mychart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

语法总结:

{{- define "name" -}}定义一个可复用的命名模板。
{{ include "func" . }}调用其他模板函数。
{{ default A B }}如果 B 为空,则返回 A。
{{ trunc 63 }}截断字符串到 63 字符。
{{ trimSuffix "-" }}删除末尾的短横线。
{{ $var := ... }}定义局部变量。
{{ if contains A B }}检查字符串 B 是否包含 A。
{{ printf "%s-%s" A B }}格式化字符串(类似 Python 的 f"{A}-{B}")。

2.7、values.yaml

作用:配置template中引用的属性对应的属性值的地方。

(1)基本格式语法

values.yaml 只需遵循一些约定和规则,并没有严格固定的格式:

1)遵循yaml语法:键值、缩进、列表都必须遵循标准yaml格式;

2)结构自由:你可以自定义字段名称和层级;注:要和template中的引用对应上。

(2)格式如何确定?

        由模板需求决定:values.yaml 的字段必须与 Chart 模板(templates/*.yaml)中通过 {{ .Values.xxx }} 引用的变量对应。例如:
        模板中引用 {{ .Values.replicaCount }},则 values.yaml 需要定义 replicaCount。

(3)常见的约定

虽然格式自由,但是一般会遵循如下模式。

# 基础配置
replicaCount: 1# 镜像配置
image:repository: nginxtag: "latest"pullPolicy: IfNotPresent# 服务配置
service:type: ClusterIPport: 80# 资源限制
resources:limits:cpu: 100mmemory: 128Mi# 自定义配置(根据应用需求)
config:env: "production"debug: false

2.9、go模板语法小结

语法作用
{{- ... -}}去除渲染后的空白符(左侧 - 删左空白,右侧 - 删右空白)。
.Values.xxx引用 values.yaml 中的字段。
include "func" .调用其他模板(如 _helpers.tpl 中的函数)。
with临时进入某个字段的作用域(如果字段存在)。
range遍历列表或字典。
quote为字符串添加引号(避免 YAML 解析问题)。
toYaml将对象转换为 YAML 格式。
nindent在缩进基础上添加换行符(保持格式对齐)。
$显式引用顶层作用域,避免被 range/with 覆盖。
’default‘设置默认值

2.8、部分语法解释

2.8.1、语法中 . 的含义

在 Helm Chart 的模板文件(如 deployment.yaml 或 ingress.yaml)中,. 是一个非常重要的概念,它代表 当前作用域的上下文对象。对于下面这行:

name: {{ include "mychart.fullname" . }}

1、"."的本质

(1)在helm模板中, . 是一个指向当前作用域数据的对象引用,类似于其他变成语言中的 thisself

(2)它最初指向 顶层对象, 包含:

  • Values:来自 values.yaml 的所有配置
  • Chart:来自 Chart.yaml 的元数据(如 Chart.Name、Chart.Version)
  • Release:部署相关的信息(如 Release.Name、Release.Namespace)
  • Files:访问 Chart 中的文件
  • Capabilities:Kubernetes 集群能力信息

注:.Values 、 .Chart 等就是这么来的!!!!!

2、在 include 函数中的作用

  • include 用于调用其他模板(如 _helpers.tpl 中定义的函数)。
  • 传递 . 的目的是让被调用的模板也能访问完整的上下文。例如:
# _helpers.tpl 中定义的函数
{{- define "mychart.fullname" -}}
{{- .Chart.Name }}-{{ .Release.Name }}
{{- end -}}

这里的 .Chart.Name 和 .Release.Name 就依赖于传入的 . 上下文。 

3. 为什么必须传递 .

如果不传递 .,被调用的模板将无法访问 Helm 的变量系统。例如:

# 错误!没有上下文,无法访问 .Chart.Name
name: {{ include "mychart.fullname" }}
4. 作用域变化时的注意事项

当进入 range 或 with 块时,. 会被临时覆盖,此时可能需要用 $ 访问顶层作用域:

{{- range .Values.pods }}
name: {{ include "mychart.fullname" $ }}  # 用 $ 指向顶层作用域
{{- end }}

2.8.2、helm chart使用的go模板提供的控制语句块梳理
2.8.2.1、if/else 条件块

作用:根据条件判断是否渲染内容。

{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
{{- else if .Values.service.enabled }}
apiVersion: v1
kind: Service
{{- else }}
# 默认内容
{{- end }}
2.8.2.2、with作用域块

作用:如果字段存在的话临时进入某字段的作用域,及那话嵌套字段访问。

{{- with .Values.resources }}
resources:limits:cpu: {{ .limits.cpu }}memory: {{ .limits.memory }}
{{- end }}

上述示例中的  {{ .Values.resources.limits.cpu }} 就等价于 with块内的 {{ .limits.cpu }}。

2.8.2.3、range迭代块

作用:遍历列表或字典。

示例:

--------------列表示例--------------
env:
{{- range .Values.envVars }}- name: {{ .name }}value: {{ .value | quote }}
{{- end }}#对应如下输入:
envVars:- name: DB_HOSTvalue: db.example.com- name: LOG_LEVELvalue: debug---------------字典示例-------------
{{- range $key, $value := .Values.labels }}{{ $key }}: {{ $value | quote }}
{{- end }}
2.8.2.4、define模板定义块

作用:定义可复用的命名模板(通常在 _helpers.tpl 中)。

{{- define "mychart.fullname" -}}
{{- .Chart.Name }}-{{ .Release.Name }}
{{- end -}}# 调用方式
metadata:name: {{ include "mychart.fullname" . }}
2.8.2.5、template模板调用块

作用:调用已定义的模板(旧语法,推荐用 include 代替)。

2.8.2.6、include函数调用块

作用:调用模板并支持管道操作(比 template 更灵活)。

labels:{{- include "mychart.selectorLabels" . | nindent 4 }}
2.8.2.7、indent/nindent 缩进块

作用:控制内容的缩进(非逻辑块,常用于格式化输出)

2.8.2.8、toYaml转换块

作用:将对象转换为yaml格式(常与 indent 配合)

{{- with .Values.affinity }}
affinity:{{- toYaml . | nindent 2 }}
{{- end }}
2.8.2.9、required必填验证块

作用:强制要求字段必须填写,否则报错。

image: {{ required "image.repository is required!" .Values.image.repository }}
2.8.2.10、default默认值块

作用:为变量提供默认值

imageTag: {{ .Values.image.tag | default .Chart.AppVersion }}
2.8.2.11、printf格式化块

作用:格式化字符串输出

{{- printf "%s-%s" .Chart.Name .Release.Name | lower }}
2.8.2.12、 $ 和 .
  • .:当前作用域对象(随块变化)。

  • $:始终指向根作用域(用于突破嵌套块)。

附录: values.yaml 

/*这个就是具体属性值的文件了。
*/
# Default values for mychart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.# This will set the replicaset count more information can be found here: https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/
replicaCount: 1# This sets the container image more information can be found here: https://kubernetes.io/docs/concepts/containers/images/
image:repository: nginx# This sets the pull policy for images.pullPolicy: IfNotPresent# Overrides the image tag whose default is the chart appVersion.tag: ""# This is for the secrets for pulling an image from a private repository more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
imagePullSecrets: []
# This is to override the chart name.
nameOverride: ""
fullnameOverride: ""# This section builds out the service account more information can be found here: https://kubernetes.io/docs/concepts/security/service-accounts/
serviceAccount:# Specifies whether a service account should be createdcreate: true# Automatically mount a ServiceAccount's API credentials?automount: true# Annotations to add to the service accountannotations: {}# The name of the service account to use.# If not set and create is true, a name is generated using the fullname templatename: ""# This is for setting Kubernetes Annotations to a Pod.
# For more information checkout: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
podAnnotations: {}
# This is for setting Kubernetes Labels to a Pod.
# For more information checkout: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
podLabels: {}podSecurityContext: {}# fsGroup: 2000securityContext: {}# capabilities:#   drop:#   - ALL# readOnlyRootFilesystem: true# runAsNonRoot: true# runAsUser: 1000# This is for setting up a service more information can be found here: https://kubernetes.io/docs/concepts/services-networking/service/
service:# This sets the service type more information can be found here: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-typestype: ClusterIP# This sets the ports more information can be found here: https://kubernetes.io/docs/concepts/services-networking/service/#field-spec-portsport: 80# This block is for setting up the ingress for more information can be found here: https://kubernetes.io/docs/concepts/services-networking/ingress/
ingress:enabled: falseclassName: ""annotations: {}# kubernetes.io/ingress.class: nginx# kubernetes.io/tls-acme: "true"hosts:- host: chart-example.localpaths:- path: /pathType: ImplementationSpecifictls: []#  - secretName: chart-example-tls#    hosts:#      - chart-example.localresources: {}# 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# This is to setup the liveness and readiness probes more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
livenessProbe:httpGet:path: /port: http
readinessProbe:httpGet:path: /port: http# This section is for setting up autoscaling more information can be found here: https://kubernetes.io/docs/concepts/workloads/autoscaling/
autoscaling:enabled: falseminReplicas: 1maxReplicas: 100targetCPUUtilizationPercentage: 80# targetMemoryUtilizationPercentage: 80# Additional volumes on the output Deployment definition.
volumes: []
# - name: foo
#   secret:
#     secretName: mysecret
#     optional: false# Additional volumeMounts on the output Deployment definition.
volumeMounts: []
# - name: foo
#   mountPath: "/etc/foo"
#   readOnly: truenodeSelector: {}tolerations: []affinity: {}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/902217.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

display的一些学习记录

收集的SDM的log: 01-01 00:00:15.311 933 933 I SDM : Creating Display HW Composer HAL 01-01 00:00:15.311 933 933 I SDM : Scheduler priority settings completed 01-01 00:00:15.311 933 933 I SDM : Configuring RPC threadpool 0…

【Rust 精进之路之第2篇-初体验】安装、配置与 Hello Cargo:踏出 Rust 开发第一步

系列: Rust 精进之路:构建可靠、高效软件的底层逻辑 **作者:**码觉客 发布日期: 2025-04-20 引言:磨刀不误砍柴工,装备先行! 在上一篇文章中,我们一起探索了 Rust 诞生的缘由&…

【深度学习】计算机视觉(17)——ViT理解与应用

文章目录 Embedding1 概念2 Q&A (1)3 Positional Encoding4 Q&A (2) ViT样例及Embedding可视化理解1 简化ViT练习2 CLS Token3 Embedding可视化4 多头注意力可视化 Embedding技术体系结构参考来源 在研究中对特征的编码和…

肖特基二极管详解:原理、作用、应用与选型要点

一、肖特基二极管的基本定义 肖特基二极管(Schottky Diode) 是一种基于金属-半导体结(肖特基势垒)的二极管,其核心特性是低正向压降(Vf≈0.3V)和超快开关速度。 结构特点:阳极采用金…

DeepSeek在数据仓库的10大应用场景

一、智能数据集成与清洗 多源数据整合:DeepSeek能够从多种数据源中提取、转换和加载数据,实现跨系统数据的高效整合。 数据清洗与标准化:通过智能算法自动识别并纠正数据中的错误、不一致性和缺失值,提升数据质量。 二、数据仓…

提示词构成要素对大语言模型跨模态内容生成质量的影响

提示词构成要素对大语言模型跨模态内容生成质量的影响 提示词清晰度、具象性与质量正相关 限定指向性要素优于引导指向性要素 大语言模型生成内容保真度偏差 以讯飞星火大模型为实验平台,选取100名具备技术素养的人员,从提示词分类、构成要素和实践原则归纳出7种提示词组…

BeautifulSoup 库的使用——python爬虫

文章目录 写在前面python 爬虫BeautifulSoup库是什么BeautifulSoup的安装解析器对比BeautifulSoup的使用BeautifulSoup 库中的4种类获取标签获取指定标签获取标签的的子标签获取标签的的父标签(上行遍历)获取标签的兄弟标签(平行遍历)获取注释根据条件查找标签根据CSS选择器查找…

关于MacOS使用Homebrew的详细介绍

Homebrew 是 macOS(和 Linux)上最流行的包管理工具(Package Manager),用于快速安装、更新和管理各种开发工具、命令行程序、开源软件等。它类似于: Ubuntu/Debian 的 aptCentOS/RHEL 的 yumWindows 的 Cho…

最新扣子空间实操指南

一、首先要先获取到内部测试的邀请码, 我们先打开扣子空间官网:https://space.coze.cn/ 输入邀请码后进入该页面: 它这里支持文件上传,扩展里面有很多插件,页支持MCP各种插件. 探索模式有两种,一种是ai自…

ubuntu22.04安装dukto

1.添加源 sudo add-apt-repository ppa:xuzhen666/dukto2.进行更新和安装 sudo apt update sudo apt install dukto3.报错 $ sudo apt install dukto 正在读取软件包列表... 完成 正在分析软件包的依赖关系树... 完成 正在读取状态信息... 完成 您也许需要…

Java编程基础(第四篇:字符串初次介绍)

前言 HelloWorld写的多了,语法熟悉一点了吧,其中有段代码还没介绍,它就是字符串 public class HelloWorld { public static void main(String[] args) { printBaby(); } static void printBaby() { System.out.print("baby"); } } …

安卓手机怎样配置数据加速

利用系统自带功能: 选择网络模式:进入手机 “设置”,找到 “网络” 或 “移动网络” 选项,点击 “高级设置”,选择合适的网络模式,如优先选择 4G 或 5G 网络,以获得更快的速度。开启网络加速功能…

Day3:个人中心页面布局前端项目uniapp壁纸实战

接下来我们来弄一下个人中心页面布局user.vue <template><view class"userLayout"><view class"userInfo"><view class"avatar"><image src"../../static/Kx.jpg" mode"aspectFill"></im…

线性回归之正则化(regularization)

文章目录 机器学习中的"防过拟合神器"&#xff1a;正则化全解析1. 正则化&#xff1a;不只是"规矩"那么简单1.1 鲁棒性案例说明 2. L1正则化&#xff1a;冷酷的特征选择器3. L2正则化&#xff1a;温柔的约束者4. L1 vs L2&#xff1a;兄弟间的较量5. 正则化…

mapbox基础,加载视频到地图

👨‍⚕️ 主页: gis分享者 👨‍⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅! 👨‍⚕️ 收录于专栏:mapbox 从入门到精通 文章目录 一、🍀前言1.1 ☘️mapboxgl.Map 地图对象1.2 ☘️mapboxgl.Map style属性1.3 ☘️raster 栅格图层 api二、🍀加载视频到…

Linux系统的远程终端登录、远程图形桌面访问、 X图形窗口访问

目录 一、配置Ubuntu系统的网络和用户 1、设置虚拟机网络为桥接模式 2.查看当前ip、子网掩码、网关 3.修改配置文件 二、远程终端登录Ubuntu 三、使用XShell远程连接 1、确保SSH服务已启动 2、检查SSH服务状态 3、获取树莓派IP地址 4、Xming安装好之后打开让它在后台…

多模态大语言模型arxiv论文略读(三十一)

From GPT-4 to Gemini and Beyond: Assessing the Landscape of MLLMs on Generalizability, Trustworthiness and Causality through Four Modalities ➡️ 论文标题&#xff1a;From GPT-4 to Gemini and Beyond: Assessing the Landscape of MLLMs on Generalizability, Tr…

基于Matlab求解矩阵电容等效容值

1需求 仿真测试8*10阶举证电容等效容值。 2模型搭建 2.1打开simscape 在打开simulink之后打开simscape库&#xff0c;Simscape库位置如下 2.2搭建模型 在库中寻找需要的元件搭建电路。 2.2.1基本元件 电阻电容电感等基础器件&#xff0c;搭建电路之后需要对其进行幅值&…

【C++】 —— 笔试刷题day_22

一、添加字符 题目解析 这道题&#xff0c;给定两个字符串A和B&#xff0c;字符串A的长度要小于B的长度&#xff1b; 现在我们要对A字符串添加字符&#xff0c;使得A字符串长度等于B字符串的长度&#xff0c;并且要求对应位置的字母尽量相等&#xff0c;然后求出来不相等的字符…

错误: 找不到或无法加载主类 HelloWorld,cmd窗口,java命令,提示

错误: 找不到或无法加载主类 HelloWorld 解决办法 检查classpath是否 .; 开头的