<p>I have a <code>StatefulSet</code> which looks like this</p>

<pre><code>apiVersion: v1
kind: StatefulSet
metadata:
  name: web
spec:
  ...
  volumeClaimTemplates:
  — metadata:
      name: www
    spec:
      resources:
        requests:
          storage: 1Gi
</code></pre>

<p>It will create a <code>PersistentVolumeClaim</code> (PVC) and a <code>PersistentVolume</code> (PV) for each <code>Pod</code> of a <code>Service</code> it controls. </p>

<p>I want to execute some commands on those PVs before the <code>Pod</code> creation.</p>

<p>I was thinking to create a <code>Job</code> which mounts those PVs and runs the commands but how do I know how many of PVs were created?</p>

<p>Is there a kubernetes-native solution to trigger some pod execution on PV creation?</p>

LEQADA
<p>The solution is <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">InitContianer</a>.</p>

<p>You can add it to a spec of your <code>StatufulSet</code>:</p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name:  web
spec:
...
  spec:
    initContainers:
    - name: init-myapp
      image: ubuntu:latest
      command:
      - bash
      - "-c"
      - "your command"
      volumeMounts:
      - name: yourvolume
        mountPath: /mnt/myvolume
</code></pre>
