0

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

我怎么做?

先感谢您!

码头工人-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: {}
4

1 回答 1

2

您在评论中写道:

当我想全部删除时,我运行以下命令:

kubectl delete -f kubernetesFile.yaml

当我再次启动所有服务时:

kubectl apply -f kubernetesFile.yaml

我的数据库是空的。

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

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

于 2022-01-08T13:07:23.683 回答