共用方式為


使用 ConfigMap 從 Kubernetes 叢集建立自訂 Prometheus 抓取作業

自訂 Kubernetes 叢集中的 Prometheus 指標集合說明如何使用 ConfigMap 自訂從 Kubernetes 叢集中的預設目標抓取的 Prometheus 指標。 本文說明如何使用 ConfigMap 建立自訂抓取作業,以進一步自訂和其他目標。

ConfigMaps

下表說明用來建立自訂抓取作業的 ConfigMap。 啟用受管理 Prometheus 時,這些 ConfigMap 預設不會存在於叢集中。

ConfigMap Description
ama-metrics-prometheus-config推薦 建立具有此名稱的 ConfigMap 時,其中定義的抓取作業會從叢集中執行的 Azure 監視器計量複本 Pod 執行。
ama-metrics-prometheus-config-node進階 提供 Prometheus 抓取設定給在叢集中的每個 Linux 節點上執行的附加元件 DaemonSet,以及每個節點上的任何節點層級目標。 請參閱 進階設定
ama-metrics-prometheus-config-node-windows進階 提供 Prometheus 抓取設定給在叢集中的每個 Windows 節點上執行的附加元件 DaemonSet,以及每個節點上的任何節點層級目標。 請參閱 進階設定

建立 Prometheus 設定檔

與直接修改 ama-metrics-prometheus-config相比,建立設定檔然後將其轉換為 ConfigMap 會更容易。 請參閱下面的 抓取配置設置 ,以了解有關此文件不同部分的詳細信息。

建立一個 Prometheus 抓取設定檔 prometheus-config ,使用以下格式命名。 這會列出區段 scrape_configs 區段下的抓取設定,並可選擇使用全域區段來設定全域 scrape_intervalscrape_timeoutexternal_labels。 請參閱 Prometheus.io 抓取設定參考 ,以取得抓取設定選項的完整詳細資訊。

global:
  scrape_interval: <duration>
  scrape_timeout: <duration>
  external_labels:
    <labelname1>: <labelvalue>
    <labelname2>: <labelvalue>
scrape_configs:
  - <job-x>
  - <job-y>

以下是 Prometheus 抓取設定檔範例:

global:
  scrape_interval: 30s
scrape_configs:
- job_name: my_static_config
  scrape_interval: 60s
  static_configs:
    - targets: ['my-static-service.svc.cluster.local:1234']
- job_name: prometheus_example_app
  scheme: http
  kubernetes_sd_configs:
    - role: service
  relabel_configs:
    - source_labels: [__meta_kubernetes_service_name]
      action: keep
      regex: "prometheus-example-service"

小提示

請參閱適用於 Kubernetes 叢集的抓取設定 Prometheus 範例

驗證抓取設定檔

代理程式會使用自訂 promconfigvalidator 工具來驗證透過 ConfigMap 提供給它的 Prometheus 組態。 如果組態無效,則客服專員會拒絕自訂組態。 建立 Prometheus 組態檔後,您可以使用此工具來驗證您的組態,然後再為代理程式建立 ConfigMap。

promconfigvalidator 工具會隨附於 Azure 監視器計量附加元件 Pod 內。 您可以使用叢集中命名空間中ama-metrics-node-*的任何 kube-system Pod 來下載工具進行驗證。 執行下列命令,使用 kubectl cp 下載工具及其設定。

for podname in $(kubectl get pods -l rsName=ama-metrics -n=kube-system -o json | jq -r '.items[].metadata.name'); do kubectl cp -n=kube-system "${podname}":/opt/promconfigvalidator ./promconfigvalidator;  kubectl cp -n=kube-system "${podname}":/opt/microsoft/otelcollector/collector-config-template.yml ./collector-config-template.yml; chmod 500 promconfigvalidator; done

複製可執行檔和 yaml 後,找到 Prometheus 配置文件的路徑。 然後替換 <config path> 命令並使用以下命令運行驗證器。

./promconfigvalidator/promconfigvalidator --config "<config path>" --otelTemplate "./promconfigvalidator/collector-config-template.yml"

如果未提供選擇性merged-otel-config.yaml參數的路徑,則執行驗證器會產生合併的組態檔output。 請勿使用此自動產生的合併檔案,因為它僅用於工具驗證和偵錯目的。

將設定檔部署為 ConfigMap

您的自訂 Prometheus 組態檔會在 kube-system 命名空間的 ama-metrics-prometheus-configama-metrics-prometheus-config-nodeama-metrics-prometheus-config-node-windows ConfigMap 內作為欄位使用,名稱為 prometheus-config。 從抓取組態檔建立 configmap,方法是將 Prometheus 設定檔重新命名為 prometheus-config (不含副檔名),然後根據您想要為自訂抓取作業設定建立的 ConfigMap,執行下列一或多個範例命令。

