Title

helm常用函数

1、quote 强制转换字符串 " " , squote 强制转换成字符串 ' '

# 示例
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name }}
      version: {{ .Values.app.version }}            # 添加一个version的标签
  template:
    metadata:
      labels:
        app: {{ .Chart.Name }}
        version: {{ .Values.app.version }}          # 添加一个version的标签
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        name: nginx

[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
  repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
  tag: aaa

nodePort: 30000
app:                     # 添加标签
  version: 1

[root@master-11 mychart]# helm install nginx .                  # 这里安装提示需要字符串类型的标签
Error: INSTALLATION FAILED: Deployment in version "v1" cannot be handled as a Deployment: json: cannot unmarshal number into Go struct field LabelSelector.spec.selector.matchLabels of type string


# 使用quote转换
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name }}
      version: {{ quote .Values.app.version }}            # 在前面添加quote
  template:
    metadata:
      labels:
        app: {{ .Chart.Name }}
        version: {{ quote .Values.app.version }}          # # 在前面添加quote
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        name: nginx

# 再次安装
[root@master-11 mychart]# helm uninstall nginx                
release "nginx" uninstalled
[root@master-11 mychart]# helm install nginx .
NAME: nginx
LAST DEPLOYED: Sat Oct  4 00:09:58 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
国亲节快乐
国亲节快乐
国亲节快乐
国亲节快乐
国亲节快乐
国亲节快乐
国亲节快乐


[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mychart-svc
  labels:
    app: mychart
spec:
  type: NodePort
  selector:
    app: mychart
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30000
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mychart
  name: mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mychart
      version: "1"         # 这个地方可以看见多了一个双引号,
  template:
    metadata:
      labels:
        app: mychart
        version: "1"
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:aaa"
        name: nginx

# squote和quote的区别就是squote是转换为'',示例
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
  repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
  tag: aaa

nodePort: 30000
app:
  version: 1
enable: true                  # 添加
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name }}
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}                # 添加squote
  template:
    metadata:
      labels:
        app: {{ .Chart.Name }}
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}              # 添加squote
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        name: nginx

[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mychart-svc
  labels:
    app: mychart
spec:
  type: NodePort
  selector:
    app: mychart
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30000
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mychart
  name: mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mychart
      version: "1"
      enable: 'true'                         # 变成了单引号
  template:
    metadata:
      labels:
        app: mychart
        version: "1"
        enable: 'true'                       # 变成了单引号
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:aaa"
        name: nginx

2、default默认值

# 示例
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name }}
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name }}
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "1" }}"            # 在这个地方添加 | default "1"表示在.Values.image.tag不存在时将默认值设置为1
        name: nginx

# 将tag注释掉
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
  repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
    #  tag: aaa

nodePort: 30000
app:
  version: 1
enable: true

[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mychart-svc
  labels:
    app: mychart
spec:
  type: NodePort
  selector:
    app: mychart
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30000
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mychart
  name: mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mychart
      version: "1"
      enable: 'true'
  template:
    metadata:
      labels:
        app: mychart
        version: "1"
        enable: 'true'
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:1"                    # 将tag注释掉以后使用了默认值"1"
        name: nginx

3、upper,title,lower大小写转换

# upper将全部字母大写,title将首字母大写,lower全部小写,示例
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name | upper }}               # 在后面添加| upper表示将所有的字母大写
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name | upper }}             # 在后面添加| upper表示将所有的字母大写
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "1" }}"
        name: nginx

[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mychart-svc
  labels:
    app: mychart
spec:
  type: NodePort
  selector:
    app: mychart
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30000
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mychart
  name: mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MYCHART               # 这个地方成功的变成了大写
      version: "1"
      enable: 'true'
  template:
    metadata:
      labels:
        app: MYCHART             # 这个地方成功的变成了大写
        version: "1"
        enable: 'true'
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:1"
        name: nginx

4、indent,nindent缩进与换行缩进

# 效果展示
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name | indent 5 }}          # 在后面添加| indent 5表示缩进5个字符 
  name: {{ .Chart.Name | nindent 5 }}          # 在后面添加| nindent 5表示换行缩进5个字符 
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name | upper }}
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name | upper }}
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "1" }}"
        name: nginx

