How do I get logs from all pods of a Kubernetes replication controller?

前端 未结 14 1354
不思量自难忘°
不思量自难忘° 2021-01-29 23:14

Running kubectl logs shows me the stderr/stdout of one Kubernetes container.

How can I get the aggregated stderr/stdout of a set of pods, preferably those

14条回答
  •  感情败类
    2021-01-29 23:42

    If the pods are named meaningfully one could use simple Plain Old Bash:

    keyword=nodejs
    command="cat <("
    for line in $(kubectl get pods | \
      grep $keyword | grep Running | awk '{print $1}'); do 
        command="$command (kubectl logs --tail=2 -f $line &) && "
      done
    command="$command echo)"
    eval $command
    

    Explanation: Loop through running pods with name containing "nodejs". Tail the log for each of them in parallel (single ampersand runs in background) ensuring that if any of the pods fail the whole command exits (double ampersand). Cat the streams from each of the tail commands into a unique stream. Eval is needed to run this dynamically built command.

提交回复
热议问题