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