未处理的PromiseRejection警告:未处理的promise拒绝.此错误源自于在没有catch块的情况下抛出异步



我使用kubectl在本地docker hub容器上运行mongo express实例,当我开始部署时,我在mongo express实例中收到异常。下面是我的mongo-express.yaml文件。

apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-express
labels:
app: mongo-express
spec:
replicas: 1
selector:
matchLabels:
app: mongo-express
template:
metadata:
labels:
app: mongo-express
spec:
containers:
- name: mongo-express
image: mongo-express
ports:
- containerPort: 8081
env:
- name: ME_CONFIG_MONGODB_ADMINUSERNAME
valueFrom:
secretKeyRef: 
name: mongodb-secret
key: mongo-root-username  
- name: ME_CONFIG_MONGODB_ADMINPASSWORD
valueFrom:
secretKeyRef: 
name: mongodb-secret
key: mongo-root-password 
- name: ME_CONFIG_MONGODB_SERVER
valueFrom:
configMapKeyRef: 
name: mongodb-configmap
key: database_url  

下面是我的mongo-deployment.yaml文件,

apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef: 
name: mongodb-secret
key: mongo-root-username 
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef: 
name: mongodb-secret
key: mongo-root-password 
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports: 
- protocol: TCP
port: 80
targetPort: 27017 

同样,我也有configmap和secret文件然而,当我应用这两个文件时,我会在docker容器上得到以下异常

Welcome to mongo-express
------------------------

(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Could not connect to database using connectionString: mongodb://root:root@mongodb-service:27017/"
(node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb-service:27017] on first connect [MongoNetworkTimeoutError: connection timed out
at connectionFailureError (/node_modules/mongodb/lib/core/connection/connect.js:342:14)
at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:310:16)
at Object.onceWrapper (events.js:420:28)
at Socket.emit (events.js:314:20)
at Socket._onTimeout (net.js:483:8)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)]
at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:438:11)
at Pool.emit (events.js:314:20)
at /node_modules/mongodb/lib/core/connection/pool.js:562:14
at /node_modules/mongodb/lib/core/connection/pool.js:995:11
at /node_modules/mongodb/lib/core/connection/connect.js:32:7
at callback (/node_modules/mongodb/lib/core/connection/connect.js:280:5)
at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:310:7)
at Object.onceWrapper (events.js:420:28)
at Socket.emit (events.js:314:20)
at Socket._onTimeout (net.js:483:8)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我不知道为什么会出现这个错误。有人能告诉我该怎么办吗?

欢迎加入社区。

您为mongo db提供的服务有一个小错误。侦听端口应该是27017而不是80:

apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports: 
- protocol: TCP
port: 27017
targetPort: 27017

这允许我的mongo express连接到数据库:

kubectl logs mongo-express-yyyyyyyyy-xxxxx
Welcome to mongo-express
------------------------

(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Mongo Express server listening at http://0.0.0.0:8081
Server is open to allow connections from anyone (0.0.0.0)
basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!

请尝试在mongo express yaml文件中是否有一个选项,方法是添加一个依赖变量。在docker中,当我在mongo express的yaml中添加dependens_on时,它就起作用了。

depends_on:
- mongodb-service

mongodb服务我认为是您在mongodbyaml中使用的服务的名称。

相关内容

最新更新