?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模板中, . 是一個指向當前作用域數據的對象引用,類似于其他變成語言中的 this 或 self。
(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: {}