当前位置: 首页 > news >正文

【2】k8s集群管理系列--包应用管理器之helm(Chart语法深入应用)

一、Chart模板:函数与管道

常用函数:
• quote:将值转换为字符串,即加双引号
• default:设置默认值,如果获取的值为空则为默认值
• indent和nindent:缩进字符串
• toYaml:引用一块YAML内容
• 其他函数:upper(转换为大写)、title(只首字母大写)等

1.1 quote:将值转换为字符串,即加双引号

示例:nodeSelector标签的值用了true正常使用会报错,这是因为它是关键字,需要加引号才可以。

# values.yaml
nodeSelector:
gpu: true
# templates/deployment.yamlnodeSelector:
disktype: {{ quote .Values.nodeSelector.gpu }}

输出效果:

# templates/deployment.yamlnodeSelector:
disktype: "true"

1.2 default:设置默认值,如果获取的值为空则为默认值

示例:以防止忘记定义而导致模板文件缺少字段无法创建资源,这时可以为字段定义一个默认值。

image: {{ .Values.image.repository }}:{{ .Values.image.tag | default "latest" }}

这里用到了管道符“|”,前面的值传递后函数验证是否为空。
假如.Values.image.tag这个变量值为空,输出效果就如下:

image: nginx:latest

1.3 indent和nindent函数

indent和nindent函数都是缩进字符串,主要区别在于nindent会在缩进前多添加一个换行符。
示例:

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: {{ .Release.Name | indent 6 }}
app: {{ .Release.Name | nindent 6 }}
...

后面的数字6,标识往后缩进6个字符
示例效果:

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web-ng
app: 
     web-ng
...

1.4 toYaml:引用一块YAML内容

示例:在values.yaml里写结构化数据,引用内容块

# values.yaml
resources:
   limits:
    cpu: 100m
    memory: 128Mi
requests:
  cpu: 100m
  memory: 128Mi

deployment.yaml引用values.yaml的变量值,并换行缩进10个字符

# templates/deployment.yaml
...
resources:
{{ toYaml .Values.resources | nindent 10 }}

deployment.yaml输出的效果:

# templates/deployment.yaml
...
resources:

         limits:
           cpu: 100m
           memory: 128Mi
        requests:
          cpu: 100m
          memory: 128Mi

二、Chart模板:流程控制

Helm模板语言提供以下流程控制语句:
• if/else:条件判断
• range:循环
• with:指定变量作用域

2.1 流程控制之if/else

