0

k8s 1.21

After job is successfully completed I need remove local directory /myjob-result on node. How I can do it?

### example job.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl:5.34.0
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
        volumeMounts:
          - name: my-data
            mountPath: /My_Data
      volumes:
       - name: my-data
         hostPath:
            path:/myjob-result
            type: Directory
      restartPolicy: Never
  backoffLimit: 4

tuytuy20
  • 1
  • 2
  • If you want to remove volume every time you a job successfully ends, then avoid using volume mounts. Otherwise, you can simply SSH into the node and delete the directory. – Taimoor Mirza May 24 '23 at 11:44
  • Thank you for your response, but ssh is not a solution for my case. I need to do it automaticaly. – tuytuy20 May 24 '23 at 11:52
  • 1
    Will your job work file without using `volumeMounts`? If this works, then use this. Pod's volumes are ephemeral by default i,e they are tied with its life-cycle. A job's pod upon completion will destroy it's own volume. The other way would be to use `preStop` container lifecycle hook to execute custom script which purges data. Check this out https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ – Taimoor Mirza May 24 '23 at 12:11

0 Answers0