參考
1. 使用 Jaeger
- jaegertracing/jaeger 是一個用於分布式跟蹤的開源軟件,提供原生
OpenTracing
支持,向下兼容 ZipKin
;
- istio 中采用 Jaeger 作為分布式跟蹤組件;
- istio sidecar 為網格中的應用提供的跟蹤功能只能提供調用環節的數據,如果需要支持整條鏈路,需要根據
OpenTracing
規范對應用進行改寫。
1.1 啟用 Jaeger
1.1.1 定制 Jaeger values.yaml
- Jaeger 默認不啟用,需要定制安裝清單,可以通過
helm template
的 --set
參數定制安裝清單文件。
# 同時啟用 "ingress" ,開放服務
vim install/kubernetes/helm/istio/charts/tracing/values.yaml
ingress:
# 啟用 "ingress",默認值 "false"
enabled: true
# Used to create an Ingress record.
hosts:
# 修改 "domain"
- tracing.istio
annotations:
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
tls:
# Secrets must be manually created in the namespace.
# - secretName: tracing-tls
# hosts:
# - tracing.local
1.1.2 渲染並啟用 Jaeger
# 直接修改 Jaeger 的 `values.yaml` 文件的 "enabled: false" 為 "enabled: true" 並不生效;
# 但修改 `values.yaml` 文件的其余部分是有效的
helm template install/kubernetes/helm/istio \
--set tracing.enabled=true \
--name istio \
--namespace istio-system > default-tracing.yaml
# 查看資源對象
kubectl get pod -n istio-system -w
# `values.yaml` 文件的 "contextPath" 參數不體現,默認值即 "/jaeger"
URL: http://tracing.istio/jaeger/
# 構建流量觀察 jaeger 服務
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v1 -o jsonpath={.items[0].metadata.name}) /bin/bash
bash-4.4# for i in `seq 100` ; do http --body http://flaskapp/env/version ; done
1.2 跟蹤參數傳遞
1.2.1 孤立的跟蹤信息
# 樣例服務在 istio 源代碼的 samples 目錄下
cd istio-1.1.7/samples/httpbin
kubectl apply -f httpbin.yaml
# 在 "sleep" 服務的 Pod 內發起請求,要求 "flaskapp" 調用 "httpbin" 服務的 "/get" 路徑,並返回 "httpbin" 給出的響應,同時要顯示 "sleep" 發出的請求 Header 的內容;
# 完整路徑:"sleep" --> "flaskapp" --> "httpbin",但 "OpenTracing" 所依賴的 `header` 沒有被傳遞,導致 Jaeger 無法確定調用之間的關系
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v1 -o jsonpath={.items[0].metadata.name}) /bin/bash
bash-4.4# http --debug http://flaskapp/fetch_with_header?url=http://httpbin:8000/get
- 返回信息中,
requests.request
是 httpie 客戶端發出請求的 header 的原始內容;
flaskapp
收到的 header 內容中有一系列 "X-*" 的請求 header ,是 Evory
代理對請求進行的修改,其中包含分布式跟蹤所需要的 Request-Id
等請求 header 。
1.2.2 跟蹤參數傳遞
1.2.2.1 fetch_with_trace
- 在
flaskapp
服務的代碼中定義了一個 fetch_with_trace
方法:
# 路徑:"/app/main.py"
@app.route('/fetch_with_trace')
def fetch_with_trace():
url = request.args.get('url', '')
request_headers = dict(request.headers)
new_header = {}
for key in request_headers.keys():
if key.lower() in TRACE_HEADERS:
new_header[key] = request_headers[key]
req = Request(url, headers = new_header)
return urlopen(req).read()
- 通過
fetch_with_trace
方法:將中間服務收到的請求在進行下一級請求時,將其中用於跟蹤的 header
傳遞下去,即可將孤立的跟蹤信息融合在一起。
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v1 -o jsonpath={.items[0].metadata.name}) /bin/bash
bash-4.4# for i in `seq 100` ; do http http://flaskapp/fetch_with_trace?url=http://httpbin:8000/ip ; done
- 通過
/fetch_with_header
方法,可以觀察 header 傳遞過程中的變化:
bash-4.4# do http http://flaskapp/fetch_with_header?url=http://httpbin:8000/get
- 返回兩組 http header :
- 第一組來自
flaskapp
,表示 sleep --> flaskapp
的請求內容;
- 第二組來自
httpbin
,表示 flaskapp --> httpbin
的請求內容。
2. 使用 Kiali
- kiali 也是一個用於 istio 可視化的軟件:
- 與 Grafana & Prometheus 不同的是,Kiali 目前專用於 istio 系統;
- 除提供監控,可視化,跟蹤等通用功能外,還專門提供了 istio 的配置驗證,健康評估等高級功能。
2.1 啟用 Kiali
2.1.1 定制 Kiali values.yaml
- Kiali 默認未啟用,需要定制安裝清單,可以通過
helm template
的 --set
參數渲染定制安裝清單文件。
# 同時啟用 "ingress" ,開放服務
vim install/kubernetes/helm/istio/charts/kiali/values.yaml
# 使用當前最新(2019年6月前后)穩定鏡像
tag: v0.20
# "ingress" 資源的 "spec.rules.host.http.paths.path" 字段,即 "subpath"
contextPath: /kiali
ingress:
# 啟用 "ingress",默認值 "false"
enabled: true
# Used to create an Ingress record.
hosts:
# 修改 "domain"
- kiali.istio
annotations:
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
tls:
# Secrets must be manually created in the namespace.
# - secretName: kiali-tls
# hosts:
# - kiali.local
dashboard:
# 以名為 "kiali" 的 "Secret" 對象傳遞登陸賬號與密碼
secretName: kiali # You must create a secret with this name - one is not provided out-of-box.
# 重定向,以 "ConfigMap" 對象的形式掛載到工作負載
# 注意:可以被訪問的外部地址
grafanaURL: http://grafana:3000 # If you have Grafana installed and it is accessible to client browsers, then set this to its external URL. Kiali will redirect users to this URL when Grafana metrics are to be shown.
jaegerURL: http://tracing:80 # If you have Jaeger installed and it is accessible to client browsers, then set this property to its external URL. Kiali will redirect users to this URL when Jaeger tracing is to be shown.
2.1.2 渲染並啟用 Kiali
# 直接修改 Jaeger 的 `values.yaml` 文件的 "enabled: false" 為 "enabled: true" 並不生效;
# 但修改 `values.yaml` 文件的其余部分是有效的
helm template install/kubernetes/helm/istio \
--set kiali.enabled=true \
--name istio \
--namespace istio-system > default-tracing.yaml
# 創建 "Secret" 對象 "kiali"
kubectl create secret generic kiali -n istio-system --from-literal "username=admin" --from-literal "passphrase=admin"
# 查看資源對象
kubectl get pod -n istio-system -w
# 訪問地址
URL: http://kiali.istio/kiali/