Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get kubectl logs to output pod name alongside logs?

I'm using the handy kubectl logs -l label=value command to get log from all my pods matching a label. I want to see which pod outputted what log, but only the log text is displayed. Is there a way to control the log format, or a command argument which will let me do this?

like image 321
Dhiraj Gupta Avatar asked Nov 23 '18 06:11

Dhiraj Gupta


People also ask

How do you get the pod logs with kubectl?

These logs can be accessed by adding the -p ( --previous ) flag. Kubectl will then surface the entirety of the stored log for the Pod, including lines that were emitted by containers that have since been terminated.

How do I show pod logs?

Checking the logs of a running pod All that you need to do to do that is to run the following command: kubectl logs nginx-7d8b49557c-c2lx9.

How do you set the container name in kubectl logs?

Use the --container ( -c ) flag to specify the target container with kubectl logs command. However, in the case of the main container and a sidecar container configuration, the logs you probably want to see in most cases are the main container's. It is troublesome to have to specify the main container every time.


2 Answers

kubectl now has a --prefix option that allows you to prefix the pod name before the log message.

like image 160
Erik L Avatar answered Sep 30 '22 23:09

Erik L


Use the awesome kubetail script

like image 22
Nima Hashemi Avatar answered Sep 30 '22 23:09

Nima Hashemi