为 Kubernetes Deployment 中的每个副本绑定不同的 Persistent Volume [英] Bind different Persistent Volume for each replica in a Kubernetes Deployment

查看:29
本文介绍了为 Kubernetes Deployment 中的每个副本绑定不同的 Persistent Volume的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有 ReadWriteOnce 访问模式的 PVC,它由将运行有状态应用程序并使用此 PVC 的 logstash 部署使用.部署中的每个 pod 将尝试绑定到相同的持久卷声明.如果副本数 > 1,则会失败(因为它支持 ReadWriteOnce,只有第一个能够成功绑定).如何指定每个 Pod 绑定到单独的 PV.

I am using a PVC with ReadWriteOnce access mode, which is used by a logstash Deployment which will run a stateful application and use this PVC.Each pod in the deployment will try to bind to the same persistent volume claim. In case of replicas > 1, it will fail (as it supports ReadWriteOnce, only the first one will be able to bind successfully). How do I specify that each pod is to be bound to a separate PV.

我不想为每个 logstash 副本/实例定义 3 个单独的 yaml

I don't want to define 3 separate yamls for each logstash replica / instance

apiVersion: apps/v1
kind: Deployment
metadata:
  name: logstash
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: logstash
    spec:
      containers:
        image: "logstash-image"
        imagePullPolicy: IfNotPresent
        name: logstash
        volumeMounts:
        - mountPath: /data
          name: logstash-data
      restartPolicy: Always
      volumes:
      - name: logstash-data
        persistentVolumeClaim:
          claimName: logstash-vol

需要一种方法将不同 PV 的卷挂载到不同的 pod 副本.

Need a way to do volume mount of different PVs to different pod replicas.

推荐答案

对于部署,您无法正确执行此操作.您应该使用 StatefulSet 和 PVC 模板来实现您的目标.您的 StatefulSet YAML 代码片段的一部分可能如下所示:

With Deployments you cannot do this properly. You should use StatefulSet with PVC template to achieve your target. The part of your StatefulSet YAML code snippet could look like this:

...
volumeClaimTemplates:
- metadata:
    name: pv-data
  spec:
    accessModes: 
      - ReadWriteOnce
    resources:
      requests:
        storage: 5G

假设你有 3 个副本,你会看到 pod 是一个一个依次创建的,并且在 pod 创建过程中请求了 PVC.

assuming you have 3 replicas, you will see the pods are created one by one sequentially, and the PVC is requested during the pod creation.

PVC 被命名为volumeClaimTemplate name + pod-name + ordinal number 结果,您将拥有新创建的 PVC 的列表:

The PVC is named as volumeClaimTemplate name + pod-name + ordinal number and as result, you will have the list of newly created PVCs:

pv-data-<pod_name>-0
pv-data-<pod_name>-1
pv-data-<pod_name>-N

StatefulSet 使您的 Pod 的名称(不仅是实际上的名称)成为静态的,并根据副本数量递增它们,这就是为什么每个 Pod 将分别匹配自己的 PVC 和 PV

StatefulSet makes the names (not only names in fact) of your pods static and increments them depending on replica count, thats why every Pod will match its own PVC and PV respectively

注意:这称为动态配置.你应该熟悉配置 kubernetes 控制平面组件(如控制器管理器)来实现这一点,因为你需要配置持久存储(其中之一)提供程序并了解您数据的保留政策,但这完全是另一回事问题...

Note: this is called dynamic provisioning. You should be familiar with configuring kubernetes control plane components (like controller-manager) to achieve this, because you will need configured persistent storage (one of them) providers and understand the retain policy of your data, but this is completely another question...

这篇关于为 Kubernetes Deployment 中的每个副本绑定不同的 Persistent Volume的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