How to include data with mongo image in kubernetes

10/21/2017

I am trying to get my app running on minikube, reachable at the minikube ip address. Below are the commands I am using to get it running. It all works as I want it to (if I try to log in, it returns the expected 401 since the mongo DB is empty).

I would like to actually include a database in my app with, say, an existing user. I cannot find a clear way with how to include data (say in a tar file that works with mongo) with what I am doing. How can I accomplish this?

### Get mongo up
docker pull mongo:3.2
kubectl run mongo --image=mongo:3.2 --port=27017
kubectl expose deployment mongo

### Get API up
cd /api
docker build -t api:v1 .
kubectl run api --image=api:v1 --port=3000 --env="MONGODB_URI=mongodb://mongo:27017/myapp-dev"
kubectl apply -f api-deployment.yml
kubectl expose deployment api --type=LoadBalancer

### Get dashboard up
docker build -t myapp-dashboard:v1 .
kubectl apply -f ./tooling/minikube/myapp-dashboard-qa-config-map.yml
kubectl apply -f ./tooling/minikube/myapp-dashboard-deployment.yml
kubectl expose deployment myapp-dashboard --type=LoadBalancer

### Ingress setup
minikube addons enable ingress
kubectl create -f ingress.yml
kubectl apply -f ./tooling/minikube/ingress.yml
-- swagrov
docker
kubernetes
minikube
mongodb

1 Answer

10/21/2017

The nice way of doing it making use of the initdb infrastructure in the mongo image. The mongo:3.2 includes an entrypoint shell script that iterates trough /docker-entrypoint-initdb.d/*.{sh,js} (in the container).

Depending on the type of data you need to insert into the newly made database using a ConfigMap or a Secret as a volume is the way to go. But you can't do that with kubectl run.

1. Create a mongo.yaml like this:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mongo
spec:
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
      - image: mongo:3.2
        name: mongo
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-initdb
          mountPath: /docker-entrypoint-initdb.d
      volumes:
      - name: mongo-initdb
        configMap:
          name: mongo-initdb

2. Create createuser.sh like this (just an example):

mongo <<EOF
use reporting
db.createUser(
  {
    user: "reportsUser",
    pwd: "12345678",
    roles: [
       { role: "read", db: "reporting" },
       { role: "read", db: "products" },
       { role: "read", db: "sales" },
       { role: "readWrite", db: "accounts" }
    ]
  }
)
EOF

3. Create the ConfigMap:

$ kubectl create configmap mongo-initdb --from-file=createuser.sh
configmap "mongo-initdb" created

4. Create the mongo deployment (instead of kubectl run mongo --image=mongo:3.2 --port=27017):

$ kubectl apply -f mongo.yaml
deployment "mongo" created

5. Check the logs:

$ kubectl logs -f deploy/mongo
[...]
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/createuser.sh
MongoDB shell version: 3.2.17
[...]
Successfully added user: {
[...]

As I mentioned a very similar solution can be done using Secrets for sensitive data. The kubectl create configmap --from-file=... command also accepts a name of a directory instead of a file and will work as expected.

-- Janos Lenart
Source: StackOverflow