# values.yaml
ingress:
enabled: false
# templates/ingress.yaml
{{ if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web
spec:
rules:
- host: www.ctnrs.com
http:
  paths:
  - path: /
  pathType: Prefix
  backend:
    service:
     name: web
     port:
      number: 80
{{ end }}

测试:helm install test --set ingress.enabled=true --dry-run mychart,效果为:不填充if包裹的内容

2.2 流程控制之range

# cat values.yaml
test:
- 1
- 2
- 3
# templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}
data:
test: |
{{- range .Values.test }}
{{ . }} # 引用当前元素
{{- end }}

输出效果:

# templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
 name: web-ng
data:
 test: 
   - 1
   - 2
   - 3

2.3 流程控制之with

with语句可以允许将当前范围 . 设置为特定的对象,比如我们前面一直使用
的 .Values.nodeSelecotr,我们可以使用 with来将 . 范围指向 .Values.nodeSelecotr

# cat values.yaml
...
nodeSelector:
  team: a
  gpu: yes
# cat templates/deployment.yaml
...
{{- with .Values.nodeSelector }}
nodeSelector:
team: {{ .team }}
gpu: {{ .gpu }}
{{- end }}

填充后的效果:

# templates/deployment.yaml
...
nodeSelector:
team: a
gpu: yes

三、Chart模板:命名模板

命名模板类似于开发语言中的函数。指一段可以直接被另一段程序或代码引用的程序或代码。
在编写chart时,可以将一些重复使用的内容写在命名模板文件中供公共使用,这样可减少重
复编写程序段和简化代码结构。
命名模块使用define定义,template或include引入,在templates目录中默认下划线开头的
文件为公共模板(helpers.tpl)。

定义模板:
示例:资源名称生成指令放到公共模板文件,作为所有资源名称

# cat templates/_helpers.tpl 注意是在_helpers.tpl文件写模板
{{- define "fullname" -}}
{{- .Chart.Name -}}-{{ .Release.Name }}
{{- end -}}
# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ template "fullname" . }}
...

使用模板:
template指令是将一个模板包含在另一个模板中的方法。但是,template函
数不能用于Go模板管道。为了解决该问题,引入include指令。(平时我们用include就行)

示例:
1 定义:

# cat _helpers.tpl
{{- define "labels" -}}
app: {{ template "fullname" . }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
release: "{{ .Release.Name }}"
{{- end -}}

2 使用:

# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "fullname" . }}
labels:
{{- include "labels" . | nindent 4 }}

...

文章转载自:

http://YGKjAav2.wyctq.cn
http://gmU7cIWp.wyctq.cn
http://FNcpu9SB.wyctq.cn
http://psoKd0TP.wyctq.cn
http://d9rHj2zh.wyctq.cn
http://7UeOhasP.wyctq.cn
http://HtuJEZox.wyctq.cn
http://9EhhGqYD.wyctq.cn
http://JaRsYXwi.wyctq.cn
http://ZUQ8Pl2a.wyctq.cn
http://lTABnDMl.wyctq.cn
http://pN7mTgQi.wyctq.cn
http://1DzPhvWW.wyctq.cn
http://MI68TN9E.wyctq.cn
http://ndhRIOL4.wyctq.cn
http://uAeXOXFo.wyctq.cn
http://ijnQ6yP8.wyctq.cn
http://nZCoAs1P.wyctq.cn
http://42GQXiI5.wyctq.cn
http://gJc1hBlz.wyctq.cn
http://YzxzO7pE.wyctq.cn
http://6g02mvu3.wyctq.cn
http://iiyaiGci.wyctq.cn
http://X0GDBEPm.wyctq.cn
http://v86t0ic0.wyctq.cn
http://tgmz8R2d.wyctq.cn
http://9ZoKKxLz.wyctq.cn
http://JYN1DjNd.wyctq.cn
http://HjNvpk96.wyctq.cn
http://tjeJRSva.wyctq.cn
http://www.dtcms.com/a/128595.html

相关文章:

  • 149页研读——华为基于IPD全过程研发质量管理【附全文阅读】
  • Echarts基础入门
  • web自动化测试
  • .net Core 和 .net freamwork 调用 deepseek api 使用流输出文本(对话补全)
  • 如何在多线程中安全地使用 PyAudio
  • Python中字符串分隔与连接函数
  • 客户端负载均衡与服务器端负载均衡详解
  • qt pyqt5的开发, 修改psd图像
  • Python(11)Python判断语句全面解析:从基础到高级模式匹配
  • simpy仿真
  • 基于51单片机的温度报警器proteus仿真
  • Matlab绘制函数方程图形
  • 操作系统学习2025.04.02-2025.04.08
  • 设计模式 --- 策略模式
  • 卒/兵过河前的判断和走法触发器优化
  • PyTorch核心函数详解:gather与where的实战指南
  • FISCO BCOS区块链Postman接口测试:高级应用与实战技巧 [特殊字符]
  • 达梦数据校验系统(DMDVS):数据完整性保障的不二之选
  • 项目管理(高软56)
  • Transformer揭秘:革新人工智能的突破性架构
  • AI大模型:(二)2.2 分词器Tokenizer
  • comfyui点击执行没反应一例
  • 哪些人适合考城市客运安全员证?
  • React 获得dom节点和组件通信
  • 辅助记忆数字和唱名的小工具【仅PC端】
  • 基于 Redis 实现一套动态配置中心 DCC 服务与反射基础知识讲解
  • 【SpringBoot Druid Mysql多数据源整合】
  • mindsdb AI 开源的查询引擎 - 用于构建 AI 的平台,该平台可以学习和回答大规模联合数据的问题。
  • 海洋大地测量基准与水下导航系列之八我国海洋水下定位装备发展现状
  • Doris数据库建表语法以及分区分桶简介