gpt4 book ai didi

node.js - 当从kubernetes上托管的Docker容器中请求时,Nodejs Mongodb返回EHOSTUNREACH

转载 作者:行者123 更新时间:2023-12-02 21:04:03 25 4
gpt4 key购买 nike

我一直在努力解决这个问题。
这可能是我对stackoverflow的第二个或第三个请求,因此,如果我忘记提及一些重要的内容,请告诉我。

首先,这里是有关设置的一些信息:

  • 所有应用程序都托管在kubernetes集群
  • Nginx充当负载平衡器(不在kubernetes集群内部)
  • 服务通过dns(例如.default.svc.cluster.local)相互通信
  • 通过dns名称和端口可以访问所有服务(每个服务都有kubernetes服务资源)

  • 问题是这样的:
    每当我的Web应用程序尝试从Internet访问服务时,该服务都会以HTTP 500内部服务器错误作为响应。
    当我查看日志时,错误提示:
    { MongoNetworkError: failed to connect to server [172.16.62.2:8635] on first connect [MongoNetworkError: connect EHOSTUNREACH 172.16.62.2:8635]
    at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:433:11)
    at Pool.emit (events.js:189:13)
    at createConnection (/node_modules/mongodb/lib/core/connection/pool.js:571:14)
    at connect (/node_modules/mongodb/lib/core/connection/pool.js:1008:9)
    at makeConnection (/node_modules/mongodb/lib/core/connection/connect.js:40:11)
    at callback (/node_modules/mongodb/lib/core/connection/connect.js:262:5)
    at Socket.err (/node_modules/mongodb/lib/core/connection/connect.js:287:7)
    at Object.onceWrapper (events.js:277:13)
    at Socket.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    name: 'MongoNetworkError',
    [Symbol(mongoErrorContextSymbol)]: {} }
    { MongoNetworkError: failed to connect to server [172.16.62.2:8635] on first connect [MongoNetworkError: connect EHOSTUNREACH 172.16.62.2:8635]
    at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:433:11)
    at Pool.emit (events.js:189:13)
    at createConnection (/node_modules/mongodb/lib/core/connection/pool.js:571:14)
    at connect (/node_modules/mongodb/lib/core/connection/pool.js:1008:9)
    at makeConnection (/node_modules/mongodb/lib/core/connection/connect.js:40:11)
    at callback (/node_modules/mongodb/lib/core/connection/connect.js:262:5)
    at Socket.err (/node_modules/mongodb/lib/core/connection/connect.js:287:7)
    at Object.onceWrapper (events.js:277:13)
    at Socket.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    name: 'MongoNetworkError',
    [Symbol(mongoErrorContextSymbol)]: {} }

    我的配置文件如下所示:
    const MongoClient = require('mongodb').MongoClient;

    // Connection URL
    const url = 'mongodb://mongo-service.default.svc.cluster.local:27017';

    let mongoClient;

    /**
    * creates mongo client
    * @param {string} dbName name of the database you want to connect to
    * @return {Promise<unknown>} mongo client
    */
    const _getMongoDB = (dbName = 'oli') => {
    return new Promise((resolve, reject) => {
    if (mongoClient) {
    resolve(mongoClient);
    } else {
    MongoClient.connect(url, {useNewUrlParser: true}).then(client => {
    console.log('Connected successfully to server');
    mongoClient = client.db(dbName);
    resolve(mongoClient);
    }).catch(err => {
    console.log(err);
    reject(err);
    });
    }
    });
    };

    const mongoDB = {
    getClient: _getMongoDB,
    };

    module.exports = mongoDB;

    奇怪的是,当我从容器内部使用mongo shell时,一切正常。我只需要删除前面的“mongodb://”。我已经在服务中尝试过了,但没有成功。

    我非常感谢您的帮助或提示。也许有人遇到过类似的问题。

    编辑:
    我发现,mongo-service的日志中充满了错误,除非我是从shell连接到它的:
    2019-12-03T08:27:24.177+0000 W  NETWORK  [listener] Error accepting new connection SocketException: remote_endpoint: Transport endpoint is not connected
    2019-12-03T08:27:24.205+0000 W NETWORK [listener] Error accepting new connection SocketException: remote_endpoint: Transport endpoint is not connected
    2019-12-03T08:27:24.600+0000 W NETWORK [listener] Error accepting new connection SocketException: remote_endpoint: Transport endpoint is not connected
    2019-12-03T08:27:25.587+0000 W NETWORK [listener] Error accepting new connection SocketException: remote_endpoint: Transport endpoint is not connected
    2019-12-03T08:27:26.036+0000 W NETWORK [listener] Error accepting new connection SocketException: remote_endpoint: Transport endpoint is not connected
    2019-12-03T08:27:26.441+0000 W NETWORK [listener] Error accepting new connection SocketException: remote_endpoint: Transport endpoint is not connected
    2019-12-03T08:27:26.962+0000 W NETWORK [listener] Error accepting new connection SocketException: remote_endpoint: Transport endpoint is not connected
    2019-12-03T08:31:40.862+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54830 #512206 (1 connection now open)
    2019-12-03T08:31:40.863+0000 I NETWORK [conn512206] Error receiving request from client: ProtocolError: Client sent an HTTP request over a native MongoDB connection. Ending connection from 172.16.5.1:54830 (connection id: 512206)
    2019-12-03T08:31:40.863+0000 I NETWORK [conn512206] end connection 172.16.5.1:54830 (0 connections now open)
    2019-12-03T08:31:51.356+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54832 #512207 (1 connection now open)
    2019-12-03T08:32:25.055+0000 I NETWORK [conn512207] end connection 172.16.5.1:54832 (0 connections now open)
    2019-12-03T08:32:31.034+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54834 #512208 (1 connection now open)
    2019-12-03T08:33:33.251+0000 I NETWORK [conn512208] Error receiving request from client: SSLHandshakeFailed: SSL handshake received but server is started without SSL support. Ending connection from 172.16.5.1:54834 (connection id: 512208)
    2019-12-03T08:33:33.251+0000 I NETWORK [conn512208] end connection 172.16.5.1:54834 (0 connections now open)
    2019-12-03T08:35:18.704+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54844 #512209 (1 connection now open)
    2019-12-03T08:35:18.826+0000 E - [conn512209] Assertion: Location34348: cannot translate opcode 2010 src/mongo/rpc/message.h 120
    2019-12-03T08:35:18.859+0000 I NETWORK [conn512209] DBException handling request, closing client connection: Location34348: cannot translate opcode 2010
    2019-12-03T08:35:18.859+0000 I NETWORK [conn512209] end connection 172.16.5.1:54844 (0 connections now open)
    2019-12-03T08:35:33.191+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54846 #512210 (1 connection now open)
    2019-12-03T08:35:33.192+0000 E - [conn512210] Assertion: Location34348: cannot translate opcode 2010 src/mongo/rpc/message.h 120
    2019-12-03T08:35:33.192+0000 I NETWORK [conn512210] DBException handling request, closing client connection: Location34348: cannot translate opcode 2010
    2019-12-03T08:35:33.192+0000 I NETWORK [conn512210] end connection 172.16.5.1:54846 (0 connections now open)
    2019-12-03T08:35:39.370+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54848 #512211 (1 connection now open)
    2019-12-03T08:35:39.371+0000 E - [conn512211] Assertion: Location34348: cannot translate opcode 2010 src/mongo/rpc/message.h 120
    2019-12-03T08:35:39.371+0000 I NETWORK [conn512211] DBException handling request, closing client connection: Location34348: cannot translate opcode 2010
    2019-12-03T08:35:39.371+0000 I NETWORK [conn512211] end connection 172.16.5.1:54848 (0 connections now open)
    2019-12-03T08:38:01.610+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54850 #512212 (1 connection now open)
    2019-12-03T08:38:01.611+0000 E - [conn512212] Assertion: Location34348: cannot translate opcode 2010 src/mongo/rpc/message.h 120
    2019-12-03T08:38:01.612+0000 I NETWORK [conn512212] DBException handling request, closing client connection: Location34348: cannot translate opcode 2010
    2019-12-03T08:38:01.612+0000 I NETWORK [conn512212] end connection 172.16.5.1:54850 (0 connections now open)
    2019-12-03T08:38:15.269+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54852 #512213 (1 connection now open)
    2019-12-03T08:38:15.270+0000 E - [conn512213] Assertion: Location34348: cannot translate opcode 2010 src/mongo/rpc/message.h 120
    2019-12-03T08:38:15.270+0000 I NETWORK [conn512213] DBException handling request, closing client connection: Location34348: cannot translate opcode 2010
    2019-12-03T08:38:15.270+0000 I NETWORK [conn512213] end connection 172.16.5.1:54852 (0 connections now open)
    2019-12-03T08:41:17.804+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54856 #512214 (1 connection now open)
    2019-12-03T08:41:17.804+0000 I NETWORK [conn512214] received client metadata from 172.16.5.1:54856 conn512214: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.2.1" }, os: { type: "Linux", name: "PRETTY_NAME="Debian GNU/Linux 9 (stretch)"", architecture: "x86_64", version: "Kernel 4.9.0-8-amd64" } }
    2019-12-03T08:41:21.328+0000 I NETWORK [conn512214] end connection 172.16.5.1:54856 (0 connections now open)
    2019-12-03T08:42:02.199+0000 I NETWORK [listener] connection accepted from 172.16.5.1:54858 #512215 (1 connection now open)
    2019-12-03T08:42:02.199+0000 I NETWORK [conn512215] received client metadata from 172.16.5.1:54858 conn512215: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.2.1" }, os: { type: "Linux", name: "PRETTY_NAME="Debian GNU/Linux 9 (stretch)"", architecture: "x86_64", version: "Kernel 4.9.0-8-amd64" } }
    2019-12-03T08:42:15.324+0000 I NETWORK [conn512215] end connection 172.16.5.1:54858 (0 connections now open)

    显然,当pod在负载均衡器后面时,可能会发生这种情况。更改后,错误日志将保持干净。但是,这不能解决原始问题。

    最佳答案

    事实证明,问题不在于连接性或其他任何问题。
    在调试时,我创建了连接测试脚本并通过节点-e执行了该脚本。连接成功。所以必须有所不同...

    由于我没有开发微服务(仅负责基础结构部分),所以我不知道在不同文件中存在个以上的连接字符串。 (第一次搜索文件并没有带给我该知识)。
    较旧的连接字符串指向已久的mongodb。现在一切正常。
    感谢@Oles Rid帮助我缩小问题范围。

    关于node.js - 当从kubernetes上托管的Docker容器中请求时,Nodejs Mongodb返回EHOSTUNREACH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59139626/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com