2

I deployed MongoDB replica on Kubernetes And it works great! And I defined a NodePort service for my primary pod Which it Name always is like <Statefulset_name>-0 . I Connect to my primary node with this service And problem is when my primary node that is <Statefulset_name>-0 get terminated and MongoDB set another primary node which means I can't connect to primary node with that Nodeport service. How can always connect to primary node even in this situation

My manifests is like below .

statefulset.yaml :

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: "mongo"
  replicas: 1
  template:
    metadata:
      labels:
        role: mongo
        environment: test
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo:3.4.9
          command:
          - /bin/sh
          - -c
          - >
            if [ -f /data/db/admin-user.lock ]; then
              mongod --replSet rs0 --clusterAuthMode keyFile --keyFile /etc/secrets-volume/mongodb-keyfile --setParameter authenticationMechanisms=SCRAM-SHA-1;
            else
              mongod --auth;
            fi;
          lifecycle:
            postStart:
              exec:
                command:
                - /bin/sh
                - -c
                - >
                  if [ ! -f /data/db/admin-user.lock ]; then
                    sleep 5;
                    touch /data/db/admin-user.lock
                    if [ "$HOSTNAME" = "mongo-0" ]; then
                      mongo --eval 'db = db.getSiblingDB("admin"); db.createUser({ user: "admin", pwd: "password", roles: [{ role: "root", db: "admin" }]});';
                    fi;
                    mongod --shutdown;
                  fi;
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-key
              mountPath: "/etc/secrets-volume"
              readOnly: true
            - 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"
            - name: MONGODB_USERNAME
              value: admin
            - name: MONGODB_PASSWORD
              value: password
            - name: MONGODB_DATABASE
              value: admin
      volumes:
      - name: mongo-key
        secret:
          defaultMode: 0400
          secretName: mongo-key
  volumeClaimTemplates:
  - metadata:
      name: mongo-persistent-storage
      annotations:
        volume.beta.kubernetes.io/storage-class: "fast-rbd"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 100Gi

service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    name: mongo
spec:
  ports:
  - port: 27017
    targetPort: 27017
  clusterIP: None
  selector:
    role: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-sv
  labels:
    name: mongo
spec:
  ports:
  - port: 27017
    targetPort: 27017
  selector:
    statefulset.kubernetes.io/pod-name: mongo-0
  type: NodePort

What should I do to always connect to my primary node

meisam bahrami
  • 107
  • 1
  • 8
  • Did you see this link ever? https://stackoverflow.com/questions/52978472/cannot-connect-to-a-mongodb-pod-in-kubernetes-connection-refused – Milad Yarmohammadi Jul 06 '19 at 15:22
  • @MilaDroid Yes but that's not useful for me. – meisam bahrami Jul 06 '19 at 15:54
  • Can you please edit the question's wording to make clearer what the issue is? Additionally, using the word 'node' might be confusing as is a concept in Kubernetes and MongoDB. Please add some clarficiation as well. Thank you. – yyyyahir Jul 09 '19 at 08:50
  • We are facing the same issue. Anytime we access to the service NodePort, it uses primary and/or secondary randomly, but we need to always access through the primary node. In our service we have one node(minikube) and three pods. – albertinisg Jul 10 '19 at 14:13

0 Answers0