How to make Kubernetes manage my pods log files?

6/26/2019

Currently I have a Kubernetes pod that is running a cron job. The cron job writes to a log file at /var/log/cronlog. However, when I type kubectl log my-pod-name, the contents of /var/log/cronlog is not included.

How do I make it so that the contents of /var/log/cronlog is included with the output of kubectl log my-pod-name?

-- AlanSTACK
docker
kubernetes

3 Answers

6/27/2019

I think you should write to stdout and stderr not to a file to be able to use kubectl log

-- M.Elkady
Source: StackOverflow

6/27/2019

you can append the container startup script to include the logs

something like run.sh && tail -f /var/log/cronlog

-- P Ekambaram
Source: StackOverflow

6/27/2019

First why you don't create Jobs using a CronJob Controller?

I think you should take advantage of using Kubernetes and make use of Jobs if they are supposed to run instead of plain cronjob. This will consume less resources and you will quickly check if cronjob failed.

Also as it was already mentioned in previous answers, you need to send messages/logs to stdout

Here is an example of a CronJob that does send date and prints Hello from the Kubernetes cluster to stdout, and logs can be accessed by kubectl logs job/<JOB_NAME>

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
  namespace: default
spec:
  schedule: "* * * * *" # run every minute
  startingDeadlineSeconds: 10 # if a job hasn't starting in this many seconds, skip
  concurrencyPolicy: Forbid # either allow|forbid|replace
  successfulJobsHistoryLimit: 3 # how many completed jobs should be
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: hello
              image: busybox
              args:
                - /bin/sh
                - -c
                - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

I hope this provide a bit of overview for You.

-- Crou
Source: StackOverflow