Mongo ReplicaSet using K8s Statefulset

Asking for help? Comment out what you need so we can get more information to help you!

Cluster information:

minikube version: v1.18.1, virtualbox

Kubernetes version:
Client Version: v1.20.4
Server Version: v1.20.2

Cloud being used: (put bare-metal if not on a public cloud)
Installation method:
Host OS: macOS
CNI and version:
CRI and version:

I followed this instruction GitHub - pkdone/minikube-mongodb-demo: Demo project showing how to deploy MongoDB in a local Minikube Kubernetes environment to setup a replicaset in mongodb.
The final result consits: 3 pods and each of them consists one mongo process (mongod-0, mongod-1, mongod-2),these threre processes run on port 27017 , one service named ‘mongodb-service’ has targetPort 27017.

If I establish a connection from one of that processes using the connection string formatted “mongodb://mongo-0.mongodb-service:27017,mongo-1.mongodb-service:27017,mongo-2.mongodb-service:27017/admin?replicaset=MainRepSet” then it works .
My issue is I want to access it outside the cluster, I tried to create an ingress that has 3 backends, but the ingress syntax does not allow backend.service.name as ‘mongod-0.mongodb-service’ as correct name.

Can anyone help me?

apiVersion: v1
kind: Service
metadata:
name: mongodb-service
labels:
name: mongo
spec:
ports:

  • port: 27111
    nodePort: 30000
    targetPort: 27017
    type: NodePort
    selector:
    role: mongo

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongod
spec:
serviceName: mongodb-service
replicas: 3
selector:
matchLabels:
role: mongo
environment: test
replicaset: MainRepSet
template:
metadata:
labels:
role: mongo
environment: test
replicaset: MainRepSet
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: replicaset
operator: In
values:
- MainRepSet
topologyKey: kubernetes.io/hostname
terminationGracePeriodSeconds: 10
volumes:
- name: secrets-volume
secret:
secretName: shared-bootstrap-data
defaultMode: 256
containers:
- name: mongod-container
#image: pkdone/mongo-ent:3.4
image: mongo
command:
# - “numactl”
# - “–interleave=all”
- “mongod”
- “–wiredTigerCacheSizeGB”
- “0.1”
- “–bind_ip”
- “0.0.0.0”
- “–replSet”
- “MainRepSet”
- “–auth”
- “–clusterAuthMode”
- “keyFile”
- “–keyFile”
- “/etc/secrets-volume/internal-auth-mongodb-keyfile”
- “–setParameter”
- “authenticationMechanisms=SCRAM-SHA-1”
resources:
requests:
cpu: 0.2
memory: 200Mi
ports:
- containerPort: 27017
volumeMounts:
- name: secrets-volume
readOnly: true
mountPath: /etc/secrets-volume
- name: mongodb-persistent-storage-claim
mountPath: /data/db
volumeClaimTemplates:


apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: mongo-ingress
annotations:
ingress.kubernetes.io/rewrite-target: /
spec:
rules:

  • host: mongo-0
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: ‘mongod-0.mongodb-service’
    port:
    number: 27017
  • host: mongo-1
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: ‘mongod-1.mongodb-service’
    port:
    number: 27017
  • host: mongo-2
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: ‘mongod-2.mongodb-service’
    port:
    number: 27017