How to create collection after pod initialize mongodb kubernetes

Viewed 47

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:

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: ["mongo --eval 'db.createCollection(\"Profile\");' test"]

Still this code is not working.

1 Answers

you can use configmap and mount the db creation script to init

When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. 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.

create file create_db.js

db.createCollection("user")
db.createCollection("movies")
db.user.insert({name: "Ada Lovelace", 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' ]
    }
 ] )

create configmap

kubectl create configmap create-db-configmap --from-file=./create_db.js

now we are all set, create deployment and check the magic

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: ["--dbpath","/data/db"]
        livenessProbe:
          exec:
            command:
              - mongo
              - --disableImplicitSessions
              - --eval
              - "db.adminCommand('ping')"
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        readinessProbe:
          exec:
            command:
              - mongo
              - --disableImplicitSessions
              - --eval
              - "db.adminCommand('ping')"
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        env:
        - name: MONGO_INITDB_DATABASE
          value: demodb
        - name: MONGO_INITDB_ROOT_USERNAME
          value: "root"
        - name: MONGO_INITDB_ROOT_PASSWORD
          value: "password"
        volumeMounts:
        - name: "mongo-data-dir"
          mountPath: "/data/db"
        - name: "init-database"
          mountPath: "/docker-entrypoint-initdb.d/"
      volumes:
      - name: "mongo-data-dir"
      - name: "init-database"
        configMap:
          name: create-db-configmap


you can find complete example here

enter image description here

Related