Certified Kubernetes Administrator Exam Series (Part-3): Logging & Monitoring
In the previous blog of this 10-part series, we discussed Scheduling. This blog introduces the various monitoring and logging options available for Kubernetes clusters.
Here are the eight other blogs in the series:
- Certified Kubernetes Administrator Exam Series (Part-1): Core Concepts
- Certified Kubernetes Administrator Exam Series (Part-4): Application Lifecycle Management
- Certified Kubernetes Administrator Exam Series (Part-5): Cluster Maintenance
- Certified Kubernetes Administrator Exam Series (Part-6): Security
- Certified Kubernetes Administrator Exam Series (Part-7): Storage
- Certified Kubernetes Administrator Exam Series (Part-8): Networking
- Certified Kubernetes Administrator Exam Series (Part-9): Troubleshooting
- Certified Kubernetes Administrator Exam Series (Part-10): Practice Topics
Introduction
Logging and monitoring Kubernetes cluster components and containerized applications is crucial to debugging and lifecycle management.
Where a basic difference between the two is:
- Monitoring involves the consistent examination of components to gain an understanding of their health status.
- Logging involves the recording of cluster events so that problems in application runtime can easily be traced back to the source.
Monitor Cluster Components
When monitoring the elements of a Kubernetes cluster, we look for two types of metrics:
- POD-level Metrics – include performance data such as CPU and memory requirements.
- Node-level Metrics – include the number of healthy nodes, CPU usage, Memory Requirements, and networking.
A monitoring solution should be able to retrieve these metrics, store data, and offer analytics for optimized clusters. Heapster was an original monitoring feature created for Kubernetes clusters but was later deprecated and replaced by Metrics Server– a more trimmed-down version. The Metrics Server essentially receives aggregates and stores performance metrics in memory. Other advanced solutions can also store historical cluster performance data on disk.
The Kubelet service receives instructions from the Kube-API Server on how to run a Pod. It also contains an element known as the container advisor (cAdvisor), which receives Pod performance metrics and exposes them to the Metrics Server through Kube-API Server.
For a minikube
cluster, the server is enabled using the command:
minikube addons enable metrics-server
When the server has been enabled, it returns an output similar to this:
The 'metrics-server' addon is enabled
For other types of Kubernetes clusters, the metrics server is installed by first cloning the Git repository. You can do that by running the command below:
git clone https://github.com/kodekloudhub/kubernetes-metrics-server.git
Navigate to the metric server directory using the command:
cd kubernetes-metrics-server
Enable the metric server by applying this command:
kubectl create -f .
With the Metrics Server enabled, node-level metrics can be accessed with the command:
kubectl top node
To access the POD-level metrics, the following command is used:
kubectl top pod
Managing Application Logs
In Docker, we run an event simulator container to check application event logs. The event simulator can be downloaded from KodeKloud’s Git Repository using the following:
git clone https://github.com/kodekloudhub/event-simulator.git
Navigate to the event-simulator directory using the command:
cd event-simulator
Build the event simulator using the command:
docker build -t event-simulator
The event simulator is then initiated by running the command:
docker run event-simulator
This container generates and displays random events, emulating a web server. The application then streams these events to the standard output. Running this container in detached mode records the metrics without displaying them:
docker run -d event-simulator
For Kubernetes, event logs can be viewed using a Pod definition file that uses the event-simulator image. The specifications for the Pods YAML file are:
apiVersion: v1
kind: Pod
metadata:
name: event-simulator
spec:
containers:
- name: event-simulator
image: kodekloud/event-simulator
The simulator is then created by running the POD:
kubectl create -f event-simulator-pod.yaml
We can now stream the metrics live by checking these PODs logs:
kubectl logs -f event-simulator
This concludes the Logging & Monitoring section of the CKA certification exam.
You can now proceed to the next part of this series: Certified Kubernetes Administrator Exam Series (Part-4): Application Lifecycle Management.
Here is the previous part of the series: Certified Kubernetes Administrator Exam Series (Part-2): Scheduling.
Research Questions
Here is a quick quiz to help you assess your knowledge. Leave your answers in the comments below and tag us back.
Quick Tip – Questions below may include a mix of DOMC and MCQ types.
1. Which command can be used to identify the node that consumes the most CPU resource?
[A] kubectl get nodes
[B] kubectl list nodes
[C] kubectl htop nodes
[D] kubectl top nodes
2. Which command can be used to identify the Pod that consumes the most memory?
[A] kubectl get pods
[B] kubectl list pods
[C] kubectl htop pods
[D] kubectl top pods
3. What is the command to get logs of the Pod webapp
and store these logs under /root/webapp.log
?
4. Metrics Server discovers all nodes on the cluster and queries each node’s kubelet for CPU and memory usage.
[A] True
[B] False
Conclusion
Microservices architecture introduces a layer of complexity in Kubernetes applications, necessitating continuous monitoring of cluster performance. This is because a single application may contain a large number of services on different Pods interfaced with each other, introducing multiple points of failure. Monitoring and logging of events also help in anticipating problems before they completely wreck the application in production.
This part of the course explored basic tools needed for logging and monitoring Kubernetes clusters and applications. KodeKloud’s hands-on course also includes in-depth practicals with advanced monitoring tools so that learners can familiarize themselves with the practicalities of logging and monitoring in the production environment.
Exam Preparation Course
Our CKA Exam Preparation course explains all the Kubernetes concepts included in the certification’s curriculum. After each topic, you get interactive quizzes to help you internalize the concepts learned. At the end of the course, we have mock exams that will help familiarize you with the exam format, time management, and question types.
Explore our CKA exam preparation course curriculum.