How to insert configuration directories in k8s init container before running main app?

11/19/2021

I'm trying to deploy mongo in Kubernetes, but before I run the mongo itself, it should do some prerequisites in init containers.

This is the list of configMaps

mongo-auth-env       4      16m
mongo-config         1      16m
mongo-config-env     7      16m
mongo-scripts        10     16m

StatefulSet looks like:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
  labels:
    component: mongo
spec:
  selector:
    matchLabels:
      component: mongo
  serviceName: mongo
  replicas: 1
  template:
    metadata:
      labels:
        component: mongo
    spec:
      initContainers:
        - name: mongo-init
          image: curlimages/curl:latest
          volumeMounts:
            - mountPath: /mongodb/mongodb-config.sh
              name: mongo-config
              subPath: mongodb-config.sh
            - mountPath: /mongo/scripts
              name: mongo-scripts
      containers:
        - name: mongo
          image: bitnami/mongodb:latest
          command: [ "/bin/sh", "-c" ]
          args:
          - /scripts/mongo-run.sh
          livenessProbe:
            exec:
              command:
                - '[ -f /data/health.check ] && exit 0 || exit 1'
            failureThreshold: 300
            periodSeconds: 2
            timeoutSeconds: 60
          ports:
            - containerPort: 27017
          imagePullPolicy: Always
          volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
            - name: mongo-scripts
              mountPath: /mongo/scripts
          env:
            - name: MONGO_USER_APP_NAME
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER_APP_NAME
                  name: mongo-auth-env
            - name: MONGO_USER_APP_PASSWORD
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER_APP_PASSWORD
                  name: mongo-auth-env
            - name: MONGO_INITDB_ROOT_USERNAME
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER_ROOT_NAME
                  name: mongo-auth-env
            - name: MONGO_INITDB_ROOT_PASSWORD
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER_ROOT_PASSWORD
                  name: mongo-auth-env
            - name: MONGO_WIREDTIGER_CACHE_SIZE
              valueFrom:
                configMapKeyRef:
                  key: MONGO_WIREDTIGER_CACHE_SIZE
                  name: mongo-config-env
      restartPolicy: Always
      volumes:
        - name: mongo-scripts
          configMap:
            name: mongo-scripts
            defaultMode: 0777
        - name: mongo-config
          configMap:
            name: mongo-config
            defaultMode: 0777
  volumeClaimTemplates:
    - metadata:
        name: mongo-persistent-storage
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi

Pod description:

Init Containers:
  mongo-init:
    Container ID:   docker://9a4c20c9b67470af03ee4f60a24eabc428ecafd3875b398ac52a54fe3b2b7b96
    Image:          curlimages/curl:latest
    Image ID:       docker-pullable://curlimages/curl@sha256:d588ff348c251f8e4d1b2053125c34d719a98ff3ef20895c49684b3743995073
    Port:           <none>
    Host Port:      <none>
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    2
      Started:      Fri, 19 Nov 2021 02:04:16 +0100
      Finished:     Fri, 19 Nov 2021 02:04:16 +0100
    Ready:          False
    Restart Count:  8
    Environment:    <none>
    Mounts:
      /mongo/scripts from mongo-scripts (rw)
      /mongodb/mongodb-config.sh from mongo-config (rw,path="mongodb-config.sh")
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-9jwkz (ro)
Containers:
  mongo:
    Container ID:  
    Image:         mongo:4.2.12-bionic
    Image ID:      
    Port:          27017/TCP
    Host Port:     0/TCP
    Command:
      /bin/sh
      -c
    Args:
      /scripts/mongo-run.sh
    State:          Waiting
      Reason:       PodInitializing
    Ready:          False
    Restart Count:  0
    Liveness:       exec [[ -f /data/health.check ] && exit 0 || exit 1] delay=0s timeout=60s period=2s #success=1 #failure=300
    Environment:
      MONGO_USER_APP_NAME:          <set to the key 'MONGO_USER_APP_NAME' of config map 'mongo-auth-env'>            Optional: false
      MONGO_USER_APP_PASSWORD:      <set to the key 'MONGO_USER_APP_PASSWORD' of config map 'mongo-auth-env'>        Optional: false
      MONGO_INITDB_ROOT_USERNAME:   <set to the key 'MONGO_USER_ROOT_NAME' of config map 'mongo-auth-env'>           Optional: false
      MONGO_INITDB_ROOT_PASSWORD:   <set to the key 'MONGO_USER_ROOT_PASSWORD' of config map 'mongo-auth-env'>       Optional: false
      MONGO_WIREDTIGER_CACHE_SIZE:  <set to the key 'MONGO_WIREDTIGER_CACHE_SIZE' of config map 'mongo-config-env'>  Optional: false
    Mounts:
      /data/db from mongo-persistent-storage (rw)
      /mongo/scripts from mongo-scripts (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-9jwkz (ro)
Conditions:
  Type              Status
  Initialized       False 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  mongo-persistent-storage:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mongo-persistent-storage-mongo-0
    ReadOnly:   false
  mongo-scripts:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      mongo-scripts
    Optional:  false
  mongo-config:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      mongo-config
    Optional:  false

After all files/folders are mounted, there is a run.sh script in the scripts directory which should spin things up, but I can't get to that part because the init container is crashing. Is there any other way of doing this, I tried with Jobs but without success. Am I missing something obvious again, because I can see configMaps in the description, but unfortunately can't get logs because Pod is always initializing, and can't exec into the container because of the same reason? I followed some ideas from this repo. Any input would be appreciated.

-- dejanmarich
kubernetes
mongodb

1 Answer

11/26/2021

To clarify I am posting Community Wiki answer.

The problem here was small mistake - incorrect path (/scripts/mongo-run.sh) - the correct one: /mongodb/scripts/mongo-run.sh

@dejanmarich: correct path s /mongodb/scripts/mongo-run.sh, i just missed that, fixed now

-- kkopczak
Source: StackOverflow