0

I have a StatefulSet which looks like this

apiVersion: v1
kind: StatefulSet
metadata:
  name: web
spec:
  ...
  volumeClaimTemplates:
  — metadata:
      name: www
    spec:
      resources:
        requests:
          storage: 1Gi

It will create a PersistentVolumeClaim (PVC) and a PersistentVolume (PV) for each Pod of a Service it controls.

I want to execute some commands on those PVs before the Pod creation.

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

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

LEQADA
  • 1,913
  • 3
  • 22
  • 41

1 Answers1

1

The solution is InitContianer.

You can add it to a spec of your StatufulSet:

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
Anton Kostenko
  • 8,200
  • 2
  • 30
  • 37
  • Won't `InitContainer` execute on every deployment? I want to execute only on PV creation. – LEQADA May 25 '20 at 18:15
  • 1
    It will execute on each container start, but you can manage that situation by a flag file on your volume to mark it as "processed", just create a file "processed" in the root of the volume. Unfortunately you cannot somehow run anything between volume creation by PVC and pod starts, because that is a fully async process and moreover some of volume provides even not create a volume until the first mount. Also, you can create something like a custom script/controller etc. but the way with Init Container is much more straight and clear. – Anton Kostenko May 25 '20 at 18:17