天天看點

在Istio中,到底怎麼擷取 Envoy 通路日志?

Envoy 通路日志記錄了通過 Envoy 進行請求 / 響應互動的相關記錄,可以友善地了解具體通信過程和調試定位問題。

環境準備

部署

httpbin

服務:

kubectl apply -f samples/httpbin/httpbin.yaml
           

部署

sleep

服務:

kubectl apply -f samples/sleep/sleep.yaml 
           

httpbin

服務作為接收請求的服務端,

sleep

服務作為發送請求的用戶端。

還需要開啟 Envoy 通路日志,執行以下指令修改 istio 配置:

kubectl -n istio-system edit configmap istio
           

編輯yaml檔案的對應配置:

data:
  mesh: |-
    accessLogEncoding: JSON
    accessLogFile: /dev/stdout
           

其中,

accessLogEncoding

表示 accesslog 輸出格式,Istio 預定義了

TEXT

JSON

兩種日志輸出格式。預設使用

TEXT

,通常改成

JSON

以提升可讀性;

accessLogFile

:表示 accesslog 輸出位置,通常指定到 /dev/stdout (标準輸出),以便使用 kubectl logs 來檢視日志。

保證yaml檔案後,配置随即生效。

測試通路日志

sleep

服務中向

httpbin

服務送出請求:

export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}')
kubectl exec "$SLEEP_POD" -c sleep -- curl -sS http://httpbin:8000/headers
           

傳回結果如下:

{
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin:8000", 
    "User-Agent": "curl/7.81.0-DEV", 
    "X-B3-Parentspanid": "ed0178f3e1f48dd1", 
    "X-B3-Sampled": "0", 
    "X-B3-Spanid": "6c38b689ee5ab0c8", 
    "X-B3-Traceid": "f17ce19c174cae85ed0178f3e1f48dd1", 
    "X-Envoy-Attempt-Count": "1", 
    "X-Forwarded-Client-Cert": "......"
  }
}
           

執行以下指令,檢視

sleep

服務的Envoy日志:

kubectl logs -l app=sleep -c istio-proxy
           

可以看到

sleep

服務對

httpbin

服務的調用的日志:

{
     "authority": "httpbin:8000",
     "bytes_received": 0,
     "bytes_sent": 533,
     "connection_termination_details": null,
     "downstream_local_address": "172.24.146.239:8000",
     "downstream_remote_address": "172.24.158.25:49350",
     "duration": 3,
     "method": "GET",
     "path": "/headers",
     "protocol": "HTTP/1.1",
     "request_id": "ea40d320-348f-4f58-86d4-da157b0e0cca",
     "requested_server_name": null,
     "response_code": 200,
     "response_code_details": "via_upstream",
     "response_flags": "-",
     "route_name": "default",
     "start_time": "2022-07-04T10:00:09.401Z",
     "upstream_cluster": "outbound|8000||httpbin.istio-demo.svc.cluster.local",
     "upstream_host": "172.24.158.96:80",
     "upstream_local_address": "172.24.158.25:41812",
     "upstream_service_time": "2",
     "upstream_transport_failure_reason": null,
     "user_agent": "curl/7.81.0-DEV",
     "x_forwarded_for": null
}
           

執行以下指令,檢視

httpbin

服務的Envoy日志:

kubectl logs -l app=httpbin -c istio-proxy
           

可以看到

httpbin

服務被

sleep

服務調用的Envoy日志:

{
     "authority": "httpbin:8000",
     "bytes_received": 0,
     "bytes_sent": 533,
     "connection_termination_details": null,
     "downstream_local_address": "172.24.158.96:80",
     "downstream_remote_address": "172.24.158.25:41812",
     "duration": 2,
     "method": "GET",
     "path": "/headers",
     "protocol": "HTTP/1.1",
     "request_id": "ea40d320-348f-4f58-86d4-da157b0e0cca",
     "requested_server_name": "outbound_.8000_._.httpbin.istio-demo.svc.cluster.local",
     "response_code": 200,
     "response_code_details": "via_upstream",
     "response_flags": "-",
     "route_name": "default",
     "start_time": "2022-07-04T10:00:09.401Z",
     "upstream_cluster": "inbound|80||",
     "upstream_host": "172.24.158.96:80",
     "upstream_local_address": "127.0.0.6:33665",
     "upstream_service_time": "1",
     "upstream_transport_failure_reason": null,
     "user_agent": "curl/7.81.0-DEV",
     "x_forwarded_for": null
}
           

看到這麼多參數,是不是有點懵逼?沒關系接下來,我們詳細看看!

刨析Envoy日志

名稱 HTTP TCP
authority 請求授權頭 未實作(“-”)
bytes_received 接收到消息體位元組數 在連接配接上從下遊接收的位元組數
bytes_sent 發送的包體位元組數 在連接配接上發送給下遊的位元組數
connection_termination_details 連接配接中斷詳情 連接配接中斷詳情
downstream_local_address 下遊連接配接的本地位址 下遊連接配接的本地位址
downstream_remote_address 下遊連接配接的遠端位址 下遊連接配接的遠端位址
duration 請求從起始時間到最後一個位元組發出的持續總時長(以毫秒為機關) 下遊連接配接的持續總時長(以毫秒為機關)
method HTTP請求方法 未實作(“-”)
path HTTP請求路徑 未實作(“-”)
protocol 協定,目前不是 HTTP/1.1 就是 HTTP/2 未實作(“-”)
request_id 由envoy建立的

X-REQUEST-ID

請求頭的值
未實作(“-”)
requested_server_name 設定在 ssl 連接配接套接字上表示伺服器名稱訓示 (SNI) 的字元值 未實作(“-”)
response_code HTTP 響應碼 未實作(“-”)
response_code_details TTP 響應狀态碼詳情提供關于響應狀态碼的附加資訊。 未實作(“-”)
response_flags 響應或者連接配接的附加詳情 響應或者連接配接的附加詳情
route_name 路由名 路由名
start_time 請求開始時間(包括毫秒) 下遊連接配接開始時間(包括毫秒)
upstream_cluster 上遊主機所屬的上遊叢集 上遊主機所屬的上遊叢集
upstream_host 上遊主機 URL 上遊主機 URL
upstream_local_address 上遊連接配接的本地位址 上遊連接配接的本地位址
upstream_transport_failure_reason 如果上遊因傳輸套接字而連接配接失敗,從傳輸套接字中提供失敗原因。 未實作(“-”)
user_agent

User-Agent

請求頭的值
未實作(“-”)
x_forwarded_for

X-Forwarded-For

請求頭的值
未實作(“-”)

清理

删除

httpbin

sleep

服務:

kubectl delete -f samples/httpbin/httpbin.yaml
kubectl delete -f samples/sleep/sleep.yaml 
           

繼續閱讀