随着 Kubernetes 集群规模的扩大,云基础设施成本往往迅速失控。FinOps 作为一种融合财务、技术和业务的云成本管理方法论,帮助团队在 Kubernetes 环境中实现成本的可视化、优化和治理。
FinOps 核心原则
FinOps 的三大阶段包括:可见性(了解成本流向)、优化(减少浪费和提高效率)和治理(建立持续的成本管理流程)。
资源请求与限制配置
合理的资源配额是成本优化的基础。以下是一个典型的生产配置示例:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: web-app-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
updatePolicy:
updateMode: Auto
resourcePolicy:
containerPolicies:
- containerName: '*'
minAllowed:
cpu: 100m
memory: 128Mi
maxAllowed:
cpu: 2
memory: 2Gi
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: web-app-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
updatePolicy:
updateMode: Auto
resourcePolicy:
containerPolicies:
- containerName: '*'
minAllowed:
cpu: 100m
memory: 128Mi
maxAllowed:
cpu: 2
memory: 2Gi成本分配与工具
通过 Kubecost 可以实现按命名空间、标签或工作负载的成本分摊,精确计算每个团队的支出。Karpenter 作为动态节点编排器,自动选择最优实例类型和 Spot 实例,可将计算成本降低 40-60%。结合废弃资源回收、合理设置 Pod 请求限制以及利用 Spot 实例,企业可以在 Kubernetes 上实现显著的成本节约。