使用 Prometheus 監控 Config Sync

Config Sync 會自動收集及匯出指標,您可以使用 Prometheus 擷取這些指標,追蹤同步處理健康狀態、偵測協調錯誤,以及監控叢集資源用量。您可以直接在 Prometheus 中查看這些指標,也可以將指標提取至 Cloud Monitoring,做為自訂指標。詳情請參閱「Google Cloud Managed Service for Prometheus」。

如要瞭解如何直接將指標匯出至 Monitoring,而不使用 Prometheus,請參閱「使用 Cloud Monitoring 監控 Config Sync」。您也可以匯出自訂指標。

抓取指標

所有 Prometheus 指標都可在通訊埠 8675 抓取。您必須先透過下列兩種方式之一,為 Prometheus 設定叢集,才能抓取指標。以下任一項:

  • 按照 Prometheus 說明文件設定叢集,以便抓取資料,或

  • 使用 Prometheus Operator 和下列資訊清單,每 10 秒抓取所有 Config Sync 指標。

    1. 建立一個臨時目錄來保存資訊清單檔案。

      mkdir config-sync-monitor
      cd config-sync-monitor
      
    2. 使用 curl 指令,從 CoreOS 存放區下載 Prometheus Operator 資訊清單。

      curl -o bundle.yaml https://raw.githubusercontent.com/coreos/prometheus-operator/master/bundle.yaml
      

      這個資訊清單設為使用 default 命名空間,但我們不建議這麼做。下一步是修改設定,改用名為 monitoring 的命名空間。如要使用其他命名空間,請在後續步驟中將 monitoring 替換為該命名空間。

    3. 建立檔案,更新上述套件中 ClusterRoleBinding 的命名空間。

      # patch-crb.yaml
      apiVersion: rbac.authorization.k8s.io/v1
      kind: ClusterRoleBinding
      metadata:
        name: prometheus-operator
      subjects:
      - kind: ServiceAccount
        name: prometheus-operator
        namespace: monitoring # we are patching from default namespace
      
    4. 建立 kustomization.yaml 檔案,套用修補程式並修改資訊清單中其他資源的命名空間。

      # kustomization.yaml
      resources:
      - bundle.yaml
      
      namespace: monitoring
      
      patchesStrategicMerge:
      - patch-crb.yaml
      
    5. 如果沒有 monitoring 命名空間,請建立一個。您可以使用其他名稱,但如果這麼做,也請變更先前步驟中 YAML 資訊清單的 namespace 值。

      kubectl create namespace monitoring
      
    6. 使用下列指令套用 Kustomize 資訊清單:

      kubectl apply -k .
      
      until kubectl get customresourcedefinitions servicemonitors.monitoring.coreos.com ; \
      do date; sleep 1; echo ""; done

      第二個指令會封鎖,直到叢集上有 CRD 為止。

    7. 建立資源資訊清單,用於設定從 Config Sync 抓取指標的 Prometheus 伺服器。

      # config-sync-monitoring.yaml
      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: prometheus-config-sync
        namespace: monitoring
      ---
      apiVersion: rbac.authorization.k8s.io/v1
      kind: ClusterRole
      metadata:
        name: prometheus-config-sync
      rules:
      - apiGroups: [""]
        resources:
        - nodes
        - services
        - endpoints
        - pods
        verbs: ["get", "list", "watch"]
      - apiGroups: [""]
        resources:
        - configmaps
        verbs: ["get"]
      - nonResourceURLs: ["/metrics"]
        verbs: ["get"]
      ---
      apiVersion: rbac.authorization.k8s.io/v1
      kind: ClusterRoleBinding
      metadata:
        name: prometheus-config-sync
      roleRef:
        apiGroup: rbac.authorization.k8s.io
        kind: ClusterRole
        name: prometheus-config-sync
      subjects:
      - kind: ServiceAccount
        name: prometheus-config-sync
        namespace: monitoring
      ---
      apiVersion: monitoring.coreos.com/v1
      kind: Prometheus
      metadata:
        name: config-sync
        namespace: monitoring
        labels:
          prometheus: config-sync
      spec:
        replicas: 2
        serviceAccountName: prometheus-config-sync
        serviceMonitorSelector:
          matchLabels:
            prometheus: config-management
        alerting:
          alertmanagers:
          - namespace: default
            name: alertmanager
            port: web
        resources:
          requests:
            memory: 400Mi
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: prometheus-config-sync
        namespace: monitoring
        labels:
          prometheus: config-sync
      spec:
        type: NodePort
        ports:
        - name: web
          nodePort: 31900
          port: 9190
          protocol: TCP
          targetPort: web
        selector:
          prometheus: config-sync
      --- 
      --- 
      
    8. 使用下列指令套用資訊清單:

      kubectl apply -f config-sync.yaml
      
      until kubectl rollout status statefulset/prometheus-config-sync -n monitoring; \
      do sleep 1; done
      

      第二個指令會封鎖,直到 Pod 執行為止。

    9. 您可以將 Prometheus 伺服器的網路通訊埠轉送到本機電腦,確認是否順利安裝。

      kubectl -n monitoring port-forward svc/prometheus-config-sync 9190
      

      您現在可以透過 http://localhost:9190 存取 Prometheus 網頁 UI。

    10. 移除臨時目錄。

      cd ..
      rm -rf config-sync-monitor
      