建立 ConfigMap 以供 replicaset 使用:

kubectl create ConfigMap ama-metrics-prometheus-config --from-file=prometheus-config -n kube-system

這會在kube-system命名空間中建立一個名為ama-metrics-prometheus-config的 ConfigMap。 若要查看設定驗證、處理或合併是否有任何問題,您可以查看 ama-metrics 複本 Pod

建立要供 Linux DaemonSet 使用的 ConfigMap:

kubectl create ConfigMap ama-metrics-prometheus-config-node --from-file=prometheus-config -n kube-system

這會在kube-system命名空間中建立名為ama-metrics-prometheus-config-node的 ConfigMap。 若要查看設定驗證、處理或合併是否有任何問題,您可以查看 ama-metrics-node Linux DeamonSet Pod

建立要供 Windows DaemonSet 使用的 ConfigMap

kubectl create ConfigMap ama-metrics-prometheus-config-node-windows --from-file=prometheus-config -n kube-system

這會在kube-system命名空間中建立一個名為ama-metrics-prometheus-config-node-windows的 ConfigMap。 若要查看設定驗證、處理或合併是否有任何問題,您可以查看 ama-metrics-win-node Windows DeamonSet Pod

故障排除

如果您已在 kube-system 命名空間中成功建立 ConfigMap,但仍看不見系統抓取的自訂目標,請使用 kubectl logsama-metrics-prometheus-config ConfigMap 的複本 Pod 記錄中檢查錯誤,或是在 ama-metrics-prometheus-config-node ConfigMap 的DaemonSet Pod 中檢查錯誤,並確定 Start Merging Default and Custom Prometheus Config 區段中沒有具有 prometheus-config-merger 前置詞的任何錯誤

抓取設定

目前,scrape config 支援的目標探索方法為 static_configskubernetes_sd_configs,用於指定或探索目標。

靜態設定具有靜態目標清單,以及要新增至其中的任何額外標籤,如下所示。

scrape_configs:
  - job_name: example
    - targets: [ '10.10.10.1:9090', '10.10.10.2:9090', '10.10.10.3:9090' ... ]
    - labels: [ label1: value1, label1: value2, ... ]

使用 kubernetes_sd_configs 探索的目標會視指定的角色而有不同的 __meta_* 標籤。 您可以在 relabel_configs 區段使用這些標籤,以篩選目標或取代目標的標籤。

重新指派標籤設定

relabel_configs 區段會在目標探索時套用,並套用至作業的每個目標。 下列範例示範使用 relabel_configs 的方式。

新增標籤將名為該值example_label的新標籤example_value新增至任務的每個指標。 請只使用 __address__ 做為來源標籤,因為該標籤存在,並新增作業每個目標的標籤。

relabel_configs:
- source_labels: [__address__]
  target_label: example_label
  replacement: 'example_value'

使用 Kubernetes 服務探索標籤

如果作業使用 kubernetes_sd_configs 來探索目標,則每個角色對於計量皆有相關的 __meta_* 標籤。 探索目標之後,會卸除 __* 標籤。 若要在計量層級篩選這些標籤,請先指派標籤名稱,使其維持使用 relabel_configs。 然後使用 metric_relabel_configs 來篩選。

# Use the kubernetes namespace as a label called 'kubernetes_namespace'
relabel_configs:
- source_labels: [__meta_kubernetes_namespace]
  action: replace
  target_label: kubernetes_namespace

# Keep only metrics with the kubernetes namespace 'default'
metric_relabel_configs:
- source_labels: [kubernetes_namespace]
  action: keep
  regex: 'default'

任務和例項重新標籤

您可根據來源標籤變更 jobinstance 標籤值,就像任何其他標籤一樣。

# Replace the job name with the pod label 'k8s app'
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_k8s_app]
  target_label: job

# Replace the instance name with the node name. This is helpful to replace a node IP
# and port with a value that is more readable
relabel_configs:
- source_labels: [__meta_kubernetes_node_name]
  target_label: instance

計量重新指派標籤設定

抓取之後和擷取之前,會套用計量重新指派標籤設定。 使用 metric_relabel_configs 區段以篩選抓取後的計量。 請參閱下列範例。

依名稱移除指標

# Drop the metric named 'example_metric_name'
metric_relabel_configs:
- source_labels: [__name__]
  action: drop
  regex: 'example_metric_name'

僅按名稱保留特定指標

