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

kubernetes资源------pv/pvc持久卷

一、volume数据卷

用于pod中的数据的持久化存储

支持很多的卷类型, 例如EmptyDir、hostPath、NAS、SAN、CEPH

1、hostPath

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

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
    name: test1-hostpath
spec:
    replicas: 1
    selector:
         matchLabels:
             app: hostpath
    template:
       metadata:
           labels:
              app: hostpath
       spec:
           containers:
           - name: test1-hostpath
             image: centos:7
             imagePullPolicy: IfNotPresent
             command:
             - sleep
             - "3600"
             volumeMounts:													// 挂载卷
                 - name: test1-volume
                   mountPath: /test1
           volumes:																	// 创建卷,名称为test1-volume, 类型为hostpath
              - name: test1-volume
                hostPath:
                   path: /test/data
bash 复制代码
[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实现持久化

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
    name: test2-pod-nfs
spec:
    replicas: 1
    selector:
         matchLabels:
             app: nfs
    template:
       metadata:
           labels:
              app: nfs
       spec:
           containers:
           - name: test2-pod-nfs
             image: centos:7
             imagePullPolicy: IfNotPresent
             command:
             - sleep
             - "3600"
             volumeMounts:
                 - name: test2-nfs-volume
                   mountPath: /test2
           volumes:
              - name: test2-nfs-volume
                nfs:
                   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

yaml 复制代码
apiVersion: v1
kind: PersistentVolume
metadata:
    name: pv-2g
spec:
    capacity:
        storage: 2G
    accessModes:
        - ReadWriteMany
    persistentVolumeReclaimPolicy: Recycle
    nfs:
       server: "192.168.140.13"
       path: "/test6"
yaml 复制代码
apiVersion: v1
kind: PersistentVolume
metadata:
    name: pv-3g
spec:
    capacity:
        storage: 3G
    accessModes:
        - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
    nfs:
       server: "192.168.140.13"
       path: "/test7"

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

  • ReadWriteOnce

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

  • ReadWriteMany

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

  • ReadOnlyMany

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

  • ReadOnlyOnce, k8s 1.29

    被单节点以只读模式挂载

persistentVolumeReclaimPolicy:

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

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          Available                          <unset>                          2m26s
pv-3g   3G         RWO            Recycle          Available                          <unset>                          46s

2.2 创建pvc

yaml 复制代码
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: pvc-1g
spec:
    accessModes:
        - ReadWriteMany
    resources:
        requests:
            storage: 1G
bash 复制代码
[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做持久化

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
    name: test1-pod
spec:
    replicas: 1
    selector:
         matchLabels:
             app: pv
    template:
       metadata:
           labels:
              app: pv
       spec:
           containers:
           - name: test1-pod
             image: centos:7
             imagePullPolicy: IfNotPresent
             command:
             - sleep
             - "3600"
             volumeMounts:
                 - name: test1-pod-pv-volume
                   mountPath: /test1
           volumes:
              - name: test1-pod-pv-volume
                persistentVolumeClaim:														// 通过关联pvc创建数据卷
                     claimName: pvc-1g
相关推荐
梅见十柒6 分钟前
wsl2中kali linux下的docker使用教程(教程总结)
linux·经验分享·docker·云原生
Python私教1 小时前
ubuntu搭建k8s环境详细教程
linux·ubuntu·kubernetes
运维&陈同学2 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列
O&REO2 小时前
单机部署kubernetes环境下Overleaf-基于MicroK8s的Overleaf应用部署指南
云原生·容器·kubernetes
politeboy2 小时前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
运维小文3 小时前
K8S资源限制之LimitRange
云原生·容器·kubernetes·k8s资源限制
登云时刻3 小时前
Kubernetes集群外连接redis集群和使用redis-shake工具迁移数据(二)
redis·容器·kubernetes
wuxingge12 小时前
k8s1.30.0高可用集群部署
云原生·容器·kubernetes
志凌海纳SmartX13 小时前
趋势洞察|AI 能否带动裸金属 K8s 强势崛起?
云原生·容器·kubernetes
锅总13 小时前
nacos与k8s service健康检查详解
云原生·容器·kubernetes