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
posted @ 2020-05-22 22:12  活的潇洒80  阅读(742)  评论(0编辑  收藏  举报