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.
Advertisement
Answer
kubectl get pods | awk '{print $1}' | tail -n +2 | xargs -I{} sh -c 'kubectl logs $1 > $1' -- {}
Courtesy to this answer