You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Kubernetes hostPath PV存储容量配置的技术疑问

HostPath类型PV的存储容量疑问解答

场景说明

使用hostPath创建了静态PersistentVolume(PV),相关配置及现象如下:

PV配置

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysqlvol
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi #Size of the volume
  accessModes:
    - ReadWriteOnce #type of access # RWO RWX ROX
  hostPath:
    path: "/tmp/new.txt" #host location

PVC配置

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysqlvol
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi # < 10Gi

Pod配置

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: mysqlvol
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

实际现象

发现可以向该卷写入超过10GiB的数据:

root@task-pv-pod:/usr/share/nginx/html# ls -lh
total 15G
-rw-r--r-- 1 root root 15G May 22 04:16 bigfile.txt
-rw-r--r-- 1 root root   3 May 18 12:04 new.txt
root@task-pv-pod:/usr/share/nginx/html#   

疑问解答

1. 此处配置的10GiB有何意义?为何我可以写入任意大小的数据(比如15GiB)?

  • PV中声明的capacity.storage: 10Gi是标称容量,仅用于Kubernetes的PV/PVC匹配调度逻辑,以及集群内的资源统计展示,hostPath类型PV不会基于这个值在节点上预留或强制限制实际存储大小。
  • hostPath本质是直接挂载节点本地的文件或目录,实际可用容量完全取决于该路径所在节点磁盘的剩余空间,因此你可以写入超过10GiB的数据,Kubernetes不对hostPath做硬容量限制。

2. 既然该卷能够容纳更大容量的数据,为何当PVC请求的存储容量超过10GiB时会创建失败?

  • Kubernetes的PV/PVC绑定遵循严格的容量匹配规则:PVC请求的存储容量必须小于等于PV声明的标称容量,否则K8s判定该PV无法满足PVC的需求,不会完成绑定,导致PVC处于Pending状态(创建失败)。
  • 虽然hostPath底层实际能容纳更大数据,但K8s不会去检测节点磁盘的实际剩余空间,仅以PV声明的标称容量作为匹配依据。

内容的提问来源于stack exchange,提问作者ambikanair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:37:07