当前位置:网站首页>10個 Istio 流量管理 最常用的例子,你知道幾個?
10個 Istio 流量管理 最常用的例子,你知道幾個?
2022-07-06 04:03:00 【萬猫學社】
10 個 Istio 流量管理 最常用的例子,强烈建議收藏起來,以備不時之需。
為了方便理解,以Istio官方提供的Bookinfo應用示例為例,引出 Istio 流量管理的常用例子。
Bookinfo應用的架構圖如下:

其中,包含四個單獨的微服務:
productpage:調用details和reviews兩個服務,用來生成頁面。details:包含了書籍的信息。reviews:包含了書籍相關的評論。它還會調用 ratings 微服務。rating:包含了由書籍評價組成的評級信息。
其中,reviews 服務有 3 個版本:
- v1 版本不會調用
ratings服務。 - v2 版本會調用
ratings服務,並使用 1 到 5 個黑色星形圖標來顯示評分信息。 - v3 版本會調用
ratings服務,並使用 1 到 5 個紅色星形圖標來顯示評分信息。
流量轉移
目標1:把
reviews服務的所有流量都路由到v1版本。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
- labels:
version: v3
name: v3
目標2:把
reviews服務的50%流量轉移到v3版本。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 50
- destination:
host: reviews
subset: v3
weight: 50
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
- labels:
version: v3
name: v3
目標3:把
reviews服務的所有流量都路由到v3版本。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v3
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
- labels:
version: v3
name: v3
基於用戶身份的路由
目標:來自名為 OneMore 的用戶的所有流量都路由到v2版本,其他流量都路由到v1版本。
Istio 對用戶身份沒有任何特殊的內置機制。在應用示例中,productpage服務在所有到 reviews 服務的 HTTP 請求中都增加了一個自定義的 end-user 請求頭,其值為用戶名。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: OneMore
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
- labels:
version: v3
name: v3
注入 HTTP 延遲故障
目標:用戶 OneMore 訪問時,
ratings服務注入一個 2 秒的延遲,productpage頁面在大約 2 秒鐘加載完成並且沒有錯誤。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings
spec:
hosts:
- ratings
http:
- match:
- headers:
end-user:
exact: OneMore
fault:
delay:
percentage:
value: 100.0
fixedDelay: 2s
route:
- destination:
host: ratings
subset: v1
- route:
- destination:
host: ratings
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: ratings
spec:
host: ratings
subsets:
- labels:
version: v1
name: v1
注入 HTTP 中止故障
目標:用戶 OneMore 訪問時,
ratings服務注入一個503的中止故障,productpage頁面能够立即被加載,同時顯示 “Ratings service is currently unavailable” 這樣的消息。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings
spec:
hosts:
- ratings
http:
- fault:
abort:
httpStatus: 503
percentage:
value: 100
match:
- headers:
end-user:
exact: OneMore
route:
- destination:
host: ratings
subset: v1
- route:
- destination:
host: ratings
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: ratings
spec:
host: ratings
subsets:
- labels:
version: v1
name: v1
設置請求超時
首先,用戶 OneMore 訪問時, ratings 服務注入一個 2 秒的延遲,productpage頁面在大約 2 秒鐘加載完成並且沒有錯誤。
按照上文注入 HTTP 延遲故障進行操作,不再贅述。
目標:用戶 OneMore 訪問時,
reviews服務的請求超時設置為 1 秒,同時顯示 “Sorry, product reviews are currently unavailable for this book.” 這樣的消息。
kind: VirtualService
apiVersion: networking.istio.io/v1alpha3
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: OneMore
route:
- destination:
host: reviews
subset: v2
timeout: 1s
- route:
- destination:
host: reviews
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
- labels:
version: v3
name: v3
在Jaeger可以看到具體的調用鏈如下:

設置請求重試
首先,用戶 OneMore 訪問時, ratings 服務注入一個 2 秒的延遲,productpage頁面在大約 2 秒鐘加載完成並且沒有錯誤。
按照上文注入 HTTP 延遲故障進行操作,不再贅述。
目標:用戶 OneMore 訪問時,
reviews服務的請求重試次數為2次,重試超時時間為 0.5 秒,同時顯示 “Sorry, product reviews are currently unavailable for this book.” 這樣的錯誤消息。
kind: VirtualService
apiVersion: networking.istio.io/v1alpha3
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: OneMore
route:
- destination:
host: reviews
subset: v2
retries:
attempts: 2
perTryTimeout: 0.5s
- route:
- destination:
host: reviews
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
- labels:
version: v3
name: v3
拒絕目標IP的請求
目標:除了IP為
10.201.240.131的客戶端可以訪問/api/v1/products/1,其他客戶端拒絕請求。
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: deny-by-ip
spec:
selector:
matchLabels:
app: productpage
action: DENY
rules:
- to:
- operation:
paths: ["/api/v1/products/1"]
when:
- key: remote.ip
notValues: ["10.201.240.131"]
熔斷
目標:設置
details服務的並發上限為1。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: details
spec:
host: details
trafficPolicy:
connectionPool:
tcp:
maxConnections: 1
http:
http1MaxPendingRequests: 1
maxRequestsPerConnection: 1
可以使用 Fortio 進行負載測試,發送並發數為 2 的連接(-c 2),請求 20 次(-n 20):
kubectl exec fortio-deploy-684b6b47f8-tzsg8 -c fortio -- /usr/bin/fortio load -c 3 -qps 0 -n 20 -loglevel Warning http://details:9080/details/0
其中,fortio-deploy-684b6b47f8-tzsg8是Fortio的Pod名稱,效果如下:

