k8s-cost
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKubernetes Cost Optimization
Kubernetes 成本优化
Cost analysis and optimization using kubectl-mcp-server's cost tools.
使用kubectl-mcp-server的成本工具进行成本分析与优化。
When to Apply
适用场景
Use this skill when:
- User mentions: "cost", "savings", "optimize", "expensive", "budget"
- Operations: cost analysis, right-sizing, cleanup unused resources
- Keywords: "how much", "reduce", "efficiency", "waste", "overprovisioned"
在以下场景中使用该技能:
- 用户提及:"cost"、"savings"、"optimize"、"expensive"、"budget"(成本、节省、优化、昂贵、预算)
- 操作需求:成本分析、资源合理配置、清理未使用资源
- 关键词:"how much"、"reduce"、"efficiency"、"waste"、"overprovisioned"(多少钱、降低、效率、浪费、过度配置)
Priority Rules
优先级规则
| Priority | Rule | Impact | Tools |
|---|---|---|---|
| 1 | Find and delete unused PVCs | CRITICAL | |
| 2 | Right-size overprovisioned pods | HIGH | |
| 3 | Identify idle LoadBalancers | HIGH | |
| 4 | Scale down non-prod off-hours | MEDIUM | |
| 5 | Consolidate small namespaces | LOW | Analysis |
| 优先级 | 规则 | 影响程度 | 工具 |
|---|---|---|---|
| 1 | 查找并删除未使用的PVC | 关键 | |
| 2 | 合理配置过度配置的Pod | 高 | |
| 3 | 识别闲置的LoadBalancers | 高 | |
| 4 | 非生产环境非工作时间缩容 | 中 | |
| 5 | 合并小型命名空间 | 低 | 分析 |
Quick Reference
快速参考
| Task | Tool | Example |
|---|---|---|
| Namespace cost | | |
| Cluster cost | | |
| Unused PVCs | | |
| Right-sizing | | |
| 任务 | 工具 | 示例 |
|---|---|---|
| 命名空间成本 | | |
| 集群成本 | | |
| 未使用的PVC | | |
| 资源合理配置 | | |
Quick Cost Analysis
快速成本分析
Get Cost Summary
获取成本摘要
python
get_namespace_cost(namespace)
get_cluster_cost()python
get_namespace_cost(namespace)
get_cluster_cost()Find Unused Resources
查找未使用资源
python
find_unused_resources(namespace)
find_orphaned_pvcs(namespace)python
find_unused_resources(namespace)
find_orphaned_pvcs(namespace)Resource Right-Sizing
资源合理配置
python
get_resource_recommendations(namespace)
get_pod_metrics(name, namespace)python
get_resource_recommendations(namespace)
get_pod_metrics(name, namespace)Cost Optimization Workflow
成本优化工作流
1. Identify Overprovisioned Resources
1. 识别过度配置的资源
python
get_resource_recommendations(namespace="production")
get_pod_metrics(name, namespace)
get_resource_usage(namespace)python
get_resource_recommendations(namespace="production")
get_pod_metrics(name, namespace)
get_resource_usage(namespace)2. Find Idle Resources
2. 查找闲置资源
python
find_orphaned_pvcs(namespace)
find_unused_resources(namespace)python
find_orphaned_pvcs(namespace)
find_unused_resources(namespace)3. Analyze Node Utilization
3. 分析节点利用率
python
get_nodes()
get_node_metrics()python
get_nodes()
get_node_metrics()Right-Sizing Guidelines
资源合理配置指南
| Current State | Recommendation |
|---|---|
| CPU usage < 10% of request | Reduce request by 50% |
| CPU usage > 80% of request | Increase request by 25% |
| Memory < 50% of request | Reduce request |
| Memory near limit | Increase limit, monitor OOM |
| 当前状态 | 建议 |
|---|---|
| CPU使用率 < 请求值的10% | 将请求值降低50% |
| CPU使用率 > 请求值的80% | 将请求值提高25% |
| 内存使用率 < 请求值的50% | 降低请求值 |
| 内存使用率接近限制值 | 提高限制值,监控OOM情况 |
Cost by Resource Type
按资源类型划分的成本
Compute (Pods/Deployments)
计算资源(Pods/Deployments)
python
get_resource_usage(namespace)
get_pod_metrics(name, namespace)python
get_resource_usage(namespace)
get_pod_metrics(name, namespace)Storage (PVCs)
存储资源(PVCs)
python
get_pvc(namespace)
find_orphaned_pvcs(namespace)python
get_pvc(namespace)
find_orphaned_pvcs(namespace)Network (LoadBalancers)
网络资源(LoadBalancers)
python
get_services(namespace)python
get_services(namespace)Multi-Cluster Cost Analysis
多集群成本分析
Compare costs across clusters:
python
get_cluster_cost(context="production")
get_cluster_cost(context="staging")
get_cluster_cost(context="development")跨集群对比成本:
python
get_cluster_cost(context="production")
get_cluster_cost(context="staging")
get_cluster_cost(context="development")Cost Reduction Actions
成本削减措施
Immediate Wins
立即可行的优化
- Delete unused PVCs: then delete
find_orphaned_pvcs() - Right-size pods: Apply
get_resource_recommendations() - Scale down dev/staging: Off-hours scaling
- 删除未使用的PVC:执行后删除
find_orphaned_pvcs() - 合理配置Pod资源:应用的结果
get_resource_recommendations() - 缩容开发/预发布环境:非工作时间自动缩容
Medium-term Optimizations
中期优化方案
- Use Spot/Preemptible nodes: For fault-tolerant workloads
- Implement HPA: Auto-scale based on demand
- Use KEDA: Scale to zero for event-driven workloads
- 使用Spot/抢占式节点:适用于容错型工作负载
- 实现HPA:基于需求自动扩缩容
- 使用KEDA:针对事件驱动型工作负载缩容至零
Long-term Strategy
长期策略
- Reserved instances: For stable production workloads
- Multi-tenant clusters: Consolidate small clusters
- Right-size node pools: Match workload requirements
- 预留实例:适用于稳定的生产工作负载
- 多租户集群:合并小型集群
- 合理配置节点池:匹配工作负载需求
Automated Analysis Script
自动化分析脚本
For comprehensive cost analysis, see scripts/find-overprovisioned.py.
如需全面成本分析,请查看scripts/find-overprovisioned.py。
KEDA for Cost Savings
使用KEDA节省成本
Scale to zero with KEDA:
python
keda_scaledobjects_list_tool(namespace)
keda_scaledobject_get_tool(name, namespace)KEDA reduces costs by:
- Scaling pods to 0 when idle
- Event-driven scaling (queue depth, etc.)
- Cron-based scaling for predictable patterns
通过KEDA缩容至零:
python
keda_scaledobjects_list_tool(namespace)
keda_scaledobject_get_tool(name, namespace)KEDA通过以下方式降低成本:
- 闲置时将Pod缩容至0
- 基于事件驱动扩缩容(如队列深度等)
- 基于Cron的扩缩容,适配可预测的负载模式
Related Skills
相关技能
- k8s-autoscaling - HPA, VPA, KEDA
- k8s-troubleshoot - Resource debugging
- k8s-autoscaling - HPA、VPA、KEDA
- k8s-troubleshoot - 资源调试