View a markdown version of this page

擷取採用 Prometheus 格式的控制平面原始指標 - Amazon EKS

協助改進此頁面

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

若要為本使用者指南貢獻內容,請點選每個頁面右側面板中的在 GitHub 上編輯此頁面連結。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

擷取採用 Prometheus 格式的控制平面原始指標

Kubernetes 控制平面公開眾多採用 Prometheus 格式的指標。這些指標對於監視和分析非常有用。透過指標端點在內部公開這些指標,並且可在完全不部署 Prometheus 的情況下存取。然而,部署 Prometheus 可更輕鬆地分析一段時間內的指標。

如需檢視原始指標輸出,可取代 endpoint,然後執行以下命令。

kubectl get --raw endpoint

此命令可讓您傳遞任何端點路徑並傳回原始回應。輸出會逐行列示不同的指標,每一行都包括指標名稱、標籤和值。

metric_name{tag="value"[,...]} value

透過 API 伺服器擷取指標

一般 API 伺服器端點在 Amazon EKS 控制平面公開。此端點主要在查看特定指標時有用。

kubectl get --raw /metrics

範例輸出如下。

[...] # HELP rest_client_requests_total Number of HTTP requests, partitioned by status code, method, and host. # TYPE rest_client_requests_total counter rest_client_requests_total{code="200",host="127.0.0.1:21362",method="POST"} 4994 rest_client_requests_total{code="200",host="127.0.0.1:443",method="DELETE"} 1 rest_client_requests_total{code="200",host="127.0.0.1:443",method="GET"} 1.326086e+06 rest_client_requests_total{code="200",host="127.0.0.1:443",method="PUT"} 862173 rest_client_requests_total{code="404",host="127.0.0.1:443",method="GET"} 2 rest_client_requests_total{code="409",host="127.0.0.1:443",method="POST"} 3 rest_client_requests_total{code="409",host="127.0.0.1:443",method="PUT"} 8 # HELP ssh_tunnel_open_count Counter of ssh tunnel total open attempts # TYPE ssh_tunnel_open_count counter ssh_tunnel_open_count 0 # HELP ssh_tunnel_open_fail_count Counter of ssh tunnel failed open attempts # TYPE ssh_tunnel_open_fail_count counter ssh_tunnel_open_fail_count 0

此原始輸出會逐字傳回 API 伺服器公開的內容。

透過 metrics.eks.amazonaws.com 擷取控制平面指標

若是 Kubernetes 1.28 版及更新版本的叢集,Amazon EKS 亦會在 API 群組 metrics.eks.amazonaws.com 項下公開指標。這些指標包括 kube-schedulerkube-controller-manager 等控制平面元件。

注意

若您的 Webhook 組態可能會阻止在叢集上建立新的 APIService 資源 v1.metrics.eks.amazonaws.com,則可能無法使用指標端點功能。您可藉由搜尋 v1.metrics.eks.amazonaws.com 關鍵字,在 kube-apiserver 稽核日誌中進行確認。

擷取 kube-scheduler 指標

如需擷取 kube-scheduler 指標,請使用以下命令。

kubectl get --raw "/apis/metrics.eks.amazonaws.com/v1/ksh/container/metrics"

範例輸出如下。

# TYPE scheduler_pending_pods gauge scheduler_pending_pods{queue="active"} 0 scheduler_pending_pods{queue="backoff"} 0 scheduler_pending_pods{queue="gated"} 0 scheduler_pending_pods{queue="unschedulable"} 18 # HELP scheduler_pod_scheduling_attempts [STABLE] Number of attempts to successfully schedule a pod. # TYPE scheduler_pod_scheduling_attempts histogram scheduler_pod_scheduling_attempts_bucket{le="1"} 79 scheduler_pod_scheduling_attempts_bucket{le="2"} 79 scheduler_pod_scheduling_attempts_bucket{le="4"} 79 scheduler_pod_scheduling_attempts_bucket{le="8"} 79 scheduler_pod_scheduling_attempts_bucket{le="16"} 79 scheduler_pod_scheduling_attempts_bucket{le="+Inf"} 81 [...]

