Kubernetes Pod - Application log

10/24/2018

I have a requirement to extract my application log which is available under /var/etc/logs directory. How do I extract this from K8 pods?

Tried kubectl exec but didnt help.

-- Anup
kubernetes
logging

3 Answers

10/24/2018

You might need a log collector like fluentd.

k8s itself won't persist log for you.

If you are using GKE, stackdriver might be an approach (heard, never tried).

-- wilbeibi
Source: StackOverflow

10/24/2018

The purest and simplest way to get the application log from your pod is using kubectl cp

kubectl cp <pod-name>:/var/etc/logs logs

This will copy the pod logs to your local machine.

However..

That's probably not a good long term solution. I'd recommend:

  • Outputting the logs to stdout inside the container, which would mean kubectl logs would become availabla ro you
  • Using a fluentd as a sidecar container to manage the logs

More information can be found here

-- jaxxstorm
Source: StackOverflow

10/24/2018

Besides the Fluentd sidecar option described in the other answers, you can also add simple tail -n+1 -f /var/etc/logs sidecar to output your logs to stdout:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: container
    image: yourimage
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: logs
    image: busybox
    args: [/bin/sh, -c, 'tail -n+1 -f /var/etc/logs']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}
-- Rico
Source: StackOverflow