流量鏡像
目標:把流量全部路由到reviews服務的 v2 版本,再把流量全部鏡像到 v3 版本。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2
mirror:
host: reviews
subset: v3
mirrorPercentage:
value: 100.0
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
- labels:
version: v3
name: v3
執行如下命令查看reviews服務 v3 版本的 Envoy 訪問日志:
kubectl logs -l app=reviews,version=v3 -c istio-proxy
可以看到reviews服務 v3 版本被調用的日志:
{
"authority": "reviews-shadow:9080",
"bytes_received": 0,
"bytes_sent": 375,
"connection_termination_details": null,
"downstream_local_address": "10.1.1.64:9080",
"downstream_remote_address": "10.1.1.59:0",
"duration": 1914,
"method": "GET",
"path": "/reviews/0",
"protocol": "HTTP/1.1",
"request_id": "b79cefe6-1277-9c39-b398-f94a704840cc",
"requested_server_name": "outbound_.9080_.v3_.reviews.default.svc.cluster.local",
"response_code": 200,
"response_code_details": "via_upstream",
"response_flags": "-",
"route_name": "default",
"start_time": "2022-06-27T07:34:19.129Z",
"upstream_cluster": "inbound|9080||",
"upstream_host": "10.1.1.64:9080",
"upstream_local_address": "127.0.0.6:59837",
"upstream_service_time": "1913",
"upstream_transport_failure_reason": null,
"user_agent": "curl/7.79.1",
"x_forwarded_for": "10.1.1.59"
}
Ingress的路由
目標:請求頭
app-id為details的所有流量都路由到details服務中。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: bookinfo
spec:
hosts:
- '*'
gateways:
- bookinfo-gateway
http:
- match:
- uri:
exact: /productpage
- uri:
prefix: /static
- uri:
exact: /login
- uri:
exact: /logout
- uri:
prefix: /api/v1/products
route:
- destination:
host: productpage
port:
number: 9080
- match:
- headers:
app-id:
exact: details
route:
- destination:
host: details
port:
number: 9080
使用curl命令驗證一下:
curl -H "app-id: details" -v http://127.0.0.1/details/2
返回結果如下:
* Trying 127.0.0.1:80...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /details/2 HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.79.1
> Accept: */*
> app-id: details
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< content-type: application/json
< server: istio-envoy
< date: Tue, 28 Jun 2022 07:14:40 GMT
< content-length: 178
< x-envoy-upstream-service-time: 4
<
{"id":2,"author":"William Shakespeare","year":1595,"type":"paperback","pages":200,"publisher":"PublisherA","language":"English","ISBN-10":"1234567890","ISBN-13":"123-1234567890"}
* Connection #0 to host 127.0.0.1 left intact
返回結果可以看出,訪問的是details服務。
最後,感謝你這麼帥,還給我點贊。
边栏推荐
- 简易博客系统
- C mouse event and keyboard event of C (XXVIII)
- mysql关于自增长增长问题
- 脚本生命周期
- 【leetcode】22. bracket-generating
- Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
- How to standardize the deployment of automated testing?
- Ks008 SSM based press release system
- Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
- Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
猜你喜欢

【FPGA教程案例11】基于vivado核的除法器设计与实现

Database, relational database and NoSQL non relational database

C#(二十九)之C#listBox checkedlistbox imagelist

Tips for using dm8huge table

Blue Bridge Cup - Castle formula

Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)

Ks003 mall system based on JSP and Servlet

在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒

【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用

The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
随机推荐
潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
How does technology have the ability to solve problems perfectly
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
【leetcode】1189. Maximum number of "balloons"
Thread sleep, thread sleep application scenarios
Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
[Massey] Massey font format and typesetting requirements
Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
Chinese brand hybrid technology: there is no best technical route, only better products
Facebook等大廠超十億用戶數據遭泄露,早該關注DID了
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
Mapping between QoE and KQI
【FPGA教程案例11】基于vivado核的除法器设计与实现
C (XXIX) C listbox CheckedListBox Imagelist
TCP/IP协议里面的网关地址和ip地址有什么区别?
What is the difference between gateway address and IP address in tcp/ip protocol?
Proof of Stirling formula
Oracle ORA error message
如何修改表中的字段约束条件(类型,default, null等)