How to get pod logs from Kubernetes environment?
Published:
This article outlines the steps to retrieve logs from pods in a Kubernetes namespace.
Prerequisites
Ensure kubectl is installed and configured to access your cluster.
Get the pod logs using the following command
kubectl get pods -n <namespace>
Example: kubectl get pods -n bold-services
Use the following command to save logs for each pod into separate .log files
For Powershell (Windows)
kubectl get pods -n <namespace> --no-headers -o custom-columns=":metadata.name" | % { kubectl logs $_ -n {namespace} > "$_.log" }
Example : kubectl get pods -n bold-services --no-headers -o custom-columns=“:metadata.name” | % { kubectl logs $_ -n bold-services > “$_.log” }
Output reference when you run the command to get the pod logs:
For Linux
kubectl get pods -n <namespace> --no-headers -o custom-columns=":metadata.name" | while read pod; do kubectl logs "$pod" -n {namespace} > "$pod.log"; done
Example: kubectl get pods -n bold-services --no-headers -o custom-columns=“:metadata.name” | while read pod; do kubectl logs “$pod” -n bold-services > “$pod.log”; done
Each pod’s logs will be saved as a separate file named after the pod, as in the image above.