Istio 访问外部服务流量控制最常用的5个技巧示例

2023-12-07 0 556
目录
  • 环境准备
    • 访问外部服务
    • 设置请求超时
    • 注入 HTTP 延迟故障
    • 流量转移
    • 基于请求头的路由

环境准备

5 个 Istio 访问外部服务的流量控制常用例子,强烈建议收藏起来,以备不时之需。

部署sleep服务,作为发送请求的测试源:

kubectl apply -f samples/sleep/sleep.yaml

在 Istio 外部,使用 Nginx 搭建duckling服务的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.

访问外部服务

执行如下命名访问外部服务 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

返回结果如下:

{ "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 的流量监控和控制特性。创建 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

再此次访问,返回结果如下:

{ "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~……" }}

可以发现由 Istio 边车添加的请求头:X-Envoy-Decorator-Operation。

设置请求超时

向外部服务 httpbin.org 的 /delay 发出请求:

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

返回结果如下:

200real 0m 5.69suser 0m 0.00ssys 0m 0.00s

请求大约在 5 秒后返回 200 (OK)。

创建虚拟服务,访问外部服务 httpbin.org 时, 请求超时设置为 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

再此次访问,返回结果如下:

504real 0m 3.01suser 0m 0.00ssys 0m 0.00s

可以看出,在 3 秒后出现了 504 (Gateway Timeout)。 Istio 在 3 秒后切断了响应时间为 5 秒的 httpbin.org 服务。

注入 HTTP 延迟故障

向外部服务 httpbin.org 的 /get 发出请求:

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

返回结果如下:

200real 0m 0.45suser 0m 0.00ssys 0m 0.00s

请求不到 1 秒就返回 200 (OK)。

创建虚拟服务,访问外部服务 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 ,返回结果如下:

200real 0m 3.43suser 0m 0.00ssys 0m 0.00s

可以看出,在 3 秒后出现了 200 (OK)。

流量转移

访问duckling服务时,所有流量都路由到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

执行如下命名访问外部服务 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/

多次访问后,返回结果一直是:This is the v1 version of duckling.

访问duckling服务时,把50%流量转移到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

多次访问外部服务 duckling.com ,有时返回This is the v1 version of duckling.,有时返回This is the v2 version of duckling.。

访问duckling服务时,所有流量都路由到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

多次访问外部服务 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

执行如下命名访问外部服务 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/

多次访问的返回结果一直是:This is the v1 version of duckling.

设置请求头end-user为OneMore,访问外部服务 duckling.com :

kubectl exec \”$SLEEP_POD\” -c sleep — curl -H \”end-user:OneMore\” -s http://duckling.com/

多次访问的返回结果一直是:This is the v2 version of duckling.

以上就是5个 Istio 访问外部服务流量控制最常用的例子的详细内容,更多关于Istio 访问外部服务流量控制的资料请关注悠久资源网其它相关文章!

您可能感兴趣的文章:

  • Istio 访问外部服务流量控制最常用的5个技巧示例
  • 基于域名的方式访问Istio服务网格中的多个应用程序的方法详解
  • 在Kubernetes集群中搭建Istio微服务网格的过程详解
  • 关于docker部署服务时ip无法访问服务正常的问题
  • springboot部署linux访问服务器资源的方法

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 编程技巧 Istio 访问外部服务流量控制最常用的5个技巧示例 https://www.u-9.cn/biancheng/jiqiao/126112.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务