2

我有一个使用 docker-compose 开发和测试的微服务。现在我想将它部署到 Kubernetes。

我的 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

我使用猫鼬连接到数据库,它工作正常:

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

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

利用 Kompose,我创建了一个部署文件,但是我必须相应地修改持久卷和持久卷声明。

我有这样的事情:

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,如下所示

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: {}

有几个服务我为我的路由添加了一个入口资源:

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”之类的这个。我猜,路径没有链接到数据库服务?任务服务吊舱提供了这个错误

  2. 每当有电涌并且我的机器关闭时,在我从持久卷中删除配置文件之前,启动数据库部署都会失败,如何防止这种文件损坏?

  3. 当我打算将集群转移到不同的网络时,我一直在研究如何更改集群的主 IP。请问有什么指导吗?

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

以上给出了这个: 错误

4

2 回答 2

0

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

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

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

于 2021-01-24T13:03:52.063 回答
0

我相信这是您的 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: {}

当您在 kubernetes 中创建 mongodb 实例时,它会在 pod 中运行。要连接到 pod,我们必须通过集群 IP 服务。每当我们尝试连接到集群 IP 服务时,我们都会为连接 url 的域写入该集群 IP 服务的名称。在这种情况下,您的连接 url 必须是

mongodb://tasksdb:6004/nameOfDatabase

于 2021-04-10T17:36:16.330 回答