由於傳回的資料量可能很大,因此有額外的kube-scheduler指標端點可供擷取 kube_pod_resource_requestkube_pod_resource_limit指標。若要擷取這些指標,請使用下列命令。

kubectl get --raw "/apis/metrics.eks.amazonaws.com/v1/ksh/container/resourcemetrics"

範例輸出如下。

# HELP kube_pod_resource_limit [STABLE] Resources limit for workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any. # TYPE kube_pod_resource_limit gauge kube_pod_resource_limit{namespace="kube-system",node="ip-192-168-87-3.us-west-2.compute.internal",pod="coredns-7bf648ff5d-dj4ss",priority="2000000000",resource="memory",scheduler="default-scheduler",unit="bytes"} 1.7825792e+08 # HELP kube_pod_resource_request [STABLE] Resources requested by workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any. # TYPE kube_pod_resource_request gauge kube_pod_resource_request{namespace="kube-system",node="ip-192-168-87-3.us-west-2.compute.internal",pod="aws-node-x7znh",priority="2000001000",resource="cpu",scheduler="default-scheduler",unit="cores"} 0.05 kube_pod_resource_request{namespace="kube-system",node="ip-192-168-87-3.us-west-2.compute.internal",pod="coredns-7bf648ff5d-dj4ss",priority="2000000000",resource="cpu",scheduler="default-scheduler",unit="cores"} 0.1 [...]

擷取 kube-controller-manager 指標

如需擷取 kube-controller-manager 指標,請使用以下命令。

kubectl get --raw "/apis/metrics.eks.amazonaws.com/v1/kcm/container/metrics"

範例輸出如下。

[...] workqueue_work_duration_seconds_sum{name="pvprotection"} 0 workqueue_work_duration_seconds_count{name="pvprotection"} 0 workqueue_work_duration_seconds_bucket{name="replicaset",le="1e-08"} 0 workqueue_work_duration_seconds_bucket{name="replicaset",le="1e-07"} 0 workqueue_work_duration_seconds_bucket{name="replicaset",le="1e-06"} 0 workqueue_work_duration_seconds_bucket{name="replicaset",le="9.999999999999999e-06"} 0 workqueue_work_duration_seconds_bucket{name="replicaset",le="9.999999999999999e-05"} 19 workqueue_work_duration_seconds_bucket{name="replicaset",le="0.001"} 109 workqueue_work_duration_seconds_bucket{name="replicaset",le="0.01"} 139 workqueue_work_duration_seconds_bucket{name="replicaset",le="0.1"} 181 workqueue_work_duration_seconds_bucket{name="replicaset",le="1"} 191 workqueue_work_duration_seconds_bucket{name="replicaset",le="10"} 191 workqueue_work_duration_seconds_bucket{name="replicaset",le="+Inf"} 191 workqueue_work_duration_seconds_sum{name="replicaset"} 4.265655885000002 [...]

了解排程器與控制器管理員指標

下面的資料表列示了可用於 Prometheus 樣式湊集的排程器與控制器管理員指標。若要了解這些指標的相關詳細資訊,請參閱 Kubernetes 文件中的 Kubernetes 指標參考

指標 控制平面元件 Description

scheduler_pending_pods

排程器

等候排程到節點上執行的 Pod 數目。

scheduler_schedule_attempts_total

排程器

排程 Pod 的嘗試次數。

scheduler_preemption_attempts_total

排程器

排程器藉由移出較低優先級 Pod 以排程較高優先級 Pod 的嘗試次數。

scheduler_preemption_victims

排程器

已經選取要移出,以便為較高優先級 Pod 騰出空間的 Pod 數目。

scheduler_pod_scheduling_attempts

排程器

已成功排程 Pod 的嘗試次數。

scheduler_scheduling_attempt_duration_seconds

排程器

