是否可以将不同的 pod 挂载到本地持久卷的同一部分? [英] Is it possible to mount different pods to the same portion of a local persistent volume?

查看:38
本文介绍了是否可以将不同的 pod 挂载到本地持久卷的同一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在创建一个包含 2 个 Pod(单主机集群)的 StatefulSet 部署,我希望每个 Pod 都能够挂载到主机中的一个基本文件夹,以及它下面的一个子文件夹:

基本文件夹挂载:/mnt/disks/ssd

Pod#1 -/mnt/disks/ssd/pod-1

Pod#2 -/mnt/disks/ssd/pod-2

我只设法将第一个 pod 挂载到基本文件夹,但第二个文件夹无法挂载(因为卷已被占用)

这是音量:

apiVersion: v1种类:持久卷元数据:名称:example-local-pv规格:容量:存储:5Gi访问模式:- 读写一次持久卷回收策略:删除存储类名称:本地存储当地的:路径:/mnt/disks/ssd节点亲和力:必需的:节点选择器条款:- 匹配表达式:- 密钥:kubernetes.io/hostname接线员:在价值观:- ubuntukuber

这是有状态集中的用法:

apiVersion: apps/v1beta1种类:StatefulSet元数据:名称:应用程序命名空间:test-ns规格:服务名称:应用程序复制品:2........卷挂载:- 名称:数据挂载路径:/var/lib/app/data体积索赔模板:- 元数据:名称:数据规格:访问模式:[ReadWriteOnce"]存储类名称:本地存储"资源:要求:存储:2Gi

所以,我基本上希望每个副本都使用自己的子文件夹 - 如何实现?

== 编辑 ==

我已经取得了一些进展,我能够使用以下 YAML 将多个副本安装到同一个挂载中(我尝试在其上执行此操作的应用程序是 rabbitmq - 所以我将应用程序名称保留为是)

---种类:持久卷api版本:v1元数据:名称:光伏本地命名空间:test-rabbitmq标签:类型:本地规格:存储类名称:本地容量:存储:6Gi访问模式:- 读写一次主机路径:路径:/mnt/磁盘"---种类:PersistentVolumeClaimapi版本:v1元数据:名称:hostpath-pvc命名空间:test-rabbitmq规格:存储类名称:本地访问模式:- 读写一次资源:要求:存储:3Gi选择器:匹配标签:类型:本地---

在 StatefulSet 中,我声明了这个卷:

 卷:- 名称:rabbitmq-data持久卷声明:声明名称:hostpath-pvc

并安装rabbitmq-data".

两个 pod 都挂载到同一个文件夹,但不会创建子文件夹 - 这并不可怕,因为默认情况下有 rabbitmq 的子文件夹 - 我会尝试将其扩展到每个 pod 中以使用子文件夹

解决方案

我能够实现上述场景,您需要的是在您的 pv 中使用claimRef"来绑定您的 PVC.请看下面的 pv json 和 statefulset json

PV-0.json

<代码>{"kind": "PersistentVolume","apiVersion": "v1",元数据":{"name": "pv-data-vol-0",标签": {类型":本地"}},规格":{容量": {存储":10Gi"},访问模式":[读写一次"],"storageClassName": "本地存储",当地的": {路径":/prafull/data/pv-0"},索赔参考":{命名空间":默认",名称":数据测试-SF-0"},节点亲和力":{必需的": {节点选择器条款":[{匹配表达式":[{"key": "kubernetes.io/主机名","operator": "在",价值观":[ip-10-0-1-46.ec2.internal"]}]}]}}}}

PV-1.json

<代码>{"kind": "PersistentVolume","apiVersion": "v1",元数据":{"name": "pv-data-vol-1",标签": {类型":本地"}},规格":{容量": {存储":10Gi"},访问模式":[读写一次"],"storageClassName": "本地存储",当地的": {路径":/prafull/data/pv-1"},索赔参考":{命名空间":默认",名称":数据测试-SF-1"},节点亲和力":{必需的": {节点选择器条款":[{匹配表达式":[{"key": "kubernetes.io/主机名","operator": "在",价值观":[ip-10-0-1-46.ec2.internal"]}]}]}}}}

Statefulset.json

