如何使用 emptyDir 在 kubernetes 上挂载卷

How to mount volumes on kubernetes using emptyDir

我正在尝试从我的 kompose 文件中创建部署,但每当我尝试时:

kompose convert -f docker-compose.yaml

我收到错误:

Volume mount on the host "[file directory]" isn't supported - ignoring path on the host

我尝试了几种不同的解决方案来解决我的问题,首先尝试将 hostPath 添加到我的 kompose convert 以及使用持久卷,但是两者都不起作用。

我的 kompose 文件如下所示:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert -f docker-compose.yaml --volumes emptyDir
    kompose.version: 1.7.0 (HEAD)
  creationTimestamp: null
  labels:
    io.kompose.service: es01
  name: es01
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: es01
    spec:
      containers:
      - env:
        - name: COMPOSE_CONVERT_WINDOWS_PATHS
          value: "1"
        - name: COMPOSE_PROJECT_NAME
          value: elastic_search_container
        - name: ES_JAVA_OPTS
          value: -Xms7g -Xmx7g
        - name: discovery.type
          value: single-node
        - name: node.name
          value: es01
        image: docker.elastic.co/elasticsearch/elasticsearch:7.2.1
        name: es01
        ports:
        - containerPort: 9200
        resources: {}
        volumeMounts:
        - mountPath: /usr/share/elasticsearch/data
          name: es01-empty0
      restartPolicy: Always
      volumes:
      - emptyDir: {}
        name: es01-empty0
status: {}

我正在使用 kompose 版本 1.7.0

我的Docker撰写版本:

version: '3'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.2.1
    container_name: es01
    environment:
      - node.name=es01
      - COMPOSE_PROJECT_NAME=elastic_search_container
      - discovery.type=single-node
      - "ES_JAVA_OPTS=-Xms7g -Xmx7g"
      - COMPOSE_CONVERT_WINDOWS_PATHS=1
    ulimits:
      nproc: 3000
      nofile: 65536
      memlock: -1
    volumes:
      - /home/centos/Sprint0Demo/Servers/elasticsearch:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - kafka_demo

您需要查看收到的警告:

Volume mount on the host "[file directory]" isn't supported - ignoring path on the host

docker-compose.yaml 中的卷配置为直接路径时发生。

示例如下:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - "./storage1:/test1"
      - "./storage2:/test2"
  redis:
    image: "redis:alpine"
volumes:
  storage1:
  storage2:

持久卷声明

看看这个 link:Conversion matrix。 它描述了 kompose 如何将 Docker 的卷转换为 Kubernetes 的卷。

执行不带--volumes参数的转换命令:

$ kompose convert -f docker-compose.yml

使用 kompose 1.19 输出将产生:

WARN Volume mount on the host "SOME_PATH" isn't supported - ignoring path on the host 
WARN Volume mount on the host "SOME_PATH" isn't supported - ignoring path on the host 
INFO Kubernetes file "web-service.yaml" created   
INFO Kubernetes file "redis-deployment.yaml" created 
INFO Kubernetes file "web-deployment.yaml" created 
INFO Kubernetes file "web-claim0-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "web-claim1-persistentvolumeclaim.yaml" created

警告消息意味着您明确告诉 docker-compose 使用直接路径创建卷。默认情况下 kompose 会将 Docker 的音量转换为 Persistent Volume Claim

空目录

正在执行带有--volumes emptyDir参数的转换命令:

$ kompose convert -f docker-compose.yml --volumes emptyDir

将产生效果:

WARN Volume mount on the host "SOME_PATH" isn't supported - ignoring path on the host 
WARN Volume mount on the host "SOME_PATH" isn't supported - ignoring path on the host 
INFO Kubernetes file "web-service.yaml" created   
INFO Kubernetes file "redis-deployment.yaml" created 
INFO Kubernetes file "web-deployment.yaml" created 

kompose 将在 web-deployment.yaml 中创建 emptyDir 声明,而不是像默认情况下那样创建单独的 PVC 定义。

主机路径

正在执行带有--volumes hostPath参数的转换命令:

$ kompose convert -f docker-compose.yml --volumes hostPath

将产生效果:

INFO Kubernetes file "web-service.yaml" created   
INFO Kubernetes file "redis-deployment.yaml" created 
INFO Kubernetes file "web-deployment.yaml" created 

如您所见,没有关于不受支持路径的警告。没有警告,因为它使用您自己从 docker-compose.yml 提供的路径显式创建了 hostPath

查看 web-deployment.yaml 卷部分:

      volumes:
      - hostPath:
          path: /LOCAL_PATH-/POD_PATH/storage1
        name: web-hostpath0
      - hostPath:
          path: /LOCAL_PATH-/POD_PATH/storage2
        name: web-hostpath1