首页 > 解决方案 > Kubernetes 卷挂载设置类型检查失败

问题描述

我无法在 kubernetes 中部署应用程序,这是部署 yaml。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  labels:
    app: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: openjdk:14
        ports:
        - containerPort: 8080
        volumeMounts: 
        - name: testing
          mountPath: "/usr/src/myapp/docker.jar"
        workingDir: "/usr/src/myapp"
        command: ["java"]
        args: ["-jar", "docker.jar"]        
      volumes: 
      - hostPath: 
          path: /home/user/docker.jar
          type: File
        name: testing

这是我收到的错误,我可以验证该文件确实存在并且在此文件夹中。我尝试删除该类型,但它只上传一个空目录,似乎无法识别该文件存在。

MountVolume.SetUp failed for volume "testing" : hostPath type check failed: /home/user/docker.jar is not a file

标签: kubernetes

解决方案


当您使用默认驱动程序启动minikube时,您是在机器内部创建。dockerDocker VM

背景

当你使用你的虚拟机时,你的终端看起来像:

user@nameofyourhost:~$

但是,如果您愿意ssh,您的Minikube VM终端将如下所示:

docker@minikube:~$ 

HostPath你有信息:

hostPath 卷将文件或目录从主机节点的文件系统安装到您的 Pod 中。

minikube您使用时hostPath,节点被认为不是您的,machine而是Minikube VM在期间创建的minikube start

测试

因为我没有你的文件,所以我用过nginx. 请记住,您应该对要挂载的目录具有适当的权限。我tmpnginx这个目录中使用过,每个人都可以完全访问。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  labels:
    app: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: nginx
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: testing
          mountPath: "/tmp/docker.jar"   #this is path to the file
      volumes:
      - hostPath:
          path: <path to docker.jar file> #should be on Minikube VM
          type: File
        name: testing

docker.jar在 VM 上创建时。

user@nameOfMyVM:~$ pwd
/home/user
user@nameOfMyVM:~$ ls
docker.jar

当你hostpath.path进入/home/user/docker.jar它时会返回警告

Warning  FailedMount  4s (x4 over 7s)  kubelet            MountVolume.SetUp failed for volume "testing" : hostPath type check failed: /home/sekreta/docker.jar is not a file`

由于 Kubernetes 没有找到这个文件。

但是当你在里面创建这个文件时Minikube VM

$ minikube ssh
docker@minikube:~$ pwd
/home/docker
docker@minikube:~$ ls
docker.jar

hostPath.path并将创建对/home/docker/docker.jarpod的部署更改。

$ kubectl get po
NAME                   READY   STATUS    RESTARTS   AGE
test-c68d959c6-kb275   1/1     Running   0          13s

文件可以在 YAML 中设置的目录中找到,即tmp.

$ kubectl exec -ti test-c68d959c6-kb275 -- bin/bash
root@test-c68d959c6-kb275:/# cd /tmp
root@test-c68d959c6-kb275:/tmp# ls
docker.jar

结论

当您使用HostPathon时,Minikube您需要记住这filesystem不是您的Machine,而是Minikube VMminikube start.


推荐阅读