可用的 Prometheus 指標

Config Sync 會收集下列指標,並提供給 Prometheus。「Labels」(標籤) 欄會列出適用於各項指標的所有標籤。不含標籤的指標代表一段時間內的單一測量值,而含有標籤的指標則代表多個測量值,每個標籤值組合各有一個測量值。

如果這個表格未同步,您可以在 Prometheus 使用者介面中依前置字元篩選指標。所有指標都以前置字串 config_sync_ 開頭。

名稱 類型 標籤 說明
config_sync_api_duration_seconds_bucket 直方圖 status, operation API 伺服器呼叫的延遲時間分布情形 (依每個週期的時間長度分組)
config_sync_api_duration_seconds_count 直方圖 status, operation API 伺服器呼叫的延遲時間分布 (忽略時間長度)
config_sync_api_duration_seconds_sum 直方圖 status, operation 所有 API 伺服器呼叫的時間長度總和
config_sync_apply_duration_seconds_bucket 直方圖 commit、status 將可靠資料來源宣告的資源套用至叢集的延遲時間分布情形 (依每個週期的時間長度分布至值區)
config_sync_apply_duration_seconds_count 直方圖 commit、status 將可靠資料來源中宣告的資源套用至叢集的延遲時間分布情形 (忽略時間長度)
config_sync_apply_duration_seconds_sum 直方圖 commit、status 將從可靠資料來源宣告的資源套用至叢集時,所有延遲時間的總和
config_sync_apply_operations_total 計數器 作業、狀態、控制器 已執行的作業數量,用於將資源從可靠資料來源同步至叢集
config_sync_cluster_scoped_resource_count 度量圖 resourcegroup ResourceGroup 中叢集範圍資源的數量
config_sync_crd_count 度量圖 resourcegroup ResourceGroup 中的 CRD 數量
config_sync_declared_resources 度量圖 commit 從 Git 剖析的宣告資源數量
config_sync_internal_errors_total 計數器 來源 Config Sync 觸發的內部錯誤數量。如果未發生內部錯誤,可能就不會顯示指標
config_sync_kcc_resource_count 度量圖 resourcegroup ResourceGroup 中的 Config Connector 資源數量
config_sync_last_apply_timestamp 度量圖 commit、status 最近一次套用作業的時間戳記
config_sync_last_sync_timestamp 度量圖 commit、status 上次從 Git 同步處理的時間戳記
config_sync_parser_duration_seconds_bucket 直方圖 status, trigger, source 從可靠資料來源同步至叢集時,各階段的延遲時間分布
config_sync_parser_duration_seconds_count 直方圖 狀態、觸發條件、來源 從可靠資料來源同步至叢集時,各階段的延遲時間分布情形 (不計時間長度)
config_sync_parser_duration_seconds_sum 直方圖 status, trigger, source 從可靠資料來源同步至叢集時,各階段延遲時間的總和
config_sync_pipeline_error_observed 度量圖 名稱、協調器、元件 RootSync 和 RepoSync 自訂資源的狀態。值為 1 表示失敗
config_sync_ready_resource_count 度量圖 resourcegroup ResourceGroup 中已就緒的資源總數
config_sync_reconcile_duration_seconds_bucket 直方圖 狀態 協調工具管理員處理的協調事件延遲時間分布情形 (依據每次呼叫的持續時間分布至值區)
config_sync_reconcile_duration_seconds_count 直方圖 狀態 協調工具管理員處理的協調事件延遲時間分布情形 (不計時間長度)
config_sync_reconcile_duration_seconds_sum 直方圖 狀態 協調工具管理員處理的所有協調事件延遲時間總和
config_sync_reconciler_errors 度量圖 component, errorclass 將資源從可靠資料來源同步至叢集時發生的錯誤數
config_sync_remediate_duration_seconds_bucket 直方圖 狀態 修復器協調事件的延遲時間分布情形 (依時間長度分組)
config_sync_remediate_duration_seconds_count 直方圖 狀態 修復器協調事件的延遲時間分布 (忽略時間長度)
config_sync_remediate_duration_seconds_sum 直方圖 狀態 修復器協調事件的所有延遲時間總和
config_sync_resource_count 度量圖 resourcegroup ResourceGroup 追蹤的資源數量
config_sync_resource_conflicts_total 計數器 commit 快取資源與叢集資源不相符,導致資源衝突的次數。如果未發生資源衝突,可能就不會顯示指標
config_sync_resource_fights_total 計數器 同步處理頻率過高的資源數量。如果沒有發生資源爭奪,可能就不會顯示指標
config_sync_resource_group_total 度量圖 ResourceGroup CR 數量
config_sync_resource_ns_count 度量圖 resourcegroup ResourceGroup 中資源使用的命名空間數量
config_sync_rg_reconcile_duration_seconds_bucket。 直方圖 stallreason ResourceGroup CR 協調時間分布 (依時間長度分組)
config_sync_rg_reconcile_duration_seconds_count 直方圖 stallreason 協調 ResourceGroup CR 的時間分布 (忽略時間長度)
config_sync_rg_reconcile_duration_seconds_sum 直方圖 stallreason ResourceGroup CR 協調時間總和
config_sync_kustomize_build_latency_bucket 直方圖 kustomize build 執行時間的延遲分布情形 (依各項作業的時間長度分布至值區)
config_sync_kustomize_build_latency_count 直方圖 kustomize build 執行時間的延遲時間分布情形 (不計時間長度)
config_sync_kustomize_build_latency_sum 直方圖 所有 kustomize build 執行時間的總和
config_sync_kustomize_ordered_top_tier_metrics 度量圖 top_tier_field 使用資源、產生器、SecretGenerator、ConfigMapGenerator、轉換器和驗證器
config_sync_kustomize_builtin_transformers 度量圖 k8s_builtin_transformer 與 Kubernetes 物件中繼資料相關的內建轉換器使用情形
config_sync_kustomize_resource_count 度量圖 kustomize build 輸出的資源數量
config_sync_kustomize_field_count 度量圖 field_name kustomization 檔案中使用特定欄位的次數
config_sync_kustomize_patch_count 度量圖 patch_field patches、patchesStrategicMerge 和 patchesJson6902 欄位中的修補程式數量
config_sync_kustomize_base_count 度量圖 base_source 遠端和本機基地數量
kustomize_deprecating_field_count 度量圖 deprecating_field 使用可能淘汰的欄位
kustomize_simplification_adoption_count 度量圖 simplification_field 簡化轉換器圖片、副本和替代項目的使用方式
kustomize_helm_inflator_count 度量圖 helm_inflator 在 kustomize 中使用 helm,無論是透過內建欄位或自訂函式

