k8s-cost

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Kubernetes 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

优先级规则

PriorityRuleImpactTools
1Find and delete unused PVCsCRITICAL
find_orphaned_pvcs
2Right-size overprovisioned podsHIGH
get_resource_recommendations
3Identify idle LoadBalancersHIGH
get_services
4Scale down non-prod off-hoursMEDIUM
scale_deployment
5Consolidate small namespacesLOWAnalysis
优先级规则影响程度工具
1查找并删除未使用的PVC关键
find_orphaned_pvcs
2合理配置过度配置的Pod
get_resource_recommendations
3识别闲置的LoadBalancers
get_services
4非生产环境非工作时间缩容
scale_deployment
5合并小型命名空间分析

Quick Reference

快速参考

TaskToolExample
Namespace cost
get_namespace_cost
get_namespace_cost(namespace)
Cluster cost
get_cluster_cost
get_cluster_cost()
Unused PVCs
find_orphaned_pvcs
find_orphaned_pvcs(namespace)
Right-sizing
get_resource_recommendations
get_resource_recommendations(namespace)
任务工具示例
命名空间成本
get_namespace_cost
get_namespace_cost(namespace)
集群成本
get_cluster_cost
get_cluster_cost()
未使用的PVC
find_orphaned_pvcs
find_orphaned_pvcs(namespace)
资源合理配置
get_resource_recommendations
get_resource_recommendations(namespace)

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 StateRecommendation
CPU usage < 10% of requestReduce request by 50%
CPU usage > 80% of requestIncrease request by 25%
Memory < 50% of requestReduce request
Memory near limitIncrease 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

立即可行的优化

  1. Delete unused PVCs:
    find_orphaned_pvcs()
    then delete
  2. Right-size pods: Apply
    get_resource_recommendations()
  3. Scale down dev/staging: Off-hours scaling
  1. 删除未使用的PVC:执行
    find_orphaned_pvcs()
    后删除
  2. 合理配置Pod资源:应用
    get_resource_recommendations()
    的结果
  3. 缩容开发/预发布环境:非工作时间自动缩容

Medium-term Optimizations

中期优化方案

  1. Use Spot/Preemptible nodes: For fault-tolerant workloads
  2. Implement HPA: Auto-scale based on demand
  3. Use KEDA: Scale to zero for event-driven workloads
  1. 使用Spot/抢占式节点:适用于容错型工作负载
  2. 实现HPA:基于需求自动扩缩容
  3. 使用KEDA:针对事件驱动型工作负载缩容至零

Long-term Strategy

长期策略

  1. Reserved instances: For stable production workloads
  2. Multi-tenant clusters: Consolidate small clusters
  3. Right-size node pools: Match workload requirements
  1. 预留实例:适用于稳定的生产工作负载
  2. 多租户集群:合并小型集群
  3. 合理配置节点池:匹配工作负载需求

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 - 资源调试