Logging and Monitoring in Kubernetes
Metrics Server
You can have one Metrics Server per Kubernetes cluster.
The Metrics Server retrieves metrics from each of the Kubernetes nodes and pods, aggregates them, and stores them in memory.
Metrics Server is only an in-memory monitoring solution and does not store the metrics on the disk. Due to this, you cannot see historical performance data.
How does it work?
Kubernetes runs an agent on each node known as kubelet, which is responsible for receiving instructions from Kube-APIServer and running pods on the nodes.
The kubelet also contains a sub-component known as cAdvisor or Container Advisor.
cAdvisor is responsible for retrieving performance metrics from pods and exposing them through kubelet API to make the metrics available for the Metrics Server.
kubectl top nodes/pods
- to view the metrics of nodes/pods.
Managing Application Logs
We can view logs in Kubernetes using
kubectl logs -f <pod_name>
. Here-f
option is to view the live logs of a container.But if we have multiple containers inside a pod (multi-container pod), then at that time we have to specify the container name as well in the logs command to view the logs specific to those containers. If not done so, Kubernetes will throw an error.
kubectl logs -f <pod_name> <container_name>