Prometheus 偵錯程序範例

以下範例說明如何使用 Prometheus 指標、物件狀態欄位和物件註解,偵測及診斷 Config Sync 相關問題。這些範例說明如何從高層級監控開始偵測問題,然後逐步精細調整搜尋範圍,深入探究並診斷問題的根本原因。

依狀態查詢設定

reconciler 程序提供高階指標,可讓您深入瞭解 Config Sync 在叢集上的整體運作情形。您可以查看是否發生任何錯誤,甚至可以設定錯誤快訊。

config_sync_reconciler_errors

依對帳工具查詢指標

如果您使用 Config Sync RootSync 和 RepoSync API,則可監控 RootSync 和 RepoSync 物件。RootSync 和 RepoSync 物件會透過高階指標進行檢測,讓您深入瞭解 Config Sync 在叢集上的運作方式。幾乎所有指標都會標記協調器名稱,因此您可以查看是否發生任何錯誤,並在 Prometheus 中設定快訊。

查看可用於篩選的指標標籤完整清單。

在 Prometheus 中,您可以對 RootSyncs 或 RepoSyncs 使用下列篩選器:

# Querying RootSync
config_sync_reconciler_errors{configsync_sync_name=ROOT_SYNC_NAME}

# Querying RepoSync
config_sync_reconciler_errors{configsync_sync_name=REPO_SYNC_NAME}

