当前位置: 首页 > news >正文

9. kubernetes资源——pv/pvc持久卷

kubernetes资源——pv/pvc持久卷

  • 一、volume数据卷
    • 1、hostPath
    • 2、挂载nfs实现持久化
  • 二、pv/pvc 持久卷/持久卷声明
    • 1、pv/pvc介绍
    • 2、pv/pvc的使用流程
      • 2.1 创建pv
      • 2.2 创建pvc
      • 2.3 创建pod,使用pv做持久化

一、volume数据卷

用于pod中的数据的持久化存储
支持很多的卷类型, 例如EmptyDir、hostPath、NAS、SAN、CEPH

1、hostPath

挂载物理机上的目录做持久化

apiVersion: apps/v1
kind: Deployment
metadata:name: test1-hostpath
spec:replicas: 1selector:matchLabels:app: hostpathtemplate:metadata:labels:app: hostpathspec:containers:- name: test1-hostpathimage: centos:7imagePullPolicy: IfNotPresentcommand:- sleep- "3600"volumeMounts:													// 挂载卷- name: test1-volumemountPath: /test1volumes:																	// 创建卷,名称为test1-volume, 类型为hostpath- name: test1-volumehostPath:path: /test/data
[root@k8s-master ~]# kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE    IP              NODE                   NOMINATED NODE   READINESS GATES
test1-hostpath-584f599fcf-wzt2q   1/1     Running   0          117s   10.88.201.193   k8s-node01.linux.com   <none>           <none>
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl exec -ti test1-hostpath-584f599fcf-wzt2q bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@test1-hostpath-584f599fcf-wzt2q /]# 
[root@test1-hostpath-584f599fcf-wzt2q /]# touch /test1/{1..10}
[root@test1-hostpath-584f599fcf-wzt2q /]# ls /test1/
1  10  2  3  4  5  6  7  8  9
[root@test1-hostpath-584f599fcf-wzt2q /]# 
[root@test1-hostpath-584f599fcf-wzt2q /]# exit
exit

2、挂载nfs实现持久化

apiVersion: apps/v1
kind: Deployment
metadata:name: test2-pod-nfs
spec:replicas: 1selector:matchLabels:app: nfstemplate:metadata:labels:app: nfsspec:containers:- name: test2-pod-nfsimage: centos:7imagePullPolicy: IfNotPresentcommand:- sleep- "3600"volumeMounts:- name: test2-nfs-volumemountPath: /test2volumes:- name: test2-nfs-volumenfs:server: "192.168.140.13"path: "/test2"

二、pv/pvc 持久卷/持久卷声明

1、pv/pvc介绍

pv, 持久卷,后端真实存储空间的映射
pvc, 持久卷声明,使用存储的申请

使用流程:
1、创建pv,描述后端真实的存储空间
2、创建pvc,描述使用存储的申请
3、创建pod,挂载pvc使用存储

2、pv/pvc的使用流程

2.1 创建pv

apiVersion: v1
kind: PersistentVolume
metadata:name: pv-2g
spec:capacity:storage: 2GaccessModes:- ReadWriteManypersistentVolumeReclaimPolicy: Recyclenfs:server: "192.168.140.13"path: "/test6"
apiVersion: v1
kind: PersistentVolume
metadata:name: pv-3g
spec:capacity:storage: 3GaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: Recyclenfs:server: "192.168.140.13"path: "/test7"

accessModes:用于指定PV的访问模式,共有四种

  • ReadWriteOnce
    被单个节点以读写模式挂载

  • ReadWriteMany
    被多个节点以读写模式挂载

  • ReadOnlyMany
    被多个节点以只读模式挂载

  • ReadOnlyOnce, k8s 1.29
    被单节点以只读模式挂载

persistentVolumeReclaimPolicy:
指定回收模式是自动回收,当空间被释放时,K8S自动清理,然后可以继续绑定使用

[root@k8s-master pvTest]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
pv-2g   2G         RWX            Recycle          Available                          <unset>                          2m26s
pv-3g   3G         RWO            Recycle          Available                          <unset>                          46s

2.2 创建pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: pvc-1g
spec:accessModes:- ReadWriteManyresources:requests:storage: 1G
[root@k8s-master pvTest]# kubectl get pvc
NAME     STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
pvc-1g   Bound    pv-2g    2G         RWX                           <unset>                 10s```bash
[root@k8s-master pvTest]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM            STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
pv-2g   2G         RWX            Recycle          Bound       default/pvc-1g                  <unset>                          10m
pv-3g   3G         RWO            Recycle          Available                                   <unset>                          8m34s

2.3 创建pod,使用pv做持久化

apiVersion: apps/v1
kind: Deployment
metadata:name: test1-pod
spec:replicas: 1selector:matchLabels:app: pvtemplate:metadata:labels:app: pvspec:containers:- name: test1-podimage: centos:7imagePullPolicy: IfNotPresentcommand:- sleep- "3600"volumeMounts:- name: test1-pod-pv-volumemountPath: /test1volumes:- name: test1-pod-pv-volumepersistentVolumeClaim:														// 通过关联pvc创建数据卷claimName: pvc-1g
http://www.lryc.cn/news/410508.html

相关文章:

  • 2024西安铁一中集训DAY27 ---- 模拟赛((bfs,dp) + 整体二分 + 线段树合并 + (扫描线 + 线段树))
  • STM32F401VET6 PROTEUS8 ILI9341 驱动显示及仿真
  • 抖音视频素材网站有哪些?非常好用的5个抖音视频素材库分享
  • 【数据结构】链式二叉树的实现和思路分析及二叉树OJ
  • 项目成功秘诀:工单管理系统如何加速进程
  • OpenGauss和GaussDB有何不同
  • 星环科技携手东华软件推出一表通报送联合解决方案
  • YOLOv10环境搭建、训练自己的目标检测数据集、实际验证和测试
  • Harmony Next -- 通用标题栏:高度自定义,可设置沉浸式状态,正常状态下为:左侧返回、居中标题,左中右均可自定义视图。
  • 甄选范文“论数据分片技术及其应用”软考高级论文,系统架构设计师论文
  • 【elementui】记录el-table设置左、右列固定时,加大滚动条宽度至使滚动条部分被固定列遮挡的解决方法
  • Python人工智能:一、语音合成和语音识别
  • C/C++进阶 (8)哈希表(STL)
  • 2024电赛H题参考方案(+视频演示+核心控制代码)——自动行驶小车
  • 设计模式14-享元模式
  • Javascript中canvas与svg详解
  • 【BUG】已解决:No Python at ‘C:Users…Python Python39python. exe’
  • Flink SQL 的工作机制
  • [AI Mem0] 源码解读,带你了解 Mem0 的实现
  • 【LLM】-10-部署llama-3-chinese-8b-instruct-v3 大模型
  • C语言 之 理解指针(4)
  • Java设计模式—单例模式(Singleton Pattern)
  • AV1帧间预测(二):运动补偿
  • 数学建模(5)——逻辑回归
  • 【C++高阶】:深入探索C++11
  • 6. 自定义Docker镜像
  • 「12月·长沙」人工智能与网络安全国际学术会议(ISAICS 2024)
  • 【技术支持案例】使用S32K144+NSD8381驱动电子膨胀阀
  • 第二期:集成电路(IC)——智能世界的微观建筑大师
  • 基于物联网的区块链算力网络,IGP/BGP协议