部署 httpbin
服務:
kubectl apply -f samples/httpbin/httpbin.yaml
部署 sleep
服務:
kubectl apply -f samples/sleep/sleep.yaml
httpbin
服務作為接收請求的伺服器端, sleep
服務作為傳送請求的使用者端。
在 sleep
服務中向 httpbin
服務發出請求:
export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}')
kubectl exec "$SLEEP_POD" -c sleep -- time curl -o /dev/null -sS -w "%{http_code}\n" http://httpbin.org/delay/5
返回結果如下:
200
real 0m 5.69s
user 0m 0.00s
sys 0m 0.00s
可以看到,請求大約在 5 秒返回 200 (OK)。
建立虛擬服務,存取httpbin
服務時,請求超時設定為 3 秒:
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- httpbin
http:
- route:
- destination:
host: httpbin
timeout: 3s
EOF
再次存取,返回結果如下:
504
real 0m 3.01s
user 0m 0.00s
sys 0m 0.00s
可以看到,在 3 秒後出現了 504 (Gateway Timeout)。 Istio 在 3 秒後切斷了響應時間為 5 秒的httpbin
服務的請求。接下來,我們深入地看一下,Istio是怎麼切斷請求的?
執行以下命令,檢視sleep
服務的Envoy紀錄檔:
kubectl logs -l app=sleep -c istio-proxy
可以看到sleep
服務對httpbin
服務的呼叫的紀錄檔:
{
"authority": "httpbin:8000",
"bytes_received": 0,
"bytes_sent": 24,
"connection_termination_details": null,
"downstream_local_address": "172.24.146.239:8000",
"downstream_remote_address": "172.24.158.25:40384",
"duration": 3001,
"method": "GET",
"path": "/delay/5",
"protocol": "HTTP/1.1",
"request_id": "5ef38816-7f49-48c8-9627-2416e1716293",
"requested_server_name": null,
"response_code": 504,
"response_code_details": "upstream_response_timeout",
"response_flags": "UT",
"route_name": null,
"start_time": "2022-07-01T09:40:13.882Z",
"upstream_cluster": "outbound|8000||httpbin.onemore.svc.cluster.local",
"upstream_host": "172.24.158.96:80",
"upstream_local_address": "172.24.158.25:32846",
"upstream_service_time": null,
"upstream_transport_failure_reason": null,
"user_agent": "curl/7.81.0-DEV",
"x_forwarded_for": null
}
其中,response_flags
為UT
,表示上游(upstream)請求超時,也就是sleep
服務檢測到了httpbin
服務的請求超時。
執行以下命令,檢視httpbin
服務的Envoy紀錄檔:
kubectl logs -l app=httpbin -c istio-proxy
可以看到httpbin
服務被sleep
服務呼叫的Envoy紀錄檔:
{
"authority": "httpbin:8000",
"bytes_received": 0,
"bytes_sent": 0,
"connection_termination_details": null,
"downstream_local_address": "172.24.158.96:80",
"downstream_remote_address": "172.24.158.25:32846",
"duration": 2997,
"method": "GET",
"path": "/delay/5",
"protocol": "HTTP/1.1",
"request_id": "5ef38816-7f49-48c8-9627-2416e1716293",
"requested_server_name": "outbound_.8000_._.httpbin.onemore.svc.cluster.local",
"response_code": 0,
"response_code_details": "downstream_remote_disconnect",
"response_flags": "DC",
"route_name": "default",
"start_time": "2022-07-01T09:40:13.885Z",
"upstream_cluster": "inbound|80||",
"upstream_host": "172.24.158.96:80",
"upstream_local_address": "127.0.0.6:35701",
"upstream_service_time": null,
"upstream_transport_failure_reason": null,
"user_agent": "curl/7.81.0-DEV",
"x_forwarded_for": null
}
其中,response_flags
為DC
,表示下游(downstream)連線中斷,也就是sleep
服務的呼叫請求被中斷了。
文章持續更新,微信搜尋「萬貓學社」第一時間閱讀,關注後回覆「電子書」,免費獲取12本Java必讀技術書籍。
通過Envoy紀錄檔,我們可以做出一些分析和判斷:
當httpbin
服務的請求正常的時候,呼叫過程如下圖:
當httpbin
服務的請求超時的時候,呼叫過程如下圖:
雖然,我們在httpbin
服務上設定的請求超時時間,但實際上主動斷開請求的卻是sleep
服務的Envoy。
kubectl delete virtualservice httpbin
kubectl delete -f samples/httpbin/httpbin.yaml
kubectl delete -f samples/sleep/sleep.yaml
微信公眾號:萬貓學社
微信掃描二維條碼
關注後回覆「電子書」
獲取12本Java必讀技術書籍