部署文件中的 Helm chart volumes 和 volumeMounts

Helm chart volumes and volumeMounts in deployment file

我无法让我的图表使用我的 volumesvolumeMounts 值。在我的 values.yaml 文件中,我有这样的内容:

volumes:
- name: docker1
  hostPath:
    path: /var/
- name: docker2
  hostPath:
    path: /usr/
- name: docker3
  hostPath:
    path: /opt/
    
volumeMounts:
- name: docker1
  mountPath: /var/
- name: docker2
  mountPath: /usr/
- name: docker3
  mountPath: /opt/

在我的 _deployment.tpl 文件中,我有这样的内容:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "fullname" . }}
  namespace: {{ .Values.namespace }}
  labels:
    {{- include "labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  revisionHistoryLimit: {{ .Values.revisionHistory | default 2 }}
  selector:
    matchLabels:
      {{- include "selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- toYaml .Values.podAnnotations | nindent 8 }}
      labels:
        {{- include "labels" . | nindent 8 }}
    spec:
      imagePullSecrets:
        {{- toYaml .Values.imagePullSecrets | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
              {{- toYaml .Values.image.ports | nindent 10 }}
          env:
              {{- toYaml .Values.env | nindent 10 }}
          volumeMounts:
            - name: {{- toYaml .Values.volumeMounts | default "" | nindent 10 }} 
          volumes:
            - name: {{- toYaml .Values.volumes | default "" | nindent 10 }}     
      nodeSelector:
        {{- toYaml .Values.nodeSelector | nindent 8 }}
      tolerations:
        {{- toYaml .Values.tolerations | nindent 8 }}
{{- end }}
 

我尝试以与使用环境变量相同的方式挂载卷和 volumeMounts (它们有效),但遗憾的是它不起作用。

您的代码缩进有问题。

卷的缩进级别应与容器相同。

如下:

    spec:
      imagePullSecrets:
        {{- toYaml .Values.imagePullSecrets | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            {{- toYaml .Values.image.ports | nindent 12 }}
          env:
            {{- toYaml .Values.env | nindent 12 }}
          volumeMounts:
            {{- toYaml .Values.volumeMounts | default "" | nindent 12 }} 
      volumes:
        {{- toYaml .Values.volumes | default "" | nindent 8 }}     
      nodeSelector:
        {{- toYaml .Values.nodeSelector | nindent 8 }}
      tolerations:
        {{- toYaml .Values.tolerations | nindent 8 }}

如果要调试模板,可以参考官方helm文档操作

helm debug