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

文章持续更新,微信搜索「万猫学社」第一时间阅读,关注后回复「电子书」,免费获取12本Java必读技术书籍。

设置请求超时

向外部服务 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

返回结果如下:

200
real 0m 5.69s
user 0m 0.00s
sys 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

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

504
real 0m 3.01s
user 0m 0.00s
sys 0m 0.00s

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

文章持续更新,微信搜索「万猫学社」第一时间阅读,关注后回复「电子书」,免费获取12本Java必读技术书籍。

注入 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

返回结果如下:

200
real 0m 0.45s
user 0m 0.00s
sys 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 ,返回结果如下:

200
real 0m 3.43s
user 0m 0.00s
sys 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.

文章持续更新,微信搜索「万猫学社」第一时间阅读,关注后回复「电子书」,免费获取12本Java必读技术书籍。

基于请求头的路由

请求头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.

最后,感谢你这么帅,还给我点赞

微信公众号:万猫学社

微信扫描二维码

关注后回复「电子书」

获取12本Java必读技术书籍

5个 Istio 访问外部服务流量控制最常用的例子,你知道几个?的更多相关文章

  1. istio: 无法提供内部访问外部服务

    现象 能够内部无法访问外部服务. 在部署测试服务 kubectl apply -f samples/sleep/sleep.yaml 设置环境变量 export SOURCE_POD=$(kubect ...

  2. k8s利用endpoints和service访问外部服务

    一.原理解析 在k8s集群中我们通过创建service去访问对应pod内的服务,而在创建service的时候会同时创建一个与service同名的endpoints对象,endpoints与pod实际建 ...

  3. openstack集群访问外部服务出现访问失败

    场景描述: openstack私有云中的容器服务A(部署在openshift上)需要通过http访问阿里云中的B服务,中间需要经过openstack的nat网关,以及阿里云的lb.但在访问时发现访问失 ...

  4. kubernetes集群内通过endpoint访问外部服务

    kubernetes内的服务访问集群外独立的服务最好通过endpoint方式,例如MySQL 1.创建mysql-service.yaml apiVersion: v1 kind: Service m ...

  5. kubernetes 集群内部访问外部的数据库endpoint

    k8s访问集群外独立的服务最好的方式是采用Endpoint方式,以mysql服务为例: 创建mysql-service.yaml apiVersion: v1 kind: Service metada ...

  6. Istio ServiceEntry 引入外部服务

    概念及示例 使用服务入口Service Entry来添加一个入口到 Istio 内部维护的服务注册中心.添加了服务入口后,Envoy 代理可以向服务发送流量,就好像它是网格内部的服务一样.配置服务入口 ...

  7. 使用Istio治理微服务入门

    近两年微服务架构流行,主流互联网厂商内部都已经微服务化,初创企业虽然技术积淀不行,但也通过各种开源工具拥抱微服务.再加上容器技术赋能,Kubernetes又添了一把火,微服务架构已然成为当前软件架构设 ...

  8. idou老师教你学Istio 16:如何用 Istio 实现微服务间的访问控制

    摘要 使用 Istio 可以很方便地实现微服务间的访问控制.本文演示了使用 Denier 适配器实现拒绝访问,和 Listchecker 适配器实现黑白名单两种方法. 使用场景 有时需要对微服务间的相 ...

  9. 基于Istio构建微服务安全加固平台的探索

    简介 An open platform to connect, secure, control and observe services. Istio 是一个由谷歌.IBM 与Lyft共同开发的开源项 ...

  10. Linux iptables 应用控制访问SSH服务

    Title:Linux iptables 应用控制访问SSH服务  --2012-02-23 17:51 今天用到了以前从来没有用到过的,iptables控制访问,只允许外部访问SSH服务(22号端口 ...

随机推荐

  1. 选择KV数据库最重要的是什么

    本文分享自华为云社区<选择KV数据库最重要的是什么?>,作者:GaussDB 数据库 . 经常有客户提到KV数据库,但却偏偏"不要Redis".比如有个做安全威胁分析平 ...

  2. 【ACM算法竞赛日常训练】DAY4题解与分析【树】【子序列】| 组合数学 | 动态规划

    DAY4共2题: 树(组合数学) 子序列(dp,数学) 作者:Eriktse 简介:19岁,211计算机在读,现役ACM银牌选手力争以通俗易懂的方式讲解算法!️欢迎关注我,一起交流C++/Python ...

  3. ASP.NET Core - 缓存之内存缓存(上)

    1. 缓存 缓存指的是在软件应用运行过程中,将一些数据生成副本直接进行存取,而不是从原始源(数据库,业务逻辑计算等)读取数据,减少生成内容所需的工作,从而显著提高应用的性能和可伸缩性,使用好缓存技术, ...

  4. idea 热部署插件JRebel

    idea 热部署插件JRebel ​ 当开始开发web项目的时候,需要频繁的修改web页面,此时如果频繁的重启变得很麻烦,因此,可以在idea中集成JRebel插件,改动代码之后不需要重新启动应用程序 ...

  5. [IDE]IntelliJ IDEA 不能识别 Java 项目 [转]

    本文转载自 IntelliJ IDEA 不能识别 Java 项目 - 博客园/SmartJuneThx 解决方法 非maven项目 在 src 目录上点右键,选择 Mark Directory As ...

  6. vulnhub靶场之ORASI: 1

    准备: 攻击机:虚拟机kali.本机win10. 靶机:Orasi: 1,下载地址:https://download.vulnhub.com/orasi/Orasi.ova,下载后直接vbox打开即可 ...

  7. springboot项目 宿舍管理系统 (源码+数据库文件+1w字论文+ppt)

    来了就点个赞再走呗,即将毕业的兄弟有福了文章底部获取源码springboot项目 宿舍管理系统 (源码+数据库文件+1w字论文+ppt)技术框架:java+springboot+vue+mysql后端 ...

  8. [Tensorflow]模型持久化的原理,将CKPT转为pb文件,使用pb模型预测

    文章目录 [Tensorflow]模型持久化的原理,将CKPT转为pb文件,使用pb模型预测 一.模型持久化 1.持久化代码实现 convert_variables_to_constants固化模型结 ...

  9. [OpenCV-Python] 18 图像梯度

    文章目录 OpenCV-Python:IV OpenCV中的图像处理 18 图像梯度 18.1 Sobel 算子和 Scharr 算子 18.2 Laplacian 算子 OpenCV-Python: ...

  10. 刺激!ChatGPT给我虚构了一本书?

    ChatGPT很强大,可以帮我们处理很多问题,但这些问题的答案的正确性您是否有考证过呢? 昨晚,DD就收到了一个有趣的反馈: 提问:有什么关于数据权限设计的资料推荐吗? ChatGPT居然介绍了一本根 ...