kubernetes雲平台管理實戰:k8s存儲pv和pvc(十九)


一、k8s中為什么使用存儲

k8s中的副本控制器保證了pod的始終存儲,卻保證不了pod中的數據。只有啟動一個新pod的,之前pod中的數據會隨着容器的刪掉而丟失!

pv和pvc的概念

PersistentVolume(一些簡稱PV):由管理員添加的的一個存儲的描述,是一個全局資源,包含存儲的類型,存儲的大小和訪問模式等。它的生命周期獨立於Pod,例如當使用它的Pod銷毀時對PV沒有影響。
PersistentVolumeClaim(一些簡稱PVC):是Namespace里的資源,描述對PV的一個請求。請求信息包含存儲大小,訪問模式等。

二、安裝nfs

1、所有節點安裝nfs

yum install nfs-utils -y

2、配置nfs

[root@master ~]# vim /etc/exports
[root@master ~]# cat /etc/exports
/data 192.168.118.0/24(rw,async,no_root_squash,no_all_squash)
[root@master ~]# mkdir /data/k8s -p
[root@master ~]# systemctl restart rpcbind
[root@master ~]# systemctl restart nfs

3、客戶端showmount

[root@node01 ~]# showmount 192.168.118.18 -e
Export list for 192.168.118.18:
/data 192.168.118.0/24

[root@node02 ~]# showmount 192.168.118.18 -e
Export list for 192.168.118.18:
/data 192.168.118.0/24

三、在多個pvc中優先選擇滿足條件中最小的pvc

1、文件結構組成

[root@master volume]# ll
total 20
-rw-r--r-- 1 root root 153 May 15 13:59 test2-pvc.yaml
-rw-r--r-- 1 root root 278 May 15 11:57 test2-pv.yaml
-rw-r--r-- 1 root root 278 May 15 13:40 test3-pv.yaml
-rw-r--r-- 1 root root 152 May 15 13:55 test-pvc.yaml
-rw-r--r-- 1 root root 278 May 15 13:40 test-pv.yaml

1、案例一:

2、創建pv test

[root@master volume]# cat test-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: test
  labels:
    type: test
spec:
  capacity:
    storage: 10Gi 
  accessModes:
    - ReadWriteMany 
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path:  "/data/k8s"
    server: 192.168.118.18
    readOnly: false
[root@master volume]# kubectl create -f test-pv.yaml 
persistentvolume "test" created
[root@master volume]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM     REASON    AGE
test      10Gi       RWX           Recycle         Available                       12s

2、創建pv test2  

[root@master volume]# mv test-pv.yaml test2-pv.yaml 
[root@master volume]# cat test2-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: test2
  labels:
    type: test
spec:
  capacity:
    storage: 5Gi 
  accessModes:
    - ReadWriteMany 
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path:  "/data/k8s"
    server: 192.168.118.18
    readOnly: false
[root@master volume]# kubectl create -f test2-pv.yaml 
persistentvolume "test2" created

[root@master volume]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM     REASON    AGE
test      10Gi       RWX           Recycle         Available                       25m
test2     5Gi        RWX           Recycle         Available                       40s

4、創建pvc

[root@master volume]# cat test-pvc.yaml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
[root@master volume]# kubectl create -f test-pvc.yaml 
persistentvolumeclaim "nfs" created
[root@master volume]# kubectl get pvc
NAME      STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
nfs       Bound     test2     5Gi        RWX           23s

在多個pvc中優先選擇滿足條件中最小的pvc

[root@master volume]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM         REASON    AGE
test      10Gi       RWX           Recycle         Available                           2h
test2     5Gi        RWX           Recycle         Bound       default/nfs             1h

2、案例2:需求pv改變pvc自動綁定

1、修改pv test 2如下:

[root@master volume]# cat test2-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs2
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 7Gi
[root@master volume]# kubectl create -f test2-pvc.yaml 
persistentvolumeclaim "nfs2" created

2、查看pv與pvc綁定情況

[root@master volume]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM          REASON    AGE
test      10Gi       RWX           Recycle         Bound       default/nfs2             2h
test2     5Gi        RWX           Recycle         Bound       default/nfs              2h
test3     6Gi        RWX           Recycle         Available                            9m


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM