I am looking for help with connecting to a mongo db replica-set , on K8s.
Greatful for any help , pointers for the same…
I have created a mongodb replicaset using the following method.
- Created mongo instance in 3 pods.
- Attached the 3 pods at startup in a replicaset
- Exposed the services using K8s Services.
The Replicas are all up and running and can access each other [tested using ‘mongo --hostname ’]
However, I am not able to access the services using the connection string
“mongodb://mongodb-0-service:27017,mongodb-1-service:27017,mongodb-2-service:27017/admin?replicaSet=rs0”
Attached is one set of K8s manifests use to set this env up.
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongo-0-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: "2Gi"
accessModes:
- ReadWriteOnce
hostPath:
path: "/data/db/mongo-0"
---
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
namespace: default
name: mongo-0-pv-claim
spec:
accessModes:
- ReadWriteOnce
storageClassName: manual
resources:
requests:
storage: 1Gi
----
---
apiVersion: v1
kind: Service
metadata:
namespace: default
name: mongodb-0-service
labels:
run: mongodb-0-service
spec:
ports:
- port: 27017
targetPort: 27017
protocol: TCP
selector:
defacementComponent: mongodb-0
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
name: mongodb-0
labels:
env: test
defacementComponent: mongodb-0
spec:
replicas: 1
selector:
matchLabels:
defacementComponent: mongodb-0
template:
metadata:
labels:
defacementComponent: mongodb-0
spec:
terminationGracePeriodSeconds: 10
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- mongo
topologyKey: "kubernetes.io/hostname"
containers:
- image: mtlbillyfong/mongodb-replica-set:20200330-stable-1
name: mongodb-0
resources:
requests:
ephemeral-storage: "1Gi"
cpu: "500m"
memory: "1Gi"
limits:
ephemeral-storage: "2Gi"
cpu: "700m"
memory: "2Gi"
env:
- name: "MONGO_INITDB_ROOT_USERNAME"
Error that I am getting is
# mongo mongodb://mongodb-0-service:27017,mongodb-1-service:27017,mongodb-2-service:27017
MongoDB shell version v4.4.4
connecting to: mongodb://mongodb-0-service:27017,mongodb-1-service:27017,mongodb-2-service:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server mongodb-2-service:27017, connection attempt failed: SocketException: Error connecting to mongodb-2-service:27017 (10.106.185.83:27017) :: caused by :: Connection timed out :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1
PS : I have also tried the usual method of creating a headless service and Statefulset , there too, I was not able to access the replicaset using the connection string .
I am following the repo :