You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

单节点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 (named docker-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 /tmp or /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:

  1. Create the data directory on your host

    • Mac/Linux: Open your terminal and run:
      mkdir -p ~/k8s-persistent-data
      
      Make sure this directory is in a location Docker Desktop is allowed to access (by default, /Users on Mac, /home on Linux are permitted).
    • Windows: Open Command Prompt or PowerShell and run:
      mkdir %USERPROFILE%\k8s-persistent-data
      
      This creates the folder in your C:\Users\<your-username> directory, which Docker Desktop is pre-configured to access.
  2. Verify Docker Desktop has access to the directory

    • Open Docker Desktop settings:
      • Mac: Go to Resources > File Sharing and ensure /Users is checked.
      • Windows: Go to Resources > File Sharing and ensure C:\Users is checked.
    • If you created the directory outside these default paths, add it to the allowed list and restart Docker Desktop.
  3. Create your PersistentVolume (PV) and PersistentVolumeClaim (PVC)
    Create a file named pv.yaml with the following content—replace the path value 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.yaml file:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: task-pv-claim
    spec:
      storageClassName: manual
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 3Gi
    
  4. Apply the configurations
    Run these commands in your terminal:

    kubectl apply -f pv.yaml
    kubectl apply -f pvc.yaml
    
  5. Mount 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

火山引擎 最新活动