Hi everyone,
I’m having trouble connecting to my MongoDB replica set, which is managed by the MongoDB Kubernetes Operator (mongodb-kubernetes-1.3.0) . I am trying to connect from an external client on my local machine, but the connection fails after the initial handshake.
My Goal
My goal is to successfully connect to the replica set from outside the Kubernetes cluster using mongosh.
The Command I’m Using
I am running mongosh from a Docker container with the following command:
Bash
docker run --name mongotest1 -it --rm alpine/mongosh mongosh “mongodb://m1.infra-dashboards.mobicycle.pt:27017/?replicaSet=mongodb-prod&tls=true” --eval “rs.isMaster();” -u my-user
The Error I Receive
The connection attempt fails with this error, indicating it cannot resolve an internal Kubernetes service name:
Current Mongosh Log ID: 68cc177bbe1d2ccc13bae731
Connecting to: mongodb://@m1.infra-dashboards.mobicycle.pt:27017/?replicaSet=mongodb-prod&tls=true&appName=mongosh+2.0.2
MongoNetworkError: getaddrinfo ENOTFOUND mongodb-prod-0.mongodb-prod-svc.mongodb.svc.cluster.local
My Configuration
My current MongoDBCommunity resource is set up for internal cluster access, but I haven’t configured it for external access yet. Here is a simplified version of my deployment YAML:
YAML
apiVersion: MongoDB: The World’s Leading Modern Database | MongoDB
kind: MongoDBCommunity
metadata:
name: mongodb-prod
spec:
Basic replica set configuration
members: 1
version: “8.2.0”
replicaSetHorizons:
- horizon: m1.infra-dashboards.mobicycle.pt:27017
Security and User setup
security:
authentication:
modes: [“SCRAM”]
tls:
enabled: true
certificateKeySecretRef:
name: mongodb-prod-tls-secret
users:
- name: my-user
db: admin
passwordSecretRef:
name: my-user-password # Secret containing the user’s password
roles:
- name: readWriteAnyDatabase
db: admin
rs config
{
_id: ‘mongodb-prod’,
version: 1,
term: 1,
members: [
{
_id: 0,
host: ‘mongodb-prod-0.mongodb-prod-svc.mongodb.svc.cluster.local:27017’,
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
horizons: { external: ‘m1.infra-dashboards.mobicycle.pt:27017’ },
secondaryDelaySecs: Long(‘0’),
votes: 1
}
],
protocolVersion: Long(‘1’),
writeConcernMajorityJournalDefault: true,
settings: {
chainingAllowed: true,
heartbeatIntervalMillis: 2000,
heartbeatTimeoutSecs: 10,
electionTimeoutMillis: 10000,
catchUpTimeoutMillis: -1,
catchUpTakeoverDelayMillis: 30000,
getLastErrorModes: {},
getLastErrorDefaults: { w: 1, wtimeout: 0 },
replicaSetId: ObjectId(‘68cc1d464e13c69a9d81b233’)
}
rs isMaster
rs.isMaster()
{
topologyVersion: {
processId: ObjectId(‘68cc1d454e13c69a9d81b22a’),
counter: Long(‘6’)
},
hosts: [ ‘mongodb-prod-0.mongodb-prod-svc.mongodb.svc.cluster.local:27017’ ],
setName: ‘mongodb-prod’,
setVersion: 1,
ismaster: true,
secondary: false,
primary: ‘mongodb-prod-0.mongodb-prod-svc.mongodb.svc.cluster.local:27017’,
me: ‘mongodb-prod-0.mongodb-prod-svc.mongodb.svc.cluster.local:27017’,
electionId: ObjectId(‘7fffffff0000000000000001’),
lastWrite: {
opTime: { ts: Timestamp({ t: 1758209292, i: 1 }), t: Long(‘1’) },
lastWriteDate: ISODate(“2025-09-18T15:28:12.000Z”),
majorityOpTime: { ts: Timestamp({ t: 1758209292, i: 1 }), t: Long(‘1’) },
majorityWriteDate: ISODate(“2025-09-18T15:28:12.000Z”)
},
maxBsonObjectSize: 16777216,
maxMessageSizeBytes: 48000000,
maxWriteBatchSize: 100000,
localTime: ISODate(“2025-09-18T15:28:14.583Z”),
logicalSessionTimeoutMinutes: 30,
connectionId: 359,
minWireVersion: 0,
maxWireVersion: 27,
readOnly: false,
ok: 1,
‘$clusterTime’: {
clusterTime: Timestamp({ t: 1758209292, i: 1 }),
signature: {
hash: Binary.createFromBase64(“WuRFTV4vyUTVwgL+hdzsEcT2L6o=”, 0),
keyId: Long(‘7551442861678395398’)
}
},
operationTime: Timestamp({ t: 1758209292, i: 1 }),
isWritablePrimary: true
}
My Question
The problem is that the primary server reports the pods internal hostname to the client.
How can I correctly modify my MongoDBCommunity resource to advertise publicly resolvable DNS names for members of the replica set? I’ve seen documentation about replicaSetHorizons but would appreciate a clear example of how to configure this properly.
Thank you in advance for your help!