Google Distributed Cloud (GDC) 實體隔離方案的記錄收集功能,可讓您匯總 Kubernetes 工作流程的作業事件,以便監控及疑難排解。在管理 API 伺服器中部署 LoggingTarget 自訂資源,從 Kubernetes worker 節點虛擬機器 (VM) 提取、建立索引及標記專案記錄。這個管道不支援從獨立使用者 VM 收集記錄檔。
設定記錄收集功能後,您可以在 Grafana 中或透過 Log Query API 查詢及查看記錄。如需檢測建議,請參閱 Kubernetes 記錄指南。
事前準備
如要取得管理 LoggingTarget 自訂資源所需的權限,請要求 IAM 機構組織管理員或 IAM 專案管理員授予您其中一個相關聯的 LoggingTarget 角色。
視所需的存取權和權限層級而定,您可能會在組織或專案中取得這項資源的建立者、編輯者或檢視者角色。詳情請參閱「準備 IAM 權限」。
設定 LoggingTarget 自訂資源
LoggingTarget 自訂資源會指示記錄管道收集 GDC 專案中的記錄。您可以指定要收集記錄的目標、記錄剖析器、存取層級和任何其他設定。
這項資源定義了下列設定:
- 目標:選取要收集記錄檔的 Pod 和容器時,所依據的條件。您可以指定叢集名稱、Pod 名稱前置字元和容器名稱前置字元。
- 記錄分析器:預先定義的剖析器,用於解讀記錄項目、將記錄輸出內容對應至標籤,以及擷取相關欄位。
- 服務識別:套用至記錄項目的服務名稱標籤,方便識別及篩選。
授權:記錄項目的存取層級,控管誰可以查看收集到的記錄。
只要在 LoggingTarget 自訂資源中設定這些參數,即可精確控管記錄的收集和整理方式。
請按照下列步驟收集作業記錄:
- 決定要從哪個 GDC 專案收集記錄。
在
LoggingTarget設定中,指定要收集作業記錄的 Pod、專案命名空間,以及任何其他設定。下列 YAML 檔案顯示
my-project-namespace中LoggingTarget設定的範例,其中要收集記錄的 Pod 名稱前置字串為my-pod-prefix、記錄項目的存取層級授予應用程式運算子 (ao)、服務名稱為my-service-name,且記錄格式為 JSON:# Configures a log scraping job apiVersion: logging.gdc.goog/v1 kind: LoggingTarget metadata: # Choose a namespace that matches the namespace of the workload pods. namespace: my-project-namespace name: my-logging-target spec: selector: matchPodNames: - my-pod-prefix parser: json logAccessLevel: ao serviceName: my-service-name如需其他欄位和選項,請參閱完整的
LoggingTarget規格和 API 參考文件。將
LoggingTarget設定套用至 Management API 伺服器,該伺服器與目標 Pod 位於同一個命名空間:kubectl --kubeconfig KUBECONFIG_PATH apply -f LOGGING_TARGET_NAME.yaml更改下列內容:
KUBECONFIG_PATH:Management API 伺服器的 kubeconfig 檔案路徑。LOGGING_TARGET_NAME:LoggingTarget定義檔案的名稱,例如my-logging-target。
記錄管道會開始從專案的其他元件收集記錄。
您可以使用 Grafana 使用者介面或 Log Query API 查詢收集到的記錄。詳情請參閱「查詢及查看記錄」。
如果您使用 Grafana 查詢記錄,可以針對不同的記錄層級使用內建的顏色編碼功能。如要進一步瞭解如何設定記錄層級值,請參閱 https://grafana.com/docs/grafana/latest/explore/logs-integration/。
完成LoggingTarget規格
下列 YAML 檔案是 LoggingTarget 自訂資源完整規格的範例。如需更多資訊和欄位的完整說明,請參閱 API 參考文件。
# Configures a log scraping job
apiVersion: logging.gdc.goog/v1
kind: LoggingTarget
metadata:
# Choose a namespace that matches the namespace of the workload pods.
namespace: PROJECT_NAMESPACE
name: LOGGING_TARGET_NAME
spec:
# Choose a matching pattern that identifies the pods for this job.
# Optional.
# Relationship between different selectors: 'AND'
selector:
# The clusters to collect logs from.
# The default configuration is to collect logs from all clusters.
# The relationship between different clusters is an 'OR' relationship.
# Optional
matchClusters:
- my-cluster
- another-cluster
# The pod name prefixes to collect logs from.
# The logging platform scrapes all pods with names
# that start with the specified prefixes.
# The values must contain '[a-z0-9-]' characters only.
# The relationship between different list elements is an 'OR' relationship.
# Optional.
matchPodNames:
- my-pod-prefix
- another-pod-prefix
# The container name prefixes to collect logs from.
# The logging platform scrapes all containers with names
# that start with the specified prefixes.
# The values must contain '[a-z0-9-]' characters only.
# The relationship between different list elements is an 'OR' relationship.
# Optional.
matchContainerNames:
- my-container-prefix
- another-container-prefix
# Choose the predefined parser for log entries.
# Use parsers to map the log output to labels and extract fields.
# Specify the log format.
# Optional.
# Options: klog_text, klog_json, klogr, gdch_json, json
parser: klog_text
# Specify an access level for log entries.
# The default value is 'ao'.
# Optional.
# Options: ao, pa, io
logAccessLevel: ao
# Specify a service name to be applied as a label.
# For user workloads consider this field as a workload name.
# Required.
serviceName: my-service-name
# The additional static fields to apply to log entries.
# The field is a key-value pair, where the field name is the key and
# the field value is the value.
# Optional.
additionalFields:
app: workload2
key: value
更改下列內容:
PROJECT_NAMESPACE:專案命名空間。LOGGING_TARGET_NAME:LoggingTarget定義檔案的名稱。