A CLI tool for monitoring Kubernetes resources and autoscaling
Project description
Kubernetes Resource Monitor CLI
A CLI tool for monitoring and managing Kubernetes resources. This tool provides real-time monitoring, auto-scaling recommendations, resource trend visualization, and alerting functionality.
Features
- Monitor real-time resource usage (CPU and Memory) of pods in a Kubernetes namespace.
- Provide auto-scaling recommendations based on custom policies.
- Visualize resource usage trends for a specific pod.
- Configure Horizontal Pod Autoscalers (HPA) based on resource usage.
- Set up alerts for high resource usage via email and Slack.
Prerequisites
- Python 3.7+
- Kubernetes cluster and
kubectlconfigured - Kubernetes Metrics Server installed on your cluster
- Basic knowledge of Kubernetes pods and deployments
Setup Instructions
2. Create a Virtual Environment
python3 -m venv .venv
source .venv/bin/activate # On Windows use: .venv\Scripts\activate
3. Install Dependencies
pip install -r requirements.txt
4. Set Up Kubernetes Configuration
Ensure your Kubernetes cluster is configured and accessible. You should be able to run kubectl get pods from your terminal.
5. Initialize the Database
The tool uses SQLite for storing historical resource usage data. You can initialize the database by running the monitoring command (explained below).
Commands and Usage
1. Monitor Resource Usage
Monitor real-time CPU and memory usage for pods in a specific namespace.
python3 -m k8s_monitor.cli monitor --namespace <namespace-name>
Options:
--namespace: Kubernetes namespace to monitor (default: default). --use-mock: Use mock data instead of real Kubernetes cluster data. Example:
python3 -m k8s_monitor.cli monitor --namespace default
2. Auto-Scale Pods
Provide auto-scaling recommendations based on custom policies for a Kubernetes namespace.
python3 -m k8s_monitor.cli auto-scale --namespace <namespace-name>
Options:
--namespace: Kubernetes namespace to monitor (default: default). --use-mock: Use mock data instead of real Kubernetes cluster data. Example:
python3 -m k8s_monitor.cli auto-scale --namespace default
3. Visualize Resource Trends
Visualize CPU and memory usage trends for a specific pod over a specified time period.
python3 -m k8s_monitor.cli visualize-trends --namespace <namespace-name> --pod-name <pod-name> --duration <time-in-minutes>
Options:
--namespace: Kubernetes namespace to monitor (default: default). --pod-name: The name of the pod to visualize trends for. --duration: Time duration (in minutes) for historical data (default: 60 minutes). Example:
python3 -m k8s_monitor.cli visualize-trends --namespace default --pod-name nginx-pod --duration 60
4. Configure Alerts
Send email alerts independently of monitoring. You must configure email settings before running this command.
python3 -m k8s_monitor.cli email-alert --email-host <host> --email-port <port> --sender-email <sender-email> --sender-password <password> --recipient-email <recipient-email> --subject <subject> --message <message>
Options:
--email-host: SMTP host for sending alerts. --email-port: SMTP port for sending alerts. --sender-email: Sender email address. --sender-password: Sender email password. --recipient-email: Recipient email address. --subject: Subject of the email alert. --message: Message content for the email alert.
Example:
python3 -m k8s_monitor.cli email-alert --email-host smtp.example.com --email-port 587 --sender-email sender@example.com --sender-password password --recipient-email recipient@example.com --subject "Alert: High CPU Usage" --message "The CPU usage for pod nginx-pod is above the threshold."
5. Set Configuration
Configure the monitoring tool to send alerts via Slack or email.
python3 -m k8s_monitor.cli set-config --slack-webhook-url <url> --email-host <host> --email-port <port> --sender-email <email> --sender-password <password> --recipient-email <email>
Options:
--slack-webhook-url: Slack Webhook URL for sending alerts. --email-host: SMTP host for sending email alerts. --email-port: SMTP port for sending email alerts. --sender-email: Sender email address. --sender-password: Sender email password. --recipient-email: Recipient email address.
Example:
python3 -m k8s_monitor.cli set-config --slack-webhook-url https://hooks.slack.com/services/ABC123 --email-host smtp.example.com --email-port 587 --sender-email sender@example.com --sender-password password --recipient-email recipient@example.com
6. View and Reset Configuration
You can view or reset the current configuration using the following commands:
View current configuration:
python3 -m k8s_monitor.cli view-config
Reset the configuration:
python3 -m k8s_monitor.cli reset-config
7. Set Auto-Scaling Policy
Set or update the auto-scaling policy for your Kubernetes cluster. This will be used to make scaling recommendations.
python3 -m k8s_monitor.cli set-autoscaling-policy --cpu-threshold <cpu-percentage> --memory-threshold <memory-percentage> --max-replicas-change <replicas> --scaling-strategy <static|dynamic>
Options:
--cpu-threshold: CPU usage threshold for auto-scaling (in percentage). --memory-threshold: Memory usage threshold for auto-scaling (in percentage). --max-replicas-change: Maximum number of replicas to scale up or down. --scaling-strategy: Scaling strategy (static or dynamic).
Example:
python3 -m k8s_monitor.cli set-autoscaling-policy --cpu-threshold 80 --memory-threshold 75 --max-replicas-change 5 --scaling-strategy dynamic
8. Set Namespaces
Set or update the namespaces that should be monitored.
python3 -m k8s_monitor.cli set-namespaces --namespaces <namespace1> <namespace2>
Options:
--namespaces: List of namespaces to monitor.
Example:
python3 -m k8s_monitor.cli set-namespaces --namespaces default production
9. View and Reset Namespaces
You can view or reset the monitored namespaces:
View current namespaces:
python3 -m k8s_monitor.cli view-namespaces
Reset namespaces to default:
python3 -m k8s_monitor.cli reset-namespaces
Contribution
Feel free to submit issues and pull requests to enhance the tool further.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file k8s_resource_monitor-0.1.0.tar.gz.
File metadata
- Download URL: k8s_resource_monitor-0.1.0.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91b7c9f1191820daba6e93abbfd2787166777939daf76946555312adec979cb9
|
|
| MD5 |
f8367da0e7b26dccedef34e848e357c8
|
|
| BLAKE2b-256 |
1f94de4e79eaa59843fca4f45557b0d18fcda3f20d0b55a2a863dae0209f091d
|
File details
Details for the file k8s_resource_monitor-0.1.0-py3-none-any.whl.
File metadata
- Download URL: k8s_resource_monitor-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0846665d112f90b8b079592bf59c65936cf360d2321c67f51fdad351935d7b6c
|
|
| MD5 |
f1cd8971e82650f0f2b8c809e8ebabb8
|
|
| BLAKE2b-256 |
d3432afd54bf04e0e2a51d724d804fbffe133c2b3c2c2f47c9ebc76a4ed46012
|