简体   繁体   English

卷中每个pod的动态卷配置文件夹?

[英]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:一个例子是:

  • PersistentVolume = "/data"持久卷=“/数据”
  • Pods:豆荚:
    • pod-0 = "/data/pod-0" pod-0 = "/data/pod-0"
    • pod-1 = "/data/pod-1" pod-1 = "/data/pod-1"

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 construct subPath directory names from Downward API environment variables.使用subPathExpr字段从 Downward API 环境变量构造subPath目录名称。 This feature requires the VolumeSubpathEnvExpansion feature gate to be enabled.此功能需要启用VolumeSubpathEnvExpansion 功能门 It is enabled by default starting with Kubernetes 1.15.它从 Kubernetes 1.15 开始默认启用。 The subPath and subPathExpr properties are mutually exclusive. subPathsubPathExpr属性是互斥的。

Let me know if you have any questions.如果您有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Kubernetes使用Terraform动态配置Cinder卷和持久卷 - Dynamic provisioning of Cinder volume and Persistent volume using Terraform through Kubernetes Kubernetes:使用 NFS 的动态持久卷配置 - Kubernetes : Dynamic Persistent Volume provisioning using NFS Kubernetes自定义卷插件与动态配置 - Kubernetes Custom Volume Plugin with Dynamic Provisioning Kubernetes 每个节点一个 Pod,每个 Pod 一个持久卷 - Kubernetes one pod per node, one persistent volume per pod 将容器中的文件夹公开为 Kubernetes Pod 中的卷 - Expose folder from a container as a volume in a Kubernetes Pod Kubernetes - 为每个卷(而不是每个 pod)设置自定义权限/文件所有权 - Kubernetes - setting custom permissions/file ownership per volume (and not per pod) 如何为多租户环境使用动态持久卷配置 - how to use dynamic persistent volume provisioning for multitenancy environment kubernetes:使用iSCSI和NFS的动态持久卷配置 - kubernetes : dynamic persistent volume provisioning using iSCSI and NFS 有没有一种方法可以在kubernetes部署(或有状态集)中为每个Pod创建持久卷? - Is there a way to create a persistent volume per pod in a kubernetes deployment (or statefulset)? 存储 class 是否为每个 pod 动态配置持久卷? - Does the storage class dynamically provision persistent volume per pod?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM