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

问题描述

我正在使用具有ReadWriteOnce访问模式的PVC,由logstash部署使用它将运行有状态的应用程序并使用此PVC.部署中的每个pod都将尝试绑定到相同的持久卷声明.如果副本数> 1,它将失败(因为它支持ReadWriteOnce,因此只有第一个能够成功绑定).如何指定将每个吊舱绑定到单独的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个副本,您会看到按顺序依次创建容器,并且在容器创建期间请求了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 asvolumeClaimTemplate 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部署中的每个副本绑定不同的持久卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 11:23