0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Get all containers log for one namespace

Posted at

Pods managed by deploy or daemonset don't have a fixed name, you must get the pod name and then get the log for the pods and containers.

Here is a simple script gets all pods in one namespace and get logs for all containers in that pod.


#!/bin/bash

ns=some-namespace
for pname in `kubectl -n $ns get pods | grep Running | awk '{ print $1}'`; do
  # pname=$(echo $pname | tr -d '"')
  pbody=$(kubectl -n $ns get pods $pname -o json)
  csize=$(echo $pbody | jq '.status.containerStatuses | length')
  echo "pod $pname has $csize containers"
  
  for i in $( seq 0 $(($csize - 1)) ); do
    cname=$(echo $pbody | jq .status.containerStatuses[$i].name | tr -d '"')
    echo "  get logs for $cname"
    kubectl -n $ns logs $pname -c $cname >"$pname-$cname".log
    echo "  get logs for previous $cname"
    kubectl -n $ns logs $pname -c $cname -p >"$pname-$cname-previous".log 2>&1
  done
done


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?