Kubernetes MongoDB replica set external access

I have exposed 2 mongodb pods (replica set members) with LoadBalancer type services with the external static IP addresses.
DNS server (a network CoreDNS, not a kubernetes coredns ) with

mongo-cluster.foo.com.       IN A     10.10.5.201
mongo-0.foo.com.               IN A     10.10.5.203
mongo-1.foo.com.               IN A     10.10.5.204


_mongodb._tcp.mongo-cluster.foo.com. IN SRV 0 0 27017 mongo-0.foo.com.
_mongodb._tcp.mongo-cluster.foo.com. IN SRV 0 0 27017 mongo-1.foo.com.

I can connect to the replica set with the following commands from Ubuntu machine:

mongo mongodb+srv://<credentials>@mongo-cluster.foo.com/admin?ssl=false

mongo mongodb://<credentials>@mongo-0.foo.com:27017,mongo-1.foo.com:27017/admin

But it doesn’t work with mongosh and Compass. With mongosh I get this:

MongoNetworkError: getaddrinfo ENOTFOUND mongodb-cluster-0.mongodb-cluster-svc.mongodb.svc.cluster.local

It tries to resolve internal dns names. I suspect that Compass does the same.

mongo and mongosh do not accept more than one option (get stuck), like:

?authSource=admin&replicaSet=mongodb-cluster&authMechanism=SCRAM-SHA-256

How is it possible to connect externally to the MongoDB replica set deployed in Kubernetes?

Tried to connect via a headless service/Traefik IngressRoute and it failed either.

@lk777 Hi Leon! Which mongosh (and Compass) versions were you using for this? Can you share a log file for the mongosh connection attempt (https://docs.mongodb.com/mongodb-shell/logs/)?

1 Like

mongosh 1.3.0
Compass 1.30.1

cat ~/.mongodb/mongosh/6239b6fe11850fa92c3c1faf_log:

{"t":{"$date":"2022-03-22T11:46:06.181Z"},"s":"I","c":"MONGOSH","id":1000000000,"ctx":"log","msg":"Starting log","attr":{"execPath":"/usr/bin/mongosh","envInfo":{"EDITOR":null,"NODE_OPTIONS":null,"TERM":"xterm-256color"},"version":"1.3.0","distributionKind":"compiled","buildArch":"x64","buildPlatform":"linux","buildTarget":"linux-x64","buildTime":"2022-03-17T12:26:34.323Z","gitVersion":"df88ce833d498163c966e34c815ab669d2bee50c","nodeVersion":"v14.18.3"}}
{"t":{"$date":"2022-03-22T11:46:06.185Z"},"s":"I","c":"MONGOSH","id":1000000005,"ctx":"config","msg":"User updated"}
{"t":{"$date":"2022-03-22T11:46:06.186Z"},"s":"I","c":"MONGOSH","id":1000000048,"ctx":"config","msg":"Loading global configuration file","attr":{"filename":"/etc/mongosh.conf","found":false}}
{"t":{"$date":"2022-03-22T11:46:06.233Z"},"s":"I","c":"DEVTOOLS-CONNECT","id":1000000042,"ctx":"mongosh-connect","msg":"Initiating connection attempt","attr":{"uri":"mongodb://<credentials>@mongo-0.foo.com:27017,mongo-1.foo.com:27017/admin?appName=mongosh+1.3.0","driver":{"name":"nodejs|mongosh","version":"4.4.0"},"devtoolsConnectVersion":"1.2.4","host":"mongo-0.foo.com:27017,mongo-1.foo.com:27017"}}
{"t":{"$date":"2022-03-22T11:46:06.241Z"},"s":"I","c":"DEVTOOLS-CONNECT","id":1000000035,"ctx":"mongosh-connect","msg":"Server heartbeat succeeded","attr":{"connectionId":"mongo-1.foo.com:27017"}}
{"t":{"$date":"2022-03-22T11:46:06.253Z"},"s":"I","c":"DEVTOOLS-CONNECT","id":1000000035,"ctx":"mongosh-connect","msg":"Server heartbeat succeeded","attr":{"connectionId":"mongo-0.foo.com:27017"}}
{"t":{"$date":"2022-03-22T11:46:06.267Z"},"s":"W","c":"DEVTOOLS-CONNECT","id":1000000034,"ctx":"mongosh-connect","msg":"Server heartbeat failure","attr":{"connectionId":"mongodb-cluster-1.mongodb-cluster-svc.mongodb.svc.cluster.local:27017","failure":"getaddrinfo ENOTFOUND mongodb-cluster-1.mongodb-cluster-svc.mongodb.svc.cluster.local","isFailFast":true,"isKnownServer":true}}

This is my workaround.

For CoreDNS specifically (but it can be done with any DNS server):

Created /etc/coredns/db.cluster.local file with the following content


$ORIGIN cluster.local.
@       3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. (
                                2017042745 ; serial
                                7200       ; refresh (2 hours)
                                3600       ; retry (1 hour)
                                1209600    ; expire (2 weeks)
                                3600       ; minimum (1 hour)
                                )

        3600 IN NS a.iana-servers.net.
        3600 IN NS b.iana-servers.net.

mongodb-cluster-0.mongodb-cluster-svc.mongodb.svc             IN A     10.10.5.203
mongodb-cluster-1.mongodb-cluster-svc.mongodb.svc             IN A     10.10.5.204

Added this to the /etc/coredns/Corefile:


cluster.local {
    file /etc/coredns/db.cluster.local
    log
}


mongodb-cluster-X.mongodb-cluster-svc.mongodb.svc is kubernetes internal FQDN of the cluster members. With these changes foo.com/cluster.local are resolved to the same external IP addresses.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.