Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

docker - Connect to mongodb with mongoose both in kubernetes

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

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);
  
 };

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

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:

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 here.

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

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

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

the above gives this : error

question from:https://stackoverflow.com/questions/65870380/connect-to-mongodb-with-mongoose-both-in-kubernetes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...