<代码>{"kind": "StatefulSet","apiVersion": "apps/v1beta1",元数据":{"name": "test-sf",标签": {状态":测试-SF"}},规格":{副本":2,模板": {元数据":{标签": {应用程序":测试-SF"},注释":{"pod.alpha.kubernetes.io/initialized": "true"}}......},volumeClaimTemplates":[{元数据":{名称":数据"},规格":{访问模式":[读写一次"],"storageClassName": "本地存储",资源": {要求": {存储":10Gi"}}}}]}}

将创建两个 pod,test-sf-0 和 test-sf-1 依次创建两个 PVC data-test-sf-0 和 data-test-sf-1,它们将绑定到 PV-0 和 Pv-1 分别.因此,test-sf-0 将写入 PV-0 中指定的位置,而 test-sf-1 将写入 PV-1 中指定的位置.希望这会有所帮助.

Basically, I'm creating a StatefulSet deployment with 2 pods (single host cluster), I would like to that each pod will be able to mount to a base folder in the host, and to a subfolder beneath it:

Base folder mount: /mnt/disks/ssd

Pod#1 - /mnt/disks/ssd/pod-1

Pod#2 - /mnt/disks/ssd/pod-2

I've managed only to mount the first pod to the base folder, but the 2nd folder cannot mount (as the volume is already taken)

This is the volume:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-local-pv
spec:
  capacity:
    storage: 5Gi 
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /mnt/disks/ssd
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - ubuntukuber

This is the usage in the stateful set:

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: app
  namespace: test-ns
spec:
  serviceName: app
  replicas: 2
....
....
        volumeMounts:
          - name: data
            mountPath: /var/lib/app/data
volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: "local-storage"
        resources:
          requests:
            storage: 2Gi

So, i basically would like that each replica would use its own subfolder - how can one achieve it?

== EDIT ==

I've made some progress, i'm able to mount several replicas into the same mount, using the following YAMLs (the app i'm trying to do it on is rabbitmq - so i'll leave the app name as is)

---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-local
  namespace: test-rabbitmq
  labels:
    type: local
spec:
  storageClassName: local
  capacity:
    storage: 6Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/disks"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: hostpath-pvc
  namespace: test-rabbitmq
spec:
  storageClassName: local
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
  selector:
    matchLabels:
      type: local
---

In the StatefulSet i'm declaring this volume:

  volumes:
    - name: rabbitmq-data
      persistentVolumeClaim:
        claimName: hostpath-pvc

And mounting "rabbitmq-data".

Both pods mount to the same folder, but will not create subfolders - this is no terrible situation as by default there are rabbitmq's subfolders - i'll try to expand it into each pod to use a subfolder

解决方案

I am able to achieve the above scenario, what you need is "claimRef" in your pv to bind your PVC. Please have a look at following pv json and statefulset json

PV-0.json

{
  "kind": "PersistentVolume",
  "apiVersion": "v1",
  "metadata": {
    "name": "pv-data-vol-0",
    "labels": {
      "type": "local"
    }
  },
  "spec": {
    "capacity": {
      "storage": "10Gi"
    },
    "accessModes": [
      "ReadWriteOnce"
    ],
    "storageClassName": "local-storage",
    "local": {
      "path": "/prafull/data/pv-0"
    },
    "claimRef": {
      "namespace": "default",
      "name": "data-test-sf-0"
    },
    "nodeAffinity": {
      "required": {
        "nodeSelectorTerms": [
          {
            "matchExpressions": [
              {
                "key": "kubernetes.io/hostname",
                "operator": "In",
                "values": [
                  "ip-10-0-1-46.ec2.internal"
                ]
              }
            ]
          }
        ]
      }
    }
  }
}

PV-1.json

{
  "kind": "PersistentVolume",
  "apiVersion": "v1",
  "metadata": {
    "name": "pv-data-vol-1",
    "labels": {
      "type": "local"
    }
  },
  "spec": {
    "capacity": {
      "storage": "10Gi"
    },
    "accessModes": [
      "ReadWriteOnce"
    ],
    "storageClassName": "local-storage",
    "local": {
      "path": "/prafull/data/pv-1"
    },
    "claimRef": {
      "namespace": "default",
      "name": "data-test-sf-1"
    },
    "nodeAffinity": {
      "required": {
        "nodeSelectorTerms": [
          {
            "matchExpressions": [
              {
                "key": "kubernetes.io/hostname",
                "operator": "In",
                "values": [
                  "ip-10-0-1-46.ec2.internal"
                ]
              }
            ]
          }
        ]
      }
    }
  }
}

Statefulset.json

{
  "kind": "StatefulSet",
  "apiVersion": "apps/v1beta1",
  "metadata": {
    "name": "test-sf",
    "labels": {
      "state": "test-sf"
    }
  },
  "spec": {
    "replicas": 2,
    "template": {
      "metadata": {
        "labels": {
          "app": "test-sf"
        },
        "annotations": {
          "pod.alpha.kubernetes.io/initialized": "true"
        }
      }
      ...
      ...
    },
    "volumeClaimTemplates": [
      {
        "metadata": {
          "name": "data"
        },
        "spec": {
          "accessModes": [
            "ReadWriteOnce"
          ],
          "storageClassName": "local-storage",
          "resources": {
            "requests": {
              "storage": "10Gi"
            }
          }
        }
      }
    ]
  }
}

There will be two pods created test-sf-0 and test-sf-1 which in-turn will be created two PVC data-test-sf-0 and data-test-sf-1 which will be bound to PV-0 and Pv-1 respectively. Hence test-sf-0 will write to the location specified in PV-0 and test-sf-1 will write in location specified on PV-1. Hope this helps.

这篇关于是否可以将不同的 pod 挂载到本地持久卷的同一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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