kubernetes pv&pvc实际案例

1.安装nfs

[root@harbor-234-51 harbor]# yum -y install rpcbind nfs-utils

关于nfs 参考这一篇文档

2.配置要被创建为pv的目录

[root@harbor-234-51 harbor]# cat /etc/exports
/data/k8sdata/test/ *(insecure,rw,sync,no_root_squash)

[root@harbor-234-51 harbor]# cd /data/k8sdata/test/
[root@harbor-234-51 magedu]# ll
total 0
drwxr-xr-x 2 root root 6 Jul 30 22:59 zookeeper-datadir-1
drwxr-xr-x 2 root root 6 Jul 30 22:59 zookeeper-datadir-2
drwxr-xr-x 2 root root 6 Jul 30 22:59 zookeeper-datadir-3

到此nfs准备完毕

3.创建pv

[root@master-etcd1-234-31 pv]# cat zookeeper-persistentvolume.yaml
#这3个kind: PersistentVolume 用的网络存储192.168.234.51//data/k8sdata/test/zookeeper-datadir-1/2/3的目录作为存储
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: zookeeper-datadir-pv-1
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: 192.168.234.51
    path: /data/k8sdata/test/zookeeper-datadir-1

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: zookeeper-datadir-pv-2
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: 192.168.234.51
    path: /data/k8sdata/test/zookeeper-datadir-2

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: zookeeper-datadir-pv-3
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: 192.168.234.51
    path: /data/k8sdata/test/zookeeper-datadir-3

查看pv

[root@master-etcd1-234-31 pv]# kubectl get pv
NAME                     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                          STORAGECLASS   REASON   AGE
zookeeper-datadir-pv-1   20Gi       RWO            Retain           Bound    test/zookeeper-datadir-pvc-1                           119m
zookeeper-datadir-pv-2   20Gi       RWO            Retain           Bound    test/zookeeper-datadir-pvc-2                           119m
zookeeper-datadir-pv-3   20Gi       RWO            Retain           Bound    test/zookeeper-datadir-pvc-3                           119m

4.创建pvc

[root@master-etcd1-234-31 pv]# cat zookeeper-persistentvolumeclaim.yaml
#因为pv是全局的pvc不是全局。所以pvc的yaml形式要申请pv的空间要指明是哪一个namespace需要用的 
#这里是test
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: zookeeper-datadir-pvc-1
  namespace: test
spec:
  accessModes:
    - ReadWriteOnce
  volumeName: zookeeper-datadir-pv-1
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: zookeeper-datadir-pvc-2
  namespace: test
spec:
  accessModes:
    - ReadWriteOnce
  volumeName: zookeeper-datadir-pv-2
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: zookeeper-datadir-pvc-3
  namespace: test
spec:
  accessModes:
    - ReadWriteOnce
  volumeName: zookeeper-datadir-pv-3
  resources:
    requests:
      storage: 10Gi

[root@master-etcd1-234-31 pv]# kubectl get pvc -n test
NAME                      STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
zookeeper-datadir-pvc-1   Bound    zookeeper-datadir-pv-1   20Gi       RWO                           119m
zookeeper-datadir-pvc-2   Bound    zookeeper-datadir-pv-2   20Gi       RWO                           119m
zookeeper-datadir-pvc-3   Bound    zookeeper-datadir-pv-3   20Gi       RWO                           119m
[root@master-etcd1-234-31 pv]#