Short hand to select the first pod using kubectl

Viewed 377

I'm on a Windows 10 and using WSL.

I have 8 pods inside my namespace:

NAME                   READY   STATUS    RESTARTS   AGE
app-85b6fd4dc9-4chnq   1/1     Running   0          17m
app-85b6fd4dc9-9c5dc   1/1     Running   0          19m
app-85b6fd4dc9-cth6d   1/1     Running   0          19m
app-85b6fd4dc9-m8pc8   1/1     Running   0          19m
app-85b6fd4dc9-mrsnv   1/1     Running   0          18m
app-85b6fd4dc9-qtdtl   1/1     Running   0          17m
app-85b6fd4dc9-xzmdx   1/1     Running   0          17m
app-85b6fd4dc9-zbft7   1/1     Running   0          19m

And I really need to see the logs with haste. My current pattern is:

kubectl get pods -n my_namespace
[copy NAME of the pod]
kubectl logs --follow pod_name -n my_namespace
# live tail logs here

I want to skip the display of all pods, instead, I want to go straight to the first one available or first one on the list, whichevers applicable. Thanks for answering my noob question

2 Answers

You can try below in PowerShell, tested in PowerShell window 10 bases on output provided in the question.


 $var = (kubectl get pods -n my_namespace  | Select -First 2 | Select -Last 1 | %{ (-split $_)[0]) ; kubectl logs --follow $var -n my_namespace

I did not verify it against kubectl but it should work or You can check further details at microsoft.powershell.utility

Equelevent command using AWK

export pod=$(kubectl get pods -n namespace | awk 'FNR==2{print $1}' ) | kubectl logs -f $pod -n mypod
Related