0

I am new to Kubernetes, I am creating POD on run time to push data and after pushing and collecting data I am deleting POD.

For the processing of files I have connected SSD. and assigned its path as hostPath: /my-drive/example while creating POD. Now when i run my POD i can see the files in defined path.

But, Now I just wanted to delete files created by POD in a hostPath directory while deleting POD. is it possible?

My POD file looks like.

apiVersion: v1
kind: Pod
metadata:
  name: pod-example
  labels:
    app: pod-example
spec:
  containers:
  - name: pod-example
    image: "myimage.com/abcd:latest"
    imagePullPolicy: Always
    workingDir: /pod-example
    env:
    volumeMounts:
    - name: "my-drive"
      mountPath: "/my-drive"
  volumes:
  - name: "my-drive"
    persistentVolumeReclaimPolicy: Recycle
    hostPath:
      path: /my-drive/example
  restartPolicy: Never
  imagePullSecrets:
  - name: regcred
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: "kubernetes.io/hostname"
            operator: In
            values:
            - my-node
        topologyKey: "kubernetes.io/hostname"
1
  • Can you use a Kubernetes-managed persistent volume (maybe changing this to a StatefulSet)? You should almost never use hostPath directories, since they have a number of practical problems (most notably, you can misplace data if a pod is deleted and recreated on a different node). If the data is in a persistent volume then kubectl delete pvc will delete it. Commented Nov 7, 2022 at 10:23

2 Answers 2

2

You can achieve this by using lifecycle hooks in K8s. Under them, preStop hook can be used here since you need to do action when stopping the pod.

Check docs related to lifecycle hooks: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/

If you exact know the files or let's say a directory to delete, you can use Exec hook hanlder. Check the sample below that I've added for your reference.

lifecycle:
  preStop:
    exec:
      command:
        - "sh"
        - "-c"
        - >
          echo "Deleting files in my-drive/example/to-be-deleted" > /proc/1/fd/1 # Add preStop hook's stdout to main process's stdout
          rm -r my-drive/example/to-be-deleted

P.S. According to your problem statement, you are not using the POD continuously it seems. If the task that you are looking is to execute periodically or not continuous, I would suggest you to select either K8s CronJob or Job rather a POD. Make sure to have required user access inside the container to delete files/floders.

Sign up to request clarification or add additional context in comments.

4 Comments

Sir, let me try this this solution looks like what i am looking for.... btw I am just using pod for specific purpose from a python script and i also destroys them using same script. it takes 10 minutes.... Mean while it generates somefiles in the directory. But files remains there after deletion.
If you are using the pod for a specific requirement, that is fine. Hope my solution will fit to you.
It only delete files inside container... (kubectl exec -it m-pod --/bin/sh) if I mount hostPath.. i can porcess and see files on the mounted direcectory without accessing files
@RogBoy, we can only delete files in the mounted directory. e.g. if there is test.txt file in the volume such as /my-drive/example/test.txt, this can be deleted. If the above provided logic doesn't work, you can try deleting the file inside the logic executed inside the container. lifecycle hook should be triggered before killing the pod in order to use the logic I have added in the answer. Further, you do not need to use pod anti afinity and PVM policies for hostPath volume mounts. Check docs for hostPath volume mount: kubernetes.io/docs/concepts/storage/volumes/#hostpath
0

Update persistentVolumeReclaimPolicy to Delete as shown below

persistentVolumeReclaimPolicy: Delete

6 Comments

But I am not using PV, I am directly using hostPath. I dont think so if i can use this with hostPath.
if you dont use Local persistent volume then you will have to manually delete that mount path
So for I am creating PODs using script... so If I assign PV only in POD settings ... is it enough... of do i need to delete claim as well to delete data. ? Because deletion of PV and Claim isnt possible for us right now.
if you assign pv on pod definition then you will have to manually delete the pv after the pod is deleted
so is there any work around to delete local-storage automatically while deleting POD ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.