PersistentVolume(持久卷,简称PV)是集群内,由管理员提供的网络存储的一部分。就像集群中的节点一样,PV也是集群中的一种资源。它也像Volume一样,是一种volume插件,但是它的生命周期却是和使用它的Pod相互独立的。PV这个API象,捕获了诸如NFS、ISCSI、或其他云存储系统的实现细节。
PersistentVolumeClaim(持久卷声明,简称PVC)是用户的一种存储请求。它和Pod类似,Pod消耗Node资源,而PVC消耗PV资源。Pod能够请求特定的资源(如CPU和内存)。PVC能够请求指定的大小和访问的模式(可以被映射为一次读写或者多次只读)。
有两种PV提供的方式:静态和动态。
PVC与PV的绑定是一对一的映射。没找到匹配的PV,那么PVC会无限期得处于unbound未绑定
状态。
使用
释放
回收
创建一个pv:
[root@server2 vol]# vim pv1.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce /只允许一个人读写
persistentVolumeReclaimPolicy: Recycle /在利用回收策略
storageClassName: nfs /存储类
# mountOptions: /挂载选项,可以不加
# - hard
# - nfsvers=4.1
nfs:
path: /nfsdata
server: 172.25.254.2 /指定nfs后端
[root@server2 vol]# kubectl apply -f pv1.yml
persistentvolume/pv1 created
[root@server2 vol]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 5Gi RWO Recycle Available nfs 4s
ReadWriteOnce – 该volume只能被单个节点以读写的方式映射
ReadOnlyMany – 该volume可以被多个节点以只读方式映射
ReadWriteMany – 该volume可以被多个节点以读写的方式映射
在命令行中,访问模式可以简写为:
• RWO - ReadWriteOnce
• ROX - ReadOnlyMany
• RWX - ReadWriteMany
当前,只有NFS和HostPath支持回收利用,AWS EBS,GCE PD,Azure Disk,or OpenStack Cinder卷支持删除操作。-
绑定pvc:
[root@server2 vol]# vim pv1.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: /nfs
server: 172.25.254.2
--- /在后面加入这些内容
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc1
spec:
storageClassName: nfs /和pv对应
accessModes:
- ReadWriteOnce /访问类型也和pv对应,不然绑定失败
resources:
requests:
storage: 1Gi /请求大小,必须小于 pv
[root@server2 vol]# kubectl apply -f pv1.yml
persistentvolume/pv1 unchanged
persistentvolumeclaim/pvc1 created
[root@server2 vol]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 5Gi RWO Recycle Bound default/pvc1 nfs 15m
[root@server2 vol]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Bound pv1 5Gi RWO nfs 13s
可见已经绑定了,那末接下来我们就可以直接使用 pod 挂载 pvc 了
[root@server2 vol]# vim pv1.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: /nfs
server: 172.25.254.2
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc1
spec:
storageClassName: nfs
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html //挂载pvc
name: pv1
volumes:
- name: pv1
persistentVolumeClaim:
claimName: pvc1
[root@server2 vol]# kubectl apply -f pv1.yml
persistentvolume/pv1 created
persistentvolumeclaim/pvc1 created
pod/test-pd created
[root@server2 vol]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 5Gi RWO Recycle Bound default/pvc1 nfs 6s
[root@server2 vol]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Bound pv1 5Gi RWO nfs 7s
[root@server2 vol]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-pd 1/1 Running 0 9s
[root@server2 vol]# kubectl describe pod test-pd
Volumes:
pv1:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: pvc1
挂载成功。
[root@server2 vol]# curl 10.244.141.227
88888 /是nfs中的页面
[root@server1 nfsdata]# vim index.html
88888
88888 /更改一下页面
88888
[root@server2 vol]# curl 10.244.141.227
88888
88888 /直接更新了
88888
多创建几个pv:
在创建两个nfs目录,共享出去
[root@server1 /]# mkdir /nfsdata2
[root@server1 /]# mkdir /nfsdata3
[root@server1 nfsdata2]# echo nfs22222 > index.html
[root@server1 nfsdata2]# cd ../nfsdata3
[root@server1 nfsdata3]# echo nfs33333 >index.html
[root@server1 /]# vim /etc/exports
/nfsdata *(rw,sync)
/nfsdata2 *(rw,sync)
/nfsdata3 *(ro)
[root@server1 /]# chmod 777 /nfsdata2
[root@server1 /]# exportfs -rv
exporting *:/nfsdata3
exporting *:/nfsdata2
exporting *:/nfsdata
[root@server1 /]# showmount -e
Export list for server1:
/nfsdata3 *
/nfsdata2 *
/nfsdata *
创建pv2
[root@server2 vol]# vim pv2.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsdata2 /指定路径为nfsdata2
server: 172.25.254.1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc2
spec:
storageClassName: nfs
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: Pod
metadata:
name: test-pd-2
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: pv2
volumes:
- name: pv2
persistentVolumeClaim:
claimName: pvc2
[root@server2 vol]# kubectl apply -f pv2.yml
persistentvolume/pv2 created
persistentvolumeclaim/pvc2 created
pod/test-pd-2 created
[root@server2 vol]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 5Gi RWO Recycle Bound default/pvc1 nfs 21m
pv2 5Gi RWX Recycle Bound default/pvc2 nfs 5s
[root@server2 vol]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Bound pv1 5Gi RWO nfs 21m
pvc2 Bound pv2 5Gi RWX nfs 6s
[root@server2 vol]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-pd 1/1 Running 0 21m 10.244.141.227 server3
test-pd-2 1/1 Running 0 32s 10.244.141.228 server3
[root@server2 vol]# curl 10.244.141.228
nfs22222
[root@server2 vol]# curl 10.244.141.227
88888
88888
88888 /pvc1的pod 和pvc2 的pod访问的内容不同,以此类推
由于策略是读写的,所以我们可以进入pod中进行更改:
[root@server2 vol]# kubectl exec -it test-pd-2 -- bash /进入pod中
root@test-pd-2:/# cd /usr/share/nginx/html/
root@test-pd-2:/usr/share/nginx/html# ls
index.html
root@test-pd-2:/usr/share/nginx/html# echo nfs222222\nnfs222222 >index.html
bash: index.html: Permission denied 无法更改原文件,因为源文件是root写的
root@test-pd-2:/usr/share/nginx/html# echo baidu.com > test.html /可以创建新文件
root@test-pd-2:/usr/share/nginx/html# ls
index.html test.html
root@test-pd-2:/usr/share/nginx/html# exit
[root@server2 vol]# curl 10.244.141.228/test.html
baidu.com /可以访问到
[root@server1 nfsdata3]# cd ../nfsdata2
[root@server1 nfsdata2]# ls
index.html test.html
[root@server1 nfsdata2]# cat test.html
baidu.com /nfs主机上也出现了这个文件
创建只读模式的pv3
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv3
spec:
capacity:
storage: 20Gi
volumeMode: Filesystem
accessModes:
- ReadOnlyMany
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsdata3
server: 172.25.254.1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc3
spec:
storageClassName: nfs
accessModes:
- ReadOnlyMany
resources:
requests:
storage: 20Gi
---
apiVersion: v1
kind: Pod
metadata:
name: test-pd-3
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: pv3
volumes:
- name: pv3
persistentVolumeClaim:
claimName: pvc3
[root@server2 vol]# kubectl apply -f pv3.yml
persistentvolume/pv3 created
persistentvolumeclaim/pvc3 created
pod/test-pd-3 created
[root@server2 vol]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 5Gi RWO Recycle Bound default/pvc1 nfs 34m
pv2 5Gi RWX Recycle Bound default/pvc2 nfs 13m
pv3 20Gi ROX Recycle Bound default/pvc3 nfs 4s
[root@server2 vol]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-pd 1/1 Running 0 35m 10.244.141.227 server3
test-pd-2 1/1 Running 0 13m 10.244.141.228 server3
test-pd-3 1/1 Running 0 14s 10.244.22.28 server4
[root@server2 vol]# curl 10.244.22.28
nfs33333
[root@server2 vol]# kubectl exec -it test-pd-3 -- bash
root@test-pd-3:/#
root@test-pd-3:/# cd /usr/share/nginx/html/
root@test-pd-3:/usr/share/nginx/html# echo 78967 > index.html
bash: index.html: Read-only file system
root@test-pd-3:/usr/share/nginx/html# echo 8768676 > test.html
bash: test.html: Read-only file system /不能更改也不能创建,因为是只读的文件系统,
目前我们使用的方式就是使用静态的方式,手动创建,不可能总是给每个用户都创建动态pv,当没有匹配的pv时。
集群可能会尝试专门地供给volume
给PVC
。这种供给基于StorageClass
,它可以使用多种后端
配置一下nfs共享:
[root@server1 nfsdata2]# vim /etc/exports
/nfsdata *(rw,sync,no_root_squash) /只要一个就够了
[root@server1 nfsdata2]# exportfs -rv
exporting *:/nfsdata
[root@server1 nfsdata2]# showmount -e
Export list for server1:
/nfsdata *
[root@server1 nfsdata2]# cd /nfsdata
[root@server1 nfsdata]# rm -f *
里面将来自动创建pv。
provisioner
、parameters
和 reclaimPolicy
字段, 这些字段会在StorageClass需要动态分配 PersistentVolume 时会使用到。由于我们这里使用的是nfs的存储,所以我们使用nfs的 Provisioner。
配置授权:
[root@server2 vol]# mkdir nfs-client
[root@server2 vol]# cd nfs-client/
[root@server2 nfs-client]# vim rbac.yml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
部署NFS Client Provisioner 分配器:
[root@server2 nfs-client]# vim deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: nfs-client-provisioner:latest /从harbor仓库下载
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env: /给pod中指定环境变量
- name: PROVISIONER_NAME
value: caoaoyuan.org/nfs
- name: NFS_SERVER
value: 172.25.254.1
- name: NFS_PATH
value: /nfsdata
volumes: /卷
- name: nfs-client-root
nfs:
server: 172.25.254.1 /地址
path: /nfsdata /目录
创建 NFS SotageClass存储类:
[root@server2 nfs-client]# vim class.yml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage /存储类 的名字
provisioner: caoaoyuan.org/nfs /和上面的PROVISIONER_NAME环境变量值相同
parameters:
archiveOnDelete: "false" /删除的时候不打包
创建PVC:
[root@server2 nfs-client]# vim pvsc.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage" /定义存储类
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
创建测试Pod:
[root@server2 nfs-client]# vim pvc.yml
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox /harbor私有仓库中有
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1" /创建/mnt下的文件,相当于在nfs服务器上创建
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt" /挂载到mnt上
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim /使用上面的pvc
准备工作就做好了,全部应用:
[root@server2 nfs-client]# kubectl apply -f .
[root@server2 nfs-client]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-576d464467-6k84n 1/1 Running 0 13s
test-pod 0/1 Completed 0 14s / 完成
在nfs服务器上查看;
[root@server2 nfs-client]# kubectl get pv
NAME /当前pv名
pvc-4d20ab64-8a73-4c7c-a70b-22216d7d5812
[root@server2 nfs-client]# kubectl get pvc
NAME
test-claim /当前pvc名
nfs上查看:
[root@server1 ~]# cd /nfsdata
[root@server1 nfsdata]# ls
default-test-claim-pvc-4d20ab64-8a73-4c7c-a70b-22216d7d5812
PV以 ${namespace}-${pvcName}-${pvName}的命名格式自动在NFS服务器上创建.
[root@server2 nfs-client]# kubectl delete pod test-pod
pod "test-pod" deleted
[root@server2 nfs-client]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
test-claim Bound pvc-4d20ab64-8a73-4c7c-a70b-22216d7d5812 100Mi RWX managed-nfs-storage 13m
删除 pod 后 pvc 依然存在,pv也在。
[root@server2 nfs-client]# kubectl delete pvc test-claim
persistentvolumeclaim "test-claim" deleted
[root@server2 nfs-client]# kubectl get pv
No resources found in default namespace.
[root@server2 nfs-client]# kubectl get pvc
No resources found in default namespace.
当我们删除pvc的时候,pv也被删除。
[root@server1 nfsdata]# ls
default-test-claim-pvc-4d20ab64-8a73-4c7c-a70b-22216d7d5812
[root@server1 nfsdata]# ls
[root@server1 nfsdata]# ls
[root@server2 nfs-client]# kubectl get storageclasses.storage.k8s.io
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
managed-nfs-storage caoaoyuan.org/nfs Delete Immediate false 19m
因为我们没有定义回收策略,默认为delete,而且我们在 存储类中设置了删除时不打包.
如果我们需要保存生成的数据时:
[root@server2 nfs-client]# vim class.yml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: caoaoyuan.org/nfs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "true" /改为ture,就会自动的为你保存
[root@server2 nfs-client]# kubectl describe storageclasses.storage.k8s.io managed-nfs-storage
Provisioner: caoaoyuan.org/nfs
Parameters: archiveOnDelete=true
[root@server2 nfs-client]# kubectl apply -f class.yml
storageclass.storage.k8s.io/managed-nfs-storage created
[root@server2 nfs-client]# kubectl apply -f pvc.yml /创建pvc会自动创建pv
persistentvolumeclaim/test-claim created
[root@server1 nfsdata]# ls
default-test-claim-pvc-f76a4858-2128-48f8-b3dc-3895043968de /pv创建了
[root@server2 nfs-client]# kubectl delete -f pvc.yml /在删除pvc
persistentvolumeclaim "test-claim" deleted
[root@server1 nfsdata]# ls
archived-default-test-claim-pvc-f76a4858-2128-48f8-b3dc-3895043968de
/文件被打包了,以archived 开头
在存储比较紧的情况下,还不建议开启这个参数的。我们现在在改回false。
我们还可以通多创建多个pvc,一次创建多个 pv:
[root@server2 nfs-client]# vim pvc.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc1
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1G
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc2
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadOnlyMany
resources:
requests:
storage: 6G
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc3
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10G
[root@server2 nfs-client]# kubectl apply -f class.yml
storageclass.storage.k8s.io/managed-nfs-storage created
[root@server2 nfs-client]# kubectl apply -f pvc.yml
persistentvolumeclaim/pvc1 created
persistentvolumeclaim/pvc2 created
persistentvolumeclaim/pvc3 created
[root@server2 nfs-client]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-c46eecca-1852-442d-8cfb-7f03423f26c2 1G RWX Delete Bound default/pvc1 managed-nfs-storage 4s
pvc-e2f0581c-ac78-47a0-b3b6-453bfbbc2582 6G ROX Delete Bound default/pvc2 managed-nfs-storage 4s
pvc-fae89201-5f59-4a82-a310-a6c40c161eb1 10G RWO Delete Bound default/pvc3 managed-nfs-storage 4s
[root@server2 nfs-client]# kubectl delete -f pvc.yml
persistentvolumeclaim "pvc1" deleted
persistentvolumeclaim "pvc2" deleted
persistentvolumeclaim "pvc3" deleted
[root@server2 nfs-client]# kubectl get pv
No resources found in default namespace.
有这样一种情况:
[root@server2 nfs-client]# vim pvc.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc1
# annotations: /注释掉存储类
# volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1G
[root@server2 nfs-client]# kubectl apply -f pvc.yml
kube persistentvolumeclaim/pvc1 created
[root@server2 nfs-client]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Pending /等待状态 6s
[root@server2 nfs-client]# kubectl get pv
No resources found in default namespace.
当我们不绑定存储类时,pvc就不能通过存储类来让分配器给他分配pv。
因此我们还应该在创建一个 default (默认)的存储类
StorageClass
将被用于动态的为没有特定 storage class
需求的PersistentVolumeClaims
配置存储:(只能有一个默认StorageClass)默认StorageClass
,PVC 也没有
指定storageClassName
的值,那么意味着它只能够跟 storageClassName 也是“”
的 PV 进行绑定。当前我们没有指定存储类进行绑定,因为恶哦门门注释掉了参数。
[root@server2 nfs-client]# kubectl delete -f pvc.yml
[root@server2 nfs-client]# kubectl patch storageclass managed-nfs-storage -p '{"metadata":{"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
也可以将参数加到资源清单中,永久生效:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
annotations:
"storageclass.kubernetes.io/is-default-class": "true" /加到这里
provisioner: caoaoyuan.org/nfs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "false"
相当于在 annotations
这里加了一个参数,将 managed-nfs-storage 这个存储类设置成了默认的存储类,这样我们在动态配置存储时就可以不指定特定的存储类而使用这个默认的存储类。
[root@server2 nfs-client]# kubectl get storageclasses.storage.k8s.io
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
managed-nfs-storage caoaoyuan.org/nfs Delete Immediate false 3d17h
[root@server2 nfs-client]# kubectl patch storageclass managed-nfs-storage -p '{"metadata":{"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
storageclass.storage.k8s.io/managed-nfs-storage patched
[root@server2 nfs-client]# kubectl get storageclasses.storage.k8s.io
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
managed-nfs-storage (default) caoaoyuan.org/nfs Delete Immediate false 3d17h
前后对比我们可以发现存储类后面多了一个default。
应用:
[root@server2 nfs-client]# kubectl apply -f pvc.yml
persistentvolumeclaim/pvc1 created
[root@server2 nfs-client]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Bound pvc-ac06d238-bc06-4126-bdf0-cef617a8ccf9 1G RWX managed-nfs-storage 5s
当没有静态和动态的存储类时,就匹配到了默认存储类。