指示排程器能夠依據資源可用性及排程規則等各種因素,尋找執行 Pod 的適當位置的速度快慢程度。

scheduler_pod_scheduling_sli_duration_seconds

排程器

自 Pod 進入排程佇列開始,正在排程的 Pod 的端對端延遲。這可能會涉及多次排程嘗試。

kube_pod_resource_limit

排程器

叢集上工作負載的資源限制,依 Pod 細分。這會顯示排程器和 kubelet 針對資源每個 Pod 預期的資源用量,以及如果有的話的資源單位。

kube_pod_resource_request

排程器

叢集上工作負載請求的資源,依 Pod 細分。這會顯示排程器和 kubelet 針對資源每個 Pod 預期的資源用量,以及如果有的話的資源單位。

cronjob_controller_job_creation_skew_duration_seconds

控制器管理員

排程執行 cronjob 的時間,及建立相應任務的時間。

workqueue_depth

控制器管理員

佇列目前的深度。

workqueue_adds_total

控制器管理員

工作佇列處理的新增項目總數。

workqueue_queue_duration_seconds

控制器管理員

發出請求之前,項目保持在工作佇列的時間 (秒)。

workqueue_work_duration_seconds

控制器管理員

處理工作佇列中的項目所需的時間 (秒)。

部署 Prometheus 湊集器以持續湊集指標

若要部署 Prometheus 湊集器以持續湊集指標,請使用下列組態:

--- apiVersion: v1 kind: ConfigMap metadata: name: prometheus-conf data: prometheus.yml: |- global: scrape_interval: 30s scrape_configs: # apiserver metrics - job_name: apiserver-metrics kubernetes_sd_configs: - role: endpoints scheme: https tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt insecure_skip_verify: true bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token relabel_configs: - source_labels: [ __meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name, ] action: keep regex: default;kubernetes;https # Scheduler metrics - job_name: 'ksh-metrics' kubernetes_sd_configs: - role: endpoints metrics_path: /apis/metrics.eks.amazonaws.com/v1/ksh/container/metrics scheme: https tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt insecure_skip_verify: true bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token relabel_configs: - source_labels: [ __meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name, ] action: keep regex: default;kubernetes;https # Controller Manager metrics - job_name: 'kcm-metrics' kubernetes_sd_configs: - role: endpoints metrics_path: /apis/metrics.eks.amazonaws.com/v1/kcm/container/metrics scheme: https tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt insecure_skip_verify: true bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token relabel_configs: - source_labels: [ __meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name, ] action: keep regex: default;kubernetes;https --- apiVersion: v1 kind: Pod metadata: name: prom-pod spec: containers: - name: prom-container image: prom/prometheus ports: - containerPort: 9090 volumeMounts: - name: config-volume mountPath: /etc/prometheus/ volumes: - name: config-volume configMap: name: prometheus-conf

Pod 存取全新的指標端點需要下列許可。

{ "effect": "allow", "apiGroups": [ "metrics.eks.amazonaws.com" ], "resources": [ "kcm/metrics", "ksh/metrics" ], "verbs": [ "get" ] },

若要修補使用的角色,您可使用以下命令。

kubectl patch clusterrole <role-name> --type=json -p='[ { "op": "add", "path": "/rules/-", "value": { "verbs": ["get"], "apiGroups": ["metrics.eks.amazonaws.com"], "resources": ["kcm/metrics", "ksh/metrics"] } } ]'

然後,您可用 Prometheus 湊集器的連接埠代理本機連接埠,以檢視 Prometheus 儀表板。

kubectl port-forward pods/prom-pod 9090:9090

若是 Amazon EKS 叢集,核心 Kubernetes 控制平面指標亦可擷取到 AWS/EKS 命名空間項下的 Amazon CloudWatch 指標。如需檢視這些指標,開啟 CloudWatch 主控台,然後從左側導覽窗格中選取全部指標。在指標選取頁面上,選擇叢集的 AWS/EKS 命名空間與指標維度。