# Keep only the metric named 'example_metric_name'
metric_relabel_configs:
- source_labels: [__name__]
  action: keep
  regex: 'example_metric_name'
# Keep only metrics that start with 'example_'
metric_relabel_configs:
- source_labels: [__name__]
  action: keep
  regex: '(example_.*)'

依標籤篩選指標

# Keep metrics only where example_label = 'example'
metric_relabel_configs:
- source_labels: [example_label]
  action: keep
  regex: 'example'
# Keep metrics only if `example_label` equals `value_1` or `value_2`
metric_relabel_configs:
- source_labels: [example_label]
  action: keep
  regex: '(value_1|value_2)'
# Keep metrics only if `example_label_1 = value_1` and `example_label_2 = value_2`
metric_relabel_configs:
- source_labels: [example_label_1, example_label_2]
  separator: ';'
  action: keep
  regex: 'value_1;value_2'
# Keep metrics only if `example_label` exists as a label
metric_relabel_configs:
- source_labels: [example_label_1]
  action: keep
  regex: '.+'

重新命名計量 不支援計量重新命名。

備註

如果您想要將標籤新增至自訂組態中的所有工作,請為每個工作明確使用 metrics_relabel_configs 來新增標籤。 不支援使用以 ConfigMap 為基礎的 Prometheus 組態的全域外部標籤。

relabel_configs:
- source_labels: [__address__]
  target_label: example_label
  replacement: 'example_value'

基本驗證與持有人權杖

如果您在抓取配置中使用使用者名稱、密碼或憑證作為純文字,則不需要進行其他變更。 組態中指定的值將用於抓取。 如果您在 Prometheus 組態中使用 username_filepassword_file(或任何 _file 設定)來進行 basic_authbearer_token 設定,請遵循下列步驟:

  1. 在名為 kube-systemama-metrics-mtls-secret 命名空間中建立祕密。

    機碼 password1 的名稱可以是任何項目,惟其必須符合下一個步驟中 Prometheus 抓取組態 password_file 檔案路徑中的檔案名稱。 該機碼的值必須是 base64 編碼值。

    apiVersion: v1
    kind: Secret
    metadata:
      name: ama-metrics-mtls-secret
      namespace: kube-system
    type: Opaque
    data:
      password1: <base64-encoded-string>
    

    ama-metrics-mtls-secret 祕密會掛接至位於路徑 ama-metrics/etc/prometheus/certs/ Pod 上,並可供 Prometheus 抓取工具使用。 機碼 (上面範例中的 password1) 將會是檔案名稱。 值會進行 base64 解碼,並新增為容器內檔案的內容。

  2. 在 ConfigMap 的自訂抓取設定中提供檔案路徑:

    基本授權 欄位應 username 包含實際的使用者名稱字串。 password_file 欄位應該包含包含密碼的檔案路徑。

    # Sets the `Authorization` header on every scrape request with the
    # configured username and password.
    basic_auth:
      username: <username string>
      password_file: /etc/prometheus/certs/password1
    

    承載者令牌 欄位應 bearer_token_file 包含權杖所在檔案的路徑。

    # Sets the `Authorization` header on every scrape request with the bearer token
    # read from the configured file. It is mutually exclusive with `bearer_token`.
    bearer_token_file: /etc/prometheus/certs/password1
    

如需這些設定的詳細資訊,請參閱 Prometheus scrape_config 文件

TLS 型抓取

如果您想要從 https 端點抓取 Prometheus 計量,Prometheus 組態、PodMonitor 或 ServiceMonitor 應該已將 scheme 設定為 https 與額外的 TLS 設定。

  1. 在名為 kube-systemama-metrics-mtls-secret 命名空間中建立祕密。 在祕密物件之資料區段中指定的每個機碼值組,都會掛接為此 /etc/prometheus/certs 位置中的個別檔案,且檔案名稱與資料區段中指定的機碼相同。 祕密值應該以 base64 編碼。

    以下是秘密的 YAML 範例:

    apiVersion: v1
    kind: Secret
    metadata:
      name: ama-metrics-mtls-secret
      namespace: kube-system
    type: Opaque
    data:
      <certfile>: base64_cert_content    
      <keyfile>: base64_key_content 
    

    ama-metrics-mtls-secret 祕密會掛接至位於路徑 ama-metrics/etc/prometheus/certs/ Pod 上,並可供 Prometheus 抓取工具使用。 索引鍵將是檔案名稱。 值會進行 base64 解碼,並新增為容器內檔案的內容。

  2. 在 Prometheus 組態、PodMonitor 或 ServiceMonitor 中提供檔案路徑:

    • 使用下列範例在 ConfigMap 中提供 TLS 組態設定:
    tls_config:
       # CA certificate to validate API server certificate with.
       ca_file: /etc/prometheus/certs/<certfile>
    
       # Certificate and key files for client cert authentication to the server.
       cert_file: /etc/prometheus/certs/<certfile>
       key_file: /etc/prometheus/certs/<keyfile>
    
       # Disable validation of the server certificate.
       insecure_skip_verify: false
    

