K8s知识(4)Kubernetes 存储 volume

Kubernetes Volume 详解(含命令 & 案例)

一、为什么需要 Volume

问题 Kubernetes 解决方案
容器重启后数据丢失 使用 Volume
同 Pod 内容器共享数据 emptyDir
Pod 访问节点文件 hostPath
多 Pod 共享持久数据 NFS / PV / PVC

⚠️ 容器文件系统是临时的,Volume 才是数据的归宿


二、emptyDir(临时存储卷)

1️⃣ 核心特性

属性 说明
生命周期 与 Pod 一致
数据持久性 ❌ 不持久
跨节点 ❌
典型用途 缓存、临时文件、容器间通信

2️⃣ YAML 示例

复制代码

yaml

yaml

复制代码
# test-emptydir.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-emptydir
spec:
  containers:
  - name: container-a
    image: alpine
    command: ["/bin/sh", "-c"]
    args:
      - echo "Hello emptyDir" > /cache/hello.txt && sleep 3600
    volumeMounts:
    - name: cache-volume
      mountPath: /cache
  volumes:
  - name: cache-volume
    emptyDir: {}

3️⃣ 常用命令

复制代码

bash

bash

复制代码
kubectl apply -f test-emptydir.yaml
kubectl get pod test-emptydir
kubectl exec test-emptydir -- cat /cache/hello.txt
kubectl delete pod test-emptydir

✅ Pod 删除后,数据消失


三、hostPath(节点级存储卷)

1️⃣ 核心特性

属性 说明
生命周期 依赖节点
数据持久性 ✅(节点未重装)
安全风险 ⚠️ 高
典型用途 日志、系统配置、调试

2️⃣ YAML 示例

复制代码

yaml

yaml

复制代码
# test-hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-hostpath
spec:
  containers:
  - name: container-b
    image: alpine
    command: ["/bin/sh", "-c"]
    args:
      - ls -l /host/tmp && sleep 3600
    volumeMounts:
    - name: host-tmp
      mountPath: /host/tmp
      readOnly: true
  volumes:
  - name: host-tmp
    hostPath:
      path: /tmp
      type: Directory

3️⃣ 常用命令

复制代码

bash

bash

复制代码
kubectl apply -f test-hostpath.yaml
kubectl exec test-hostpath -- ls /host/tmp

⚠️ 生产环境建议:

  • 使用 readOnly: true

  • 限制 hostPath.path

  • 配合 PodSecurityPolicy / PSA


四、NFS(共享持久存储)

1️⃣ 核心特性

属性 说明
跨节点 ✅
多 Pod 共享 ✅
持久化 ✅
访问模式 ReadWriteMany

五、NFS + PV + PVC 实战

1️⃣ 前提条件

  • NFS Server:192.168.1.100

  • 导出目录:/nfs/data

  • 节点已安装 nfs-utils


2️⃣ PersistentVolume(PV)

复制代码

yaml

yaml

复制代码
# nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 192.168.1.100
    path: /nfs/data

3️⃣ PersistentVolumeClaim(PVC)

复制代码

yaml

yaml

复制代码
# nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

4️⃣ Pod 使用 NFS PVC

复制代码

yaml

yaml

复制代码
# nfs-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nfs-test-pod
spec:
  containers:
  - name: app
    image: alpine
    command: ["/bin/sh", "-c"]
    args:
      - echo "NFS Data" > /data/test.txt && sleep 3600
    volumeMounts:
    - name: nfs-storage
      mountPath: /data
  volumes:
  - name: nfs-storage
    persistentVolumeClaim:
      claimName: nfs-pvc

5️⃣ 操作命令汇总

复制代码

bash

bash

复制代码
kubectl apply -f nfs-pv.yaml
kubectl apply -f nfs-pvc.yaml
kubectl apply -f nfs-pod.yaml

kubectl get pv
kubectl get pvc
kubectl exec nfs-test-pod -- cat /data/test.txt

✅ Pod 删除后,NFS 数据仍然存在


六、三种 Volume 对比总结

类型 持久化 跨节点 多 Pod 共享 风险
emptyDir ❌ ❌ ✅(同 Pod) 低
hostPath ✅ ❌ ❌ 高
NFS ✅ ✅ ✅ 低
相关推荐
蜡笔婧萱7 小时前
Docker介绍及其基础简介(一)
云原生·eureka
谢亮_vipxieliang10 小时前
Kubernetes 的“操作系统化”:从容器编排到 AI 基础设施控制平面
人工智能·平面·kubernetes
sbjdhjd12 小时前
云安全 | Docker 容器逃逸复盘(三):docker.sock 挂载与 Docker-in-Docker 风险
经验分享·网络安全·docker·云原生·容器·kubernetes·云安全
热爱Liunx的丘丘人13 小时前
1、什么是K8s
云原生·容器·kubernetes
帷幕落秋14 小时前
Docker的两种安装
运维·docker·容器
fengkai454514 小时前
十二、Redis -2
运维·数据库·redis·容器
DolitD16 小时前
3D应用多实例并发中的GPU隔离技术路径分析
容器·gru·接口隔离原则
姜鱼问生18 小时前
docker exec 排查容器问题:从日志到进程
运维·网络·数据库·docker·容器
小匠石钧知20 小时前
07_在k8s集群中安装ingress-nginx
nginx·容器·kubernetes·ingress
零基础12321 小时前
Docker 部署实战:从入门到精通
docker·容器