nice guy
nice guy

Reputation: 151

Unable to get logs from Kubernetes POD

I am new to Kubernetes and currently working on logging. What I have tried so far is using minikube, created a mount-point and by using PV/PVC I am mounting it in POD.

The problem I am having is when I do kubectl logs test-app I did not get any logs. But when I do:

kubectl exec -it test-app -- bash

[root@test-app /]# tail -f /var/log/test.log 
Mon Jul 4 06:36:31 UTC 2022
Mon Jul 4 06:36:36 UTC 2022
Mon Jul 4 06:36:41 UTC 2022
Mon Jul 4 06:36:46 UTC 2022
Mon Jul 4 06:36:51 UTC 2022
Mon Jul 4 06:36:56 UTC 2022

I get the logs. My question is how can I get these logs with 'kubectl logs test-app' ?

pv-pvc.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

test-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-app
spec:
  volumes:
  - name: ps
    persistentVolumeClaim:
      claimName: task-pv-claim
  containers:
  - name: app
    image: centos
    command: ["/bin/sh"]
    args: ["-c", "while true; do echo $(date -u) >> /var/log/test.log; sleep 5; done"]
    volumeMounts:
    - mountPath: "/var/log"
      name: ps

Upvotes: 0

Views: 2277

Answers (1)

Fritz Duchardt
Fritz Duchardt

Reputation: 11950

Kubernetes only collects container logs written to stdout and this is what you get from kubectl logs. For more information, please refer to the documentation.

If you need to collect logs that are written to a file, you will have to use a more sophisticated log collector like fluentd or fluentbit.

Upvotes: 3

Related Questions