Viewing Container Logs in Kubernetes

I’m currently working as a developer but also manages the infrastructure of our project which is hosted in Google Cloud Platform. GCP has web console for viewing logs for each of our services and using the web is a bit tedious to me. Using the command line works like charm.

First, display all pods of your services and the specific namespace. In my case, I wanted to show all pods for all my services in staging and my namespace is jx-staging.

To show all namespaces, just run:

kubectl get ns

To show all pods in jx-staging namespace, run:

jx get pods --namespace jx-staging

It should show you something like below:

NAME                                          READY   STATUS    RESTARTS   AGE
jx-project-manager-7fb98d978b-88dg4           1/1     Running   0          3h20m
jx-project-manager-7fb98d978b-h4mqf           1/1     Running   0          3h20m
jx-project-manager-7fb98d978b-j8zbn           1/1     Running   0          3h19m
jx-pm-mongodb-ha-0                            1/1     Running   0          33h
jx-pm-mongodb-ha-1                            1/1     Running   0          4d14h
jx-pm-mongodb-ha-2                            1/1     Running   0          159m
jx-authservice-5b94558886-bjfhp               1/1     Running   0          2d6h
jx-authservice-5b94558886-fcd9j               1/1     Running   0          2d6h
jx-authservice-5b94558886-fdzdm               1/1     Running   0          2d6h

And to view the logs of a specific pod which contains the service, just run something like:

kubectl logs jx-project-manager-7fb98d978b-88dg4 --namespace jx-staging

You should get the logs of the specified service/application.

That’s it!

Leave a reply

Your email address will not be published. Required fields are marked *