在kubernetes中使用mongoose连接到mongodb [英] Connect to mongodb with mongoose both in kubernetes

查看:71
本文介绍了在kubernetes中使用mongoose连接到mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个微服务,它是使用docker-compose开发和测试的.现在,我想将其部署到kubernetes.

I have a microservice which I developed and tested using docker-compose. Now I would like to deploy it to kubernetes.

我的docker-compose文件的一部分看起来像这样:

Part of my docker-compose file looks like this:

  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:

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

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

我使用Kompose创建了一个部署文件,但是我不得不相应地修改持久卷和持久卷声明.

Utilizing Kompose, I created a deployment file however I had to modify the persistent volume and persistent volume claim accordingly.

我有这样的东西:

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

我更改了mongourl,如下所示此处这样:

I changed the mongourl as shown here like this:

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

我的部署如下:

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

您可以看到.

但是,我有三个问题:

  1. 尽管我可以点击我的默认路径来读取任务服务已启动"这一事实.我无法访问连接到数据库的像/api/task/raise这样的猫鼬路由,它说"..buffering timed out".像.我猜想,该路径没有链接到数据库服务? 任务服务窗格提供此

  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 . I guess, the path does not link up to the database service? the tasks service pod gives this

每当出现电涌并且我的机器关闭时,启动数据库部署都会失败,直到我从持久卷中删除配置文件为止,如何防止文件损坏?

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?

当我打算将群集转移到其他网络时,我一直在进行精心研究,以更改群集的主IP.有任何指导吗?

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. Any guidance please?

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

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

上面给出了这个:

推荐答案

您的tasksdb服务公开端口6004,而不是27017.尝试使用以下URL:

Your tasksdb Service exposes port 6004, not 27017. Try using the following URL:

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

更改网络取决于您所使用的网络CNI插件.每个插件都有不同的步骤.对于Calico,请参阅 https://docs.projectcalico.org/networking/migrate-pools

Changing your network depends on what networking CNI plugin you are using. Every plugin has different steps . For Calico please see https://docs.projectcalico.org/networking/migrate-pools

这篇关于在kubernetes中使用mongoose连接到mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