基本驗證和 TLS

如果您想要在 ConfigMap 中使用基本驗證或持有人權杖 (檔案型認證) 和 TLS 驗證設定,請確定密碼 ama-metrics-mtls-secret 包含資料區段下的所有金鑰及其對應的 base64 編碼值,如下列範例所示:

apiVersion: v1
kind: Secret
metadata:
  name: ama-metrics-mtls-secret
  namespace: kube-system
type: Opaque
data:
  certfile: base64_cert_content    # used for TLS
  keyfile: base64_key_content      # used for TLS
  password1: base64-encoded-string # used for basic auth
  password2: base64-encoded-string # used for basic auth

備註

/etc/prometheus/certs/ 路徑是必要項目,但 password1 可以是任何字串,而且必須符合上面所建立祕密中資料的機碼。 這是因為祕密 ama-metrics-mtls-secret 會掛接在容器內的路徑 /etc/prometheus/certs/ 中。

當祕密掛接為檔案時,ama-metrics Pod 會自動將 base64 編碼值解碼。 請確保 Secret 名稱是 ama-metrics-mtls-secret 並且在 kube-system 命名空間中。

應先建立密碼,然後應在命名空間中 kube-system 建立 ConfigMap、PodMonitor 或 ServiceMonitor。 祕密建立的順序很重要。 當不存在密鑰,而存在指向該密鑰的 ConfigMap、PodMonitor 或 ServiceMonitor 時,ama-metrics prometheus-collector 容器日誌中會出現以下錯誤:no file found for cert....

如需 TLS 組態設定的詳細資訊,請參閱 tls_config

進階設定:為 DaemonSet 設定自訂 Prometheus 抓取作業

ama-metrics 複本 Pod 會取用自訂的 Prometheus 設定,並抓取指定的目標。 對於具有大量節點和 Pod 以及要抓取大量指標的叢集,可以將一些適用的自訂抓取目標從單一 ama-metrics 複本 Pod 卸載到 ama-metrics DaemonSet Pod。

ama-metrics-prometheus-config-node ConfigMap 類似複本集 ConfigMap,建立後可以在每個節點上指定靜態抓取設定。 抓取設定應該只以單一節點為目標,不應使用服務探索/Pod 註釋。 否則,每個節點都會嘗試抓取所有目標,並對 Kubernetes API 伺服器進行多次呼叫。

自訂抓取目標可以遵循相同的格式,使用static_configs與目標結合,並利用環境變數$NODE_IP來指定要抓取的埠。 每個 DaemonSet Pod 將採用設定、抓取計量,並為該節點傳送計量。

下列 node-exporter 組態是 DaemonSet Pod 的預設目標之一。 它使用 $NODE_IP 環境變數,該變數已針對每個 ama-metrics 附加元件容器設定,以節點上的特定連接埠為目標。

- job_name: nodesample
  scrape_interval: 30s
  scheme: http
  metrics_path: /metrics
  relabel_configs:
  - source_labels: [__metrics_path__]
    regex: (.*)
    target_label: metrics_path
  - source_labels: [__address__]
    replacement: '$NODE_NAME'
    target_label: instance
  static_configs:
  - targets: ['$NODE_IP:9100']

抓取組態設定

以下各節說明 ConfigMap 中使用的 Prometheus 組態檔中支援的設定。 請參閱 Prometheus 組態參考, 以取得這些設定的詳細資訊。

全域設定

全域設定的組態格式與 OSS prometheus 組態 (英文) 支援的格式相同

global:
  scrape_interval: <duration>
  scrape_timeout: <duration>
  external_labels:
    <labelname1>: <labelvalue>
    <labelname2>: <labelvalue>
scrape_configs:
  - <job-x>
  - <job-y>

全域區段中提供的設定會套用至所有抓取作業 (Configmap 和 Custom 資源中的作業),但如果在個別作業中指定,則會覆寫這些設定。

備註

如果您想使用適用於所有抓取作業的全域設定,並且只有 自訂資源 ,您仍然需要僅使用全域設定建立 ConfigMap(自訂資源中每個設定的設定將覆蓋全域部分中的設定)

後續步驟