unable to create read replica for mongodb with statefulset and sidecar

4/5/2019

I am trying to make my existing mongodb as a high availability by creating replicas. in order to perform it i took reference of enter link description here

following is my stateful set

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: skeleton-mongodb
spec:
  serviceName: skeleton-mongodb
  replicas: 3
  selector:
    matchLabels:
      app: skeleton-mongodb
  template:
    metadata:
      labels:
        app: skeleton-mongodb
    spec:
      containers:
        - name: mongodb
          image: mongo:4
          resources:
            requests:
              memory: 250Mi
              cpu: 100m
            limits:
              memory: 500Mi
          ports:
            - containerPort: 27017
              name: mongo
          args:
            - "--bind_ip_all"
            - "--replSet"
            - rs0
          volumeMounts:
            - name: skeleton-mongodb-data
              mountPath: /data/db
            - name: skeleton-mongodb-init
              mountPath: /docker-entrypoint-initdb.d
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_root-username
            - name: MONGO_INITDB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: skeleton
                  key: mongodb_root-password
            - name: MONGO_INITDB_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_db
            - name: MONGO_USERNAME
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_username
            - name: MONGO_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: skeleton
                  key: mongodb_user-password
        - name: mongo-sidecar
          image: cvallance/mongo-k8s-sidecar
          env:
            - name: MONGO_SIDECAR_POD_LABELS
              value: "app=skeleton-mongodb"
            - name: MONGODB_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_db
            - name: MONGODB_USERNAME
              valueFrom:
                configMapKeyRef:
                  name: skeleton
                  key: mongo_username
            - name: MONGODB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: skeleton
                  key: mongodb_user-password
      volumes:
      - name: skeleton-mongodb-init
        configMap:
          name: common-mongodb-init
  volumeClaimTemplates:
    - metadata:
        name: skeleton-mongodb-data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
        storageClassName: managed-premium

when i try to find which pod is my master i am unable to find any pod as a master.

for ((i = 0; i < 3; ++i)); 
do 
   kubectl exec --namespace default skeleton-mongodb-$i -- sh -c 'mongo --eval="printjson(rs.isMaster())"'; 
done

i am getting for all 3 pods

{
        "ismaster" : false,
        "secondary" : false,
        "info" : "Does not have a valid replica set config",
        "isreplicaset" : true,
        ....
        "readOnly" : false,
        "ok" : 1
}

not sure if i am missing something

-- Ganesh Pol
azure
azure-aks
kubernetes
kubernetes-mongodb-sidecar
mongodb

1 Answer

4/5/2019

I don't know MongoDB enough to advise, but with StatefulSet you are going to have the exact same configuration for all the replicas.

-- suren
Source: StackOverflow