Nugget: Check Kubernetes Pod Logs Without kubectl
kubernetes debugging tipsEver needed to check pod logs but couldn’t use kubectl?
Here’s a quick way using the Kubernetes API directly:
curl -k \
-H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
https://kubernetes.default.svc/api/v1/namespaces/default/pods/<pod-name>/log
Breaking down this command:
- The name
kubernetes.default.svcis the DNS name that points to the API server in the Kubernetes cluster. It is a part of the built-in service discovery system in the cluster. - The path
/api/v1/namespaces/default/pods/<pod-name>/logfollows the RESTful nature of the way the API server is exposed:
- the
/api/v1specifies the API version. - the
/namespaces/<namespace>indicates the namespace we are looking in. - the
/pods/<pod-name>/logrequests the logs for the specific Pod.
This method is useful in scenarios where:
- the
kubectlbinary is not available in the environment. - you have automation scripts that need to access logs.
- a case where you are troubleshooting kubectl itself.
This essentially expands to how you can access a Kubernetes cluster without having the kubectl tool.