首页 > 解决方案 > 如何使用 emptyDir 在 Kubernetes 上挂载卷

问题描述

我正在尝试从我的 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

标签: dockerkubernetesdocker-volume

解决方案


您需要查看收到的警告:

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:

持久卷声明

看看这个链接:转换矩阵。它描述了如何kompose将 Docker 的卷转换为 Kubernetes 的卷。

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

$ kompose convert -f docker-compose.yml

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

看看web-deployment.yaml音量部分:

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

推荐阅读