简体   繁体   English

连接到 mongodb 与 mongoose 两者都在 kubernetes

[英]Connect to mongodb with mongoose both in kubernetes

I have a microservice which I developed and tested using docker-compose.我有一个使用 docker-compose 开发和测试的微服务。 Now I would like to deploy it to kubernetes.现在我想将它部署到 kubernetes。

Part of my docker-compose file looks like this:我的 docker-compose 文件的一部分如下所示:

  tasksdb:
    container_name: tasks-db
    image: mongo:4.4.1
    restart: always
    ports:
      - '6004:27017'
    volumes:
      - ./tasks_service/tasks_db:/data/db
    networks:
      - backend

  tasks-service:
    container_name: tasks-service
    build: ./tasks_service
    restart: always
    ports:
      - "5004:3000"
    volumes:
      - ./tasks_service/logs:/usr/src/app/logs
      - ./tasks_service/tasks_attachments/:/usr/src/app/tasks_attachments
    depends_on:
      - tasksdb
    networks:
      - backend

I used mongoose to connect to the database and it worked fine:我使用 mongoose 连接到数据库,它工作正常:

const connection = "mongodb://tasks-db:27017/tasks"; 

const connectDb = () => {
   mongoose.connect(connection, {useNewUrlParser:true, useCreateIndex:true, useFindAndModify: false});
   return mongoose.connect(connection);
  
 };

Utilizing Kompose, I created a deployment file however I had to modify the persistent volume and persistent volume claim accordingly.利用 Kompose,我创建了一个部署文件,但是我必须相应地修改持久卷和持久卷声明。

I have something like this:我有这样的事情:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: tasks-volume
  labels:
    type: local
spec:
  storageClassName: manual
  volumeMode: Filesystem
  capacity:
    storage: 10Gi 
  accessModes:
    - ReadWriteMany
  nfs:
    server: 192.168.60.50
    path: /tasks_db

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: tasksdb-claim0
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

I changed the mongourl as shown here like this:我改变了 mongourl,如下所示

const connection = "mongodb://tasksdb.default.svc.cluster.local:27017/tasks";

My deployment looks like this:我的部署如下所示:

apiVersion: v1
items:
  - apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o k8manifest.yml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: tasks-service
      name: tasks-service
    spec:
      ports:
        - name: "5004"
          port: 5004
          targetPort: 3000
      selector:
        io.kompose.service: tasks-service

    status:
      loadBalancer: {}
  - apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o k8manifest.yml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: tasksdb
      name: tasksdb
    spec:
      ports:
        - name: "6004"
          port: 6004
          targetPort: 27017
      selector:
        io.kompose.service: tasksdb
    status:
      loadBalancer: {}
  - apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o k8manifest.yml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: tasks-service
      name: tasks-service
    spec:
      replicas: 1
      selector:
        matchLabels:
          io.kompose.service: tasks-service
      strategy:
        type: Recreate
      template:
        metadata:
          annotations:
            kompose.cmd: kompose convert -f docker-compose.yml -o k8manifest.yml
            kompose.version: 1.22.0 (955b78124)
          creationTimestamp: null
          labels:
            io.kompose.service: tasks-service
        spec:
          containers:
            - image: 192.168.60.50:5000/blascal_tasks-service
              name: tasks-service
              imagePullPolicy: IfNotPresent
              ports:
                - containerPort: 3000
          restartPolicy: Always
    status: {}
  - apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o k8manifest.yml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: tasksdb
      name: tasksdb
    spec:
      replicas: 1
      selector:
        matchLabels:
          io.kompose.service: tasksdb
      strategy:
        type: Recreate
      template:
        metadata:
          annotations:
            kompose.cmd: kompose convert -f docker-compose.yml -o k8manifest.yml
            kompose.version: 1.22.0 (955b78124)
          creationTimestamp: null
          labels:
            io.kompose.service: tasksdb
        spec:
          containers:
            - image: mongo:4.4.1
              name: tasks-db
              ports:
                - containerPort: 27017 
              resources: {}
              volumeMounts:
                - mountPath: /data/db
                  name: tasksdb-claim0
          restartPolicy: Always
          volumes:
            - name: tasksdb-claim0
              persistentVolumeClaim:
                claimName: tasksdb-claim0
    status: {}

Having several services I added an ingress resource for my routing:有几个服务我为我的路由添加了一个入口资源:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          serviceName: tasks-service
          servicePort: 5004

