1

I have my application running in K8S pods. my application writes logs to a particular path for which I already have volume mounted on the pod. my requirement is to schedule cronjob which will trigger weekly once and read the logs from that pod volume and generate a report base on my script (which is basically filtering the logs based on some keywords). and send the report via mail. unfortunately I am not sure how I will proceed on this as I couldn't get any doc or blog which talks about integrating conrjob to existing pod or volume.

apiVersion: v1
kind: Pod
metadata:
name: webserver
spec:
  volumes:
  - name: shared-logs
    emptyDir: {}

  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/nginx

   - name: sidecar-container
     image: busybox
     command: ["sh","-c","while true; do cat /var/log/nginx/access.log /var/log/nginx/error.log; sleep 30; done"]
     volumeMounts:
      - name: shared-logs
        mountPath: /var/log/nginx


apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: "discovery-cronjob"
labels:
  app.kubernetes.io/name: discovery
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
  spec:
    template:
      spec:
        containers:
        - name: log-report
          image: busybox
          command: ['/bin/sh']
          args:  ['-c', 'cat /var/log/nginx/access.log > nginx.log']
          volumeMounts:
          - mountPath: /log
            name: shared-logs
        restartPolicy: Never

1 Answers1

0

I see two things here that you need to know:

  1. Unfortunately, it is not possible to schedule a cronjob on an existing pod. Pods are ephemeral and job needs to finish. It would be impossible to tell if the job completed or not. This is by design.

  2. Also in order to be able to see the files from one pod to another you must use a PVC. The logs created by your app have to be persisted if your job wants to access it. Here you can find some examples of how to Create ReadWriteMany PersistentVolumeClaims on your Kubernetes Cluster:

Kubernetes allows us to provision our PersistentVolumes dynamically using PersistentVolumeClaims. Pods treat these claims as volumes. The access mode of the PVC determines how many nodes can establish a connection to it. We can refer to the resource provider’s docs for their supported access modes.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37