[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app:      mychart                           # 安装后这前面多了5个空格
  name:                                         # 这个地方放了一行并多了5个空格
     mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MYCHART
      version: "1"
      enable: 'true'
  template:
    metadata:
      labels:
        app: MYCHART
        version: "1"
        enable: 'true'
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:1"
        name: nginx

5、toYaml拷贝yaml块

# 示例
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
  repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
    #  tag: aaa

    #nodePort: 30000
app:
  version: 1
enable: true
resources:                        # 在values字段中将resources下面的字段全部添加上
  requests:
    cpu: 1
    memory: 1Gi
  limits:
    cpu: 2
    memory: 2Gi

[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name | upper }}
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name | upper }}
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "1" }}"
        name: nginx
        resources: {{ toYaml .Values.resources | nindent 10 }}         # toYaml可以将values下的resources字段下的值全部引用,就像下面注释的效果一样,如果ninden不加上的话则requests就会跑到resources的同一行,所以需要给requests换一行再进行缩进
      # resources:                                       
        # requests:
        #   cpu: 1
        #   memory: 1Gi
        # limits:
        #   cpu: 2
        #   memory: 2Gi

[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mychart
  name: mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MYCHART
      version: "1"
      enable: 'true'
  template:
    metadata:
      labels:
        app: MYCHART
        version: "1"
        enable: 'true'
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:1"
        name: nginx
        resources:                          # 此时resources的字段下的内容就都被打印出来了,可以在values中随意修改cpu与内存的值
          limits:
            cpu: 2
            memory: 2Gi
          requests:
            cpu: 1
            memory: 1Gi
      # resources:
        # requests:
        #   cpu: 1
        #   memory: 1Gi
        # limits:
        #   cpu: 2
        #   memory: 2Gi

6、printf格式化输出

# 示例
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name | upper }}
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}
      chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version) }}              # 使用printf将后面的两个参数以"%s字符串-%s字符串"进行打印
  template:
    metadata:
      labels:
        app: {{ .Chart.Name | upper }}
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}
        chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version) }}            # 使用printf将后面的两个参数以"%s字符串-%s字符串"进行打印
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "1" }}"
        name: nginx
        resources: {{ toYaml .Values.resources | nindent 10 }}

[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mychart
  name: mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MYCHART
      version: "1"
      enable: 'true'
      chart: 'mychart-0.1.0'        # 两个值成功以字符串的形式被拼接在一起
  template:
    metadata:
      labels:
        app: MYCHART
        version: "1"
        enable: 'true'
        chart: 'mychart-0.1.0'      # 两个值成功以字符串的形式被拼接在一起
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:1"
        name: nginx
        resources:
          limits:
            cpu: 2
            memory: 2Gi
          requests:
            cpu: 1
            memory: 1Gi

7、replace "A" "B"替换

# 示例
[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name | upper }}
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}
      chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}   # 在后面添加| replace "-" "_" 表示将"-"替换成"_"
  template:
    metadata:
      labels:
        app: {{ .Chart.Name | upper }}
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}
        chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}  # 在后面添加| replace "-" "_" 表示将"-"替换成"_"
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "1" }}"
        name: nginx
        resources: {{ toYaml .Values.resources | nindent 10 }}

