简体   繁体   中英

Redirect logs of pods in my k8s to a file with pod name

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.

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

Courtesy to this answer

for i in $(kubectl get po -oname | awk -F'/' '{print $2}'); do kubectl logs $i > $i; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM