上一節咱們介紹了 PV 和 PVC,本節經過 NFS 實踐。html
做爲準備工做,咱們已經在 k8s-master 節點上搭建了一個 NFS 服務器,目錄爲 /nfsdata
:服務器
下面建立一個 PV mypv1
,配置文件 nfs-pv1.yml
以下:ide
① capacity
指定 PV 的容量爲 1G。3d
② accessModes
指定訪問模式爲 ReadWriteOnce
,支持的訪問模式有:
ReadWriteOnce – PV 能以 read-write 模式 mount 到單個節點。
ReadOnlyMany – PV 能以 read-only 模式 mount 到多個節點。
ReadWriteMany – PV 能以 read-write 模式 mount 到多個節點。code
③ persistentVolumeReclaimPolicy
指定當 PV 的回收策略爲 Recycle
,支持的策略有:
Retain – 須要管理員手工回收。
Recycle – 清除 PV 中的數據,效果至關於執行 rm -rf /thevolume/*
。
Delete – 刪除 Storage Provider 上的對應存儲資源,例如 AWS EBS、GCE PD、Azure Disk、OpenStack Cinder Volume 等。htm
④ storageClassName
指定 PV 的 class 爲 nfs
。至關於爲 PV 設置了一個分類,PVC 能夠指定 class 申請相應 class 的 PV。blog
⑤ 指定 PV 在 NFS 服務器上對應的目錄。ci
建立 mypv1
:資源
STATUS
爲 Available
,表示 mypv1
就緒,能夠被 PVC 申請。get
接下來建立 PVC mypvc1
,配置文件 nfs-pvc1.yml
以下:
PVC 就很簡單了,只須要指定 PV 的容量,訪問模式和 class。
建立 mypvc1
:
從 kubectl get pvc
和 kubectl get pv
的輸出能夠看到 mypvc1
已經 Bound 到 mypv1
,申請成功。
接下來就能夠在 Pod 中使用存儲了,Pod 配置文件 pod1.yml
以下:
與使用普通 Volume 的格式相似,在 volumes
中經過 persistentVolumeClaim
指定使用 mypvc1
申請的 Volume。
建立 mypod1
:
驗證 PV 是否可用:
可見,在 Pod 中建立的文件 /mydata/hello
確實已經保存到了 NFS 服務器目錄 /nfsdata/pv1
中。
若是再也不須要使用 PV,可用刪除 PVC 回收 PV,下節咱們詳細討論。
書籍:
1.《天天5分鐘玩轉Kubernetes》
https://item.jd.com/26225745440.html
2.《天天5分鐘玩轉Docker容器技術》
https://item.jd.com/16936307278.html
3.《天天5分鐘玩轉OpenStack》
https://item.jd.com/12086376.html