run mongodb inside kubernetes - azure aks fails

8/13/2018

I try to run an mongodb inside an kubernetes cluster which is hosted on azure aks

I was not able to get it running, following this tutorial: https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/

here is the yaml I use:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: default-view
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: view
subjects:
  - kind: ServiceAccount
    name: default
    namespace: default
---
apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    name: mongo
spec:
  ports:
  - port: 27017
    targetPort: 27017
  clusterIP: None
  selector:
    role: mongo
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: "mongo"
  replicas: 2
  template:
    metadata:
      labels:
        role: mongo
        environment: test
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo
          command:
            - mongod
            - "--replSet"
            - rs0
            - "--bind_ip"
            - 0.0.0.0            
            - "--smallfiles"
            - "--noprealloc"
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
        - name: mongo-sidecar
          image: cvallance/mongo-k8s-sidecar
          env:
            - name: MONGO_SIDECAR_POD_LABELS
              value: "role=mongo,environment=test"
  volumeClaimTemplates:
  - metadata:
      name: mongo-persistent-storage
      annotations:
        volume.beta.kubernetes.io/storage-class: "managed-premium"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 32Gi

The connection string I use is:

"mongodb://mongo-0.mongo,mongo-1.mongo:27017/databasename\_?"

from my JS application I get:

database names cannot contain the character '\'

How I can connect from the JS application to the mongodb?

-- aumanjoa
azure
azure-kubernetes
kubernetes
mongodb

2 Answers

8/14/2018

I was able to fix the problem using this connection string:

mongodb://mongo-0.mongo,mongo-1.mongo:27017/chronas-api_?

But can someone please explain me this connection string magic and also how I can connect to this cluster using "Robo 3T"?

-- aumanjoa
Source: StackOverflow

8/14/2018

mongodb://mongo-0.mongo,mongo-1.mongo:27017

You are indicating that you have a replicaset with 2 members and both use the port 27017. Your mongodb library will handle that url to connect to the cluster.

In order to connect locally you have to do a port forwarding:

kubectl port-forward mongo-0 27017 # or mongo-1

Then you can connect to the chosen mongodb with Robo 3T using your localhost (127.0.0.1) and the port 27017.

-- mendrugory
Source: StackOverflow