[英]Dynamic volume provisioning folder per pod in a volume?
What I am aiming for is a folder(pod name) per pod created inside of a volume using a volumeClaimsTemplate in a StatefulSet.我的目标是使用 StatefulSet 中的 volumeClaimsTemplate 在卷内创建的每个 pod 的文件夹(pod 名称)。
An example would be:一个例子是:
I am struggling with getting the replicas to create new folders for themselves.我正在努力让副本为自己创建新文件夹。 Any help with how to do this would be grateful.任何有关如何做到这一点的帮助将不胜感激。
volumeClaimTemplates
is a list of claims that pods are allowed to reference.volumeClaimTemplates
是允许 pod 引用的声明列表。 The StatefulSet controller is responsible for mapping network identities to claims in a way that maintains the identity of a pod. StatefulSet controller 负责以维护 Pod 身份的方式将网络身份映射到声明。 Every claim in this list must have at least one matching (by name) volumeMount in one container in the template.此列表中的每个声明必须在模板的一个容器中至少有一个匹配的(按名称)volumeMount。 A claim in this list takes precedence over any volumes in the template, with the same name.此列表中的声明优先于模板中具有相同名称的任何卷。
This means that with volumeClaimTemplates
you can request the PVC from the storage class dynamically.这意味着使用volumeClaimTemplates
您可以动态地从存储 class 请求 PVC。
If we use this yaml
as an example:如果我们以这个yaml
为例:
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: "standard"
resources:
requests:
storage: 1Gi
Once you deploy your pods you notice that your pods are being created and PVC
is requested during the creation.部署 pod 后,您会注意到正在创建 pod,并且在创建过程中请求了PVC
。 PVC
is name in the following convention: PVC
是以下约定中的名称:
volumeClaimTemplate
name + Pod-name
+ Ordinal-number
volumeClaimTemplate
name + Pod-name
+ Ordinal-number
So if you take above yaml as an example you will receive three PVC (assuming 3 replicas):因此,如果您以 yaml 以上为例,您将收到三个 PVC(假设 3 个副本):
NAME STATUS VOLUME
www-web-0 Bound pvc-12d77135...
www-web-1 Bound pvc-08724947...
www-web-2 Bound pvc-50ac9f96
It's worth mentioning that Persistent Volume Claims
represent the exclusive usage of a Persistent Volume by a particular Pod.值得一提的是, Persistent Volume Claims
代表了特定 Pod 对持久卷的独占使用。 This means that if we look into the volumes individually we find that each is assign to a particular pod:这意味着,如果我们单独查看卷,我们会发现每个卷都分配给特定的 pod:
➜ ~ pwd
/tmp/hostpath-provisioner/pvc-08724947...
➜ ~ ls
web-1
➜ ~ pwd
/tmp/hostpath-provisioner/pvc-50ac9f96...
➜ ~ ls
web-2
While testing this I did achieve your goal but I had to create persistentvolumes
manually and they had to point towards the same local path:在对此进行测试时,我确实实现了您的目标,但我必须手动创建persistentvolumes
卷,并且它们必须指向相同的本地路径:
local:
path: /home/docker/data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
This combined with subPathExpr
mounted the directories named after the pods into the specified path.这与subPathExpr
结合将 pod 命名的目录安装到指定路径中。
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
subPathExpr: $(NAME)
env:
- name: NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
And the result of this ( web
was the name of the deployment):结果( web
是部署的名称):
➜ ~ pwd
/home/docker/data
➜ ~ pwd
web-0 web-1 web-2
Here`s more information how subpath with expanded env variables works.这里有更多信息,扩展环境变量的子路径是如何工作的。
Use the
subPathExpr
field to constructsubPath
directory names from Downward API environment variables.使用subPathExpr
字段从 Downward API 环境变量构造subPath
目录名称。 This feature requires theVolumeSubpathEnvExpansion
feature gate to be enabled.此功能需要启用VolumeSubpathEnvExpansion
功能门。 It is enabled by default starting with Kubernetes 1.15.它从 Kubernetes 1.15 开始默认启用。 ThesubPath
andsubPathExpr
properties are mutually exclusive.subPath
和subPathExpr
属性是互斥的。
Let me know if you have any questions.如果您有任何问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.