0

Using helm to create my own chart to deploy an instance of mongodb on kubernetes cluster, I am using a custom configmap for having my own mongo configuration.

My config map is looking like this:

{{- if .Values.configMap.enabled -}}
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/name: {{ .Values.fullnameOverride }}
    helm.sh/chart: {{ include "mongodb.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}-{{ .Values.nameOverride }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}-{{ .Values.nameOverride }}
  name: mongod-conf
data:
  mongod.conf: |-
    systemLog:
      destination: file
      path: "/var/log/mongodb/mongod.log"
      logAppend: true
    storage:
      journal:
        enabled: true
      dbPath: "/data/db"
      wiredTiger:
        collectionConfig:
          blockCompressor: none
    net:
       bindIp: 0.0.0.0
       bindIpAll: true
       port: {{ .Values.service.port }}
{{- end -}}

So I apply this configuration in my deployment.yaml file like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.nameOverride}}
  labels:
    app.kubernetes.io/name: {{ .Values.fullnameOverride }}
    helm.sh/chart: {{ include "mongodb.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}-{{ .Values.nameOverride }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}-{{ .Values.nameOverride }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ .Values.fullnameOverride }}
      app.kubernetes.io/instance: {{ .Release.Name }}-{{ .Values.nameOverride }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ .Values.fullnameOverride }}
        app.kubernetes.io/instance: {{ .Release.Name }}-{{ .Values.nameOverride }}
    spec:
      containers:
        - name: {{ .Values.nameOverride }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: {{ .Values.image.command }}
          args: {{ .Values.image.args }}
          env:
          - name: MONGO_INITDB_ROOT_USERNAME
            valueFrom:
              secretKeyRef:
                name: mongo-admin
                key: username
          - name: MONGO_INITDB_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mongo-admin
                key: password
          volumeMounts:
          - name: {{ .Values.configMap.volumeName }}
            mountPath: {{ .Values.configMap.mountPath }}
            subPath:  {{ .Values.configMap.subPath }}
          - name: {{ .Values.volumeMounts.name }}
            mountPath: {{ .Values.volumeMounts.mountPath }}
          ports:
            - name: http
              containerPort: {{ .Values.image.containerPort }}
              protocol: TCP
          resources:
{{ toYaml .Values.resources | indent 12 }}
      volumes:
        - name: {{ .Values.namespaceVolumeClaim.name }}
          persistentVolumeClaim:
            claimName: {{ .Values.namespaceVolumeClaim.claimName }}
        - name: {{ .Values.configMap.volumeName }}
          configMap:
            name: {{ .Values.configMap.name }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}

I have set the configmap like this in my values.yaml file:

configMap:
  enabled: true
  name: mongod-conf
  mountPath: /etc/mongod.conf
  subPath: mongod.conf
  volumeName: config-volume

Using helm command helm install --name mongodb mongodb -f mongodb/values.yaml --namespace test, all is working well and the chart is deployed correctly, so no error occured in this step.

You can see that in my deployment, I use a command and args in order to launch an instance of mongodb in the pod e-g:

containers:
  - name: {{ .Values.nameOverride }}
    image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
    imagePullPolicy: {{ .Values.image.pullPolicy }}
    command: {{ .Values.image.command }}
    args: {{ .Values.image.args }}

In my values.yaml file, this part looks like this:

image:
  repository: mongo
  tag: 3.6
  pullPolicy: IfNotPresent
  containerPort: 27017
  command: ["mongod"]
  args: ["-f /etc/mongod.conf --auth"]

Finally, when the pod try to start the instance of mongodb docker image, an error of type CrashLoopBackOff events occured. Looking for the logs mongo returns taht it can't find the file in path /etc/mongod.conf. But if I remove the args -f /etc/mongod.conf, all is working well, and in the container I can find the configuration file. And if I run the command mongo -f /etc/mongod.conf --auth no error occured, so it's not a problem from a configuration file. The purpose here is to set up the configuration when the pod is deployed and launch the mongodb docker image. Not to set up manually in the container itself.

What I have missed in this process in order to apply automatically the /etc/mongod.conf file when the pod is deployed and launch mongodb ?

  • I would wrap the commandine that starts Mongo into a script /usr/local/bin/launch.sh then in that script spin waiting for the file to be at /etc/mongo.conf before launching mongo. this idea being that we don't want to bake the inode of any dynamic files and folders into the launched cmd. rather we have it launch a script that doesn't move in the container or get late mounted onto the container at runtime. the script will then cause late resolution of the path to the inode of the mounted configuration. – simbo1905 Mar 26 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.