我已经使用node创建了REST API,并使用Kubernetes和Docker将其容器化。kubernetes容器在minikube环境中运行,用于开发目的。
该应用程序运行良好,现在出现以下错误。
[distribution] Initial Distribution API Database connection error occured - MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
[distribution] at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:830:32)
[distribution] at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:335:15)
[distribution] at /app/src/index.ts:60:8
[distribution] at step (/app/src/index.ts:34:23)
[distribution] at Object.next (/app/src/index.ts:15:53)
[distribution] at fulfilled (/app/src/index.ts:6:58)
[distribution] at processTicksAndRejections (node:internal/process/task_queues:93:5) {
[distribution] reason: TopologyDescription {
[distribution] type: 'ReplicaSetNoPrimary',
[distribution] setName: null,
[distribution] maxSetVersion: null,
[distribution] maxElectionId: null,
[distribution] servers: Map(3) {
[distribution] 'cluster0-shard-00-00.psdty.mongodb.net:27017' => [ServerDescription],
[distribution] 'cluster0-shard-00-01.psdty.mongodb.net:27017' => [ServerDescription],
[distribution] 'cluster0-shard-00-02.psdty.mongodb.net:27017' => [ServerDescription]
[distribution] },
[distribution] stale: false,
[distribution] compatible: true,
[distribution] compatibilityError: null,
[distribution] logicalSessionTimeoutMinutes: null,
[distribution] heartbeatFrequencyMS: 10000,
[distribution] localThresholdMS: 15,
[distribution] commonWireVersion: null
[distribution] }
[distribution] }
该问题似乎是MongoDB连接URL /访问问题,但连接字符串正确。(在MongoDB Cloud支持下再次对其进行了检查)。相关网络访问权限授予每个人
我还可以确认可以使用相同的连接URL通过MongoDB指南针访问MongoCloudDB
我的猜测是无法从MonkDB数据库的minikube内部的pod中建立连接。
你们中的任何人对如何克服这一点有任何想法吗?
Pod和外部服务的kubernetes配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: distribution-depl
spec:
replicas: 1
selector:
matchLabels:
app: distribution
template:
metadata:
labels:
app: distribution
spec:
containers:
- name: distribution
image: ssomlk/distribution
env:
- name: MONGO_URI
value: 'mongodb://ssomlk:<password>@cluster0-shard-00-00.yeu7t.mongodb.net:27017,cluster0-shard-00-01.yeu7t.mongodb.net:27017,cluster0-shard-00-02.yeu7t.mongodb.net:27017/<db_name>?ssl=true&replicaSet=atlas-fznj9q-shard-0&authSource=admin&retryWrites=true&w=majority'
- name: JWT_ACCESS_TOKEN_KEY
valueFrom:
secretKeyRef:
name: jwt-secret
key: JWT_ACCESS_TOKEN_KEY
- name: JWT_REFRESH_TOKEN_KEY
valueFrom:
secretKeyRef:
name: jwt-secret
key: JWT_REFRESH_TOKEN_KEY
- name: JWT_ACCESS_TOKEN_EXPIRES_IN
value: '15m'
- name: JWT_REFRESH_TOKEN_EXPIRES_IN
value: '60m'
- name: NATS_CLIENT_ID
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: NATS_URL
value: 'http://nats-srv:4222'
- name: NATS_CLUSTER_ID
value: nats-distribution-mailing
- name: MAIL_USER
valueFrom:
secretKeyRef:
name: mail-secret
key: MAIL_USER
- name: MAIL_PWD
valueFrom:
secretKeyRef:
name: mail-secret
key: MAIL_PWD
- name: POOL_SIZE
value: '8'
---
apiVersion: v1
kind: Service
metadata:
name: distribution-srv
spec:
type: ClusterIP
selector:
app: distribution
ports:
- name: distribution
protocol: TCP
port: 3000
targetPort: 3000
在您的配置中,无法从群集内部解析mongdb uri,您必须创建一个外部服务以使uri可解析。请以下面的配置为例。
kind: Deployment
metadata:
name: distribution-depl
spec:
replicas: 1
selector:
matchLabels:
app: distribution
template:
metadata:
labels:
app: distribution
spec:
containers:
- name: distribution
image: ssomlk/distribution
env:
- name: MONGO_URI
value: my-service
- name: JWT_ACCESS_TOKEN_KEY
valueFrom:
secretKeyRef:
name: jwt-secret
key: JWT_ACCESS_TOKEN_KEY
- name: JWT_REFRESH_TOKEN_KEY
valueFrom:
secretKeyRef:
name: jwt-secret
key: JWT_REFRESH_TOKEN_KEY
- name: JWT_ACCESS_TOKEN_EXPIRES_IN
value: '15m'
- name: JWT_REFRESH_TOKEN_EXPIRES_IN
value: '60m'
- name: NATS_CLIENT_ID
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: NATS_URL
value: 'http://nats-srv:4222'
- name: NATS_CLUSTER_ID
value: nats-distribution-mailing
- name: MAIL_USER
valueFrom:
secretKeyRef:
name: mail-secret
key: MAIL_USER
- name: MAIL_PWD
valueFrom:
secretKeyRef:
name: mail-secret
key: MAIL_PWD
- name: POOL_SIZE
value: '8'
---
apiVersion: v1
kind: Service
metadata:
name: distribution-srv
spec:
type: ClusterIP
selector:
app: distribution
ports:
- name: distribution
protocol: TCP
port: 3000
targetPort: 3000
kind: Service
metadata:
name: my-service
namespace: prod
spec:
type: ExternalName
externalName: 'mongodb://ssomlk:<password>@cluster0-shard-00-00.yeu7t.mongodb.net:27017,cluster0-shard-00-01.yeu7t.mongodb.net:27017,cluster0-shard-00-02.yeu7t.mongodb.net:27017/<db_name>?ssl=true&replicaSet=atlas-fznj9q-shard-0&authSource=admin&retryWrites=true&w=majority'```
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句