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

1、k8s中爲何使用存儲

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

pv和pvc的概念

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

2、安裝nfs

一、全部節點安裝nfs

yum install nfs-utils -yapi

二、配置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

三、客戶端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

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

一、文件結構組成bash

[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

一、案例一:

二、建立pv testasync

[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

二、建立pv test2  spa

[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

四、建立pvcrest

[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中優先選擇知足條件中最小的pvcserver

[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:需求pv改變pvc自動綁定

一、修改pv test 2以下:blog

[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

二、查看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
相關文章
相關標籤/搜索