k8s数据卷(volume)管理

volume数据卷管理

一、volume介绍

用于实现数据的持久化存储

Kubernetes支持多种类型的卷,例如EmptyDir、HostPath、nfs、glusterfs、ceph等

二、emptyDir

创建Pod时,Pod运行的node节点会创建临时卷,并将卷挂载到Pod指定的目录中

临时卷存放目录

/var/lib/kubelet/pods//volumes/kubernetes.io~empty-dir/卷名称

Pod宕机销毁后,该临时卷中的数据会随之被删除

bash 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
    name: test1-deploy
spec:
    replicas: 2
    selector:
         matchLabels:
             app: test1-pod
    template:
       metadata:
           labels:
              app: test1-pod
       spec:
           containers:
           - name: test1-pod
             image: busybox
             imagePullPolicy: IfNotPresent
             command:
             - sleep
             - "3600"
             volumeMounts:
                - name: test1-volume
                  mountPath: /test1
           volumes:
               - name: test1-volume
                 emptyDir: {}

三、hostPath

创建Pod时,Pod运行的node节点会在本地创建目录,并该目录挂载到Pod中

Pod宕机后,宿主机对应目录中的文件不会消失

bash 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
    name: test2-deploy
    labels:
        app: test2-deploy
spec:
    replicas: 2
    selector:
         matchLabels:
             app: test2-pod
    template:
       metadata:
           labels:
              app: test2-pod
       spec:
           containers:
           - name: test2-pod
             image: busybox
             imagePullPolicy: IfNotPresent
             command:
             - sleep
             - "3600"
             volumeMounts:
                - name: test2-volume
                  mountPath: /test1
           volumes:
               - name: test2-volume
                 hostPath:
                    path: /data

四、基于nfs的网络卷

bash 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
    name: test3-deploy
    labels:
        app: test3-deploy
spec:
    replicas: 2
    selector:
         matchLabels:
             app: test3-pod
    template:
       metadata:
           labels:
              app: test3-pod
       spec:
           containers:
           - name: test3-pod
             image: busybox
             imagePullPolicy: IfNotPresent
             command:
             - sleep
             - "3600"
             volumeMounts:
                - name: test3-volume
                  mountPath: /test1
           volumes:
               - name: test3-volume
                 nfs:
                     server: "192.168.140.11"
                     path: "/data"

五、PV与PVC 持久卷

1、PV、PVC介绍

PV PersistentVolume

后端真实存储空间的映射

PVC PersistentVolumeClain

使用存储空间的申请

使用流程

创建PV标识真实的存储空间

创建PVC发送申请存储空间的请求,K8S集群会自动在PV、PVC间建立映射关系

建立映射关系:

依靠存储空间的大小

依靠PV的访问模式

创建POD时绑定PVC,实现持久化

bash 复制代码
1、创建PV
apiVersion: v1
kind: PersistentVolume
metadata:
    name: nfs-data1
spec:
    capacity:
       storage: 2G
    accessModes:
       - ReadWriteMany
    persistentVolumeReclaimPolicy: Recycle
    nfs:
        server: 192.168.140.13
        path: /data1
相关推荐
Linux运维老纪3 小时前
DNS缓存详解(DNS Cache Detailed Explanation)
计算机网络·缓存·云原生·容器·kubernetes·云计算·运维开发
JunLan~8 小时前
Docker 部署 GLPI(IT 资产管理软件系统)
运维·docker·容器
青草地溪水旁9 小时前
c++ list的front和pop_front的概念和使用案例
c++·容器·list
元气满满的热码式11 小时前
K8S部署DevOps自动化运维平台
运维·kubernetes·devops
LUCIAZZZ12 小时前
基于Docker以KRaft模式快速部署Kafka
java·运维·spring·docker·容器·kafka
IT艺术家-rookie16 小时前
k8s--部署k8s集群--控制平面节点
容器·kubernetes
康世行16 小时前
Windows环境下MaxKB大模型 Docker部署图文指南
windows·docker·容器
Elastic 中国社区官方博客17 小时前
使用 Ollama 和 Kibana 在本地为 RAG 测试 DeepSeek R1
大数据·数据库·人工智能·elasticsearch·ai·云原生·全文检索
Linux运维老纪1 天前
windows部署deepseek之方法(The Method of Deploying DeepSeek on Windows)
linux·人工智能·分布式·云原生·运维开发·devops
Elastic 中国社区官方博客1 天前
Elastic Cloud Serverless 获得主要合规认证
大数据·数据库·elasticsearch·搜索引擎·云原生·serverless·全文检索