1.概念
Volume:NFS、CEPH、GFS
PersistentVolume:NFS、CEPG/GFS
PV、PVC
PV:由k8s配置的存储,PV同样是集群的一类资源,yaml。
PVC:对PV的申请,
PersistentVolumeClaim
PV文档:https://kubernetes.io/docs/concepts/storage/persistent-volumes/
Nfs类型的PV:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0003
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: slow
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: /tmp
server: 172.17.0.2
persistentVolumeReclaimPolicy:#类型
Recycle: 回收,rm -rf
Deployment -> PVC PV, Recycle。
Retain:保留。
Delete:PVC –-> PV,删除PVC后PV也会被删掉,这一类的PV,需要支持删除的功能,动态存储默认方式。
Capacity:PV的容量。
volumeMode:挂载的类型,Filesystem,block
accessModes:这个的PV访问模式:
ReadWriteOnce:RWO,可以被单节点以读写的模式挂载。
ReadWriteMany:RWX,可以被多节点以读写的形式挂载。
ReadOnlyMany:ROX,可以被多个节点以只读的形式挂载。
storageClassName:PV的类,可以说是一个类名,PVC和PV的这个名字一样,才能被绑定。
Pv的状态:
Available:空闲的PV,没有被任何PVC绑定。
Bound:已经被PVC绑定
Released:PVC被删除,但是资源未被重新使用
Failed:自动回收失败。
2.创建nfs的pV
[root@k8s-master01 ~]# cat nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
nfs:
path: /data/k8s # 指定nfs的挂载点
server: 192.168.0.120 # 指定nfs服务地址
[root@k8s-master01 ~]# kubectl create -f nfs-pv.yaml
persistentvolume/pv-nfs created
3.创建一个PVC
[root@k8s-master01 ~]# cat test-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
storageClassName: manual ###跟pv的名字对应
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
4.查看pv&pvc状态
[root@master01 pvc]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
nfs-pv 1Gi RWO Retain Bound default/nfs-pvc manual 2m30s
[root@master01 pvc]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nfs-pvc Bound nfs-pv 1Gi RWO manual 3m53s
5.pod使用pvc
apiVersion: v1
kind: Pod
metadata:
name: test-volumes
spec:
volumes:
- name: nfs
persistentVolumeClaim:
claimName: nfs-pvc
containers:
- name: web
image: nginx
ports:
- name: web
containerPort: 80
volumeMounts:
- name: nfs
subPath: test-volumes
mountPath: "/usr/share/nginx/html"
6.定义html,进行访问测试
[root@master01 ~]# cat /data/k8s/test-volumes/index.html
1
[root@master01 ~]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-volumes 1/1 Running 0 3m26s 10.244.2.31 node02 <none> <none>
[root@master01 ~]# curl 10.244.2.31
1