Home > front end >  Using kubectl to grep multiple pod/service logs for a specific phrase
Using kubectl to grep multiple pod/service logs for a specific phrase

Time:10-22

I have a bunch of services deployed to a Kubernetes cluster (various pods and services all load-balanced behind a gateway). I am making a REST call to one of them and getting unexpected errors, but the problem is I'm not actually sure which pod/service is actually throwing the error. I would like to check all the logs of every pod/service.

When I run kubectl get namespace I get:

NAME               STATUS     AGE
another-app        Active     22d
myapp              Active     22d
myapp-database     Active     22d
default            Active     22d
kube-public        Active     22d
kube-system        Active     22d
zanzabar           Active     22d

Is there a kubectl log command that can scan the entire cluster for logs and search them for a specific error message? For instance, say the error message I'm getting back from the REST (curl) is "Sorry mario your princess is in another castle". Is there any way I can use kubectl log to scan all pod/service logs for that phrase and display the results back to me? If not, then whats the best/easiest way using kubectl to find the pod/service with the error message (and hopefully, more details behind my error)?

CodePudding user response:

You can fetch the logs of a particular pod or container(use -c flag for container) and grep the error logs by pipelining the log command with the grep command.

For example if I want to get logs of a pod with name my-pod and want to grep "error exists" word then command goes like:

kubectl logs my-pod | grep “error exist” 

Refer this document for multiple ways of fetching logs and this similar question for more information on grep usage.

  • Related