依狀態查詢匯入和同步作業

在 Prometheus 中,您可以使用下列查詢:

# Check for errors that occurred when sourcing configs.
config_sync_reconciler_errors{component="source"}

# Check for errors that occurred when syncing configs to the cluster.
config_sync_reconciler_errors{component="sync"}

您也可以查看來源和同步處理程序本身的指標:

config_sync_parser_duration_seconds{status="error"}
config_sync_apply_duration_seconds{status="error"}
config_sync_remediate_duration_seconds{status="error"}

使用 Google Cloud Managed Service for Prometheus 監控資源

Google Cloud Managed Service for Prometheus 是 Google Cloud的全代管多雲端解決方案,適用於 Prometheus 指標。支援兩種資料收集模式:代管收集 (建議模式) 或自行部署資料收集。請按照下列步驟,在代管收集模式下,設定使用 Google Cloud Managed Service for Prometheus 監控 Config Sync。

  1. 按照「設定代管收集功能」的操作說明,在叢集啟用 Managed Prometheus。

  2. 將下列範例資訊清單儲存為 pod-monitoring-config-sync-monitoring.yaml。這個資訊清單會設定 PodMonitoring 資源,在 config-management-monitoring 命名空間下 otel-collector-* Pod 的 8675 通訊埠上,擷取 Config Sync 指標。PodMonitoring 資源會使用 Kubernetes 標籤選取器,找出 otel-collector-* Pod。

    apiVersion: monitoring.googleapis.com/v1
    kind: PodMonitoring
    metadata:
      name: config-sync-monitoring
      namespace: config-management-monitoring
    spec:
      selector:
        matchLabels:
          app: opentelemetry
          component: otel-collector
      endpoints:
      - port: 8675
        interval: 10s
    
  3. 將資訊清單套用至叢集:

    kubectl apply -f pod-monitoring-config-sync-monitoring.yaml
    

  4. 請按照「Cloud Monitoring 中的 Managed Service for Prometheus 資料」一文的操作說明,在 Google Cloud 控制台的 Cloud Monitoring Metrics Explorer 頁面,確認 Prometheus 資料是否已匯出。

後續步驟