无法连接到MongoDB:Mongonetworkerror&用猫鼬连接到kubernetis mongodb豆荚的mongonetworkerror

发布于 2025-02-09 02:05:14 字数 5178 浏览 1 评论 0原文

我正在尝试使用Nodejs,Kubernetes,Ingress和Skaffold在一个基于微服务的项目中连接到MongoDB。

我遇到了两个错误:

  1. 我在第一个连接[mongonetworktimeOuterror:连接时间范围内连接到服务器[auth-mongo-srv:21017]时,

    mongonetworkerror:

    失败[auth-mongo-srv:21017]

  2. mongoose默认连接错误:mongonetworkerror:mongonetworkerror:在第一个连接上无法连接到服务器[auth-mongo-srv:21017]

我的auth-mongo-deploy.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-mongo-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth-mongo
  template:
    metadata:
      labels:
        app: auth-mongo
    spec:
      containers:
        - name: auth-mongo
          image: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: auth-mongo-srv
spec:
  selector:
    app: auth-mongo
  ports:
    - name: db
      protocol: TCP
      port: 27017
      targetPort: 27017

我的server.ts

const dbURI: string = "mongodb://auth-mongo-srv:21017/auth"
 
logger.debug(dbURI)
logger.info('connecting to database...')
 
// changing {} --> options change nothing!
mongoose.connect(dbURI, {}).then(() => {
    logger.info('Mongoose connection done')
    app.listen(APP_PORT, () => {
      logger.info(`server listening on ${APP_PORT}`)
    })
    console.clear();
  }).catch((e) => {
    logger.info('Mongoose connection error')
    logger.error(e)
})

其他信息:

1. pod is created:

rhythm@vivobook:~/Documents/TicketResale/server$ kubectl get pods
NAME                                 STATUS  RESTARTS   AGE
auth-deploy-595c6cbf6d-9wzt9         1/1     Running   0          5m53s
auth-mongo-deploy-6b96b7798c-9726w   1/1     Running   0          5m53s
tickets-deploy-675b7b9b58-f5bzs      1/1     Running   0          5m53s

2. pod description:

    kubectl describe pod auth-mongo-deploy-6b96b7798c-9726w
    Name:         auth-mongo-deploy-694b67f76d-ksw82
    Namespace:    default
    Priority:     0
    Node:         minikube/192.168.49.2
    Start Time:   Tue, 21 Jun 2022 14:11:47 +0530
    Labels:       app=auth-mongo
                  pod-template-hash=694b67f76d
                  skaffold.dev/run-id=2f5d2142-0f1a-4fa4-b641-3f301f10e65a
    Annotations:  <none>
    Status:       Running
    IP:           172.17.0.2
    IPs:
      IP:           172.17.0.2
    Controlled By:  ReplicaSet/auth-mongo-deploy-694b67f76d
    Containers:
      auth-mongo:
        Container ID:   docker://fa43cd7e03ac32ed63c82419e5f9722deffd2f93206b6a0f2b25ae9be8f6cedf
        Image:          mongo
        Image ID:       docker-pullable://mongo@sha256:37e84d3dd30cdfb5472ec42b8a6b4dc6ca7cacd91ebcfa0410a54528bbc5fa6d
        Port:           <none>
        Host Port:      <none>
        State:          Running
          Started:      Tue, 21 Jun 2022 14:11:52 +0530
        Ready:          True
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-zw7s9 (ro)
    Conditions:
      Type              Status
      Initialized       True 
      Ready             True 
      ContainersReady   True 
      PodScheduled      True 
    Volumes:
      kube-api-access-zw7s9:
        Type:                    Projected (a volume that contains injected data from multiple sources)
        TokenExpirationSeconds:  3607
        ConfigMapName:           kube-root-ca.crt
        ConfigMapOptional:       <nil>
        DownwardAPI:             true
    QoS Class:                   BestEffort
    Node-Selectors:              <none>
    Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
    Events:
      Type    Reason     Age   From               Message
      ----    ------     ----  ----               -------
      Normal  Scheduled  79s   default-scheduler  Successfully assigned default/auth-mongo-deploy-694b67f76d-ksw82 to minikube
      Normal  Pulling    79s   kubelet            Pulling image "mongo"
      Normal  Pulled     75s   kubelet            Successfully pulled image "mongo" in 4.429126953s
      Normal  Created    75s   kubelet            Created container auth-mongo
      Normal  Started    75s   kubelet            Started container auth-mongo

我也尝试了:

kubectl describe service auth-mongo-srv
Name:              auth-mongo-srv
Namespace:         default
Labels:            skaffold.dev/run-id=2f5d2142-0f1a-4fa4-b641-3f301f10e65a
Annotations:       <none>
Selector:          app=auth-mongo
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.100.42.183
IPs:               10.100.42.183
Port:              db  27017/TCP
TargetPort:        27017/TCP
Endpoints:         172.17.0.2:27017
Session Affinity:  None
Events:            <none>
And then changed:

const dbURI: string = "mongodb://auth-mongo-srv:21017/auth" to
const dbURI: string = "mongodb://172.17.0.2:27017:21017/auth"
generated a different error of MongooseServerSelectionError.

I am trying to connect to MongoDB in a microservice-based project using NodeJs, Kubernetes, Ingress, and skaffold.

I got two errors on doing skaffold dev:

  1. MongoNetworkError: failed to connect to server [auth-mongo-srv:21017] on first connect [MongoNetworkTimeoutError: connection timed out.

  2. Mongoose default connection error: MongoNetworkError: MongoNetworkError: failed to connect to server [auth-mongo-srv:21017] on first connect [MongoNetworkTimeoutError: connection timed out at connectionFailureError.

My auth-mongo-deploy.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-mongo-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth-mongo
  template:
    metadata:
      labels:
        app: auth-mongo
    spec:
      containers:
        - name: auth-mongo
          image: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: auth-mongo-srv
spec:
  selector:
    app: auth-mongo
  ports:
    - name: db
      protocol: TCP
      port: 27017
      targetPort: 27017

My server.ts

const dbURI: string = "mongodb://auth-mongo-srv:21017/auth"
 
logger.debug(dbURI)
logger.info('connecting to database...')
 
// changing {} --> options change nothing!
mongoose.connect(dbURI, {}).then(() => {
    logger.info('Mongoose connection done')
    app.listen(APP_PORT, () => {
      logger.info(`server listening on ${APP_PORT}`)
    })
    console.clear();
  }).catch((e) => {
    logger.info('Mongoose connection error')
    logger.error(e)
})

Additional information:

1. pod is created:

rhythm@vivobook:~/Documents/TicketResale/server$ kubectl get pods
NAME                                 STATUS  RESTARTS   AGE
auth-deploy-595c6cbf6d-9wzt9         1/1     Running   0          5m53s
auth-mongo-deploy-6b96b7798c-9726w   1/1     Running   0          5m53s
tickets-deploy-675b7b9b58-f5bzs      1/1     Running   0          5m53s

2. pod description:

    kubectl describe pod auth-mongo-deploy-6b96b7798c-9726w
    Name:         auth-mongo-deploy-694b67f76d-ksw82
    Namespace:    default
    Priority:     0
    Node:         minikube/192.168.49.2
    Start Time:   Tue, 21 Jun 2022 14:11:47 +0530
    Labels:       app=auth-mongo
                  pod-template-hash=694b67f76d
                  skaffold.dev/run-id=2f5d2142-0f1a-4fa4-b641-3f301f10e65a
    Annotations:  <none>
    Status:       Running
    IP:           172.17.0.2
    IPs:
      IP:           172.17.0.2
    Controlled By:  ReplicaSet/auth-mongo-deploy-694b67f76d
    Containers:
      auth-mongo:
        Container ID:   docker://fa43cd7e03ac32ed63c82419e5f9722deffd2f93206b6a0f2b25ae9be8f6cedf
        Image:          mongo
        Image ID:       docker-pullable://mongo@sha256:37e84d3dd30cdfb5472ec42b8a6b4dc6ca7cacd91ebcfa0410a54528bbc5fa6d
        Port:           <none>
        Host Port:      <none>
        State:          Running
          Started:      Tue, 21 Jun 2022 14:11:52 +0530
        Ready:          True
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-zw7s9 (ro)
    Conditions:
      Type              Status
      Initialized       True 
      Ready             True 
      ContainersReady   True 
      PodScheduled      True 
    Volumes:
      kube-api-access-zw7s9:
        Type:                    Projected (a volume that contains injected data from multiple sources)
        TokenExpirationSeconds:  3607
        ConfigMapName:           kube-root-ca.crt
        ConfigMapOptional:       <nil>
        DownwardAPI:             true
    QoS Class:                   BestEffort
    Node-Selectors:              <none>
    Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
    Events:
      Type    Reason     Age   From               Message
      ----    ------     ----  ----               -------
      Normal  Scheduled  79s   default-scheduler  Successfully assigned default/auth-mongo-deploy-694b67f76d-ksw82 to minikube
      Normal  Pulling    79s   kubelet            Pulling image "mongo"
      Normal  Pulled     75s   kubelet            Successfully pulled image "mongo" in 4.429126953s
      Normal  Created    75s   kubelet            Created container auth-mongo
      Normal  Started    75s   kubelet            Started container auth-mongo

I have also tried:

kubectl describe service auth-mongo-srv
Name:              auth-mongo-srv
Namespace:         default
Labels:            skaffold.dev/run-id=2f5d2142-0f1a-4fa4-b641-3f301f10e65a
Annotations:       <none>
Selector:          app=auth-mongo
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.100.42.183
IPs:               10.100.42.183
Port:              db  27017/TCP
TargetPort:        27017/TCP
Endpoints:         172.17.0.2:27017
Session Affinity:  None
Events:            <none>
And then changed:

const dbURI: string = "mongodb://auth-mongo-srv:21017/auth" to
const dbURI: string = "mongodb://172.17.0.2:27017:21017/auth"
generated a different error of MongooseServerSelectionError.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2025-02-16 02:05:15

const dburi:string =“ mongodb:// auth-mongo-srv:27017/auth”

const dbURI: string = "mongodb://auth-mongo-srv:27017/auth"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文