I have a k8s dev cluster (in aks). I previously used the bitnami chart for deployment of a single MongoDB pod and I never had the following problem.
As I'm now using a feature that only works with replicaSets, I recently moved to using the MongoDBCommunity Operator to create a single instance MongoDB replicaSet running in its own namespace (in dev). My application is connecting to and using this instance with no issues at all and my new feature works like a dream.
$ k get pods -n db
NAME READY STATUS RESTARTS AGE
mongodb-kubernetes-operator-748c85557-bffz5 1/1 Running 0 2d16h
nga-mongodb-0 2/2 Running 0 2d19h
Now I want to use the MongoDB extension in vscode to help with debugging, and that's where the problem is. As I mentioned, with the bitnami chart I had no problem -- I provided the db connection string and it just worked.
When I try to do the same with this new deployment though I get the following error:
getaddrinfo ENOTFOUND nga-mongodb-0.nga-mongodb-svc.db.svc.cluster.local
The operator creates a secret containing the connection string in 2 forms, for the service and for the pod, both give the same error
Svc:
mongodb+srv://username:password@nga-mongodb-svc.db.svc.cluster.local/admin?ssl=false
Pod:
mongodb://username:password@nga-mongodb-0.nga-mongodb-svc.db.svc.cluster.local:27017/admin?ssl=false
I have noticed that neither of those strings includes a replica name, which I find odd, but I use the svc based string for my application and it works fine so it should work ok when port-forwarding.
My port-forwarding statement is pretty basic:
k port-forward -n db nga-mongodb-0 27017:27017
And if I try curl localhost:27017
I get the expected message
It looks like you are trying to access MongoDB over HTTP on the native driver port.
Which does suggest that I have a working port forwarder.
The connection string I use (assuming the pod connection string) then changes to:
mongodb://username:password@localhost:27017/admin?ssl=false
I suspect, but don't know, that the replica set is trying to forward the client to the primary (which I already connect to if I use the pod connection string) and is sending a kubernetes URL which doesn't translate well via the port-forwarding method. So, I doubt that this is something weird the operator has done.
Has anyone else actually got this working or know how to get it working?
So, based on writing up the question and therefore organising my thoughts, I have found the reason this is happening and the answer.
I had suggested in my question that this might be a redirect problem, and now I have proof that this is the issue. I've added the following entry to my hosts file:
127.0.0.1 nga-mongodb-0.nga-mongodb-svc.db.svc.cluster.local
And now it works. So, the MongoDB replicaSet is trying to forward me to the primary (even though I'm already connected to the primary) and is doing so by, quite rightly, returning the only host it knows. By adding that hostname to the /etc/hosts
file my machine resolves the name to the localhost and back through the forwarded connection. I haven't actually tried this with the svc approach, but I doubt it would work as I believe it'll constantly redirect.