单节点Kubernetes集群中无法创建目录的问题求助
Ah, I get it, you're hitting a common gotcha with Docker Desktop's Kubernetes setup—it's not a "regular" single-node cluster running directly on your host OS, but instead the Kubernetes nodes are actually Docker containers themselves. That's why your mkdir commands are failing. Let me break down what's happening and how to fix it:
Why the commands fail
sudo: command not found: The Kubernetes node container (nameddocker-desktop) runs as the root user by default, so you don't need sudo at all. But even better, you shouldn't be trying to modify the node container's filesystem directly anyway.- Read-only filesystem error: The root filesystem of the Docker Desktop Kubernetes node is mounted read-only for stability. Only specific directories (like
/tmpor/var/lib/docker) are writable, but any data there will be lost when the node container restarts—totally useless for persistent storage.
The correct approach: Use hostPath with your host machine's filesystem
Since Docker Desktop maps parts of your host OS's filesystem into the Kubernetes node container, you should create your persistent directory on your actual host machine (not inside the node container), then reference that path in your PersistentVolume (PV) config.
Here's how to do it step-by-step:
Create the data directory on your host
- Mac/Linux: Open your terminal and run:
Make sure this directory is in a location Docker Desktop is allowed to access (by default,mkdir -p ~/k8s-persistent-data/Userson Mac,/homeon Linux are permitted). - Windows: Open Command Prompt or PowerShell and run:
This creates the folder in yourmkdir %USERPROFILE%\k8s-persistent-dataC:\Users\<your-username>directory, which Docker Desktop is pre-configured to access.
- Mac/Linux: Open your terminal and run:
Verify Docker Desktop has access to the directory
- Open Docker Desktop settings:
- Mac: Go to Resources > File Sharing and ensure
/Usersis checked. - Windows: Go to Resources > File Sharing and ensure
C:\Usersis checked.
- Mac: Go to Resources > File Sharing and ensure
- If you created the directory outside these default paths, add it to the allowed list and restart Docker Desktop.
- Open Docker Desktop settings:
Create your PersistentVolume (PV) and PersistentVolumeClaim (PVC)
Create a file namedpv.yamlwith the following content—replace thepathvalue with the correct mapped path for your OS:apiVersion: v1 kind: PersistentVolume metadata: name: task-pv-volume labels: type: local spec: storageClassName: manual capacity: storage: 10Gi accessModes: - ReadWriteOnce hostPath: # Mac: /host_mnt/Users/<your-username>/k8s-persistent-data # Windows: /run/desktop/mnt/host/c/Users/<your-username>/k8s-persistent-data # Linux: /home/<your-username>/k8s-persistent-data path: "/host_mnt/Users/john/k8s-persistent-data"Then create a
pvc.yamlfile:apiVersion: v1 kind: PersistentVolumeClaim metadata: name: task-pv-claim spec: storageClassName: manual accessModes: - ReadWriteOnce resources: requests: storage: 3GiApply the configurations
Run these commands in your terminal:kubectl apply -f pv.yaml kubectl apply -f pvc.yamlMount the PVC in your Pod
When defining your Pod, reference the PVC to mount the persistent storage:apiVersion: v1 kind: Pod metadata: name: task-pv-pod spec: volumes: - name: task-pv-storage persistentVolumeClaim: claimName: task-pv-claim containers: - name: task-pv-container image: nginx ports: - containerPort: 80 name: "http-server" volumeMounts: - mountPath: "/usr/share/nginx/html" name: task-pv-storage
Key Notes
- Any data saved to the mounted path in the Pod will be stored in your host machine's directory, so it survives Kubernetes node restarts or Docker Desktop reboots.
- Avoid modifying the Kubernetes node container's filesystem directly—it's not designed for persistent storage, and changes will be lost.
内容的提问来源于stack exchange,提问作者Snowcrash




