Istio?訪問外部服務(wù)流量控制最常用的5個技巧示例
環(huán)境準(zhǔn)備
5 個 Istio 訪問外部服務(wù)的流量控制常用例子,強烈建議收藏起來,以備不時之需。
部署 sleep
服務(wù),作為發(fā)送請求的測試源:
kubectl apply -f samples/sleep/sleep.yaml
在 Istio 外部,使用 Nginx 搭建 duckling
服務(wù)的v1和v2兩個版本,訪問時顯示簡單的文本:
> curl -s http://192.168.1.118/ This is the v1 version of duckling. > curl -s http://192.168.1.119/ This is the v2 version of duckling.
訪問外部服務(wù)
執(zhí)行如下命名訪問外部服務(wù) httpbin.org :
export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}') kubectl exec "$SLEEP_POD" -c sleep -- curl -s http://httpbin.org/headers
返回結(jié)果如下:
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.81.0-DEV",
"X-Amzn-Trace-Id": "Root=1-62bbfa10-3237e3b9662c65ae005148ab",
"X-B3-Sampled": "0",
"X-B3-Spanid": "9e650093bf7ae862",
"X-B3-Traceid": "1da46d7fafa5d71c9e650093bf7ae862",
"X-Envoy-Attempt-Count": "1",
"X-Envoy-Peer-Metadata": "......",
"X-Envoy-Peer-Metadata-Id": "sidecar~......"
}
}
此時的方法,沒有通過Service Entry,沒有 Istio 的流量監(jiān)控和控制特性。創(chuàng)建 Service Entry :
kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: ServiceEntry metadata: name: httpbin-ext spec: hosts: - httpbin.org ports: - number: 80 name: http protocol: HTTP resolution: DNS location: MESH_EXTERNAL EOF
再此次訪問,返回結(jié)果如下:
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.81.0-DEV",
"X-Amzn-Trace-Id": "Root=1-62bbfbd6-254b05344b3cde2c0c41b3b8",
"X-B3-Sampled": "0",
"X-B3-Spanid": "307c0b106c8b262e",
"X-B3-Traceid": "f684a50776c088ac307c0b106c8b262e",
"X-Envoy-Attempt-Count": "1",
"X-Envoy-Decorator-Operation": "httpbin.org:80/*",
"X-Envoy-Peer-Metadata": "......",
"X-Envoy-Peer-Metadata-Id": "sidecar~......"
}
}
可以發(fā)現(xiàn)由 Istio 邊車添加的請求頭:X-Envoy-Decorator-Operation
。
設(shè)置請求超時
向外部服務(wù) httpbin.org 的 /delay 發(fā)出請求:
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
返回結(jié)果如下:
200
real 0m 5.69s
user 0m 0.00s
sys 0m 0.00s
請求大約在 5 秒后返回 200 (OK)。
創(chuàng)建虛擬服務(wù),訪問外部服務(wù) httpbin.org 時, 請求超時設(shè)置為 3 秒:
kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: httpbin-ext spec: hosts: - httpbin.org http: - timeout: 3s route: - destination: host: httpbin.org weight: 100 EOF
再此次訪問,返回結(jié)果如下:
504
real 0m 3.01s
user 0m 0.00s
sys 0m 0.00s
可以看出,在 3 秒后出現(xiàn)了 504 (Gateway Timeout)。 Istio 在 3 秒后切斷了響應(yīng)時間為 5 秒的 httpbin.org 服務(wù)。
注入 HTTP 延遲故障
向外部服務(wù) httpbin.org 的 /get 發(fā)出請求:
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/get
返回結(jié)果如下:
200
real 0m 0.45s
user 0m 0.00s
sys 0m 0.00s
請求不到 1 秒就返回 200 (OK)。
創(chuàng)建虛擬服務(wù),訪問外部服務(wù) httpbin.org 時, 注入一個 3 秒的延遲:
kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: httpbin-ext spec: hosts: - httpbin.org http: - fault: delay: fixedDelay: 3s percentage: value: 100 route: - destination: host: httpbin.org EOF
再此次訪問 httpbin.org 的 /get ,返回結(jié)果如下:
200
real 0m 3.43s
user 0m 0.00s
sys 0m 0.00s
可以看出,在 3 秒后出現(xiàn)了 200 (OK)。
流量轉(zhuǎn)移
訪問duckling
服務(wù)時,所有流量都路由到v1版本,具體配置如下:
kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: ServiceEntry metadata: name: duckling spec: hosts: - duckling.com ports: - number: 80 name: http protocol: HTTP location: MESH_EXTERNAL resolution: STATIC endpoints: - address: 172.24.29.118 ports: http: 80 labels: version: v1 - address: 172.24.29.119 ports: http: 80 labels: version: v2 --- apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: duckling spec: hosts: - duckling.com http: - route: - destination: host: duckling.com subset: v1 --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: duckling spec: host: duckling.com subsets: - labels: version: v1 name: v1 - labels: version: v2 name: v2 EOF
執(zhí)行如下命名訪問外部服務(wù) duckling.com :
export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}') kubectl exec "$SLEEP_POD" -c sleep -- curl -s http://duckling.com/
多次訪問后,返回結(jié)果一直是:This is the v1 version of duckling.
訪問duckling
服務(wù)時,把50%流量轉(zhuǎn)移到v2版本,具體配置如下:
kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: duckling spec: hosts: - duckling.com http: - route: - destination: host: duckling.com subset: v1 weight: 50 - destination: host: duckling.com subset: v2 weight: 50 EOF
多次訪問外部服務(wù) duckling.com ,有時返回This is the v1 version of duckling.
,有時返回This is the v2 version of duckling.
。
訪問duckling
服務(wù)時,所有流量都路由到v2版本,具體配置如下:
kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: duckling spec: hosts: - duckling.com http: - route: - destination: host: duckling.com subset: v2 EOF
多次訪問外部服務(wù) duckling.com ,一直返回This is the v2 version of duckling.
。
基于請求頭的路由
請求頭end-user
為OneMore的所有流量都路由到v2版本,其他流量都路由到v1版本,具體配置如下:
kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: ServiceEntry metadata: name: duckling spec: hosts: - duckling.com ports: - number: 80 name: http protocol: HTTP location: MESH_EXTERNAL resolution: STATIC endpoints: - address: 172.24.29.118 ports: http: 80 labels: version: v1 - address: 172.24.29.119 ports: http: 80 labels: version: v2 --- apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: duckling spec: hosts: - duckling.com http: - match: - headers: end-user: exact: OneMore route: - destination: host: duckling.com subset: v2 - route: - destination: host: duckling.com subset: v1 --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: duckling spec: host: duckling.com subsets: - labels: version: v1 name: v1 - labels: version: v2 name: v2 EOF
執(zhí)行如下命名訪問外部服務(wù) duckling.com :
export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}') kubectl exec "$SLEEP_POD" -c sleep -- curl -s http://duckling.com/
多次訪問的返回結(jié)果一直是:This is the v1 version of duckling.
設(shè)置請求頭end-user
為OneMore,訪問外部服務(wù) duckling.com :
kubectl exec "$SLEEP_POD" -c sleep -- curl -H "end-user:OneMore" -s http://duckling.com/
多次訪問的返回結(jié)果一直是:This is the v2 version of duckling.
以上就是5個 Istio 訪問外部服務(wù)流量控制最常用的例子的詳細(xì)內(nèi)容,更多關(guān)于Istio 訪問外部服務(wù)流量控制的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
idea中實用的git操作問題小結(jié)(撤回commit,撤回push、暫存區(qū)使用)
我們在開發(fā)過程中經(jīng)常遇到commit代碼后,發(fā)現(xiàn)還有需要修改的地方又不想多次commit,此時如果不想敲git命令,可以利用idea實現(xiàn)commit撤回,下面給大家分享idea中實用的git操作問題小結(jié),感興趣的朋友跟隨小編一起看看吧2024-08-08Vs?Code配置前端環(huán)境及運行詳細(xì)指南
相信越來越多的前端開發(fā)者已經(jīng)遷移到VSCode陣營了,下面這篇文章主要給大家介紹了關(guān)于Vs?Code配置前端環(huán)境及運行的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10VS2019創(chuàng)建MFC程序的實現(xiàn)方法
這篇文章主要介紹了VS2019創(chuàng)建MFC程序的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08搭建websocket消息推送服務(wù),必須要考慮的幾個問題
面對各種新場景對websocket功能和性能越來越高的需求,不同的團隊有不同的選擇,下面給大家分享構(gòu)建websocket服務(wù)時必須要考慮的一些技術(shù)特性以及能顯著提高用戶體驗的功能,感興趣的朋友跟隨小編一起看看吧2020-04-04git分支(branch)操作相關(guān)命令及分支命令的使用
這篇文章主要介紹了git分支(branch)操作相關(guān)命令及分支命令的使用的相關(guān)資料,需要的朋友可以參考下2017-10-10