'Access mongodb replica set pod (kubernetes) through another pod AND through external sources
I'm running minikube on my local machine, and I can easily connect to my MongoDB pod from both external/internal sources through this setting below WITHOUT the replica set configurations (no startupProbes and no --replSet args)
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-depl
spec:
replicas: 1
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- name: mongo
image: mongo
args:
- --replSet
- rs0
startupProbe:
initialDelaySeconds: 1
successThreshold: 1
failureThreshold: 10
exec:
command:
- /bin/bash
- -c
- 'mongo --eval "rs.initiate({_id: ''rs0'', members: [{_id: 0, host: ''localhost:27017''}]})"'
---
apiVersion: v1
kind: Service
metadata:
name: mongo-srv
spec:
selector:
app: mongo
ports:
- port: 27017
protocol: TCP
targetPort: 27017
---
apiVersion: v1
kind: Service
metadata:
name: mongo-node-port
spec:
type: NodePort
selector:
app: mongo
ports:
- port: 27017
targetPort: 27017
protocol: TCP
nodePort: 30123
If I add those configurations to run MongoDB as a replica set (which is needed in my case), I can only access it externally if I manually port forward using the kubectl command
kubectl port-forward (pod-name) 27017:27017
I can not access replica sets internally in any way. Only without the replica set flags in the config above.
What am I doing wrong? I've searched thousands of times and can't find any answer to this!
This is what happens when I try to access the services externally without using port-forward:
MongoDB Compass:
connect ECONNREFUSED 127.0.0.1:27017
POD trying to connect to the ClusterIp service (Using Prisma through mongodb://mongo-srv:27017):
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'rs0',
maxSetVersion: 1,
maxElectionId: new ObjectId("7fffffff0000000000000001"),
commonWireVersion: 13,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|