1

I was trying to redirect logs of pods in a k8s into a file of their name.

kubectl get pods | awk '{print $1}' | tail -2 | xargs -I {} kubectl logs {} > {}

This is the result.

demo@demo1:~/log$ ls {}

What I need is, if this is the pod details

demo@demo1:~/log$ kubectl get pods NAME READY STATUS RESTARTS AGE pod1 1/1 Running 0 3d23h pod2 1/1 Running 0 3d23h The expected result is

demo@demo1:~/log$ ls pod1 pod2

files pod1 & pod2 will have logs of respective pods.

Yasir
  • 9
  • 5

2 Answers2

2
kubectl get pods | awk '{print $1}' | tail -n +2 | xargs -I{} sh -c 'kubectl logs $1 > $1' -- {}

Courtesy to this answer

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
0
for i in $(kubectl get po -oname | awk -F'/' '{print $2}'); do kubectl logs $i > $i; done
suren
  • 7,817
  • 1
  • 30
  • 51