How to get logs from selected container in a pod ?

Sun 07 March 2021 by admin

I faced with the problem where I have to react on specific log entry from an third party application. It wasn't possible to change behaviour of this application, but fortunately it generates specific log when something bad happened. So I decide to use liveness probe with some exec to force the application to restart on this specific log entry, but how can I get logs without using ie. kubectl. I decide to use k8s rest api for reading container logs, before I did that I ensured that I had enough permissions:

$ kubectl create job test --image=bitnami/kubectl -o yaml --dry-run -- kubectl auth can-i get pods/log \
    | kubectl patch -f -  -p='{"spec":{"template": { "spec": { "serviceAccount": "<svc_acct>"}}}}' --dry-run -o yaml \
    | kubectl apply -f
$ kubectl logs test-zkmmd
$ kubectl delete job test

Then I prepared http request:

curl --connect-timeout 5 -m 10 -s -k -H "Authorization: Bearer $(cat /run/secrets/" \

and put it into liveness probe exec as bash script with some addons. So when liveness probe see specific log it return exit code 1 and it's enough to restart this container.