标签:pre 请求头 turn pos set 好的 形式 server 之间
istio 的几个基本资源对象:
Gateway 在网格边缘接收外部访问,并将流量转发到网格内的服务。Istio 通过Gateway将网格内的服务发布成外部可访问的服务,还可以通过Gateway 配置外部访问的端口、协议及与内部服务的映射关系。
网关功能常见的应用
在实践中。我们更多需要的是1,3两个场景
在dfb服务中。 dfb-login 作为最外层的前端。是几个服务中唯一和外部用户产生交互的服务。 因此我们通过网关功能,将dfb-login 通过域名暴露给用户
创建gateway 的yml文件
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: login-gateway
namespace: dfb-istio
spec:
selector:
istio: ingressgateway
servers:
- hosts:
- istio-login.dfb.com
port:
name: https
number: 443
protocol: HTTPS
tls:
mode: SIMPLE
privateKey: /etc/istio/ingressgateway-certs/dfb.pem
serverCertificate: /etc/istio/ingressgateway-certs/dfb.pem
- hosts:
- istio-login.dfb.com
port:
name: http
number: 80
protocol: HTTP
创建virtualservice.yml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: dfb-login
namespace: dfb-istio
spec:
gateways:
- login-gateway
hosts:
- '*'
http:
- uri:
prefix: /
- route:
- destination:
host: dfb-login.dfb-istio.svc.cluster.local
我们创建了两个资源对象,一个VirtualService 用来描述流量的路由关系。所有url 前缀为/ 的请求都route 到后端dfb-login.dfb-istio.svc.cluster.local 服务。
这个virtuaservice 通过gateways 字段和我们申明的另一个gateway 资源相互绑定。在Gateway 资源对象中。 定义了入口的域名,协议,以及TLS 相关的配置。在istio 中。默认的ingressgateway 会监听gateway 资源对象的变更。多个ingressgateway 通过selector 进行选择.默认ingressgateway 是一个loadbalancer的service 。 我们将这个ingressgateway 修改为宿主机网络,并固定到一个单独的节点。这样我们就可以通过将域名解析到这个node节点的方式访问部署好的入口
我们设置了两个版本的login 服务。 V1 版本用户登录后显示的余额是从数据库取出来的。V2版本则是0。根据这两个版本的差异来判断流量的流向。
首先通过DestinationRule定义两个版本
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: dfb-login
namespace: dfb-istio
spec:
host: dfb-login
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
修改在网关功能中创建的virtualservice
kind: VirtualService
apiVersion: networking.istio.io/v1alpha3
metadata:
name: dfb-login
namespace: dfb-istio
spec:
hosts:
- '*'
gateways:
- login-gateway
http:
- match:
- uri:
prefix: /
route:
- destination:
host: dfb-login.dfb-istio.svc.cluster.local
subset: v1
weight: 50
- destination:
host: dfb-login.dfb-istio.svc.cluster.local
subset: v2
weight: 50
此时多次刷新页面。可以看到用户的现金账户余额在0 和真实额度之间变动
kind: VirtualService
apiVersion: networking.istio.io/v1alpha3
metadata:
name: dfb-login
namespace: dfb-istio
spec:
hosts:
- '*'
gateways:
- login-gateway
http:
- match:
- headers:
version:
exact: v2
route:
- destination:
host: dfb-login.dfb-istio.svc.cluster.local
subset: v2
- route:
- destination:
host: dfb-login.dfb-istio.svc.cluster.local
subset: v1
修改上文yml 。 通过postman请求dfb服务/home页面。默认情况下会路由指向V1版本的login服务。在请求头添加version=v2的header。 则会转到v2的服务。
我们首先在扩容dfb-api 这个服务为2个pod,在其中一个pod中拦截用户资产接口, 直接返回500 ,另一个pod 正常返回。 此时在dfb-login服务中请求。 我们可以发现,接口500 和200 状态码交替出现。
location =/user/account/list/338fbcd2-1be6-4dde-9918-84b7629b1ba5 {
return 500;
}
此时应用下面的重试规则,再次调用接口。此时接口状态码一直返回200.
kind: VirtualService
apiVersion: networking.istio.io/v1alpha3
metadata:
name: dfb-api
namespace: dfb-istio
spec:
hosts:
- dfb-api.dfb-istio.svc.cluster.local
gateways: ~
http:
- route:
- destination:
host: dfb-api.dfb-istio.svc.cluster.local
port:
number: 80
weight: 100
retries:
attempts: 3
perTryTimeout: 2s
retryOn: 5xx,gateway-error,connect-failure,refused-stream
通过jaeger查看调用链,可以看到调用了两次dfb-api。虽然中间调用失败了,但是最终的状态码是200
istio 中硬编码了retryOn 默认值为connect-failure,refused-stream(因为连接失败和流拒接是安全的),如果拦截的时候返回502 , 则istio会自动重试。无法在应用重试规则前观察到200和500 状态码交替出现。
标签:pre 请求头 turn pos set 好的 形式 server 之间
原文地址:https://www.cnblogs.com/itanony/p/11976370.html