Is it possible to mount different pods to the same portion of a local persistent volume? Is it possible to mount different pods to the same portion of a local persistent volume? kubernetes kubernetes

Is it possible to mount different pods to the same portion of a local persistent volume?


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.