kubernetes PVC 共享单个 PV? [英] kubernetes PVCs sharing a single PV?
问题描述
我正在尝试为 3 个 pod 部署一个持久卷,我想使用集群的节点存储,即不是像 ebs 分拆那样的外部存储.
I am trying to deploy a persistentvolume for 3 pods to work on and i want to use the cluster's node storage i.e. not an external storage like ebs spin off.
为了达到上述目的,我做了以下实验 -
To achieve the above i did the following experiment's -
1) 我只应用了下面定义的 PVC 资源 -
1) I applied only the below PVC resource defined below -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
creationTimestamp: null
labels:
io.kompose.service: pv1
name: pv1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
status: {}
此旋转是默认存储类设置的存储,在我的情况下是数字海洋的卷.所以它创建了一个 1Gi 的卷.
This spin's up a storage set by default storageclass, which in my case was digital ocean's volume. So it created a 1Gi volume.
2) 创建一个 PV 资源和 PVC 资源,如下所示 -
2) Created a PV resource and PVC resource like below -
apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
<小时>
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
creationTimestamp: null
labels:
io.kompose.service: pv1
name: pv1
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
status: {}
发布此信息,我看到我的声明受到约束.
Post this i see my claim is bound.
pavan@p1:~$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pv1 Bound task-pv-volume 10Gi RWO manual 2m5s
pavan@p1:~$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
task-pv-volume 10Gi RWO Retain Bound default/pv1 manual 118m
pavan@p1:~$ kubectl describe pvc
Name: pv1
Namespace: default
StorageClass: manual
Status: Bound
Volume: task-pv-volume
Labels: io.kompose.service=pv1
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"PersistentVolumeClaim","metadata":{"annotations":{},"creationTimestamp":null,"labels":{"io.kompose.service":"mo...
pv.kubernetes.io/bind-completed: yes
pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pvc-protection]
Capacity: 10Gi
Access Modes: RWO
VolumeMode: Filesystem
Mounted By: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning ProvisioningFailed 28s (x8 over 2m2s) persistentvolume-controller storageclass.storage.k8s.io "manual" not found
以下是我希望得到答案/指示的问题-
Below are my questions that i am hoping to get answers/pointers to -
上面的警告,找不到存储类,需要吗创建一个?如果是这样,你能告诉我为什么以及如何吗?或任何指针.(不知何故此链接没有说明 - https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/)
注意 PV 的存储容量为 10Gi,PVC 的请求容量为 1Gi,但 PVC 仍然绑定了 10Gi 的容量?我不能与其他 PVC 共享相同的 PV 容量吗?
Notice the PV has storage capacity of 10Gi and PVC with request capacity of 1Gi, but still PVC was bound with 10Gi capacity? Can't i share the same PV capacity with other PVCs?
对于问题 2) 如果我必须为具有所需容量的不同 PVC 创建不同的 PV,我是否也必须创建 storageclass?还是同一个存储类,使用选择器选择对应的PV?
For question 2) If i have to create different PVs for different PVC with the required capacity, do i have to create storageclass as-well? Or same storage class and use selectors to select corresponding PV?
推荐答案
我试图重现所有行为来回答您的所有问题.但是,我无法访问 DigitalOcean,因此我在 GKE 上对其进行了测试.
I was trying to reproduce all behavior to answer all your questions. However, I don't have access to DigitalOcean, so I tested it on GKE.
上面的警告,找不到存储类,需要吗创建一个?
The above warning, storage class could not be found, do i need to create one?
根据文档和最佳实践,强烈建议创建一个storageclass
,然后基于它创建 PV/PVC.但是,有一种叫做手动配置"的东西.在这种情况下你做了什么.
According to the documentation and best practices, it is highly recommended to create a storageclass
and later create PV / PVC based on it. However, there is something called "manual provisioning". Which you did in this case.
手动配置是指您需要先手动创建 PV,然后手动创建具有匹配 spec.storageClassName:
字段的 PVC.示例:
Manual provisioning is when you need to manually create a PV first, and then a PVC with matching spec.storageClassName:
field. Examples:
- 如果您创建的 PVC 没有
default storageclass
、PV
和storageClassName
参数(afaik kubeadm 不提供默认的storageclass
code>) - PVC 将卡在Pending
事件:没有持久卷可用于此声明并且没有设置存储类. - 如果您使用
集群上的默认存储类设置
但没有storageClassName
参数创建PVC,它将基于默认存储类创建它. - 如果您使用
storageClassName
参数(在云端、Minikube 或 kubeadm 中的某个位置)创建 PVC - PVC 也将Pending
并带有警告:storageclass.storage.k8s.io手册"未找到.但是,如果您使用相同的storageClassName
参数创建PV,它将在一段时间内被绑定.
- If you create a PVC without
default storageclass
,PV
andstorageClassName
parameter (afaik kubeadm is not providing defaultstorageclass
) - PVC will be stuck onPending
with event: no persistent volumes available for this claim and no storage class is set. - If you create a PVC with
default storageclass setup on cluster
but withoutstorageClassName
parameter it will create it based on default storageclass. - If you create a PVC with
storageClassName
parameter (somewhere in the Cloud, Minikube or kubeadm) - PVC will also bePending
with the warning: storageclass.storage.k8s.io "manual" not found. However, if you create PV with the samestorageClassName
parameter, it will be bound in a while.
示例:
$ kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/task-pv-volume 10Gi RWO Retain Available manual 4s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pv1 Pending manual 4m12s
...
kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/task-pv-volume 10Gi RWO Retain Bound default/pv1 manual 9s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pv1 Bound task-pv-volume 10Gi RWO manual 4m17s
手动配置
的缺点是您必须为每个 PVC 创建 PV.如果你使用 storageclass
你可以创建 PVC
.
The disadvantage of manual provisioning
is that you have to create PV for each PVC. If you use storageclass
you can just create PVC
.
如果是这样,你能告诉我为什么以及如何吗?或任何指针.
If so, can you tell me why and how? or any pointer.
您可以使用文档 示例或查看此处.当您使用带有默认 storageclass
的云时,您可以通过以下方式将其导出到 yaml:$ kubectl get sc -oyaml >>存储类.yaml
.或者,如果您有多个,则必须指定哪一个.storageclass
的名称可以通过
$ kubectl get sc
获取.后面可以参考K8s API 用于自定义您的 storageclass
.
You can use documentation examples or check here. As you are using cloud with default storageclass
you can export it to yaml by:
$ kubectl get sc -oyaml >> storageclass.yaml
.
Or if you have more than one, you have to specify which one. Names of storageclass
can be obtained by
$ kubectl get sc
.
Later you can refer to K8s API to customize your storageclass
.
注意 PV 有 10Gi 的存储容量和 PVC 的请求1Gi容量,PVC还是绑定10Gi容量?
Notice the PV has storage capacity of 10Gi and PVC with request capacity of 1Gi, but still PVC was bound with 10Gi capacity?
您手动创建了一个 10Gi 的 PV,而 PVC 请求了 1Gi.由于 PVC 和 PV 的边界为 1:1,因此 PVC 搜索满足所有条件的 PV 并与之绑定.PVC (pv1) 请求 1Gi 并且 PV (task-pv-volume) 满足这些要求,因此 Kubernetes 绑定了它们.不幸的是,在这种情况下,大部分空间都被浪费了.
You created manually a PV with 10Gi and the PVC requested 1Gi. As PVC and PV are bounding 1:1, PVC searched for a PV which meets all conditions and bound to it. PVC (pv1) requested 1Gi and the PV (task-pv-volume) meet those requirements so Kubernetes bound them. Unfortunately much of the space was wasted in this case.
我不能与其他 PVC 共享相同的 PV 容量
Can't i share the same PV capacity with other PVCs
不幸的是,您不能将 2 个 PVC 绑定到 1 个 PV,因为 PVC 和 PV 之间的关系是 1:1,但是您可以配置多个 pod/deployment 以使用相同的 PVC.
Unfortunately, you cannot bound 2 PVC to 1 PV as the relationship between PVC and PV is 1:1, but you can configure many pods/deployments to use the same PVC.
我建议你看看这个stackoverflowcase 因为它很好地解释了 AccessMode
细节.
I can advise you to look at this stackoverflow case as it explains very well AccessMode
specifics.
如果我必须为不同的 PVC 创建不同的 PV容量,我是否也必须创建存储类?或相同的存储class 并使用选择器来选择对应的 PV?
If i have to create different PVs for different PVC with the required capacity, do i have to create storageclass as-well? Or same storage class and use selectors to select corresponding PV?
正如我之前提到的,如果您手动创建具有特定大小的 PV 并绑定到它的 PVC,请求较少,则会浪费额外的空间.所以,你必须创建具有相同资源请求的 PV 和 PVC,或者让 storageclass
根据 PVC 请求调整存储.
As I mentioned before, if you create PV manually with a specific size and a PVC bounded to it, which request less, the extra space will be wasted. So, you have to create PV and PVC with the same resource request, or let storageclass
adjust the storage based on PVC request.
这篇关于kubernetes PVC 共享单个 PV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!