本例使用nfs
創建pv
[root@k8s-master data]# vi pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: web
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /data
server: 10.10.1.10
字段說明:
========================================================
capacity 指定 PV 的容量為 5G。
accessModes 指定訪問模式為 ReadWriteOnce,支持的訪問模式有:
ReadWriteOnce – PV 能以 read-write 模式 mount 到單個節點。
ReadOnlyMany – PV 能以 read-only 模式 mount 到多個節點。
ReadWriteMany – PV 能以 read-write 模式 mount 到多個節點。
persistentVolumeReclaimPolicy 指定當 PV 的回收策略為 Recycle,支持的策略有:
Retain – 需要管理員手工回收。
Recycle – 清除 PV 中的數據,效果相當於執行 rm -rf /thevolume/*。
Delete – 刪除 Storage Provider 上的對應存儲資源,例如 AWS EBS、GCE PD、Azure Disk、OpenStack Cinder Volume 等。
storageClassName 指定 PV 的 class 為 nfs。相當於為 PV 設置了一個分類,PVC 可以指定 class 申請相應 class 的 PV。
創建PVC
[root@k8s-master data]# vi pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: lnmp
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
[root@k8s-master data]# kubectl apply -f pvc.yaml
persistentvolumeclaim "lnmp" created
[root@k8s-master data]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
lnmp Bound web 5Gi RWO 29s
[root@k8s-master data]#
定義PV
=======================================================
apiVersion: v1
kind: PersistentVolume
metadata:
name: web
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /data
server: 10.10.1.10
定義PVC
==========================================================
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: lnmp
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
定義pod
=====================================================
apiVersion: v1
kind: ReplicationController
metadata:
name:lnmp
spec:
replicas: 1
selector:
app: o2o
template:
metadata:
labels:
app: o2o
spec:
containers:
- name: o2o
image: docker.io/winstonpro/lnmp
ports:
- containerPort: 80
volumeMounts:
- mountPath: /home/wwwroot/default
readOnly: false
name: o2o
volumes:
- name: o2o
nfs:
server: 10.10.1.10
path: "/data"