<p>I try to run an mongodb inside an kubernetes cluster which is hosted on azure aks</p>

<p>I was not able to get it running, following this tutorial: <a href="https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/" rel="nofollow noreferrer">https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/</a></p>

<p>here is the yaml I use:</p>

<pre><code>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
</code></pre>

<p>The connection string I use is:</p>

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

<p>from my JS application I get: </p>

<blockquote>
  <p>database names cannot contain the character '\'</p>
</blockquote>

<p>How I can connect from the JS application to the mongodb?</p>

<blockquote>
  <p>mongodb://mongo-0.mongo,mongo-1.mongo:27017</p>
</blockquote>

<p>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.</p>

<p>In order to connect locally you have to do a port forwarding:</p>

<blockquote>
  <p>kubectl port-forward mongo-0 27017 # or mongo-1</p>
</blockquote>

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