在 Debian 上部署 Kubernetes(k8s)时,“存储如何挂载”通常分为 节点层(Debian) 和 Kubernetes 层(PV/PVC/StorageClass) 两部分。下面按常见场景系统说明。
K8s 本身不直接“格式化磁盘”,而是依赖 节点上的挂载点和存储驱动。
lsblk
mkfs.ext4 /dev/sdb
mkdir -p /data
mount /dev/sdb /data
开机自动挂载:
blkid /dev/sdb
编辑 /etc/fstab:
UUID=xxxx-xxxx /data ext4 defaults 0 2
✅ 节点能看到 /data,K8s 才能用。
| 概念 | 作用 |
|---|---|
| PV | 真实存储(节点目录 / NFS / 云盘) |
| PVC | Pod 对存储的请求 |
| StorageClass | 自动创建 PV |
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: app
image: busybox
command: ["sleep", "3600"]
volumeMounts:
- mountPath: /data
name: data
volumes:
- name: data
hostPath:
path: /data
⚠️ 不适合生产(节点绑定)
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node1
PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: local-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Gi
storageClassName: local-storage
Debian 安装 NFS:
apt install nfs-kernel-server
mkdir /nfs
echo "/nfs *(rw,sync,no_root_squash)" >> /etc/exports
exportfs -r
PV:
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 200Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.10
path: /nfs
例如 local-path(轻量):
kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml
Pod 直接用 PVC 即可,无需手动建 PV。
volumeMounts:
- mountPath: /app/data
name: storage
volumes:
- name: storage
persistentVolumeClaim:
claimName: local-pvc
kubectl get pv,pvc
kubectl describe pvc xxx
kubectl exec -it pod -- df -h
fstab 稳定ReadWriteMany(NFS/Ceph)如果你能说明:
我可以给你 直接可用的 YAML。