<ul>
<li><p>I have an app that works by deployment service and communicates with the world with Nginx-Ingress-controller.</p>
</li>
<li><p>This app is connected to the MongoDB database.</p>
</li>
<li><p>I asked to create a cronjob that purges all MongoDB logs once daily at midnight.</p>
</li>
</ul>
<p>&quot;Add a CronJob to purge all MongoDB Logs once daily at midnight - we want to
start the day with clean logs!&quot;</p>
<p>I used Helm to create the Nginx-INgress-controller, and for MongoDB as well.</p>
<p>Does someone know which commands I should add to the CronJob?</p>
<p>Thanks in advance. :)</p>
<p>The code:</p>
<pre><code>apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: &quot;0 0 * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure
</code></pre>
<p>for MongoDB I used  I helm - artifact hub with the commands:</p>
<ul>
<li>helm repo add bitnami <a href="https://charts.bitnami.com/bitnami" rel="nofollow noreferrer">https://charts.bitnami.com/bitnami</a></li>
<li>helm install my-release bitnami/mongodb --values test-mongodb.yaml</li>
</ul>
<ul>
<li>link for the repo- <a href="https://artifacthub.io/packages/helm/bitnami/mongodb" rel="nofollow noreferrer">https://artifacthub.io/packages/helm/bitnami/mongodb</a>.</li>
</ul>
<p>The values I inserted into test-mongodb.yaml:</p>
<pre><code>architecture: replicaset
replicaCount: 3
persistence:
    storageClass: &quot;standard&quot;
auth:
  rootPassword: password
  password: password
  database: echo
  username: koren
</code></pre>

<p>If you want to manage the <strong>logs</strong> due to <strong>Kubernetes</strong> also store the <strong>stdin</strong> &amp; <strong>out</strong> logs. Kubernetes auto manages the logs rotation however if your <strong>application</strong> or <strong>mongoD</strong> internally storing the Logs in <strong>PVC</strong> or <strong>filesystem</strong> you might need to rotate it <strong>daily</strong>.</p>
<p>Instead of configuring the special job for deleting the job you can just config <strong>mongoD</strong> to rotate the logs config file</p>
<pre><code>/var/log/mongodb/*.log {
  daily
  missingok
  rotate 5
  compress
  dateext
  delaycompress
  copytruncate
  notifempty
}
</code></pre>
<p>Example : <a href="https://www.percona.com/blog/2018/09/27/automating-mongodb-log-rotation/" rel="nofollow noreferrer">https://www.percona.com/blog/2018/09/27/automating-mongodb-log-rotation/</a></p>
<p>However still if you wish to run the job for clearing the logs from <strong>PVC</strong> or <strong>filesystem</strong> you can create the <strong>side car</strong> container.</p>
<p><strong>Update</strong></p>
<p>i have not tested however you can create the simple code and run it as the Cronjob inside the Kubernetes, that <strong>node JS or any script</strong> will connect to <strong>MongoDB</strong> admin database and rotate the log simply.</p>
<pre><code>db.adminCommand( { logRotate : server } )
</code></pre>
<p>Connect to DB and rotate the log document :</p>
<p><a href="https://docs.mongodb.com/manual/tutorial/rotate-log-files/" rel="nofollow noreferrer">https://docs.mongodb.com/manual/tutorial/rotate-log-files/</a></p>
<p>I think it will be simple program will make a connection and run rotate query.</p>