[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mychart
  name: mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MYCHART
      version: "1"
      enable: 'true'
      chart: 'mychart_0.1.0'
  template:
    metadata:
      labels:
        app: MYCHART
        version: "1"
        enable: 'true'
        chart: 'mychart_0.1.0'
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:1"
        name: nginx
        resources:
          limits:
            cpu: 2
            memory: 2Gi
          requests:
            cpu: 1
            memory: 1Gi

8、trunc将字符缩短到指定长度,trimSuffix删除指定后缀,trimPrifix删除指定前缀

# 示例
[root@master-11 mychart]# cat values.yaml
replicas: 1
image:
  repository: registry.cn-beijing.aliyuncs.com/xwk123/nginx
    #  tag: aaa

    #nodePort: 30000
app:
  version: 1
enable: true
resources:
  requests:
    cpu: 1
    memory: 1Gi
  limits:
    cpu: 2
    memory: 2Gi

address:                 # 将示例字段添加进去
  a: beijing
  b: shanghai
  c: guangzhou

[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name | upper }}
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}
      chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name | upper }}
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}
        chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "v1" }}"
        name: nginx
        resources: {{ toYaml .Values.resources | nindent 10 }}
        env:
          - name: address_a
            value: {{ squote ( .Values.address.a | trunc 3 ) }}        # 将地址a的字段裁剪到只剩3个字符
          - name: address_b
            value: {{ squote ( .Values.address.b | trimSuffix "hai" ) }}      # 剔除结尾的hai
          - name: address_c
            value: {{ squote ( .Values.address.c | trimPrefix "guang" ) }}    # 剔除开头的guang

[root@master-11 mychart]# helm template nginx .
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mychart
  name: mychart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MYCHART
      version: "1"
      enable: 'true'
      chart: 'mychart_0.1.0'
  template:
    metadata:
      labels:
        app: MYCHART
        version: "1"
        enable: 'true'
        chart: 'mychart_0.1.0'
    spec:
      containers:
      - image: "registry.cn-beijing.aliyuncs.com/xwk123/nginx:v1"
        name: nginx
        resources:
          limits:
            cpu: 2
            memory: 2Gi
          requests:
            cpu: 1
            memory: 1Gi
        env:
          - name: address_a
            value: 'bei'
          - name: address_b
            value: 'shang'
          - name: address_c
            value: 'zhou'

9、namespace优先级

[root@master-11 mychart]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Chart.Name }}
  name: {{ .Chart.Name }}
  namespace: kube-system                    # 指定命名空间kube-system,安装时再次指定不一样的命名空间
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Chart.Name | upper }}
      version: {{ quote .Values.app.version }}
      enable: {{ squote .Values.enable }}
      chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name | upper }}
        version: {{ quote .Values.app.version }}
        enable: {{ squote .Values.enable }}
        chart: {{ squote (printf "%s-%s" .Chart.Name .Chart.Version | replace "-" "_" ) }}
    spec:
      containers:
      - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "v1" }}"
        name: nginx
        resources: {{ toYaml .Values.resources | nindent 10 }}
        env:
          - name: address_a
            value: {{ squote ( .Values.address.a | trunc 3 ) }}
          - name: address_b
            value: {{ squote ( .Values.address.b | trimSuffix "hai" ) }}
          - name: address_c
            value: {{ squote ( .Values.address.c | trimPrefix "guang" ) }}

# 指定default命名空间
[root@master-11 mychart]# helm install nginx . -n default
NAME: nginx
LAST DEPLOYED: Sun Oct  5 00:49:44 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
国亲节快乐
国亲节快乐
国亲节快乐
国亲节快乐
国亲节快乐
国亲节快乐
国亲节快乐
[root@master-11 mychart]# kubectl get pods                     # 此时default没有需要的资源
NAME        READY   STATUS             RESTARTS      AGE
[root@master-11 mychart]# kubectl get pods -n kube-system | grep mychart          # 出现在了kube-system命名空间下
mychart-5f5c8df96f-x8lzr             1/1     Running   0              117s

# 由此可见当deployment中写了namespace后,helm -n就没有啥意义了
posted @ 2025-10-08 14:12  xwk123  阅读(6)  评论(0)    收藏  举报