Kubernetes多节点集群Pod目录挂载方案咨询(无需外部存储)
Hey there! Let's break this down clearly since you're new to Kubernetes—no jargon overload, promise.
无需外部存储(如NFS)的直接解决方案
Since you're running a multi-node cluster and want to avoid external storage servers, here are your best options:
1. Local Persistent Volumes (Local PV)
This is exactly what you're looking into, and it's the most robust solution for persistent storage without external servers. Local PV uses direct-attached storage (DAS) on your Kubernetes nodes—so you're using the disks already connected to each node, no extra NFS or cloud storage needed.
Key points to know:
- Data is stored on a specific node's local disk, so you need to ensure your Pods are scheduled to that node (via node affinity or node selectors) to access the data.
- It's great for workloads that need low-latency access to storage (like databases or caching layers) or where data doesn't need to be shared across nodes.
- Unlike HostPath, Local PV is managed by Kubernetes, so it integrates properly with PVCs and the storage lifecycle.
2. HostPath Volumes
A simpler but less production-friendly option. HostPath mounts a directory from the node's filesystem directly into a Pod. While it doesn't require external storage, it has big limitations:
- If your Pod gets rescheduled to a different node, it won't be able to access the original data (since it's tied to the first node).
- It's not managed by Kubernetes, so there's no built-in way to track or reclaim the storage.
- Best used for testing or single-node clusters, not multi-node production setups.
3. EmptyDir Volumes
If you only need temporary storage (data that can be lost when the Pod is deleted), EmptyDir is a good fit. It creates a temporary directory shared between containers in a Pod, but it's not persistent—data is wiped when the Pod is removed. Useful for things like caching or temporary file processing, but not for long-term data storage.
Learning Local Persistent Volumes (No External Links Needed)
Since you're exploring Local PV, here's a curated breakdown of core concepts and steps to get started, based on official Kubernetes documentation:
Core Concepts
- Local PV: A PersistentVolume that represents a local disk or directory on a Kubernetes node. You manually create it (or use a dynamic provisioner in some cases) and specify which node it belongs to.
- StorageClass for Local PV: To use Local PV dynamically, you can create a StorageClass with
volumeBindingMode: WaitForFirstConsumer. This tells Kubernetes to wait until a Pod is scheduled before binding the PVC to a PV, ensuring the PV is on the same node as the Pod. - Node Affinity: Local PVs require node affinity rules to tie the PV to a specific node. This ensures Pods using the PVC are scheduled to the correct node where the data lives.
Step-by-Step Example
1. Create a Local PV YAML
First, create a PV that points to a directory on your node (replace /mnt/disks/local-disk-1 with a real directory on your node, and node-1 with your node's hostname):
apiVersion: v1 kind: PersistentVolume metadata: name: local-pv spec: capacity: storage: 10Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain # Keeps the data if PVC is deleted storageClassName: local-storage local: path: /mnt/disks/local-disk-1 nodeAffinity: required: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - node-1
Apply it with:
kubectl apply -f local-pv.yaml
2. Create a PVC to Claim the PV
Next, create a PersistentVolumeClaim that requests storage from the local-storage class:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: local-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: local-storage
Apply it:
kubectl apply -f local-pvc.yaml
3. Create a Pod That Uses the PVC
Finally, create a Pod that mounts the PVC:
apiVersion: v1 kind: Pod metadata: name: local-pv-pod spec: containers: - name: app-container image: nginx volumeMounts: - name: local-storage mountPath: /usr/share/nginx/html # Where the storage is mounted in the container volumes: - name: local-storage persistentVolumeClaim: claimName: local-pvc
Apply it:
kubectl apply -f local-pv-pod.yaml
Key Notes for Local PV
- Data Durability: If the node hosting the Local PV fails, the data will be inaccessible until the node is restored. For critical data, you'll need to add replication at the application level (like using a clustered database).
- Dynamic Provisioning: For larger clusters, you can use a dynamic provisioner (like the
local-volume-provisioner) to automatically create Local PVs from pre-configured directories on nodes, instead of creating them manually. - Reclaim Policy: Use
Retainif you want to keep data after the PVC is deleted, orDelete(less common for Local PV) to automatically delete the local directory when the PVC is removed.
内容的提问来源于stack exchange,提问作者Anil Kumar P

