【问题标题】:MountPath is not listing the filesMountPath 未列出文件
【发布时间】:2020-10-11 00:02:36
【问题描述】:

全部,

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

我看到容器的挂载路径中指定的 containerdrive 文件夹(我做了一个 kubectl exec -it podname)和 containerdrive 我希望看到这些文件在 HostPath 的 Path 中指定的文件夹“/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

【问题讨论】:

  • 你能在那个目录中创建任何东西吗?在容器中,或者你得到权限错误?
  • hostPath 设置是指运行容器的节点上的非容器目录。它不是指向本地系统的指针,并且可能是不同时间点的不同节点。通常你只将hostPath 卷与日志收集器之类的东西一起使用。包含此类内容(尤其是小规模内容)的最简单方法是将其包含在您正在运行的图像中。

标签: kubernetes yaml kubectl kubernetes-pod


【解决方案1】:

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

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

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

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

我刚刚在我自己的 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 所提到的,将您依赖的所有脚本编译到容器本身中可能是个好主意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多