Compose for docker-compose 不使用卷

Kompose for docker-compose not use volumes

我使用 Kompose 将 docker-compose 转换为 Kubernetes 文件。当我在 Docker(无 Kubernetes)上使用图像 运行ned 时,它使用 postgres 卷来拥有持久数据。现在,使用相同的 docker-compose,如果我 运行 使用 Kubernetes 使用 Kompose 创建的文件,每次启动 Kubernetes 时数据库都是空的。 这怎么可能?如果我查看 Docker Desktop,实际上,我看到卷没有标签“IN USE”。

我该怎么做?

提前致谢!

docker-compose.yaml:

version: '3'
services:
  eureka-server:
    image: com.petcare/eureka-server:0.0.1-SNAPSHOT
    container_name: eureka-server
    expose:
      - 8761

  auth-service:
    image: com.petcare/auth-service:0.0.1-SNAPSHOT
    container_name: auth-service
    expose:
      - 8081
    depends_on:
      - eureka-server
      - postgresqldb

  map-service:
    image: com.petcare/map-service:0.0.1-SNAPSHOT
    container_name: map-service
    expose:
      - 8082
    depends_on:
      - eureka-server
      - postgresqldb

  reservation-service:
    image: com.petcare/reservation-service:0.0.1-SNAPSHOT
    container_name: reservation-service
    expose:
      - 8083
    depends_on:
      - eureka-server
      - postgresqldb

  user-service:
    image: com.petcare/user-service:0.0.1-SNAPSHOT
    container_name: user-service
    expose:
      - 8084
    depends_on:
      - eureka-server
      - postgresqldb

  api-gateway:
    image: com.petcare/api-gateway:0.0.1-SNAPSHOT
    container_name: api-gateway
    expose:
      - 8080
    depends_on:
      - eureka-server
    labels:
      kompose.service.type: NodePort
      kompose.service.nodeport.port: "30000"

  postgresqldb:
    image: postgres
    container_name: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=websitepetcare
    volumes:
      - database-data:/var/lib/postgresql/data/
    labels:
      kompose.service.type: NodePort
      kompose.service.nodeport.port: "30002"

  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: root
      PGADMIN_LISTEN_PORT: 50
    ports:
      - "5050:50"
    volumes:
      - pgadmin-data:/var/lib/pgadmin
    links:
      - "postgresqldb:pgsql-server"
    depends_on:
      - postgresqldb
    labels:
      kompose.service.type: NodePort
      kompose.service.nodeport.port: "30001"

volumes:
  database-data:
    external: true
  pgadmin-data:
    external: true

使用 kompose convert 创建的文件:

apiVersion: v1
items:
  - apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        kompose.cmd: C:\ProgramData\chocolatey\lib\kubernetes-kompose\tools\kompose.exe convert -o kubernetes-services-deployments.yaml
        kompose.service.nodeport.port: "30001"
        kompose.service.type: NodePort
        kompose.version: 1.26.0 (40646f47)
      creationTimestamp: null
      labels:
        io.kompose.service: pgadmin
      name: pgadmin
    spec:
      replicas: 1
      selector:
        matchLabels:
          io.kompose.service: pgadmin
      strategy:
        type: Recreate
      template:
        metadata:
          annotations:
            kompose.cmd: C:\ProgramData\chocolatey\lib\kubernetes-kompose\tools\kompose.exe convert -o kubernetes-services-deployments.yaml
            kompose.service.nodeport.port: "30001"
            kompose.service.type: NodePort
            kompose.version: 1.26.0 (40646f47)
          creationTimestamp: null
          labels:
            io.kompose.service: pgadmin
        spec:
          containers:
            - env:
                - name: PGADMIN_DEFAULT_EMAIL
                  value: admin@admin.com
                - name: PGADMIN_DEFAULT_PASSWORD
                  value: root
                - name: PGADMIN_LISTEN_PORT
                  value: "50"
              image: dpage/pgadmin4
              name: pgadmin4-container
              ports:
                - containerPort: 50
              resources: {}
              volumeMounts:
                - mountPath: /var/lib/pgadmin
                  name: pgadmin-data
          restartPolicy: Always
          volumes:
            - name: pgadmin-data
              persistentVolumeClaim:
                claimName: pgadmin-data
    status: {}
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: pgadmin-data
      name: pgadmin-data
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 100Mi
    status: {}
  - apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        kompose.cmd: C:\ProgramData\chocolatey\lib\kubernetes-kompose\tools\kompose.exe convert -o kubernetes-services-deployments.yaml
        kompose.service.nodeport.port: "30002"
        kompose.service.type: NodePort
        kompose.version: 1.26.0 (40646f47)
      creationTimestamp: null
      labels:
        io.kompose.service: postgresqldb
      name: postgresqldb
    spec:
      replicas: 1
      selector:
        matchLabels:
          io.kompose.service: postgresqldb
      strategy:
        type: Recreate
      template:
        metadata:
          annotations:
            kompose.cmd: C:\ProgramData\chocolatey\lib\kubernetes-kompose\tools\kompose.exe convert -o kubernetes-services-deployments.yaml
            kompose.service.nodeport.port: "30002"
            kompose.service.type: NodePort
            kompose.version: 1.26.0 (40646f47)
          creationTimestamp: null
          labels:
            io.kompose.service: postgresqldb
        spec:
          containers:
            - env:
                - name: POSTGRES_DB
                  value: websitepetcare
                - name: POSTGRES_PASSWORD
                  value: postgres
                - name: POSTGRES_USER
                  value: postgres
              image: postgres
              name: postgres
              ports:
                - containerPort: 5432
              resources: {}
              volumeMounts:
                - mountPath: /var/lib/postgresql/data/
                  name: database-data
          restartPolicy: Always
          volumes:
            - name: database-data
              persistentVolumeClaim:
                claimName: database-data
    status: {}
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: database-data
      name: database-data
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 100Mi
    status: {}
kind: List
metadata: {}

在您写的评论中:

When I want to delete all, I run this command:

kubectl delete -f kubernetesFile.yaml

When I start again all services with:

kubectl apply -f kubernetesFile.yaml

I have the database empty.

Kompose 转换的 kubernetesFile.yaml 包含所有 Kubernetes 资源,包括告诉它保存持久数据的 PersistentVolumeClaim。因此,当您 kubectl delete -f kubernetesFile.yaml 时,它会删除 PersistentVolumeClaim 和数据,然后当您重新 apply 它时,您会得到一个新的空卷。

这里不需要运行kubectl delete。当您的设置更改时,只需 运行 kubectl apply -f kubernetesFile.yaml。更新的任何内容都将更改,集群将根据需要删除并重新创建 Pods。任何未更新的内容都将保持不变。如果您删除 Deployment 或其他资源,则必须手动删除它 (kubectl delete deployment unused-deployment)。