How to correctly give patterns for POD name in kubernetes?

4/30/2018

I'm using kubernetes for deploying applications, and

kubect logs

for logs viewing. Redeployments are very often, so it's bit uncomfortable - to copy pod name each time to paste in log command(because end of pod name dinamically changes each time), e.g. below are commands for same application, after 2 deployments

kubectl logs -n=testns --since=1m testapp-2818008534-jx2vv
kubectl logs -n=testns --since=1m testapp-2818008534-xls93

So, I want to use one command, which will correctly "pick up" needed POD name each time we'r running the "kubectl logs".

But smth like

$kubectl logs -n=testns --since=1h $(kubectl get pods --namespace=testns | grep testapp)

returns an error, that we are not "giving" pod name, BUT that pod exists for sure:

error: expected 'logs (POD | TYPE/NAME) [CONTAINER_NAME]'.
POD or TYPE/NAME is a required argument for the logs command

So, how to correctly set this command, to use it every time we want to look at logs, without need to re-copypaste full POD name?

-- Dmitry
kubectl
kubernetes

2 Answers

1/17/2019

Accepted answer should work in most cases but just in case try this alternate:

kubectl get po | grep testapp | tr -s ' ' | cut -d\  -f 1
-- AnilR
Source: StackOverflow

4/30/2018

When you do:

kubectl get pods --namespace=testns | grep testapp

You get something like:

testapp-54d99599bc-g2gs4 1/1 Running 0 56m

So this won't go well with logs command as it has some additional data which can not be understood by Kubectl. What you can do is:

kubectl get pods --namespace=testns | grep testapp | cut -d' ' -f1

Which will produce only name of the POD and then your log command should work.

-- Vishal Biyani
Source: StackOverflow