Crane 成本优化工具实验记录

准备集群

本文通过 kind 创建一个四节点集群环境:

# cat << EOF > cluster.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
EOF
# kind create cluster --config cluster.yaml
安装 Prometheus 和 Grafana 依赖
# helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
# helm install prometheus -n crane-system --set pushgateway.enabled=false --set alertmanager.enabled=false --set server.persistentVolume.enabled=false -f https://raw.githubusercontent.com/gocrane/helm-charts/main/integration/prometheus/override_values.yaml --create-namespace  prometheus-community/prometheus
# helm repo add grafana https://grafana.github.io/helm-charts
# helm install grafana -f https://raw.githubusercontent.com/gocrane/helm-charts/main/integration/grafana/override_values.yaml -n crane-system --create-namespace grafana/grafana
安装 Crane 和 Fadvisor
# helm repo add crane https://gocrane.github.io/helm-charts
# helm install crane -n crane-system --create-namespace crane/crane
# helm install fadvisor -n crane-system --create-namespace crane/fadvisor
查看 Grafana 控制面板
# export POD_NAME=$(kubectl get pods --namespace crane-system -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=grafana" -o jsonpath="{.items[0].metadata.name}")
# kubectl --namespace crane-system port-forward $POD_NAME 3000

浏览器打开 http://localhost:3000 用户名密码均为 admin
在这里插入图片描述

获取资源分析
# cat << EOF | kubectl apply -f -
apiVersion: analysis.crane.io/v1alpha1
kind: Analytics
metadata:
  name: craned-resource
  namespace: crane-system
spec:
  type: Resource                        # This can only be "Resource" or "HPA".
  completionStrategy:
    completionStrategyType: Periodical  # This can only be "Once" or "Periodical".
    periodSeconds: 86400                # analytics selected resources every 1 day
  resourceSelectors:                    # defines all the resources to be select with
    - kind: Deployment
      apiVersion: apps/v1
      name: craned

---

apiVersion: analysis.crane.io/v1alpha1
kind: Analytics
metadata:
  name: metric-adapter-resource
  namespace: crane-system
spec:
  type: Resource                       # This can only be "Resource" or "HPA".
  completionStrategy:
    completionStrategyType: Periodical # This can only be "Once" or "Periodical".
    periodSeconds: 3600                # analytics selected resources every 1 hour
  resourceSelectors:                   # defines all the resources to be select with
    - kind: Deployment
      apiVersion: apps/v1
      name: metric-adapter
EOF
# kubectl get analytics -n crane-system

获取推荐的资源使用量

# kubectl get analytics -n crane-system craned-resource -oyaml
# 获取 status.recommendations 的 name 和 namespace
# kubectl get recommend -n crane-system craned-resource-resource-8sl6n -oyaml
# OUTPUT 类似如下:
apiVersion: analysis.crane.io/v1alpha1
kind: Recommendation
metadata:
  creationTimestamp: "2022-03-23T03:06:08Z"
  generateName: craned-resource-resource-
  generation: 2
  labels:
    app: craned
    app.kubernetes.io/instance: crane
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: crane
    app.kubernetes.io/version: v0.2.0
    helm.sh/chart: crane-0.2.0
  name: craned-resource-resource-s7lp2
  namespace: crane-system
  ownerReferences:
  - apiVersion: analysis.crane.io/v1alpha1
    blockOwnerDeletion: false
    controller: false
    kind: Analytics
    name: craned-resource
    uid: 0e653c3e-80ac-4b35-8cbb-67952c2ea47f
  resourceVersion: "7368"
  uid: a1fa6443-4962-4144-8894-57529061afdd
spec:
  completionStrategy:
    completionStrategyType: Periodical
    periodSeconds: 86400
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: craned
    namespace: crane-system
  type: Resource
status:
  conditions:
  - lastTransitionTime: "2022-03-23T03:06:08Z"
    message: Recommendation is ready
    reason: RecommendationReady
    status: "True"
    type: Ready
  lastUpdateTime: "2022-03-23T03:06:08Z"
  recommendedValue: |
    containers:
    - containerName: craned
      target:
        cpu: 114m
        memory: 120586239m

可以看到推荐使用的 CPU 是 114m,内存是 120586239m

获取 HPA 推荐
# cat << EOF | kubectl apply -f -
apiVersion: analysis.crane.io/v1alpha1
kind: Analytics
metadata:
  name: craned-hpa
  namespace: crane-system
spec:
  type: HPA                        # This can only be "Resource" or "HPA".
  completionStrategy:
    completionStrategyType: Periodical  # This can only be "Once" or "Periodical".
    periodSeconds: 600                  # analytics selected resources every 10 minutes
  resourceSelectors:                    # defines all the resources to be select with
    - kind: Deployment
      apiVersion: apps/v1
      name: craned

---

apiVersion: analysis.crane.io/v1alpha1
kind: Analytics
metadata:
  name: metric-adapter-hpa
  namespace: crane-system
spec:
  type: HPA                       # This can only be "Resource" or "HPA".
  completionStrategy:
    completionStrategyType: Periodical # This can only be "Once" or "Periodical".
    periodSeconds: 3600                # analytics selected resources every 1 hour
  resourceSelectors:                   # defines all the resources to be select with
    - kind: Deployment
      apiVersion: apps/v1
      name: metric-adapter
EOF

# kubectl get analytics -n crane-system 

获取推荐的 HPA 配置

# kubectl get analytics -n crane-system craned-hpa -oyaml
# 获取 status.recommendations 的 name 和 namespace
# kubectl get recommend -n crane-system craned-hpa-hpa-2v9kz -oyaml
# OUTPUT 类似如下:
apiVersion: analysis.crane.io/v1alpha1
kind: Recommendation
metadata:
  creationTimestamp: "2022-03-23T03:10:30Z"
  generateName: craned-hpa-hpa-
  generation: 2
  labels:
    app: craned
    app.kubernetes.io/instance: crane
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: crane
    app.kubernetes.io/version: v0.2.0
    helm.sh/chart: crane-0.2.0
  name: craned-hpa-hpa-2v9kz
  namespace: crane-system
  ownerReferences:
  - apiVersion: analysis.crane.io/v1alpha1
    blockOwnerDeletion: false
    controller: false
    kind: Analytics
    name: craned-hpa
    uid: 18a4caa0-7f21-450d-ae0a-c7fccce51780
  resourceVersion: "8288"
  uid: 8e268dce-e71b-467a-b045-f57774111102
spec:
  completionStrategy:
    completionStrategyType: Periodical
    periodSeconds: 600
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: craned
    namespace: crane-system
  type: HPA
status:
  conditions:
  - lastTransitionTime: "2022-03-23T03:10:30Z"
    message: 'Failed to offer recommend, Recommendation crane-system/craned-hpa-hpa-2v9kz
      error EHPAAdvisor query historic metrics data is unexpected, List length is
      0 '
    reason: FailedOfferRecommend
    status: "False"
    type: Ready
  lastUpdateTime: "2022-03-23T03:10:30Z"

推荐失败,官方解释是运行时间过短导致无法预测

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