No Primary in MongoDB 4.2

4/16/2020

I'm trying to deploy MongoDB 4.2 into Kubernetes without using their Enterprise Operator using https://medium.com/devgorilla/how-to-deploy-mongodb-on-google-kubernetes-engine-gke-b099862fadbd and it seems to work fine except there is no Primary Replica Set Member.

I think so because all 3 members have type REPLICA_SET_GHOST. Here is example of an error when I try to insert something into db:

Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[
        {address=mongo-0.mongo: 27017, type=REPLICA_SET_GHOST, roundTripTime=0.8 ms, state=CONNECTED
        },
        {address=mongo-1.mongo: 27017, type=REPLICA_SET_GHOST, roundTripTime=0.9 ms, state=CONNECTED
        },
        {address=mongo-2.mongo: 27017, type=REPLICA_SET_GHOST, roundTripTime=0.9 ms, state=CONNECTED
        }
    ];
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
          command:
            - mongod
            - "--replSet"
            - rs0
            - "--bind_ip_all"
          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: "nfs-rw"
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 10Gi

Is there any possibility of configuration property for mongod to ensure that Primary always present?

-- tillias
kubernetes
mongodb

1 Answer

4/16/2020

I never used this sidecar - I manually setup replica, but I guess you are missing KUBERNETES_MONGO_SERVICE_NAME to use stable network IDs as mentioned in sidecar repository

-- FL3SH
Source: StackOverflow