首页 > 解决方案 > MountPath 未列出文件

问题描述

全部,

我部署了下面的 yaml 文件并部署了它。我正在学习 Volume、VolumeMount 和 MountPath。

我看到在容器的挂载路径中指定的containerdrive文件夹(我做了 kubectl exec -it podname),并且在containerdrive 中我希望看到路径中指定的文件夹“ /home/rgn/kubernetes/scripts ”中的文件的主机路径。但事实并非如此,实际上 containerdrive 是空的。

看来我对 Volume、VolumeMount 和 MountPath 的理解是错误的。我错过了什么?有人可以指出我正确的方向吗?

谢谢,rgn

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.4
        ports:
        - containerPort: 80
        volumeMounts:
          - mountPath: /containerdrive
            name: test-volume
      volumes:
      - name: test-volume
        hostPath:
      # directory location on host
          path: /home/rgn/kubernetes/scripts
      # this field is optional
      #    type: DirectoryOrCrate

标签: kubernetesyamlkubectlkubernetes-pod

解决方案


看来我对 Volume、VolumeMount 和 MountPath 的理解是错误的。我错过了什么?有人可以指出我正确的方向吗?

     hostPath:
   # directory location on host
       path: /home/rgn/kubernetes/scripts

该卷将Kubernetes (k8s) 主机节点的文件系统hostPath中的文件或目录安装到您的 Pod 中。这不是大多数 Pod 需要的东西,但它为某些应用程序提供了强大的逃生舱口。这是指运行容器的 k8s 节点上的路径。pathnginx

要回答您的具体情况:请确保该目录/home/rgn/kubernetes/scripts存在于您的 k8s 节点上。

我刚刚在我自己的 gke 集群(2 个节点)上运行了部署问题

$kubectl get nodes 
NAME                                         
gke-ubuntu-mnnv   
gke-ubuntu-nw9v

$ kubectl apply -f deployment.yaml


$ kubectl get pods -o wide 
NAME                               READY   STATUS    RESTARTS   AGE    NODE                                         
nginx-deployment-ddbfb785d-fggpq   1/1     Running   0          19s    gke-ubuntu-nw9v


$ kubectl exec nginx-deployment-ddbfb785d-fggpq -- ls -la containerdrive
total 8
drwxr-xr-x 2 root root 4096 Jun 22 11:54 .
drwxr-xr-x 1 root root 4096 Jun 22 11:55 ..
-rw-r--r-- 1 root root    0 Jun 22 11:54 gke-ubuntu-nw9v-file

$ kubectl describe deployments nginx-deployment 
...
    Mounts:
      /containerdrive from test-volume (rw)
  Volumes:
   test-volume:
    Type:          HostPath (bare host directory volume)
    Path:          /tmp/knp
    HostPathType:  
...

# and on k8s node itself:

$ ls -lah /tmp/knp/
total 8.0K
drwxr-xr-x  2 root root 4.0K Jun 22 11:54 .
drwxrwxrwt 10 root root 4.0K Jun 22 12:08 ..
-rw-r--r--  1 root root    0 Jun 22 11:54 gke-ubuntu-nw9v-file

我希望输出有助于清楚地看到整个画面。:)

此外,正如 David Maze 所提到的,将您依赖的所有脚本编译到容器本身中可能是一个好主意。


推荐阅读