1

I've pod with two containers, one is creating a file and one delete it, I was able to create the file but not to delete it. I want it to delete the files every 2 hours, how can I make it work in clean way? we dont want to use cron job...

apiVersion: v1
    kind: Pod
    metadata:
      name: its
    spec:
      volumes:
      - name: common
        emptyDir: {}
      containers:
      - name: 1st
        image: nginx
        volumeMounts:
        - name: common
          mountPath: /usr/share/nginx/html
      - name: 2nd
        image: debian
        volumeMounts:
        - name: common
          mountPath: /html
        command: ["/bin/sh", "-c"]
        args:
          - while true; do
              date >> /html/index.html;
              sleep 7200;
            done
PJEM
  • 557
  • 7
  • 33
  • I dont see you trying to delete it. You only write the date to that file. – The Fool May 07 '22 at 15:30
  • 1
    To ```delete the files every 2 hours``` you should use CronJob https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/ . I cannot understand why you don't want to use it. – Franxi Hidro May 07 '22 at 16:04
  • There are lots of flaws & unclarity in your question and attempt you made. Like you wanted to create file from container1 but there no code of same in container1, you wanted to remove file in every 2hr but there is no code to remove file, you are doing periodic task in you pod in clean way but not liked to use cronJob. Due to these unclarities I am marking this post to **CLOSE**. – dahiya_boy May 07 '22 at 18:16

2 Answers2

2

This is what works for me

apiVersion: v1
kind: Pod
metadata:
  name: mc1
spec:
  volumes:
  - name: html
    emptyDir: {}
  containers:
  - name: 1st
    image: nginx
    command: ["/bin/sh", "-c"]
    args:
      - while true; do
          touch /usr/share/nginx/html/test.txt;
          ls /usr/share/nginx/html/;
          echo "file created cotnainer 1";
          sleep infinity;
        done
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  - name: 2nd
    image: debian
    volumeMounts:
    - name: html
      mountPath: /html
    command: ["/bin/sh", "-c"]
    args:
      - while true; do
          ls /html;
          rm /html/test.txt;
          echo "container 2 - file removed";
          ls /html;
          sleep 7200;
        done

i am creating a file from container 1 and getting removed by container 2 what error are you getting?

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
  • I wanted to know how its working ?? A/c to ques, op wanted to create file from container1, Im not seeing anything like this. Second, you are creating file in container2 `date >> /html/index.html;` and just after it you are removing the file `rm /html/index.html` and sleep for 2 hr instead OP wanted to delete file in every 2 hour. – dahiya_boy May 07 '22 at 18:07
  • sorry my mistake updated answer. – Harsh Manvar May 07 '22 at 18:26
  • I think you must have to execute the code and add your output over here, bcz I dont think still it would work. NOTE: you can test with 10 min span. – dahiya_boy May 07 '22 at 18:30
  • could you check now once and file is for ref I don't think he will be applying as it is and creating file from nginx based image. – Harsh Manvar May 07 '22 at 18:49
  • feel free to refer this logs and output for ref : https://i.stack.imgur.com/9YN26.png in 10 min both container will be having 10 min sleep time. – Harsh Manvar May 07 '22 at 18:59
0

Using nginx container with an alpine base you need to install crond, here is an example: Enable crond in an Alpine container

Now, you could run a cron task in the same container that has the files so you just need 1 container for the pod.

Also, here is another example on how to run crond in an alpine docker container:

https://devopsheaven.com/cron/docker/alpine/linux/2017/10/30/run-cron-docker-alpine.html

paltaa
  • 2,985
  • 13
  • 28