<p>I've deployed a stateful mongodb setup in my k8s cluster. Everytime a scale a new pod, I need to add the pod from mongodb console using rs.add() command. Is there any way I can orchestrate this ?..Also how can I expose my mongodb service outside my k8s cluster..Changing the service type to nodeport didn't work for me..Please help.</p>

<p>Giving below the stateful yaml file which I used to deploy mongodb.</p>

<pre><code>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: 3
  template:
    metadata:
      labels:
        role: mongo
        environment: test
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo:3.4
          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-nfs-storage"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 2Gi
</code></pre>

Nithin Radhakrishnan
<p>As you are using Kubernetes, which is Container Orchestration platform, you can always scale your <code>deployment/statefulset</code> using <code>$ kubectl scale deployment [deployment_name] --repplicas=X</code>
or <code>$ kubectl scale statefulset [statefulset-name]  --replicas=X</code>
where X means how many pods in total you want to have in deployment. It will create autoamatically pods based on your deployment settings.
If you don't want to create it manually, you should read about Kubernetes autoscaling - <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/" rel="nofollow noreferrer">HPA</a>.</p>

<p>About exposing application outside Kubernetes you have to do it using <code>Service</code>. More information can be found <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">here</a>. I am not sure if <code>NodePort</code> is right in this scenario. You can check <a href="https://medium.com/@pczarkowski/kubernetes-services-exposed-86d45c994521" rel="nofollow noreferrer">ServiceType</a> description.</p>

<p>However I am not very familiar with MongoDB with Kubernetes, but maybe those tutorials help you.
<a href="https://medium.com/faun/scaling-mongodb-on-kubernetes-32e446c16b82" rel="nofollow noreferrer">Scaling MongoDB on Kubernetes</a>, <a href="https://www.mongodb.com/blog/post/running-mongodb-as-a-microservice-with-docker-and-kubernetes" rel="nofollow noreferrer">Running MongoDB as a Microservice with Docker and Kubernetes</a>, <a href="https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/" rel="nofollow noreferrer">Running MongoDB on Kubernetes with StatefulSets</a>.</p>

<p>Hope it will help.</p>
