Kubernetes Cost Explorer built successfully - Red Hat themed UI, real metrics support, demo fallback, cost model configurable via YAML
This is a Kubernetes Cost Explorer that estimates per namespace and per pod cost using CPU and memory usage, and a simple cost model. The application connects to a Kubernetes cluster to fetch real metrics, applies a configurable cost model, and displays the results in a Red Hat themed dashboard.
- Real-time Kubernetes metrics fetching (CPU and memory usage)
- Configurable cost model via YAML
- Red Hat themed UI with PatternFly styling
- Namespace and pod-level cost breakdown
- Demo mode when no cluster is available
- Summary cards showing total costs and expensive namespaces
- Python 3.11+
- Kubernetes cluster (optional, demo mode available)
- kubectl configured (optional, for real cluster access)
- Overview
- Features
- Architecture
- Quick Start
- Configuration
- Demo Mode
- Development
- Testing
- Deployment
- API Documentation
- Contributing
- License
CostKube is a production-ready Kubernetes cost analytics platform designed for Red Hat SWE engineers and DevOps teams. It provides real-time visibility into Kubernetes resource consumption and associated costs across namespaces and pods.
- 💡 Real-Time Insights: Live metrics from Kubernetes clusters with automatic refresh
- 🎨 Enterprise UI: Built with Red Hat's design system and PatternFly components
- 📊 Interactive Visualizations: Beautiful charts powered by Chart.js
- 🔄 Demo Mode: Automatically falls back to demo data when no cluster is available
- ⚙️ Highly Configurable: YAML-based cost model configuration
- 🌓 Dark/Light Theme: Professional theming with smooth transitions
- 🎯 Zero Configuration: Works out of the box with sensible defaults
- Build the image:
docker build -t kube-cost-explorer .- Run the container:
docker run -p 8000:8000 kube-cost-explorer- Namespace-level cost breakdown with hourly and monthly projections
- Pod-level granularity for detailed resource attribution
- Configurable cost model for CPU and memory pricing
- Real-time metrics via Kubernetes Metrics Server API
- Red Hat Sovereign Design System with PatternFly integration
- Interactive KPI cards with tooltips and contextual help
- Live charts and visualizations (cost distribution, resource allocation, trends)
- Responsive design optimized for desktop and mobile
- Dark/Light theme with persistent preference
The cost model is configured in config/cost_model.yaml:
currency: USD
cpu_per_core_hour: 0.031 # 1 vCPU hour price
mem_per_gb_hour: 0.04 # 1 GiB RAM hour price- FastAPI backend with async support
- Kubernetes Python client for cluster integration
- Automatic demo mode when cluster unavailable
- Comprehensive test suite with pytest
- Type hints throughout the codebase
- CORS support for cross-origin requests
- Real Mode: When a Kubernetes cluster is available, the app connects using the Kubernetes Python client and fetches metrics from the metrics.k8s.io API
- Demo Mode: When no cluster is available, the app uses sample data from
sample_data/demo_metrics.json
The application will automatically detect if it can connect to a cluster and switch between modes accordingly. The UI will show a "LIVE" or "DEMO" badge indicating the current mode.
┌─────────────────────────────────────────────────────┐
│ KubeCost Platform │
├─────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Frontend │◄───────►│ FastAPI │ │
│ │ (Vanilla JS)│ │ Backend │ │
│ └──────────────┘ └──────┬───────┘ │
│ │ │ │
│ │ ▼ │
│ │ ┌──────────────┐ │
│ │ │ Cost Model │ │
│ │ │ Service │ │
│ │ └──────┬───────┘ │
│ │ │ │
│ ▼ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Chart.js │ │ K8s Client │ │
│ │ Visualizations│ │ Service │ │
│ └──────────────┘ └──────┬───────┘ │
│ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ Kubernetes │ │
│ │ Metrics API │ │
│ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
| Component | Technology | Purpose |
|---|---|---|
| Frontend | Vanilla JavaScript, HTML5, CSS3 | Sovereign UI design system |
| Backend | FastAPI, Uvicorn | High-performance async API |
| K8s Client | kubernetes-python | Cluster metrics collection |
| Cost Model | Python, YAML | Configurable pricing engine |
| Visualization | Chart.js | Interactive charts |
| Styling | PatternFly, Red Hat Design | Enterprise-grade UI |
- Python 3.11 or higher
- Kubernetes cluster (optional - demo mode available)
kubectlconfigured (optional)
-
Clone the repository
git clone https://github.com/iamirrf/CostKube.git cd CostKube -
Create virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Run the application
uvicorn app.main:app --reload --host 0.0.0 --port 800
-
Open in browser
Navigate to
http://localhost:8000
Live Application: https://costkube.onrender.com
- ✅ Full FastAPI backend with real Kubernetes support
- ✅ Automatic demo mode with sample data
⚠️ First visit may take 30-60 seconds (free tier wakes from sleep)- ✅ Persistent URL - bookmark it!
Static Demo: https://iamirrf.github.io/CostKube/
- ✅ Instant load, no wait time
- ✅ Beautiful UI showcase
- ✅ Demo mode with sample data
Want to try CostKube with a real Kubernetes cluster without any costs? Use our automated deployment to Killercoda (free Kubernetes playground)!
Set it and forget it! Automatically redeploys when sessions expire.
-
Go to Killercoda Playground
Visit: https://killercoda.com/playgrounds/scenario/kubernetes
Click "Start" (no login required)
-
Run the auto-restart script
curl -sSL https://raw.githubusercontent.com/iamirrf/CostKube/main/auto_deploy_killercoda.sh | bash -
Access the app
Click the "Access Port 800" button in Killercoda's UI!
That's it! The script will:
- ✅ Auto-deploy and start CostKube
- ✅ Monitor session expiration
- ✅ Auto-redeploy every hour
- ✅ Keep running indefinitely
For manual control (session expires after 1 hour):
-
Go to Killercoda Playground
Visit: https://killercoda.com/playgrounds/scenario/kubernetes
Click "Start" (no login required)
-
Run the deployment script
curl -sSL https://raw.githubusercontent.com/iamirrf/CostKube/main/deploy_killercoda.sh | bash -
Start CostKube
cd CostKube python3 -m uvicorn app.main:app --host 0.0.0.0 --port 8000 -
Access the app
Click the "Access Port 800" button in Killercoda's UI!
The automated deployment script:
- ✅ Installs and configures metrics-server
- ✅ Creates sample namespaces (production, development, staging)
- ✅ Deploys sample workloads (nginx, redis, postgres)
- ✅ Clones CostKube and installs dependencies
- ✅ Verifies everything is working
If you prefer manual setup, see KUBERNETES_SETUP.md for detailed instructions.
Want to deploy to your own Kubernetes cluster?
kubectl apply -f k8s-deployment.yamlThis creates:
- Namespace:
costkube - ServiceAccount with proper RBAC permissions
- Deployment with resource limits
- NodePort Service on port 30800
Access at: http://<node-ip>:3080
No Kubernetes cluster? No problem! CostKube automatically falls back to demo mode.
The app will:
- ✅ Use realistic sample data
- ✅ Display all features and visualizations
- ✅ Show a "DEMO MODE" indicator
- ✅ Work without any cluster connection
Simply start the app and it will auto-detect whether to use live or demo data.
http://localhost:8000
# Build the image
docker build -t costkube:latest .
# Run the container
docker run -p 8000:8000 costkube:latestCostKube uses a YAML-based configuration file located at config/cost_model.yaml:
# Cost Model Configuration
currency: USD
# Pricing per hour
cpu_per_core_hour: 0.031 # Cost per vCPU core per hour
mem_per_gb_hour: 0.004 # Cost per GiB memory per hour
# Example pricing based on AWS EC2 pricing
# Adjust these values based on your cloud provider or on-prem costs- Edit
config/cost_model.yaml - Adjust
cpu_per_core_hourandmem_per_gb_hourbased on your infrastructure - Restart the application
Pricing Reference:
- AWS: Use EC2 instance pricing divided by cores/memory
- GCP: Use Compute Engine pricing
- Azure: Use Virtual Machine pricing
- On-Premises: Calculate based on hardware depreciation and power costs
CostKube automatically detects when a Kubernetes cluster is unavailable and switches to demo mode:
- ✅ Uses sample data from
sample_data/demo_metrics.json - ✅ Shows all UI features and functionality
- ✅ Perfect for development and presentations
- ✅ Displays a "Demo Mode Active" banner
To test demo mode:
# Simply run the app without a connected cluster
uvicorn app.main:app --reloadCostKube/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application entry point
│ ├── api/
│ │ ├── __init__.py
│ │ └── routes.py # API endpoints
│ ├── services/
│ │ ├── __init__.py
│ │ ├── cost_model.py # Cost calculation logic
│ │ ├── demo_data.py # Demo data provider
│ │ └── k8s_client.py # Kubernetes client wrapper
│ └── ui/
│ ├── static/
│ │ ├── css/
│ │ │ └── styles.css # Sovereign UI styles
│ │ └── js/
│ │ └── app.js # Frontend JavaScript
│ └── templates/
│ └── index.html # Main HTML template
├── config/
│ └── cost_model.yaml # Cost configuration
├── sample_data/
│ └── demo_metrics.json # Demo mode data
├── tests/
│ ├── __init__.py
│ ├── test_api_basic.py # API tests
│ └── test_cost_model.py # Cost model tests
├── .gitignore
├── requirements.txt
└── README.md
-
Install dev dependencies
pip install -r requirements.txt
-
Run tests
pytest -v
-
Start dev server with auto-reload
uvicorn app.main:app --reload --host 0.0.0 --port 800
-
Code formatting (recommended)
# Install black and isort pip install black isort # Format code black app/ tests/ isort --profile black app/ tests/
CostKube includes a comprehensive test suite:
# Run all tests
pytest
# Run with coverage
pytest --cov=app --cov-report=html
# Run specific test file
pytest tests/test_cost_model.py -v
# Run with detailed output
pytest -vv- ✅ API endpoint tests
- ✅ Cost model calculation tests
- ✅ Demo data validation
- ✅ Kubernetes client mocking
- ✅ Integration tests
Deploy CostKube to the cloud in 5 minutes with a permanent public URL!
- Fork/Clone this repository
- Go to Render.com (sign up for free - no credit card needed)
- Click "New +" → "Blueprint"
- Connect your GitHub repository
- Render auto-detects
render.yamland deploys - Done! Get URL like
https://costkube.onrender.com
- ✅ Permanent public URL - Share with anyone
- ✅ Automatic HTTPS - Free SSL certificate
- ✅ Auto-deployment - Push to GitHub = auto-deploy
- ✅ Professional loading screen - Smooth cold start experience
⚠️ Free tier limitations - Sleeps after 15 mins (30-60 sec wake time)
See RENDER_DEPLOYMENT.md for complete instructions, troubleshooting, and configuration options.
-
Set environment variables
export PYTHONUNBUFFERED=1 export PORT=8000
-
Run with Gunicorn
pip install gunicorn gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
apiVersion: apps/v1
kind: Deployment
metadata:
name: costkube
spec:
replicas: 2
selector:
matchLabels:
app: costkube
template:
metadata:
labels:
app: costkube
spec:
serviceAccountName: costkube-sa
containers:
- name: costkube
image: costkube:latest
ports:
- containerPort: 8000
env:
- name: PORT
value: "8000"
---
apiVersion: v1
kind: Service
metadata:
name: costkube
spec:
selector:
app: costkube
ports:
- port: 80
targetPort: 8000
type: LoadBalancerBuild and run CostKube with Docker:
# Build the image
docker build -t costkube:latest .
# Run locally
docker run -p 8000:8000 costkube:latest
# Run with custom config
docker run -p 8000:8000 -v $(pwd)/config:/app/config costkube:latestFor hosting a static UI demo, see the docs/ directory. This shows the interface with demo data only (no backend required).
Returns the current cost model configuration.
Response:
{
"currency": "USD",
"cpu_per_core_hour": 0.031,
"mem_per_gb_hour": 0.004,
"demo_mode": false
}Returns cost breakdown for all namespaces.
Response:
{
"data": [
{
"namespace": "default",
"cpu_mcores": 1500,
"memory_bytes": 2147483648,
"hourly_cost": 0.055,
"monthly_cost": 39.60
}
],
"demo_mode": false
}Returns cost breakdown for pods in a specific namespace.
Parameters:
namespace(query): Namespace name
Response:
{
"data": [
{
"pod": "nginx-deployment-abc123",
"namespace": "default",
"cpu_mcores": 500,
"memory_bytes": 536870912,
"hourly_cost": 0.018,
"monthly_cost": 12.96
}
],
"demo_mode": false
}Visit http://localhost:8000/docs for interactive Swagger UI documentation.
We welcome contributions from the community! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow PEP 8 for Python code
- Use meaningful variable and function names
- Add docstrings to all functions and classes
- Write tests for new features
- Keep the UI consistent with Red Hat design principles
This project is licensed under the MIT License - see the LICENSE file for details.
- Red Hat for the design system and PatternFly components
- Kubernetes community for excellent tooling and documentation
- FastAPI for the modern Python web framework
- Chart.js for beautiful visualizations
- 🌐 Live App: https://costkube.onrender.com
- 🎨 UI Demo: https://iamirrf.github.io/CostKube/
- 🐛 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
- 📖 Documentation: Full API docs available at
/docsendpoint