从 kubernetes pod 连接到 mongodb atlas 集群?

时间:2021-06-13 07:44:57

标签: node.js mongodb kubernetes

无法使用 kubernetes pod 连接到 mongodb atlas。尝试了互联网上几乎所有可用的方法,但没有成功。

yaml 文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: weare-auth-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: weare-auth
  template:
    metadata:
      labels:
        app: weare-auth
    spec:
      containers:
        - name: weare-auth
          image: <docker-username>/weare-auth
          env:
            - name: PORT
              value: "3001"
      restartPolicy: Always
      dnsPolicy: Default
---
apiVersion: v1
kind: Service
metadata:
  name: weare-auth-srv
spec:
  selector:
    app: weare-auth
  ports:
    - name: weare-auth
      protocol: TCP
      port: 3001
      targetPort: 3001

这是我的快递代码

import mongoose from "mongoose";

import { app } from "./app";

const start = async () => {
  try {
    await mongoose.connect(
      "mongodb://<username>:<password>@test-clus.wpg0x.mongodb.net/auth",
      {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
      }
    );
    console.log("Connected to mongodb");

    const PORT = process.env.PORT || 3001;
    app.listen(PORT, () => {
      console.log(`Listening on port ${PORT}`);
    });
  } catch (err) {
    console.error(err);
  }
};

start();

这是日志

Find the screenshot of error logs here

我屏蔽了凭据。我也可以通过 shell 和 Robo3T 连接到 mongodb atlas 集群。还尝试像帖子中提到的那样设置 dnsPolicy,但没有成功。

知道我在这里遗漏了什么吗?

0 个答案:

没有答案