<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 @PjoterS suggest you can scale the mongoDB replicas or pods inside the kubernetes using HPA.</p>

<p>But with that you should have to also take care about the volume mounting with it.also data latency between replicas.</p>

<p>I would suggest better first check the native scaling cluster option provided by the mongo db it self and configure. You can use some operators for mongoDB 
like : <code>https://docs.mongodb.com/kubernetes-operator/master/tutorial/install-k8s-operator/</code></p>

<p>Otherwise if you have current config is following native cluster and support scaling replica and data copy between replca's you can go for HPA.</p>

<p>you can also have a look at this : <code>https://medium.com/faun/scaling-mongodb-on-kubernetes-32e446c16b82</code></p>
