kubectl: how to display pod logs without specyfing the pod name explicitly?
Asked Answered
I

3

10

My pods have a dynamically generated ID appended to their names like i.e. my-app-name-7b587cd75b-dscsr which is different on every deployment (next time it could be my-app-name-xcgv83bfsd-4kjsf).

This makes using some commands really cumbersome, because every time I need to see the logs I have to list all pods first and copy-paste the changed name to the logs command: kubectl -n [namespace] logs my-app-name-7b587cd75b-dscsr.

Is there a way I can skip using the pod name or part of the name and do something like kubectl -n [namespace] logs my-pod-name-~ or kubectl -n [namespace] logs service/my-pod-name like in port-forward command?

I tried to inject grep inside the logs command to obtain the pod name and run logs in a single command, but Cmder on Windows, as great as it is, doesn't seem to support $(): kubectl -n [namespace] logs $(kubectl -n my-app-name get pod | grep my-app-name | sed 's/ .*//')

Inky answered 23/7, 2019 at 9:57 Comment(3)
I found kubetail handy when I last looked at this, but it's a bash thing, and it looks like you're Windows. There are several bash shells that run on Windows/WSL. YMMV.Amitie
Hi, you can just do with kubectl logs -f deploy/DEPLOYMENT_NAME to get the logs of podsUtimer
You can also use Stern - where you simply specify some characters from the pod name and it will give you logs for all podsAbsorber
U
15

Rather than using POD/$POD_NAME, you can use Deployment/$DEPLOYMENT_NAME to fetch the logs of pods

kubectl logs deployment/$DEPLOY_NAME


  # Return snapshot logs from container nginx-1 of a deployment named nginx
  kubectl logs deployment/nginx -c nginx-1

kubectl logs --help will provide more info

Utimer answered 23/7, 2019 at 10:20 Comment(3)
Doesn't work. λ kubectl logs deployment/stg-01-api returns Error from server (NotFound): deployments.extensions "stg-01-api" not foundInky
but it works with namespace: kubectl -n [namespace] logs deployment/stg-01-apiInky
Deployment is namespaced resources, you need to specify Specific namespace.Utimer
J
3

add a label to the deployment and use the label selector to lookup the logs from the matching pod.

Refer the below instructions

master $ kubectl run webapp --image=nginx --port=80 --labels="app=web"
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/webapp created
master $

master $ kubectl get deploy
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
webapp   1/1     1            1           2m27s
master $

master $ kubectl get po -owide
NAME                      READY   STATUS    RESTARTS   AGE   IP          NODE     NOMINATED NODE   READINESS GATES
webapp-647c6cd6f4-pxr4g   1/1     Running   0          20s   10.44.0.1   node01   <none>           <none>
master $

master $ curl 10.44.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
master $


master $ kubectl logs -l app=web
10.32.0.1 - - [23/Jul/2019:10:07:39 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.47.0" "-"
Jetport answered 23/7, 2019 at 10:9 Comment(2)
What's curl got to do with logging?Amitie
What if I don't have control over how apps are deployed?Inky
U
1

The following command will fetch logs for the first pod that contains the specified substring (e.g etcd) in its name. It is also namespace agnostic.

pod=etcd && kubectl logs `kubectl get pods --all-namespaces | grep $pod | head -1 | awk '{print $2}'` -n `kubectl get pods --all-namespaces | grep $pod | head -1 | awk '{print $1}'`

Works by first retrieving all pods that contain the substring, extracting full pod name and namespace, and calling kubectl logs with full details.

Umbilicate answered 30/11, 2021 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.