Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/ .*//')

like image 541
van_folmert Avatar asked Jul 23 '19 09:07

van_folmert


People also ask

How do I view Kubernetes pod logs?

Kubectl Logs Command References With Examples. You can view the pods on your cluster using the kubectl get pods command. Add the --namespace <namespace name> flag if your pods are running outside of the default namespace. You can also use labels to filter the results as required by adding <my-label>=<my-value> .

How do you get all pod logs?

If you want to show logs of all pods, you can use -l and specify a lable, but at the same time -f won't be used. "but this will choose one pod of the deployment, not all pods" -> true and I've spent a lot of time debugging my app before realizing that not all logs were being displayed.

How do you get the full pod log in Kubernetes?

Procedure. If you run kubectl logs pod_name , a list of containers in the pod is displayed. You can use one of the container names to get the logs for that specific container.

Which command can be used to display the events for a pod?

To see events from all namespaces, you can use the --all-namespaces argument. In addition to kubectl describe pod , another way to get extra information about a pod (beyond what is provided by kubectl get pod ) is to pass the -o yaml output format flag to kubectl get pod .


2 Answers

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

like image 92
Suresh Vishnoi Avatar answered Sep 18 '22 00:09

Suresh Vishnoi


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" "-"
like image 27
P Ekambaram Avatar answered Sep 20 '22 00:09

P Ekambaram