Writing Skills
writing-skills
效率与协作技能,来自 obra/superpowers,查看原文获取完整用途和操作说明。
optimize-cloud-costs【部署与运维】Implement cloud cost optimization strategies for Kubernetes workloads using tools like Kubecost for visibility, right-sizing recommendations, horizontal and vertical pod autoscaling, spot/preemptible instances, and resource quotas. Covers cost allocation, showback reporting, and continuous optimization practices. Use when cloud costs are growing with…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
施全策於 Kubernetes 群以減雲費。
詳例見 Extended Examples。
裝 Kubecost 或 OpenCost 以察費。
裝 Kubecost:
# Add Kubecost Helm repository
helm repo add kubecost https://kubecost.github.io/cost-analyzer/
helm repo update
# Install Kubecost with Prometheus integration
helm install kubecost kubecost/cost-analyzer \
--namespace kubecost \
--create-namespace \
--set kubecostToken="your-token-here" \
--set prometheus.server.global.external_labels.cluster_id="production-cluster" \
--set prometheus.nodeExporter.enabled=true \
--set prometheus.serviceAccounts.nodeExporter.create=true
# For existing Prometheus, configure Kubecost to use it
helm install kubecost kubecost/cost-analyzer \
--namespace kubecost \
--create-namespace \
--set prometheus.enabled=false \
--set global.prometheus.fqdn="http://prometheus-server.monitoring.svc.cluster.local" \
--set global.prometheus.enabled=true
# Verify installation
kubectl get pods -n kubecost
kubectl get svc -n kubecost
# Access Kubecost UI
kubectl port-forward -n kubecost svc/kubecost-cost-analyzer 9090:9090
# Open http://localhost:9090
設雲供應商整合:
# kubecost-cloud-integration.yaml
apiVersion: v1
kind: Secret
metadata:
name: cloud-integration
namespace: kubecost
type: Opaque
stringData:
# For AWS
cloud-integration.json: |
{
"aws": [
{
"serviceKeyName": "AWS_ACCESS_KEY_ID",
"serviceKeySecret": "AWS_SECRET_ACCESS_KEY",
"athenaProjectID": "cur-query-results",
"athenaBucketName": "s3://your-cur-bucket",
"athenaRegion": "us-east-1",
"athenaDatabase": "athenacurcfn_my_cur",
"athenaTable": "my_cur"
}
]
}
---
# For GCP
apiVersion: v1
kind: Secret
metadata:
name: gcp-key
namespace: kubecost
type: Opaque
data:
key.json: <base64-encoded-service-account-key>
---
# For Azure
apiVersion: v1
kind: ConfigMap
metadata:
name: azure-config
namespace: kubecost
data:
azure.json: |
{
"azureSubscriptionID": "your-subscription-id",
"azureClientID": "your-client-id",
"azureClientSecret": "your-client-secret",
"azureTenantID": "your-tenant-id",
"azureOfferDurableID": "MS-AZR-0003P"
}
施雲整合:
kubectl apply -f kubecost-cloud-integration.yaml
# Verify cloud costs are being imported
kubectl logs -n kubecost -l app=cost-analyzer -c cost-model --tail=100 | grep -i "cloud"
# Check Kubecost API for cost data
kubectl port-forward -n kubecost svc/kubecost-cost-analyzer 9090:9090 &
curl http://localhost:9090/model/allocation\?window\=7d | jq .
得: Kubecost pod 行成。UI 可達,現費依名空、部署、pod 之分。雲費入(首同步或 24-48 時)。API 返攤據。
敗則:
kubectl get svc -n monitoring prometheus-serverkubectl logs -n kubecost -l app=cost-analyzer -c cost-model識過備之資與優機。
問資用:
# Get resource requests vs usage for all pods
kubectl top pods --all-namespaces --containers | \
awk 'NR>1 {print $1,$2,$3,$4,$5}' > current-usage.txt
# Compare requests to actual usage
cat <<'EOF' > analyze-utilization.sh
#!/bin/bash
echo "Pod,Namespace,CPU-Request,CPU-Usage,Memory-Request,Memory-Usage"
for ns in $(kubectl get ns -o jsonpath='{.items[*].metadata.name}'); do
kubectl get pods -n $ns -o json | jq -r '
.items[] |
select(.status.phase == "Running") |
{
name: .metadata.name,
namespace: .metadata.namespace,
containers: [
.spec.containers[] |
{
name: .name,
cpuReq: .resources.requests.cpu,
memReq: .resources.requests.memory
}
]
} |
"\(.name),\(.namespace),\(.containers[].cpuReq // "none"),\(.containers[].memReq // "none")"
' 2>/dev/null
done
EOF
chmod +x analyze-utilization.sh
./analyze-utilization.sh > resource-requests.csv
# Get actual usage from metrics server
kubectl top pods --all-namespaces --containers > actual-usage.txt
用 Kubecost 之薦:
# Get right-sizing recommendations via API
curl "http://localhost:9090/model/savings/requestSizing?window=7d" | jq . > recommendations.json
# Extract top wasteful resources
jq '.data[] | select(.totalRecommendedSavings > 10) | {
cluster: .clusterID,
# ... (see EXAMPLES.md for complete configuration)
建用度盤:
# grafana-utilization-dashboard.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: utilization-dashboard
namespace: monitoring
# ... (see EXAMPLES.md for complete configuration)
得: 明見當前資請與實用之較。識用度 <30% 之 pod(過備)。優機附估省。盤示用度跨時之趨。
敗則:
kubectl get deployment metrics-server -n kube-systemcurl http://prometheus:9090/api/v1/query?query=node_cpu_seconds_total依 CPU、記憶、或自度量設自動伸縮。
建依 CPU 之 HPA:
# hpa-cpu.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-server-hpa
namespace: production
# ... (see EXAMPLES.md for complete configuration)
部並驗 HPA:
kubectl apply -f hpa-cpu.yaml
# Check HPA status
kubectl get hpa -n production
kubectl describe hpa api-server-hpa -n production
# Monitor scaling events
kubectl get events -n production --field-selector involvedObject.kind=HorizontalPodAutoscaler --watch
# Generate load to test autoscaling
kubectl run load-generator --rm -it --image=busybox -- /bin/sh -c \
"while true; do wget -q -O- http://api-server.production.svc.cluster.local; done"
# Watch replicas scale
watch kubectl get hpa,deployment -n production
得: HPA 已建,現當/目度量。負下 pod 增。負減(穩窗後)pod 減。伸縮事入記。無振(速增減環)。
敗則:
kubectl get apiservice v1beta1.metrics.k8s.iokubectl describe hpa api-server-hpa -n productionkubectl logs -n kube-system -l app=kube-controller-manager | grep horizontal-pod-autoscaler依實用模自動調資請。
裝 VPA:
# Clone VPA repository
git clone https://github.com/kubernetes/autoscaler.git
cd autoscaler/vertical-pod-autoscaler
# Install VPA
./hack/vpa-up.sh
# Verify installation
kubectl get pods -n kube-system | grep vpa
# Check VPA CRDs
kubectl get crd | grep verticalpodautoscaler
建 VPA 策:
# vpa-policies.yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: api-server-vpa
namespace: production
# ... (see EXAMPLES.md for complete configuration)
部並察 VPA:
kubectl apply -f vpa-policies.yaml
# Check VPA recommendations
kubectl get vpa -n production
kubectl describe vpa api-server-vpa -n production
# View detailed recommendations
kubectl get vpa api-server-vpa -n production -o jsonpath='{.status.recommendation}' | jq .
# Monitor VPA-initiated pod updates
kubectl get events -n production --field-selector involvedObject.kind=VerticalPodAutoscaler --watch
# Compare recommendations to current requests
kubectl get deployment api-server -n production -o json | \
jq '.spec.template.spec.containers[].resources.requests'
得: VPA 供薦或自動調資請。薦基於百分位用模(常 P95)。Auto/Recreate 模下 pod 以新請重啟。HPA 與 VPA 不衝(HPA 為複本,VPA 為每 pod 資)。
敗則:
kubectl get pods -n kube-system | grep vpakubectl logs -n kube-system -l app=vpa-admission-controllerkubectl get mutatingwebhookconfigurations vpa-webhook-config設工負排程於省費 spot 例。
建 spot 之節點池:
# For AWS (via Karpenter)
apiVersion: karpenter.sh/v1alpha5
kind: Provisioner
metadata:
name: spot-provisioner
spec:
# ... (see EXAMPLES.md for complete configuration)
設工負為 spot:
# spot-workload.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: batch-processor
namespace: production
# ... (see EXAMPLES.md for complete configuration)
部並察 spot 用:
kubectl apply -f spot-workload.yaml
# Monitor spot node allocation
kubectl get nodes -l node-type=spot
# Check workload distribution
# ... (see EXAMPLES.md for complete configuration)
得: 工負排成於 spot 節點。費大減(常較 on-demand 省 60-90%)。優雅處 spot 中斷與 pod 重排。察示中斷率與成功復。
敗則:
kubectl logs -n karpenter -l app.kubernetes.io/name=karpenter設硬限與警以控費。
建資配額:
# resource-quotas.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: production-quota
namespace: production
# ... (see EXAMPLES.md for complete configuration)
設算警:
# kubecost-budget-alerts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: budget-alerts
namespace: kubecost
# ... (see EXAMPLES.md for complete configuration)
施並察:
kubectl apply -f resource-quotas.yaml
kubectl apply -f kubecost-budget-alerts.yaml
# Check quota usage
kubectl get resourcequota -n production
kubectl describe resourcequota production-quota -n production
# ... (see EXAMPLES.md for complete configuration)
得: 配額施限於各名空。逾配額則 pod 建拒。閾破則算警鳴。費突察行。常報送於相關者。
敗則:
kubectl get resourcequota,limitrange -Akubectl get events -n production | grep quotakubectl logs -n kubecost -l app=cost-analyzer | grep alertcurl http://prometheus:9090/api/v1/query?query=kubecost_monthly_cost過急右大小:勿即施 VPA 薦。始以「Off」模,察薦一週,後漸施。突變致 OOMKill 或 CPU 節制。
HPA + VPA 衝:勿於同度(CPU/記憶)同用 HPA 與 VPA。HPA 為橫伸縮,VPA 為每 pod 資調,或 HPA 自度量 + VPA 資。
無容錯之 spot:唯容錯、無態之工負於 spot。勿用於庫、有態服、或單複本要服。必用 PodDisruptionBudget。
察期不足:優費決需歷據。變前候至少七日,VPA 薦三十日,趨析九十日。
忽突需:依均用設限過低致流突時節制。容量計用 P95 或 P99 百分位,非均。
網外費:計費於 Kubecost 可見,然外(傳)費可大。察跨 AZ 流,用拓樸路由,計傳費於架構。
忽存儲:PV 費常忘。察未用 PVC,右大小卷,用卷擴非過備,行 PV 清策。
配額過束:配額過低阻合理長。月察配額用,依實調,施前告隊。
誤度量之假省:唯 CPU/記憶為優度漏 I/O、網、存費。考總有費,非僅算。
未信前 chargeback:隊未解信費據前 chargeback 致摩。始以 showback(告),建費覺文化,後 chargeback。
deploy-to-kubernetes - 應部署附宜資請setup-prometheus-monitoring - 費度之察基設plan-capacity - 依費與性容量計setup-local-kubernetes - 本地開發避雲費write-helm-chart - 模板資請與限implement-gitops-workflow - 為優費設之 GitOps