This project aims to create a Kubernetes cronjob that pulls node metrics such as CPU, memory, and disk usage from Kubernetes nodes and stores them in individual files.
- To collect and expose metrics, we utilize Node Exporter, a Prometheus exporter for hardware and OS metrics.
- The Kubernetes cronjob designed to run at regular intervals, creating a new file with a timestamp each time it executes.
- The solution is implemented using Python scripting for the cron job, Docker for containerization, and Kubernetes YAML deployment.
- Persistent Volume Claim (PVC) for storing the files generated by cronjob ensuring the data is retained even on pod restarts.
The deployment can be performed on a local Kubernetes setup like Minikube or Kind, or on a cloud platform’s Kubernetes flavor (e.g., EKS). Here, I have used EKS (Elastic kubernetes service). This document provides a step-by-step guide on the design, deployment, and usage of the solution, along with the necessary code and configuration files.
- Metric Server:
- Purpose: Collects resource metrics for pods and nodes.
- EBS CSI Driver Addon:
- Purpose: Manages Amazon EBS volumes for persistent storage.
- Bastion Server:
- Purpose: Acts as a secure access point to the Kubernetes cluster.
- Python:
- Purpose: Used for scripting and automation.
Defines the steps to build a Docker image. Images are pushed to Docker Hub.
- python_script.py: Responsible for collecting node metrics, filtering, and storing them in files.
- requirements.txt: Contains dependencies required by the Python script.
- cron.yaml: Defines a Kubernetes cron job scheduled to run at regular intervals. It executes the Python script to collect node metrics and store them in files.
- nodemetrics.yaml: YAML file for pod creation to store log files generated by the Kubernetes cron job.
- pvc.yaml: YAML file defining a Persistent Volume Claim (PVC) used for storing metric data persistently.
- configmap.yaml: Configures Node Exporter.
- daemonset.yaml: Deploys Node Exporter as a DaemonSet on all nodes in the Kubernetes cluster.
- load_balancer.yaml: Contains resources for load balancing or exposing Node Exporter metrics externally.
-
Build and Push Image to Docker Hub:
- Use the provided Dockerfile to build a Docker image containing the necessary components, including the Python script and any dependencies.
- Push the Docker image to Docker Hub for deployment in Kubernetes.
-
Apply Manifest Files:
- Use the
kubectl applycommand to apply the manifest files located in the project directory. - Apply the
PVC.yamlfile to claim storage for metric data persistence. - Apply the
cronjob.yamlfile to schedule the execution of the Python script for collecting node metrics. - Apply any additional manifest files, such as those for Node Exporter or other related resources.
- Use the
-
Sanity Checking:
- After applying the manifest files, use
kubectl get podsto check the status of pods and ensure they are all in the Running state. - Inspect pod logs using
kubectl describe podto debug any issues if pods are not running correctly.
- After applying the manifest files, use
-
Debugging and Logging:
- If any pods encounter issues, inspect the logs of the relevant pods using
kubectl logs. - Resolve any errors reported in the logs to identify and fix potential deployment issues.
- If any pods encounter issues, inspect the logs of the relevant pods using
-
Access Node Exporter URL:
-
Once all pods are running successfully, access the URL of Node Exporter to verify that it is exposing metrics correctly.
-
Open a web browser and navigate to the Node Exporter URL (typically in the format
http://<node-exporter-ip>:<port>/metrics). -
Verify that metrics are being exposed and can be accessed from the Node Exporter endpoint.
-
-
Validation and Monitoring:
