通過 Stackdriver 將日誌匯出至 BigQuery、GCS、Pub/Sub

如何通過 Stackdriver 將 Istio 存取日誌匯出至不同的接收器,如 BigQuery、GCS、Pub/Sub。

2018 年 7 月 9 日 | 作者:Nupur Garg 和 Douglas Reid

這篇文章說明如何將 Istio 日誌導向 Stackdriver,並將這些日誌匯出至各種配置的接收器,例如 BigQueryGoogle Cloud StorageCloud Pub/Sub。在這篇文章結束後,您可以從您喜歡的地方(例如 BigQuery、GCS 或 Cloud Pub/Sub)對 Istio 數據執行分析。

整個任務中使用 Bookinfo 範例應用程式作為範例。

開始之前

在您的叢集中安裝 Istio 並部署應用程式。

配置 Istio 以匯出日誌

Istio 使用 logentry 範本匯出日誌。這指定所有可用於分析的變數。它包含來源服務、目的地服務、身份驗證指標(即將推出)等資訊。以下是管線的示意圖

Exporting logs from Istio to Stackdriver for analysis
將 Istio 的日誌匯出到 Stackdriver 進行分析

Istio 支援將日誌匯出到 Stackdriver,然後可以配置 Stackdriver 將日誌匯出到您喜歡的接收器,例如 BigQuery、Pub/Sub 或 GCS。請按照以下步驟設定您喜歡的接收器以先匯出日誌,然後在 Istio 中設定 Stackdriver。

設定各種日誌接收器

所有接收器的通用設定

  1. 為專案啟用 Stackdriver Monitoring API
  2. 確保設定接收器的 principalEmail 具有專案的寫入權限和 Logging Admin 角色權限。
  3. 確保已設定 GOOGLE_APPLICATION_CREDENTIALS 環境變數。請按照 這裡 的說明進行設定。

BigQuery

  1. 建立 BigQuery 資料集作為日誌匯出的目的地。
  2. 記錄資料集的 ID。它將用於配置 Stackdriver 處理器。它將採用 bigquery.googleapis.com/projects/[PROJECT_ID]/datasets/[DATASET_ID] 的形式
  3. 在 IAM 中,給予 接收器的寫入者身份cloud-logs@system.gserviceaccount.com BigQuery 資料編輯者角色。
  4. 如果使用 Google Kubernetes Engine,請確保在叢集上啟用 bigquery 範圍

Google Cloud Storage (GCS)

  1. 建立 GCS 儲存貯體,您希望將日誌匯出到 GCS。
  2. 記錄儲存貯體的 ID。它將用於配置 Stackdriver。它將採用 storage.googleapis.com/[BUCKET_ID] 的形式
  3. 在 IAM 中,給予 接收器的寫入者身份cloud-logs@system.gserviceaccount.com Storage Object Creator 角色。

Google Cloud Pub/Sub

  1. 建立主題,您希望將日誌匯出到 Google Cloud Pub/Sub。
  2. 記錄主題的 ID。它將用於配置 Stackdriver。它將採用 pubsub.googleapis.com/projects/[PROJECT_ID]/topics/[TOPIC_ID] 的形式
  3. 在 IAM 中,給予 接收器的寫入者身份cloud-logs@system.gserviceaccount.com Pub/Sub Publisher 角色。
  4. 如果使用 Google Kubernetes Engine,請確保在叢集上啟用 pubsub 範圍

設定 Stackdriver

必須建立 Stackdriver 處理器才能將資料匯出到 Stackdriver。有關 Stackdriver 處理器的配置說明,請參閱 這裡

  1. 將以下 yaml 檔案儲存為 stackdriver.yaml。將 <project_id>、<sink_id>、<sink_destination>、<log_filter> 替換為其特定值。

    apiVersion: "config.istio.io/v1alpha2"
    kind: stackdriver
    metadata:
      name: handler
      namespace: istio-system
    spec:
      # We'll use the default value from the adapter, once per minute, so we don't need to supply a value.
      # pushInterval: 1m
      # Must be supplied for the Stackdriver adapter to work
      project_id: "<project_id>"
      # One of the following must be set; the preferred method is `appCredentials`, which corresponds to
      # Google Application Default Credentials.
      # If none is provided we default to app credentials.
      # appCredentials:
      # apiKey:
      # serviceAccountPath:
      # Describes how to map Istio logs into Stackdriver.
      logInfo:
        accesslog.logentry.istio-system:
          payloadTemplate: '{{or (.sourceIp) "-"}} - {{or (.sourceUser) "-"}} [{{or (.timestamp.Format "02/Jan/2006:15:04:05 -0700") "-"}}] "{{or (.method) "-"}} {{or (.url) "-"}} {{or (.protocol) "-"}}" {{or (.responseCode) "-"}} {{or (.responseSize) "-"}}'
          httpMapping:
            url: url
            status: responseCode
            requestSize: requestSize
            responseSize: responseSize
            latency: latency
            localIp: sourceIp
            remoteIp: destinationIp
            method: method
            userAgent: userAgent
            referer: referer
          labelNames:
          - sourceIp
          - destinationIp
          - sourceService
          - sourceUser
          - sourceNamespace
          - destinationIp
          - destinationService
          - destinationNamespace
          - apiClaims
          - apiKey
          - protocol
          - method
          - url
          - responseCode
          - responseSize
          - requestSize
          - latency
          - connectionMtls
          - userAgent
          - responseTimestamp
          - receivedBytes
          - sentBytes
          - referer
          sinkInfo:
            id: '<sink_id>'
            destination: '<sink_destination>'
            filter: '<log_filter>'
    ---
    apiVersion: "config.istio.io/v1alpha2"
    kind: rule
    metadata:
      name: stackdriver
      namespace: istio-system
    spec:
      match: "true" # If omitted match is true.
      actions:
      - handler: handler.stackdriver
        instances:
        - accesslog.logentry
    ---
    
  2. 推送配置

    $ kubectl apply -f stackdriver.yaml
    stackdriver "handler" created
    rule "stackdriver" created
    logentry "stackdriverglobalmr" created
    metric "stackdriverrequestcount" created
    metric "stackdriverrequestduration" created
    metric "stackdriverrequestsize" created
    metric "stackdriverresponsesize" created
    
  3. 將流量傳送到範例應用程式。

    對於 Bookinfo 範例,請在您的網頁瀏覽器中瀏覽 http://$GATEWAY_URL/productpage,或發出以下命令

    $ curl http://$GATEWAY_URL/productpage
    
  4. 驗證日誌是否正在通過 Stackdriver 流向已配置的接收器。

    • Stackdriver:導覽至您專案的 Stackdriver Logs Viewer,並在「GKE Container」->「叢集名稱」->「命名空間 ID」下尋找 Istio 存取日誌。
    • BigQuery:導覽至您專案的 BigQuery 介面,您應該會在您的接收器資料集中找到一個帶有字首 accesslog_logentry_istio 的表格。
    • GCS:導覽至您專案的 Storage Browser,您應該會在您的接收器儲存貯體中找到一個名為 accesslog.logentry.istio-system 的儲存貯體。
    • Pub/Sub:導覽至您專案的 Pub/Sub 主題清單,您應該會在您的接收器主題中找到一個 accesslog 的主題。

了解發生了什麼

上面的 Stackdriver.yaml 檔案配置了 Istio 將存取日誌傳送到 Stackdriver,然後新增了一個接收器配置,以便可以匯出這些日誌。詳細資訊如下

  1. 新增了一個 stackdriver 類型的處理器

    apiVersion: "config.istio.io/v1alpha2"
    kind: stackdriver
    metadata:
      name: handler
      namespace: <your defined namespace>
    
  2. 在 spec 中新增了 logInfo

    spec:
      logInfo: accesslog.logentry.istio-system:
        labelNames:
        - sourceIp
        - destinationIp
        ...
        ...
        sinkInfo:
          id: '<sink_id>'
          destination: '<sink_destination>'
          filter: '<log_filter>'
    

    在以上配置中,sinkInfo 包含有關您希望將日誌匯出到的接收器的資訊。有關如何為不同的接收器填寫此資訊的更多資訊,請參閱 這裡

  3. 新增了 Stackdriver 的規則

    apiVersion: "config.istio.io/v1alpha2"
    kind: rule
    metadata:
      name: stackdriver
      namespace: istio-system spec:
      match: "true" # If omitted match is true
    actions:
    - handler: handler.stackdriver
      instances:
      - accesslog.logentry
    

清除

匯出接收器中日誌的可用性

匯出到 BigQuery 在幾分鐘內(我們看到幾乎是即時的),GCS 可能會有 2 到 12 小時的延遲,而 Pub/Sub 幾乎是立即的。

分享這篇文章