首页 > 解决方案 > Minikube 挂载的主机文件夹不起作用

问题描述

我正在使用带有 minikube 和虚拟盒的 ubuntu 18,并尝试挂载主机的目录以获取我的 pod 需要的输入数据。

我发现 minikube 有挂载主机目录的问题,但是根据您的操作系统和 vm 驱动程序,默认情况下,有些目录是默认挂载的

我在我的豆荚上找不到那些。他们根本不在那里。

我试图创建一个持久卷,它可以工作,我可以在仪表板上看到它,但我无法将它安装到 pod,我使用这个 yaml 来创建卷

{
  "kind": "PersistentVolume",
  "apiVersion": "v1",
  "metadata": {
    "name": "pv0003",
    "selfLink": "/api/v1/persistentvolumes/pv0001",
    "uid": "28038976-9ee4-414d-8478-b312a24a6b94",
    "resourceVersion": "2030",
    "creationTimestamp": "2019-08-08T10:48:23Z",
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"PersistentVolume\",\"metadata\":{\"annotations\":{},\"name\":\"pv0001\"},\"spec\":{\"accessModes\":[\"ReadWriteOnce\"],\"capacity\":{\"storage\":\"5Gi\"},\"hostPath\":{\"path\":\"/data/pv0001/\"}}}\n"
    },
    "finalizers": [
      "kubernetes.io/pv-protection"
    ]
  },
  "spec": {
    "capacity": {
      "storage": "6Gi"
    },
    "hostPath": {
      "path": "/user/data",
      "type": ""
    },
    "accessModes": [
      "ReadWriteOnce"
    ],
    "persistentVolumeReclaimPolicy": "Retain",
    "volumeMode": "Filesystem"
  },
  "status": {
    "phase": "Available"
  }
}

而这个 yaml 来创建工作。

apiVersion: batch/v1
kind: Job
metadata:
  name: pi31
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["sleep"]
        args: ["300"]
        volumeMounts:
        - mountPath: /data
          name: pv0003
      volumes:
        - name: pv0003
          hostPath:
            path: /user/data
      restartPolicy: Never
  backoffLimit: 1

我还尝试根据所谓的默认安装路径创建卷,但没有成功。

我试图将卷声明添加到作业创建 yaml 中,仍然没有。

当我挂载驱动器并在作业创建 yaml 文件中创建它们时,作业能够看到其他作业创建的数据,但它对主机是不可见的,并且主机的数据对它们是不可见的。

我正在从我的主要用户运行 minikube,并检查了仪表板中的日志,没有收到任何权限错误

有什么方法可以在不设置 NFS 的情况下将数据导入这个 minikube?我正在尝试将它用于 MVP,整个想法是让它变得简单......

标签: dockerubuntukubernetesdevopskubernetes-pod

解决方案


这并不容易,因为 minikube 在 Virtualbox 中创建的 VM 中工作,这就是为什么使用 hostPath 您会看到该 VM 的文件系统而不是您的 PC。

我真的建议使用minikube mount命令 - 你可以在那里找到描述

来自文档:

minikube mount /path/to/dir/to/mount:/vm-mount-path 是将目录挂载到 minikube 的推荐方式,以便它们可以在本地 Kubernetes 集群中使用。

因此,之后您可以在 minikube Kubernetes 中共享主机的文件。

编辑:

这是逐步记录如何测试它的方法:

➜  ~ minikube start
* minikube v1.3.0 on Ubuntu 19.04
* Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
* Starting existing virtualbox VM for "minikube" ...
* Waiting for the host to be provisioned ...
* Preparing Kubernetes v1.15.2 on Docker 18.09.6 ...
* Relaunching Kubernetes using kubeadm ... 
* Waiting for: apiserver proxy etcd scheduler controller dns
* Done! kubectl is now configured to use "minikube"
➜  ~ mkdir -p /tmp/test-dir
➜  ~ echo "test-string" > /tmp/test-dir/test-file
➜  ~ minikube mount /tmp/test-dir:/test-dir
* Mounting host path /tmp/test-dir into VM as /test-dir ...
  - Mount type:   <no value>
  - User ID:      docker
  - Group ID:     docker
  - Version:      9p2000.L
  - Message Size: 262144
  - Permissions:  755 (-rwxr-xr-x)
  - Options:      map[]
* Userspace file server: ufs starting
* Successfully mounted /tmp/test-dir to /test-dir

* NOTE: This process must stay alive for the mount to be accessible ...

现在打开另一个控制台:

➜  ~ minikube ssh
                         _             _            
            _         _ ( )           ( )           
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __  
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

$ cat /test-dir/test-file 
test-string

编辑2:

示例作业.yml

apiVersion: batch/v1
kind: Job
metadata:
  name: test
spec:
  template:
    spec:
      containers:
      - name: test
        image: ubuntu
        command: ["cat", "/testing/test-file"]
        volumeMounts:
        - name: test-volume
          mountPath: /testing
      volumes:
      - name: test-volume
        hostPath:
          path: /test-dir
      restartPolicy: Never
  backoffLimit: 4

推荐阅读