How to see some logs on kubernetes - A simple introduction
posted at 08-04-2021
I thought that it would be a nice and quick way to introduce kubernetes (k8s), I won't go on the details about how kubernetes works or the theory of it, it's just some commands for you to start interacting with the cluster that your company might have.
TL;DR for logs - Depending on the resource that you want to see the logs just do a
kubectl logs -f <resource> --namespace <namespace-name>
Well, first of all we gonna need install a tool that you gonna use to interact with the kubernetes cluster:
For Mac is really simple:
brew install kubectl kubectl version --client
Now, if you're using other kind of OS, take a look here: https://kubernetes.io/docs/tasks/tools/
Listing the contexts that you have:
kubectl config get-contexts
If you don't have any context, perhaps this can help https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
There you gonna be able to see the contexts that you have access to, so, if you're using Minikube for example, you gonna see it there.
Contexts are the way of saying which cluster you gonna interact with. You can also use one of those commands below to interact with k8s.
kubectl config current-context # display the current-context kubectl config use-context my-cluster-name # set the default context to my-cluster-name
Inside a cluster you can split some applications in namespaces (I might be wrong about this concept).
To list the namespaces, if you don't it already, and see what namespaces you have inside a context you can do
kubectl get namespaces, with it you can start exploring your application, something like take a look on pods, deployments, cronjobs and so on.
When you already know the namespace you can do a quick look on everything that you have inside that namespace by doing
kubectl get all -n chosen-namespace.
This will list everything that you have inside this namespace, so you gonna be able to get a pod, for example, and take a look on the logs of it like this:
kubectl logs -f my-pod --namespace my-namespace
Take a look on this cheatsheet for more commands that you might want to use.