Search code examples
kuberneteskubectl

How to correctly give patterns for POD name in kubernetes?


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?


Solution

  • 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.