Share local directory with Kind Kubernetes Cluster using hostpath

爱⌒轻易说出口 提交于 2021-02-10 14:32:18

问题


I want to share my non-empty local directory with kind cluster.

Based on answer here: How to reference a local volume in Kind (kubernetes in docker)

I tried few variations of the following:

Kind Cluster yaml:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraMounts:
  - hostPath: /Users/xyz/documents/k8_automation/data/manual/
    containerPath: /host_manual
  extraPortMappings:
  - containerPort: 30000
    hostPort: 10000

Pod yaml:

apiVersion: v1
kind: Pod
metadata:
  name: manual 
spec:
  serviceAccountName: manual-sa
  containers:
  - name: tools 
    image: tools:latest 
    imagePullPolicy: Never
    command:
    - bash
    tty: true
    volumeMounts:
    - mountPath: /home/jenkins/agent/data
      name: data
  volumes:
  - name: data
    hostPath: 
      path: /host_manual
      type: Directory
---

I see that the directory /home/jenkins/agent/data does exist when the pod gets created. However, the folder is empty.

kinds documentation here: https://kind.sigs.k8s.io/docs/user/configuration/#extra-mounts

It should be the case that whatever is in the local machine at hostpath (/Users/xyz/documents/k8_automation/data/manual/) in extraMounts in the cluster yaml be available to the node at containerPath (/host_manual), which then gets mounted at container volume mounthPath (/home/jenkins/agent/data).

I should add that even if I change the hostPath in the cluster yaml file to a non-existent folder, the empty "data" folder still gets mounted in the container, so I think it's the connection from my local to kind cluster that's the issue.

  1. Why am I not getting the contents of /Users/xyz/documents/k8_automation/data/manual/ with it's many files also available at /home/jenkins/agent/data in the container?
  2. How can I fix this?
  3. Any alternatives if there is no fix?

回答1:


Turns out these yaml configuration was just fine.

The reason the directory was not showing up in the container was related with docker settings. And because "kind is a tool for running local Kubernetes clusters using Docker container “nodes”", it matters.

It seems docker restricts resource sharing and allows only specific directories to be bind mounted into Docker containers by default. Once I added the specific directory I wanted to show up in the container to the list of directories under Preferences -> Resources -> File sharing, it worked!



来源:https://stackoverflow.com/questions/66104596/share-local-directory-with-kind-kubernetes-cluster-using-hostpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!