I'm a beginner to docker and kubernetes. I have docker containers running inside pods on the kubernetes cluster and I'm trying to connect to mongo db but it keeps failing
when I check my pods and services, everything is running all good:
NAME READY STATUS RESTARTS AGE
auth-depl-65565b6884-rxcvj 1/1 Running 0 58s
auth-mongo-depl-6c87496969-9sxj4 1/1 Running 0 56s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
auth-mongo-srv ClusterIP 10.106.91.10 <none> 27017/TCP 2m
auth-srv ClusterIP 10.96.108.116 <none> 3000/TCP 2m3s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2m20s
My Mongo Deployment file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: auth-mongo-depl
spec:
replicas: 1
selector:
matchLabels:
app: mongo-depl
template:
metadata:
labels:
app: mongo-depl
spec:
containers:
- name: mongo-depl
image: mongo
---
apiVersion: v1
kind: Service
metadata:
name: auth-mongo-srv
spec:
selector:
type: ClusterIP
app: mongo-depl
ports:
- name: db
protocol: TCP
port: 27017
targetPort: 27017
Connection to DB:
const start = async (): Promise<void> => {
try {
await mongoose.connect("mongodb://auth-mongo-srv:27017/auth", {
useCreateIndex: true,
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log("connected to db");
} catch (error) {
console.log(error);
}
};
start();
When I check my logs, I get this error:
[auth] MongooseServerSelectionError: connect ECONNREFUSED 10.106.91.10:27017
reason: TopologyDescription {
type: 'Single',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(1) { 'auth-mongo-srv:27017' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
Any clues what may cause the problem?