'Connecting to Azure CosmosDB (MongoDB APIs) from Node Server hosted inside AKS fails
I have Azure CosmosDB account with MongoDB APIs enabled, i'm trying to connect from Node Server deployed with 3 replicas (3 Pods) inside Azure Kubernetes.
Only one POD managed to connect to CosmosDB and the other Pods fails, the error is persistent and nothing in CosmosDB is configured to cause this issue.
Note that same issue happened with another Java application so it's not the Node Server i believe.
The Exception When NodeJS Try to Connect:
MongoServerSelectionError: getaddrinfo EAI_AGAIN abc.mongo.cosmos.azure.com
at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:312:38)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map {
'abc.mongo.cosmos.azure.com:10255' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'globaldb',
logicalSessionTimeoutMinutes: undefined
}
}
The code:
export const connectToDB = async () => {
const connectionString: string = process.env.CONNECTION_STRING;
try {
const mongoClient = new MongoClient(connectionString);
mongoClient
.connect()
.then(() => {
console.log("Connected successfully to server");
return Promise.resolve();
})
.catch((error: any) => {
console.log("failed to connect to mongodb!");
console.log(error);
});
} catch (error: any) {
console.log(error);
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
