Home > Software design >  Delete file in pod from one container priodically
Delete file in pod from one container priodically

Time:05-08

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

CodePudding user response:

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?

CodePudding user response:

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

  • Related