Kubernetes成本优化与资源管理
2026/5/11 20:17:39 网站建设 项目流程

Kubernetes成本优化与资源管理

一、引言

Kubernetes资源管理和成本优化是云原生运维的重要课题。通过合理配置资源、优化调度策略和实施精细化管理,可以显著降低基础设施成本。

二、成本优化架构

2.1 成本优化参考架构

┌─────────────────────────────────────────────────────────────────┐ │ 成本优化架构 │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ 资源分析 │───▶│ 优化建议 │───▶│ 自动执行 │───▶│ 成本监控 │ │ │ │ (Metrics)│ │ (Advisor)│ │ (Actions) │ │ (Billing) │ │ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │ │ │ │ │ ▼ ▼ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ Kubernetes集群 │ │ │ │ (Nodes / Pods / Storage / Network) │ │ │ └─────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────────┘

2.2 成本优化维度

维度优化方向工具/方法
计算资源CPU/内存优化HPA/VPA、资源请求/限制
存储资源存储优化Local PV、StorageClass
网络资源流量优化NetworkPolicy、CDN
节点资源节点调度优化Node Affinity、Taints
资源闲置闲置资源清理自动清理脚本

三、资源配置优化

3.1 资源请求与限制

apiVersion: apps/v1 kind: Deployment metadata: name: optimized-app spec: template: spec: containers: - name: app image: my-app:1.0.0 resources: requests: memory: "256Mi" cpu: "100m" limits: memory: "512Mi" cpu: "200m"

3.2 HPA配置优化

apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: app-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: optimized-app minReplicas: 1 maxReplicas: 5 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 60 - type: Resource resource: name: memory target: type: Utilization averageUtilization: 70

3.3 VPA配置

apiVersion: autoscaling.k8s.io/v1 kind: VerticalPodAutoscaler metadata: name: app-vpa spec: targetRef: apiVersion: "apps/v1" kind: Deployment name: optimized-app updatePolicy: updateMode: "Auto" resourcePolicy: containerPolicies: - containerName: "*" minAllowed: cpu: 50m memory: 128Mi maxAllowed: cpu: 1 memory: 1Gi

四、节点资源优化

4.1 节点调度优化

apiVersion: v1 kind: Pod metadata: name: database-pod spec: affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 preference: matchExpressions: - key: node-type operator: In values: - spot tolerations: - key: "spot" operator: "Exists" effect: "NoSchedule"

4.2 节点池配置

apiVersion: cluster.x-k8s.io/v1beta1 kind: MachineDeployment metadata: name: spot-nodes spec: replicas: 3 selector: matchLabels: node-type: spot template: spec: providerID: aws:///us-west-2/i-1234567890 nodeRef: apiGroup: infrastructure.cluster.x-k8s.io kind: AWSMachineTemplate name: spot-template

4.3 节点自动伸缩

apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: node-group-hpa spec: scaleTargetRef: apiVersion: v1 kind: Service name: node-group minReplicas: 2 maxReplicas: 10 targetCPUUtilizationPercentage: 70

五、存储成本优化

5.1 存储类配置

apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: standard-storage provisioner: ebs.csi.aws.com parameters: type: gp2 fsType: ext4 allowVolumeExpansion: true mountOptions: - noatime reclaimPolicy: Delete

5.2 Local PV配置

apiVersion: v1 kind: PersistentVolume metadata: name: local-pv spec: capacity: storage: 100Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Delete storageClassName: local-storage local: path: /mnt/disks/ssd1 nodeAffinity: required: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - node-1

5.3 存储清理策略

#!/bin/bash # 清理未使用的PVC echo "Cleaning up unused PVCs..." kubectl get pvc --all-namespaces -o json | \ jq -r '.items[] | select(.status.phase == "Bound") | .metadata.name' | \ while read pvc; do if ! kubectl get pods --all-namespaces -o json | \ jq -e '.items[].spec.volumes[] | select(.persistentVolumeClaim.claimName == "'$pvc'")' > /dev/null 2>&1; then echo "Deleting unused PVC: $pvc" kubectl delete pvc $pvc --all-namespaces fi done # 清理未绑定的PV echo "Cleaning up unbound PVs..." kubectl get pv -o json | \ jq -r '.items[] | select(.status.phase == "Available") | .metadata.name' | \ while read pv; do echo "Deleting unbound PV: $pv" kubectl delete pv $pv done

六、成本监控与分析

6.1 成本指标收集

apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: cost-monitor spec: selector: matchLabels: app: cost-exporter endpoints: - port: metrics interval: 30s

6.2 Prometheus查询

# 节点成本 sum(kube_node_labels) by (node) * on(node) group_left sum(node_hourly_cost) # Pod成本 sum(container_cpu_usage_seconds_total) by (pod, namespace) * 0.05 # 存储成本 sum(kube_persistentvolumeclaim_resource_requests_storage_bytes) by (namespace) * 0.0001

6.3 成本分析仪表盘

{ "title": "Kubernetes Cost Dashboard", "panels": [ { "type": "graph", "targets": [ { "expr": "sum(node_hourly_cost)", "legendFormat": "Total Node Cost" } ] }, { "type": "stat", "targets": [ { "expr": "sum(kube_persistentvolumeclaim_resource_requests_storage_bytes) * 0.0001", "legendFormat": "Storage Cost" } ] }, { "type": "table", "targets": [ { "expr": "sum(container_cpu_usage_seconds_total) by (namespace) * 0.05", "legendFormat": "{{namespace}}" } ] } ] }

七、成本优化最佳实践

7.1 资源利用率优化

apiVersion: apps/v1 kind: Deployment metadata: name: cost-optimized-app spec: template: spec: containers: - name: app image: my-app:1.0.0 resources: requests: memory: "{{ .Values.resources.requests.memory }}" cpu: "{{ .Values.resources.requests.cpu }}" limits: memory: "{{ .Values.resources.limits.memory }}" cpu: "{{ .Values.resources.limits.cpu }}" lifecycle: preStop: exec: command: ["sh", "-c", "sleep 5"]

7.2 闲置资源清理

#!/bin/bash # 清理终止状态的Pod kubectl delete pods --all-namespaces --field-selector status.phase=Succeeded kubectl delete pods --all-namespaces --field-selector status.phase=Failed # 清理过期的Job kubectl delete jobs --all-namespaces --field-selector status.succeeded=1 # 清理未使用的ConfigMap kubectl get configmaps --all-namespaces -o json | \ jq -r '.items[] | select(.metadata.ownerReferences == null) | .metadata.name' | \ while read cm; do kubectl delete configmap $cm --all-namespaces done

7.3 成本预算管理

apiVersion: budgets.example.com/v1 kind: Budget metadata: name: monthly-budget spec: limit: 10000 period: monthly alertThresholds: - threshold: 80 action: notify - threshold: 95 action: restrict

八、总结

Kubernetes成本优化是一个持续迭代的过程。通过合理配置资源请求和限制、优化节点调度、实施存储优化和建立成本监控体系,可以显著降低云原生基础设施的运营成本。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询