垂直自動調度 Pod 資源功能會自動設定 Kubernetes Pod 中容器的 CPU 和記憶體資源要求與限制。垂直自動調度 Pod 資源功能會分析歷來和目前的資源用量,提供建議,並顯示或自動套用建議,更新 Pod。這項功能可適當調整資源分配,進而提升穩定性和成本效益。
事前準備
設定垂直 Pod 自動調度資源功能前,請確認您符合下列必要條件:
- 您有正在執行的 Bare Metal 叢集。
- 您擁有叢集的
kubectl存取權。 - 叢集提供指標伺服器。根據預設,裸機叢集會包含指標伺服器。
啟用垂直自動調度 Pod 資源功能
如要在裸機叢集上啟用垂直 Pod 自動調度資源功能,請設定預覽註解並設定叢集規格:
在叢集自訂資源中新增或更新預先發布版註解。
直接編輯叢集自訂資源,或修改叢集設定檔並使用
bmctl update。metadata: annotations: preview.baremetal.cluster.gke.io/vertical-pod-autoscaler: enable修改 Cluster 自訂資源的
spec,加入verticalPodAutoscaling欄位並指定enableUpdater和enableMemorySaver模式:apiVersion: baremetal.cluster.gke.io/v1 kind: Cluster metadata: name: cluster1 namespace: cluster-cluster1 annotations: preview.baremetal.cluster.gke.io/vertical-pod-autoscaler: enable spec: # ... other cluster spec fields verticalPodAutoscaling: enableUpdater: true # Set to true for automated updates enableMemorySaver: true # Set to true to reduce recommender memory usage如果您修改了叢集設定檔,請使用下列指令套用變更:
bmctl update cluster -c CLUSTER_NAME --kubeconfig KUBECONFIG更改下列內容:
CLUSTER_NAME:叢集名稱。KUBECONFIG:叢集 kubeconfig 檔案的路徑。
建立 VerticalPodAutoscaler 自訂資源
在叢集上啟用垂直 Pod 自動調度資源功能後,請定義VerticalPodAutoscaler自訂資源,以指定工作負載為目標:
在與目標工作負載相同的命名空間中,定義
VerticalPodAutoscaler資源。這個自訂資源會使用
targetRef和任何資源政策,指定目標 Pod。apiVersion: "autoscaling.k8s.io/v1" kind: VerticalPodAutoscaler metadata: name: hamster-vpa spec: targetRef: apiVersion: "apps/v1" kind: Deployment name: hamster resourcePolicy: containerPolicies: - containerName: '*' minAllowed: cpu: 100m memory: 50Mi maxAllowed: cpu: 1 memory: 500Mi controlledResources: ["cpu", "memory"]使用下列指令套用
VerticalPodAutoscaler資訊清單:kubectl apply -f VPA_MANIFEST \ --kubeconfig KUBECONFIG更改下列內容:
VPA_MANIFEST:VerticalPodAutoscaler資訊清單檔案的路徑。KUBECONFIG:叢集 kubeconfig 檔案的路徑。
瞭解垂直 Pod 自動調度資源模式
垂直 Pod 自動調度資源功能有不同模式,可控制資源建議的套用方式。
建議模式
在建議模式中,垂直 Pod 自動調度資源會安裝建議工具元件。這個元件會分析資源用量,並在您建立的 VerticalPodAutoscaler 自訂資源狀態部分,發布 CPU 和記憶體要求與限制的建議值。
如要查看資源要求和限制建議,請使用下列指令:
kubectl describe vpa VPA_NAME \
--kubeconfig KUBECONFIG \
-n CLUSTER_NAMESPACE
Replace the following:
* `VPA_NAME`: the name of the `VerticalPodAutoscaler`
that's targeting the workloads for which you are considering resource
adjustments.
* `KUBECONFIG`: the path of the cluster kubeconfig
file.
* `CLUSTER_NAMESPACE`: the name of the cluster that's
running vertical Pod autoscaling.
回覆內容應包含類似以下範例的 Status 區段:
Status:
Conditions:
Last Transition Time: 2025-08-04T23:53:32Z
Status: True
Type: RecommendationProvided
Recommendation:
Container Recommendations:
Container Name: hamster
Lower Bound:
Cpu: 100m
Memory: 262144k
Target:
Cpu: 587m
Memory: 262144k
Uncapped Target:
Cpu: 587m
Memory: 262144k
Upper Bound:
Cpu: 1
Memory: 500Mi
在這個模式下,Pod 不會自動更新。請根據這些建議手動更新 Pod 設定。如果未設定 enableUpdater 或設為 false,就會發生這種預設行為。
自動更新模式
設定 enableUpdaterenableUpdatertrue 時,除了建議工具,裸機生命週期控制器也會部署垂直 Pod 自動調度更新程式和准入控制器元件。更新程式會監控 Pod,如果目前的資源要求與建議值有顯著差異,就會採取行動。
VerticalPodAutoscaler 資源中的更新政策會指定更新程式套用建議的方式。根據預設,更新模式為 Auto,這表示更新程式會在建立 Pod 時指派更新的資源設定。以下 VerticalPodAutoscaler 範例說明如何將更新模式設為 Initial:
apiVersion: "autoscaling.k8s.io/v1"
kind: VerticalPodAutoscaler
metadata:
name: hamster-vpa
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: hamster
resourcePolicy:
updatePolicy:
updateMode: "Initial"
...
更新程式支援下列五種模式:
Auto:更新程式會逐出 Pod。准入控制器會攔截新 Pod 的建立要求,並修改要求,以使用建議工具提供的建議 CPU 和記憶體值。更新資源需要重新建立 Pod,這可能會導致中斷。使用更新程式會遵守的 Pod 中斷預算,管理驅逐程序。這個模式等同於Recreate。Recreate:更新程式會逐出 Pod,並在重新建立 Pod 時指派建議的資源要求和限制。InPlaceOrRecreate(alpha):更新程式會盡量執行就地更新,但如果無法就地更新,可能會改為重新建立 Pod。詳情請參閱就地調整 Pod 大小說明文件。Initial:更新程式只會在建立 Pod 時指派資源要求,之後不會再變更。Off:更新程式不會自動變更 Pod 的資源需求。建議會經過計算,並可在VerticalPodAutoscaler物件中檢查。
如要進一步瞭解 VerticalPodAutoscaler 自訂資源,請使用 kubectl 擷取安裝在 1.33.0 以上版本叢集中的 verticalpodautoscalercheckpoints.autoscaling.k8s.io 自訂資源定義。
下列範例顯示資源建議在 hamster 容器的「Status」部分中可能顯示的樣子。這個範例也會顯示 Pod 逐出事件的範例,當更新程式在自動將建議的資源設定指派給重新建立的 Pod 之前,逐出 Pod 時就會發生這類事件:
Spec:
Resource Policy:
Container Policies:
Container Name: *
Controlled Resources:
cpu
memory
Max Allowed:
Cpu: 1
Memory: 500Mi
Min Allowed:
Cpu: 100m
Memory: 50Mi
Target Ref:
API Version: apps/v1
Kind: Deployment
Name: hamster
Update Policy:
Update Mode: Auto
Status:
Conditions:
Last Transition Time: 2025-08-04T23:53:32Z
Status: True
Type: RecommendationProvided
Recommendation:
Container Recommendations:
Container Name: hamster
Lower Bound:
Cpu: 100m
Memory: 262144k
Target:
Cpu: 587m
Memory: 262144k
Uncapped Target:
Cpu: 587m
Memory: 262144k
Upper Bound:
Cpu: 1
Memory: 500Mi
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal EvictedPod 49s vpa-updater VPA Updater evicted Pod hamster-7cb59fb657-lkrk4 to apply resource recommendation.
記憶體節省模式
記憶體節省模式會減少垂直 Pod 自動調度資源建議元件的記憶體用量。將 enableMemorySaver 設定為 true 時,建議工具只會追蹤並計算具有相符 VerticalPodAutoscaler 自訂資源的 Pod 匯總。
但缺點是,為現有工作負載建立新的 VerticalPodAutoscaler 自訂資源時,建議工具需要一段時間 (最多 24 小時) 收集足夠的記錄,才能提供準確的建議。大多數叢集類型預設為 false 模式,但邊緣叢集預設為 true 模式。
將 Prometheus 做為永久記錄提供者
根據預設,建議事項元件會將叢集上執行的工作負載資源耗用記錄保留在記憶體中,並定期將狀態儲存至 etcd 中的 VerticalPodAutoscalerCheckpoint 自訂資源,以防重新啟動。
從 Google Distributed Cloud 1.34 版起,您可以將自己的 Prometheus 執行個體做為資源用量資料 (即 CPU 和記憶體用量指標) 的持續記錄供應商。啟用這項整合功能後,推薦工具會在啟動或重新啟動時查詢 Prometheus 伺服器,擷取所有受管理 Pod 的長期資源用量歷史資料。擷取這項資料後,推薦系統就能立即使用豐富的資料集建構內部狀態,從一開始就提供更明智且準確的建議。
使用 Prometheus 做為永久記錄供應商有下列優點:
最佳化資源使用率:一開始就會生成準確的建議,協助您最佳化叢集資源使用率。
避免記憶體不足 (OOM) 錯誤:Prometheus 無須將推薦工具的內部狀態儲存在
VerticalPodAutoscalerCheckpoint自訂資源 (CR) 中,因此可提高 ETCD 記憶體使用效率。當建議工具元件重新啟動時,會遺失記憶體內的歷來資料。使用 Prometheus 做為記錄供應商時,重新啟動後,建議工具會從 Prometheus 擷取歷來指標,因此不需要VerticalPodAutoscalerCheckpointCR,並可節省 ETCD 記憶體。
您隨時可以啟用及停用 Prometheus,做為持續性記錄提供者。
搭配垂直 Pod 自動調度資源功能使用 Prometheus 的先決條件
如要使用自己的 Prometheus 執行個體做為垂直 Pod 自動調度資源的記錄供應器,您必須設定該執行個體來擷取必要指標,包括下列步驟:
如有需要,請在叢集中部署 Prometheus Operator,以便將其做為垂直 Pod 自動調度的持續性記錄供應商。詳情請參閱「如何在 Kubernetes 中部署及設定 Prometheus 運算子」。
設定擷取指標的權限。
如要允許 Prometheus 使用設定檔從 cAdvisor 擷取指標,您必須將額外權限授予 Prometheus 伺服器使用的服務帳戶。建立或更新包含這些規則的
ClusterRole,並使用ClusterRoleBinding確保該規則繫結至正確的 Prometheus 服務帳戶:apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-role labels: app: prometheus-server rules: - apiGroups: [""] resources: - nodes verbs: - get - list - watch - apiGroups: [""] resources: - nodes/proxy - nodes/metrics verbs: - get --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: prometheus-binding labels: app: prometheus-server subjects: - kind: ServiceAccount name: prometheus-server # Service account being used by prometheus namespace: prometheus # Service account's namespace roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: prometheus-role # Name of the ClusterRole created above更新 Prometheus 設定檔,從 cAdvisor 擷取下列指標:
container_cpu_usage_seconds_totalcontainer_memory_working_set_bytes
下列幾行會定義 cAdvisor 指標的擷取詳細資料:
- job_name: 'kubernetes-cadvisor' scheme: https tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token kubernetes_sd_configs: - role: node relabel_configs: - action: labelmap regex: __meta_kubernetes_node_label_(.+) - target_label: __address__ replacement: kubernetes.default.svc:443 - source_labels: [__meta_kubernetes_node_name] regex: (.+) target_label: __metrics_path__ replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor metric_relabel_configs: # Keep only the metrics VPA uses to save disk space - source_labels: [__name__] regex: (container_cpu_usage_seconds_total|container_memory_working_set_bytes) action: keep更新 Prometheus 設定檔,從
kube-state-metrics服務中抓取下列指標:kube_pod_labels
在叢集上部署
kube-state-metricsService。您可以使用下列 Helm 指令安裝新服務:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo update建立
ksm-values.yaml檔案,並加入以下內容:fullnameOverride: vpa-kube-state-metrics metricAllowlist: - kube_pod_labels metricLabelsAllowlist: - "pods=[*]"根據上一步驟中的 values 檔案安裝 Helm 資訊套件:
helm install vpa-ksm prometheus-community/kube-state-metrics \ -f ksm-values.yaml --namespace kube-system在 Prometheus 設定檔中新增下列幾行,從已安裝的
kube-state-metrics服務抓取kube_pod_labels指標:- job_name: 'kube-state-metrics' static_configs: - targets: ['vpa-kube-state-metrics.kube-system.svc.cluster.local:8080'] metric_relabel_configs: - source_labels: [ __name__ ] regex: 'kube_pod_labels' action: keep
啟用及使用 Prometheus
垂直自動調度 Pod 資源功能支援基本驗證和持有人權杖驗證,可連線至 Prometheus。使用驗證時,您需要在叢集命名空間中建立包含必要憑證的 Secret。控制器會將這個 Secret 轉送至目標叢集,並在建議工具 Pod 中掛接為磁碟區或環境變數。您也可以在不驗證的情況下使用 Prometheus。
如要啟用並使用自己的 Prometheus 執行個體搭配 Pod 垂直自動調度資源功能,您需要在叢集規格中設定 verticalPodAutoscaling 區段,並提供連線至 Prometheus 執行個體的詳細資料。
以下是叢集規格中的設定範例,可搭配不記名權杖使用:
apiVersion: baremetal.cluster.gke.io/v1
kind: Cluster
metadata:
name: cluster1
namespace: cluster-cluster1
annotations:
preview.baremetal.cluster.gke.io/vertical-pod-autoscaler: enable
spec:
# ... other existing cluster configurations ...
verticalPodAutoscaling:
# ... other vertical Pod autoscaling configurations ...
# Add this new section to configure the vpa to use prometheus using bearer token authentication as history provider
prometheus:
url: "http://prometheus.prometheus.monitoring.svc.cluster.local:9090"
auth:
bearerTokenAuth:
name: prom-bearer-creds
key: bearertoken
如要啟用 Prometheus,以便搭配垂直 Pod 自動調度資源功能使用,請按照下列步驟操作:
請確認 Prometheus 執行個體已設定為抓取必要指標,如「搭配垂直 Pod 自動調度功能使用 Prometheus 的必要條件」一文所述。
更新叢集自訂資源
spec,讓verticalPodAutoscaling.prometheus欄位指定 Prometheus 伺服器的連線設定。將
url新增至prometheus區段,並設為完整網域名稱 (FQDN),以便從叢集內連線至 Prometheus:spec: # ... other existing cluster configurations ... verticalPodAutoscaling: # ... other vpa configurations ... # Add this new section to configure the vpa to use prometheus as history provider prometheus: # Required: The URL of the Prometheus server url: "http://prometheus.prometheus.svc.cluster.local:9090"指定連線詳細資料:
垂直 Pod 自動調度資源支援下列三種連線方法:
- 無驗證
- 基本驗證 (使用者名稱、密碼)
不記名權杖驗證
無驗證
如果 Prometheus 執行個體不需要驗證,您就完成了。
prometheus區段只能包含url欄位。基本驗證
請按照下列步驟,為 Prometheus 指定基本驗證:
在
stringData區段和baremetal.cluster.gke.io/mark-source: "true"註解中,建立包含使用者名稱和密碼的密鑰。以下範例顯示支援基本驗證的 Secret:
apiVersion: v1 kind: Secret metadata: name: prom-basic-creds namespace: <cluster-namespace> annotations: baremetal.cluster.gke.io/mark-source: "true" type: Opaque stringData: username: admin password: pwd註解是必要項目,可確保來源密鑰和目標叢集中的密鑰一律保持同步。來源密鑰更新時,系統會更新 Secret。
更新叢集規格的
prometheus.auth.basicAuth部分,以參照 Secret 中data欄位的使用者名稱和密碼。以下範例顯示
basicAuth區段,其中參照上一個步驟中 Secret 的使用者名稱和密碼:# ... other vpa configurations ... prometheus: url: "http://prometheus.prometheus.svc.cluster.local:9090" auth: basicAuth: usernameRef: name: prom-basic-creds key: username passwordRef: name: prom-basic-creds key: password使用者名稱和密碼必須位於同一個 Secret。金鑰必須是 Secret 的
data欄位中的有效金鑰。
更新叢集自訂資源後,Prometheus 執行個體應會開始運作,做為垂直 Pod 自動調度資源的記錄供應商。
不記名權杖驗證
請按照下列步驟,為 Prometheus 指定不記名權杖驗證:
在
stringData區段和baremetal.cluster.gke.io/mark-source: "true"註解中,建立包含不記名權杖的 Secret。以下範例顯示支援不記名權杖驗證的 Secret:
apiVersion: v1 kind: Secret metadata: name: prom-bearer-creds namespace: <cluster-namespace> annotations: baremetal.cluster.gke.io/mark-source: "true" type: Opaque stringData: bearertoken: "SAMPLE_TOKEN"註解是必要項目,可確保來源密鑰和目標叢集中的密鑰一律保持同步。來源密鑰更新時,系統會更新 Secret。
更新叢集規格的
prometheus.auth.bearerTokenAuth區段,以參照 Secret 中data欄位的承載權杖。以下範例顯示
bearerTokenAuth區段,其中參照上一個步驟中 Secret 的不記名權杖:# ... other vertical Pod autoscaling configurations ... prometheus: url: "http://prometheus.prometheus.svc.cluster.local:9090" auth: bearerTokenAuth: name: prom-bearer-creds key: bearertoken金鑰必須是 Secret 的
data欄位中的有效金鑰。
更新叢集自訂資源後,Prometheus 執行個體應會開始運作,做為垂直 Pod 自動調度資源的記錄供應商。
停用 Prometheus
如要停用搭配垂直 Pod 自動調度資源使用 Prometheus,請從叢集自訂資源的 verticalPodAutoscaling 區段中移除 prometheus 區段。
停用垂直 Pod 自動調度資源
如要停用垂直 Pod 自動調度資源,請從叢集中移除自訂資源和設定:
刪除您建立的任何
VerticalPodAutoscaler自訂資源。修改叢集自訂資源,並從
spec中移除整個verticalPodAutoscaling區段。您可以直接編輯叢集自訂資源,或修改叢集設定檔並使用
bmctl update。從叢集自訂資源中移除
preview.baremetal.cluster.gke.io/vertical-pod-autoscaler註解。
限制
使用垂直 Pod 自動調度資源功能時,請注意下列限制:
- 由於工作負載的實際記憶體用量瀏覽權限受到限制,因此垂直自動調度 Pod 資源功能尚未準備就緒,無法與以 JVM 為基礎的工作負載搭配使用。
- 更新程式至少需要兩個 Pod 副本,才能讓 Deployment 替換資源值經過修訂的 Pod。
- 如果 Pod 因記憶體不足 (OOM) 錯誤而進入當機迴圈,更新程式不會快速更新這些 Pod。
- Pod 的
InPlaceOrRecreate更新政策是垂直 Pod 自動調度資源的 Alpha 版功能。系統會盡量就地更新,但如果無法就地更新,可能會改為重新建立 Pod。