查看Kubernets支持的存储

kubectl explain pods.spec.volumes

持久存储卷申请 persistentVolumeClaim (PVC)

  • PV 关联到后端存储服务。PV存储了配置好的存储资源,如NFS,Glusterfs,iSCSI都配置在上边。

  • PVC  需要向PV建立联系,向PV申请容量资源,如写POD配置清单时,Volumes 处写需要10G空间容量,PVC会与PV申请10G的资源,POD定义的PVC从而绑定这个PV。PV不在被其他PVC绑定。

查看persistentVolumeClaim定义

kubectl explain pods.spec.volumes.persistentVolumeClaim
kubectl explain pods.spec.volumes.pvc

定义PV、PVC 方法


前期准备好后端的存储,本例后端存储为NFS创建过程大概如下。

1.NFS存储设备:安装

yum -y install nfs-utils

2.创建共享目录

mkdir /data/volmenu
cd /data/volmenu
mkdir  v{1,2,3,4,5}

3.配置NFS

vim /etc/exports

/data/volumes/v1  172.20.0.0/16(rw,no_root_squash)
/data/volumes/v2  172.20.0.0/16(rw,no_root_squash)
/data/volumes/v3  172.20.0.0/16(rw,no_root_squash)
/data/volumes/v4  172.20.0.0/16(rw,no_root_squash)
/data/volumes/v5  172.20.0.0/16(rw,no_root_squash)

systmectl start nfs

exportfs -arv
showmount -e

1.定义 PV

kubectl explain pv.spec.nfs   #帮助信息。

NFS 配置PV例子

apiVersion: v1                                                                                                                                                                                                    
kind: PersistentVolume
metadata:
  name: pv001
  labels:
    name: pv001
spec:
  nfs:
    path: /data/volumes/v1
    server: stor01.leiyan.com
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 2Gi
---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv002
  labels:
    name: pv002
spec:
  nfs:
    path: /data/volumes/v2
    server: stor01.leiyan.com
  accessModes: ["ReadWriteOnce"]
  capacity:
    storage: 5Gi
---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv003
  labels:
    name: pv003
spec:
  nfs:
    path: /data/volumes/v3
    server: stor01.leiyan.com
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 20Gi

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv004
  labels:
    name: pv004
spec:
  nfs:
    path: /data/volumes/v4
    server: stor01.leiyan.com
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 10Gi
---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv005
  labels:
    name: pv005
spec:
  nfs:
    path: /data/volumes/v5
    server: stor01.leiyan.com
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 10Gi

[root@master volmenu]# kubectl apply -f volumen-pv.yaml 
persistentvolume/pv001 created
persistentvolume/pv002 created
persistentvolume/pv003 created
persistentvolume/pv004 created
persistentvolume/pv005 created
[root@master volmenu]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv001   2Gi        RWO,RWX        Retain           Available                                   9s
pv002   5Gi        RWO            Retain           Available                                   9s
pv003   20Gi       RWO,RWX        Retain           Available                                   9s
pv004   10Gi       RWO,RWX        Retain           Available                                   9s
pv005   10Gi       RWO,RWX        Retain           Available                                   9s

2.定义 PVC (申请资源)

配置PVC申请6Gi空间例子

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc
  namespace: default
spec:
  accessModes: ["ReadWriteMany"]
  resources:
    requests:
      storage: 6Gi

---
apiVersion: v1
kind: Pod
metadata:
  name: pod-vol-pvc
  namespace: default
spec:
  containers:
  - name: myapp 
    image: ikubernetes/myapp:v1
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html/
  volumes:
  - name: html
    persistentVolumeClaim:                                                                                                                                                            
      claimName: mypvc

查看PV状态

[root@master volmenu]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM           STORAGECLASS   REASON   AGE
pv001   2Gi        RWO,RWX        Retain           Available                                           7m33s
pv002   5Gi        RWO            Retain           Available                                           7m33s
pv003   20Gi       RWO,RWX        Retain           Available                                           7m33s
pv004   10Gi       RWO,RWX        Retain           Available                                           7m33s
pv005   10Gi       RWO,RWX        Retain           Bound       default/mypvc                           7m33s

查看PVC状态

[root@master volmenu]# kubectl get pvc
NAME    STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mypvc   Bound    pv005    10Gi       RWO,RWX                       5m25s

动态分配容量,绑定pv 略。

https://github.com/China-Honey/k8snote/blob/master/9-%E5%AD%98%E5%82%A8%E5%8D%B7PVC-PV.md