美文网首页
k8s-持久卷

k8s-持久卷

作者: 小李飞刀_lql | 来源:发表于2021-11-23 11:11 被阅读0次

    持久卷

    持久卷概述

    001 PersistentVolume(PV):对存储资源创建和使用的抽象,使得存储作为集群中的资源管理
    002 PersistentVolumeClaim(PVC):让用户不需要关心具体的Volume实现细节
    003 PV使用方式称为静态供给,需要K8s运维工程师提前创建一堆PV,供开发者使用。
    

    PV 生命周期

    ACCESS MODES(访问模式)

    AccessModes 是用来对 PV 进行访问模式的设置,用于描述用户应用对存储资源的访问权限:
    001 ReadWriteOnce(RWO):读写权限,但是只能被单个节点挂载
    002 ReadOnlyMany(ROX):只读权限,可以被多个节点挂载
    003 ReadWriteMany(RWX):读写权限,可以被多个节点挂载
    

    RECLAIM POLICY(回收策略)

    001 Retain(保留): 保留数据,需要管理员手工清理数据
    002 Recycle(回收):清除 PV 中的数据,效果相当于执行 rm -rf /ifs/kuberneres/*
    003 Delete(删除):与 PV 相连的后端存储同时删除
    

    STATUS(状态)

    一个 PV 的生命周期中,可能会处于4中不同的阶段:
    001 Available(可用):表示可用状态,还未被任何 PVC 绑定
    002 Bound(已绑定):表示 PV 已经被 PVC 绑定
    003 Released(已释放):PVC 被删除,但是资源还未被集群重新声明
    004 Failed(失败): 表示该 PV 的自动回收失败
    
    1637631932356.png

    PV与PVC使用流程

    数据卷定义

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv0001
    spec:
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteMany
      nfs:
        path: /ifs/kubernetes/pv0001
        server: 192.168.153.21
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv0002
    spec:
      capacity:
        storage: 2Gi
      accessModes:
        - ReadWriteMany
      nfs:
        path: /ifs/kubernetes/pv0002
        server: 192.168.153.21
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv0003
    spec:
      capacity:
        storage: 3Gi
      accessModes:
        - ReadWriteMany
      nfs:
        path: /ifs/kubernetes/pv0003
        server: 192.168.153.21
        
    --------------------------------------------------------------------
                        
    
    

    卷需求模板&容器应用

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment-pvc2
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx-pvc2
      template:
        metadata:
          labels:
            app: nginx-pvc2
        spec:
          containers:
          - name: nginx
            image: nginx
            volumeMounts:
            - name: wwwroot
              mountPath: /usr/share/nginx/html
            ports:
            - containerPort: 80
          volumes:
          - name: wwwroot
            persistentVolumeClaim:
              claimName: my-pvc2
    
    ---
    
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-pvc2
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 1Gi
          
     --------------------------------------------------------------------     
    

    验证

    
    [root@k8smaster volume]# mkdir /ifs/kubernetes/pv0001 -p
    [root@k8smaster volume]# mkdir /ifs/kubernetes/pv0002 -p
    [root@k8smaster volume]# mkdir /ifs/kubernetes/pv0003 -p
    
    [root@k8smaster volume]# kubectl apply -f pv.yaml 
    persistentvolume/pv0001 created
    persistentvolume/pv0002 created
    persistentvolume/pv0003 created
    
    [root@k8smaster volume]# kubectl apply -f deployment-pvc.yaml 
    [root@k8smaster volume]# kubectl get pod -o wide
    NAME                               READY   STATUS    RESTARTS   AGE   IP              NODE       NOMINATED NODE   READINESS GATES
    deployment-pvc2-5445f48f88-862kp   1/1     Running      10.244.249.47   k8snode1   
    deployment-pvc2-5445f48f88-mqh8w   1/1     Running      10.244.249.21   k8snode1   
    deployment-pvc2-5445f48f88-wpmtx   1/1     Running      10.244.249.39   k8snode1 
    
    [root@k8smaster volume]# kubectl get pv,pvc
      
    persistentvolume/pv0001  1Gi  RWX  Retain     Bound       default/my-pvc2                        
    persistentvolume/pv0002  2Gi  RWX  Retain     Available                                            
    persistentvolume/pv0003  3Gi  RWX  Retain     Available                                          
    
    NAME         STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   
    persistentvolumeclaim/my-pvc2   Bound    pv0001   1Gi        RWX   
    
    [root@k8smaster volume]# cd /ifs/kubernetes/pv0001/
    [root@k8smaster pv0001]# vi index.html
    你好,小李飞刀
    
    [root@k8smaster pv0001]# curl 10.244.249.47
    你好,小李飞刀
    [root@k8smaster pv0001]# curl 10.244.249.21
    你好,小李飞刀
    [root@k8smaster pv0001]# curl 10.244.249.39
    你好,小李飞刀
    
    

    PV 动态供给(StorageClass)

    概述

    001 PV静态供给明显的缺点是维护成本太高了!
    002 K8s开始支持PV动态供给,使用StorageClass对象实现。
    
    1637633773465.png

    基于NFS实现PV动态供给流程图

    1637636198802.png

    部署NFS实现自动创建PV插件

    # 创建存储类
    [root@k8smaster nfs]# kubectl apply -f class.yaml
    storageclass.storage.k8s.io/managed-nfs-storage created
    [root@k8smaster nfs]# kubectl get sc
    NAME                  PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   
    managed-nfs-storage   fuseim.pri/ifs   Delete          Immediate    
    
    # 授权访问apiserver
    [root@k8smaster nfs]# kubectl apply -f rbac.yaml 
    
    # 部署插件,需修改里面NFS服务器地址与共享目录
    [root@k8smaster nfs]# kubectl apply -f deployment.yaml 
    [root@k8smaster volume]# kubectl get pod
    NAME                                      READY   STATUS    RESTARTS   AGE
    nfs-client-provisioner-5f98b5cdfb-hdrs5   1/1     Running   0          7m36s
    
    
    

    示例

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment-sc
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx-sc
      template:
        metadata:
          labels:
            app: nginx-sc
        spec:
          containers:
          - name: nginx
            image: nginx
            volumeMounts:
            - name: wwwroot
              mountPath: /usr/share/nginx/html
            ports:
            - containerPort: 80
          volumes:
          - name: wwwroot
            persistentVolumeClaim:
              claimName: my-pvc3
    
    ---
    
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-pvc3
    spec:
      storageClassName: "managed-nfs-storage"
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 18Gi
          
          
    -----------------------------------------------------------------
    
    

    验证

    [root@k8smaster volume]# kubectl get pod
    NAME                                      READY   STATUS    RESTARTS   AGE
    deployment-sc-5746c65cfb-8ngh4            1/1     Running   0          28s
    deployment-sc-5746c65cfb-ljt7d            1/1     Running   0          28s
    deployment-sc-5746c65cfb-npzjq            1/1     Running   0          28s
    
    
    [root@k8smaster volume]# kubectl get pv,pvc
    --pv
    persistentvolume/pvc-51c09555-a1b9-4a3f-972f-b7d495ea6ed6   
    18Gi    RWX       Delete           Bound       default/my-pvc3   managed-nfs-storage   
    --pvc
    persistentvolumeclaim/my-pvc3   Bound    pvc-51c09555-a1b9-4a3f-972f-b7d495ea6ed6   18Gi       RWX            managed-nfs-storage  
    
    
    [root@k8smaster volume]# cd /ifs/kubernetes/
    [root@k8smaster kubernetes]# ls
     default-my-pvc3-pvc-51c09555-a1b9-4a3f-972f-b7d495ea6ed6  
    [root@k8smaster kubernetes]# cd default-my-pvc3-pvc-51c09555-a1b9-4a3f-972f-b7d495ea6ed6/
    [root@k8smaster default-my-pvc3-pvc-51c09555-a1b9-4a3f-972f-b7d495ea6ed6]# ls
    [root@k8smaster default-my-pvc3-pvc-51c09555-a1b9-4a3f-972f-b7d495ea6ed6]# 
    vi index.html
    PV 动态供给
    
    [root@k8smaster volume]# kubectl get pod -o wide
    deployment-sc-5746c65cfb-8ngh4            1/1     Running  10.244.249.48   k8snode1   
    deployment-sc-5746c65cfb-ljt7d            1/1     Running  10.244.249.62   k8snode1   
    deployment-sc-5746c65cfb-npzjq            1/1     Running  10.244.249.40   k8snode1   
    
    [root@k8smaster volume]# curl 10.244.249.48
    PV 动态供给
    [root@k8smaster volume]# curl 10.244.249.62
    PV 动态供给
    [root@k8smaster volume]# curl 10.244.249.40
    PV 动态供给
    
    
    [root@k8smaster volume]# kubectl delete -f deployment-sc.yaml 
    deployment.apps "deployment-sc" deleted
    persistentvolumeclaim "my-pvc3" deleted
    [root@k8smaster volume]# kubectl get pv,pvc
    ...
    
    [root@k8smaster volume]# cd /ifs/kubernetes/
    [root@k8smaster kubernetes]# ls
    archived-default-my-pvc3-pvc-51c09555-a1b9-4a3f-972f-b7d495ea6ed6
    #这是归档目录
    

    相关文章

      网友评论

          本文标题:k8s-持久卷

          本文链接:https://www.haomeiwen.com/subject/uphwtrtx.html