Search code examples
devopskubernetes-helm

Helm chart volumes and volumeMounts in deployment file


I can't make my chart use my volumes, and volumeMounts values. In my values.yaml file I have something like this:

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/

In my _deployment.tpl file I have something like this:

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 }}
 

I tried to mount volumes and volumeMounts the same way I do with env variables (they work) but sadly it doesn't work.


Solution

  • There is a problem with the indentation of your code.

    Volumes should be at the same indentation level as containers.

    As follow:

        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 }}
    

    If you want to debug the template, you can refer to the official helm document operation.

    helm debug