The deployment seems to run fine as you can see如您所见,部署似乎运行良好这里 . .

However, I have three issues:但是,我有三个问题:

  1. Despite the fact that I can hit my default path which just reads "tasks service is up" I cannot access my mongoose routes like /api/task/raise which connects to the db, it says "..buffering timed out" like尽管我可以点击我的默认路径,它只是读取“任务服务已启动”,但我无法访问我的 mongoose 路由,例如 /api/task/raise 连接到数据库,它说“..buffering timed out”就像这个 . . I guess, the path does not link up to the database service?我猜,路径没有链接到数据库服务? the tasks service pod gives this任务服务吊舱提供了这个错误

  2. Whenever there is a power surge and my machine goes off, bringing up the db deployment fails until I delete the config files from the persistent volume, how do I prevent this corruption of files?每当有电涌并且我的机器关闭时,在我从持久卷中删除配置文件之前,启动数据库部署都会失败,如何防止这种文件损坏?

  3. I have been researching in an elaborate way of changing the master ip of my cluster as I intend to transfer my cluster to a different network.我一直在研究如何更改集群的主 ip,因为我打算将集群转移到不同的网络。 Any guidance please?请问有什么指导吗?

    kubectl logs --namespace=kube-system -l k8s-app=kube-dns kubectl 日志 --namespace=kube-system -l k8s-app=kube-dns

the above gives this:以上给出了这个: 错误

Your tasksdb Service exposes port 6004, not 27017. Try using the following URL:您的tasksdb服务公开端口 6004,而不是 27017。尝试使用以下 URL:

const connection = "mongodb://tasksdb.default.svc.cluster.local:6004/tasks";

Changing your network depends on what networking CNI plugin you are using.更改网络取决于您使用的网络 CNI 插件。 Every plugin has different steps.每个插件都有不同的步骤。 For Calico please see https://docs.projectcalico.org/networking/migrate-pools对于印花布,请参阅https://docs.projectcalico.org/networking/migrate-pools

I believe this is your cluster ip setting for mongodb instance:我相信这是您为 mongodb 实例设置的集群 ip 设置:

apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o k8manifest.yml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: tasksdb
      name: tasksdb
    spec:
      ports:
        - name: "6004"
          port: 6004
          targetPort: 27017
      selector:
        io.kompose.service: tasksdb
    status:
      loadBalancer: {}

When you create an instance of mongodb inside kubernetes, it runs inside a pod.当您在 kubernetes 中创建 mongodb 的实例时,它会在 pod 中运行。 To connect to a pod, we have to go through the cluster IP service.要连接到 pod,我们必须通过集群 IP 服务连接到 go。 Anytime we are trying to connect to a cluster IP service we are going to write the name of that cluster iP service for the domain of connection url.每当我们尝试连接到集群 IP 服务时,我们将为连接域 url 写入该集群 iP 服务的名称。 in this case you connection url must be在这种情况下,您必须连接 url

mongodb://tasksdb:6004/nameOfDatabase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 mongoose 连接到 mongodb 数据库 - Cant connect to mongodb database using mongoose mongoose 无法连接到 docker 中的 mongodb - mongoose couldn't connect to mongodb in docker 无法使用 docker Kubernetes 连接到 mongoDB - Unable to connect to the mongoDB using docker Kubernetes 无法连接到 MongoDB:MongoNetworkError & MongoNetworkError connected to kubernetis MongoDB pod with mongoose - Unable to connect to MongoDB: MongoNetworkError & MongoNetworkError connecting to kubernetis MongoDB pod with mongoose 尝试连接到有状态集Mongodb Kubernetes部署时没有连接错误 - No connection error when attempting to connect to a stateful set Mongodb Kubernetes deployment 无法从主机上的猫鼬连接到mongodb docker容器 - Failed to connect to a mongodb docker container from mongoose on host Mongoose nodejs - 身份验证失败尝试连接到 mongodb docker - Mongoose nodejs - authentication failed trying to connect to mongodb docker 当 mongoose 尝试连接到在 docker 中运行的 mongodb 实例时,连接超时 - Connection time out when mongoose attempts to connect to a mongodb instance running in docker Node.js应用程序无法使用Kubernetes连接到MongoDB Docker容器 - Node.js application fails to connect to MongoDB Docker container using Kubernetes GCP 中 Kubernetes 中的 MongoDB - MongoDB in Kubernetes within GCP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM