← 返回首页
💰

云成本优化策略

📂 devops ⏱ 2 min 311 words

云成本优化策略

成本优化原则

  1. 可见性:了解成本构成
  2. 问责制:团队对成本负责
  3. 优化:持续改进
  4. 治理:建立成本控制机制

成本分析

AWS成本分析

# 使用AWS CLI查询成本
aws ce get-cost-and-usage \
  --time-period Start=2024-01-01,End=2024-01-31 \
  --granularity MONTHLY \
  --metrics "BlendedCost" \
  --group-by Type=DIMENSION,Key=SERVICE

标签策略

# 资源标签
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    environment: production
    team: backend
    cost-center: engineering
    project: myapp
spec:
  template:
    metadata:
      labels:
        cost-center: engineering

Kubernetes成本优化

资源请求优化

# 使用VPA建议
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: myapp-vpa
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myapp
  updatePolicy:
    updateMode: "Off"  # 只提供建议

节点自动伸缩

# Cluster Autoscaler配置
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cluster-autoscaler
  namespace: kube-system
spec:
  template:
    spec:
      containers:
        - name: cluster-autoscaler
          command:
            - ./cluster-autoscaler
            - --scale-down-enabled=true
            - --scale-down-delay-after-add=10m
            - --scale-down-unneeded-time=10m
            - --max-node-provision-time=15m

Spot实例

# 使用Spot实例
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  template:
    spec:
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              preference:
                matchExpressions:
                  - key: lifecycle
                    operator: In
                    values:
                      - spot
      containers:
        - name: myapp
          resources:
            requests:
              cpu: "100m"
              memory: "128Mi"

实践:成本优化脚本

#!/bin/bash

echo "=== Kubernetes成本优化分析 ==="

# 1. 查找资源浪费
echo ""
echo "--- 资源请求过高 ---"
kubectl get pods --all-namespaces -o json | jq -r '
  .items[] | 
  select(.spec.containers[].resources.requests.cpu != null) |
  {
    namespace: .metadata.namespace,
    pod: .metadata.name,
    cpu_request: .spec.containers[].resources.requests.cpu
  }' | head -20

# 2. 查找未使用的PVC
echo ""
echo "--- 未使用的PVC ---"
kubectl get pvc --all-namespaces | while read line; do
  pvc=$(echo $line | awk '{print $1}')
  ns=$(echo $line | awk '{print $2}')
  echo "检查 $ns/$pvc"
done

# 3. 查找未使用的LoadBalancer
echo ""
echo "--- 未使用的LoadBalancer ---"
kubectl get svc --all-namespaces -o json | jq -r '
  .items[] | 
  select(.spec.type == "LoadBalancer") |
  {
    namespace: .metadata.namespace,
    name: .metadata.name,
    loadbalancer: .status.loadBalancer.ingress[0].ip
  }'

存储优化

# 使用适当的存储类
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: cost-effective
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp3
  iopsPerGB: "10"
reclaimPolicy: Retain
allowVolumeExpansion: true

预留实例

# 购买预留实例
aws ec2 create-reserve-instances-configuration \
  --region us-west-2 \
  --purchase-token xxx \
  --reserved-instances-offering-ids xxx

成本监控

# Kubecost配置
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cost-analyzer
  namespace: kubecost
spec:
  template:
    spec:
      containers:
        - name: cost-analyzer
          image: kubecost/cost-analyzer:latest
          env:
            - name: PROMETHEUS_SERVER_ENDPOINT
              value: "http://prometheus-server:9090"

最佳实践

  1. 实施标签策略
  2. 使用Spot实例
  3. 优化资源请求
  4. 启用自动伸缩
  5. 定期审查成本
  6. 使用预留实例

总结

云成本优化是一个持续的过程。通过可见性、问责制和持续优化,可以显著降低云计算成本。