<p>I want to create a replica set of MongoDB pods and after pods are in running state, I want to create a collection on every mongo db instance. Here is the code:</p>
<pre><code>metadata:
  name: mongodb-standalone
spec:
  replicas: 3
  selector:
    matchLabels:
      app: database
  template:
    metadata:
      labels:
        app: database
        selector: mongodb-standalone
    spec:
      containers:
      - name: mongodb-standalone
        image: mongo:4.0.8
        lifecycle:
           postStart:
             exec:
               command: [&quot;mongo --eval 'db.createCollection(\&quot;Profile\&quot;);' test&quot;]
</code></pre>
<p>Still this code is not working.</p>

<p>you can use configmap and mount the db creation script to init</p>
<blockquote>
<p>When a container is started for the first time it will execute files with extensions .sh and .js that are found in <code>/docker-entrypoint-initdb.d.</code> Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.</p>
</blockquote>
<p>create file <code>create_db.js</code></p>
<pre><code>db.createCollection(&quot;user&quot;)
db.createCollection(&quot;movies&quot;)
db.user.insert({name: &quot;Ada Lovelace&quot;, age: 205})
db.movies.insertMany( [
    {
       title: 'Titanic',
       year: 1997,
       genres: [ 'Drama', 'Romance' ]
    },
    {
       title: 'Spirited Away',
       year: 2001,
       genres: [ 'Animation', 'Adventure', 'Family' ]
    },
    {
       title: 'Casablanca',
       genres: [ 'Drama', 'Romance', 'War' ]
    }
 ] )
</code></pre>
<p>create configmap</p>
<pre><code>kubectl create configmap create-db-configmap --from-file=./create_db.js
</code></pre>
<p>now we are all set, create deployment and check the magic</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mongo
  name: mongo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mongo
    spec:
      containers:
      - image: mongo
        name: mongo
        args: [&quot;--dbpath&quot;,&quot;/data/db&quot;]
        livenessProbe:
          exec:
            command:
              - mongo
              - --disableImplicitSessions
              - --eval
              - &quot;db.adminCommand('ping')&quot;
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        readinessProbe:
          exec:
            command:
              - mongo
              - --disableImplicitSessions
              - --eval
              - &quot;db.adminCommand('ping')&quot;
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        env:
        - name: MONGO_INITDB_DATABASE
          value: demodb
        - name: MONGO_INITDB_ROOT_USERNAME
          value: &quot;root&quot;
        - name: MONGO_INITDB_ROOT_PASSWORD
          value: &quot;password&quot;
        volumeMounts:
        - name: &quot;mongo-data-dir&quot;
          mountPath: &quot;/data/db&quot;
        - name: &quot;init-database&quot;
          mountPath: &quot;/docker-entrypoint-initdb.d/&quot;
      volumes:
      - name: &quot;mongo-data-dir&quot;
      - name: &quot;init-database&quot;
        configMap:
          name: create-db-configmap


</code></pre>
<p>you can find complete example <a href="https://github.com/Adiii717/kubernetes-mongo-db-init" rel="nofollow noreferrer">here</a></p>
<p><a href="https://i.stack.imgur.com/GHeFR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GHeFR.png" alt="enter image description here" /></a></p>
