10秒后断开连接
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
timeout: 10s
超时重试处理,默认会在返回code码为5开头时触发
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
retries: // 重试,默认会在返回code码为5开头时触发
attempts: 3 // 给定请求允许的重试次数
perTryTimeout: 2s // 给定请求的每次超时之间的间隔
注入错误延迟
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
fault:
delay: // 延迟
percent: 10 // 将注入延迟的请求百分比(0-100)
fixedDelay: 5s // 在转发请求之前添加固定延迟
注入错误状态码
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
fault:
abort:
percent: 10
httpStatus: 400 // 状态码 400
1. uri 匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
match:
- uri:
prefix: /api/v1
2. headers 匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
match:
- headers:
end-user:
exact: jason
3. sourceLabels 真实标签匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
match:
sourceLabels:
app: reviews
4. 以上都支持精准匹配和正则匹配
FIELDS:
exact <string>
prefix <string>
regex <string>
RE2 style regex-based match (https://github.com/google/re2/wiki/Syntax).
mirror 规则可以是 Envoy 截取所有 request,并在转发请求的同时将 request 转发至 mirror 版本,同时在 Header 的 Host/Authority 加上 -shadow。这些 mirror 请求会工作在 fire and forget 模式,所有的 response 都会被废弃。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
weight: 100 // 镜像要添加权重属性
mirror: // 镜像
host: productpage
subset: v2
路由规则对应着一或多个用 VirtualService 配置指定的请求目的主机。这些主机可以是也可以不是实际的目标负载,甚至可以不是同一网格内可路由的服务。例如要给到 reviews 服务的请求定义路由规则,可以使用内部的名称 reviews,也可以用域名 ,VirtualService 可以定义这样的 host字段:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
gateways:
- mygateway
http:
- match:
- uri:
prefix: /productpage
delegate:
name: productpage
namespace: nsA
- match:
- uri:
prefix: /reviews
delegate:
name: reviews
namespace: nsB
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
namespace: nsA
spec:
http:
- match:
- uri:
prefix: /productpage/v1/
route:
- destination:
host: productpage-v1.nsA.svc.cluster.local
- route:
- destination:
host: productpage.nsA.svc.cluster.local
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
namespace: nsB
spec:
http:
- match:
- uri:
prefix: /reviews/v1/
route:
- destination:
host: reviews-v1.nsB.svc.cluster.local
- route:
- destination:
host: reviews.nsB.svc.cluster.local
http:
- match:
- uri:
exact: /productpage
- uri:
prefix: /static
- uri:
exact: /login
- uri:
exact: /logout
- uri:
prefix: /api/v1/products
- uri:
prefix: / // 越大的规则放后面精细的规则前置
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
trafficPolicy:
loadBalancer:
simple: RANDOM // 随机
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
trafficPolicy:
connectionPool: // 连接池规则
tcp: // HTTP和TCP上游连接通用的设置
maxConnections: 1 // 最大连接数
http: // tcp连接池设置
http1MaxPendingRequests: 1 // 对目标的挂起HTTP请求的最大数量
maxRequestsPerConnection: 1 // 同时处理的最大连接数
outlierDetection: // 断路器(异常值检测)
consecutiveErrors: 1 // 记录错误,累计次数判定为错误
interval: 1s // 探测间隔
baseEjectionTime: 3m // 有问题的 endpoint 会被踢出,踢出多
久
maxEjectionPercent: 100 // 当判定错误后,百分比多少几率剔除后
端 endpoint