Is there a way to create a PV & PVC per pod created by a job in Kubernetes?

99 views Asked by At

I have a K8s Job where parallelism is set depending on the environment it runs on. So the Job can create 1+ pods depending on the value specified for parallelism in values.yaml.

Is there any way to point each Pod created by the same Job to a different PVC?

For example:

JobA - Pod1 - uses PVC1 - points to PV1
     - Pod2 - uses PVC2 - points to PV2
     - Pod3 - uses PVC3 - points to PV3

I'd like to avoid doing the following as a workaround:

  • Using 1 shared PVC & PV and creating sub-folders on the mount
  • Doing some magic in Helm templating like a loop from 1-x and for every iteration, creating a separate Job with a separate pod and a separate PV,PVC - it needs to be just one Job and separate PVCs for every Pod
1

There are 1 answers

0
Sourav On

So, it seems you're looking for a way to create separate PVCs and PVs for each pod created by the same Job. While it's not a common practice, you can achieve this by using dynamic provisioning and some scripting or customization.

Ensure that you have a dynamic storage class configured in your Kubernetes cluster. Dynamic provisioning allows you to create PVs and PVCs on-demand. You can use Helm or direct Kubernetes manifests to customize your Job template. Define a PVC template within your Job template, but with dynamic names using placeholders, like {POD_NAME}. You can write a custom Kubernetes controller, like a Mutating Admission Controller or a webhook, to intercept the creation of pods by your Job and dynamically generate separate PVCs for each pod. You can create a script that interacts with the Kubernetes API using kubectl to create individual PVCs and PVs for each pod, based on the number of pods created by the Job.

Here is an example of a script to create individual PVCs for each pod created by a Job:

Check this

And, do replace your-job-name and parallelism with your actual values.

NOTE: Keep in mind that this approach involves some complexity and may not be a recommended best practice and you should carefully evaluate whether this approach is suitable for your specific use case and requirements.