服务网格ASM提供了访问外部服务的三种方式,包含设置外部服务访问策略、配置ServiceEntry和设置拦截对外访问的网段。本文介绍如何在服务网格ASM上访问外部服务。
设置外部服务访问策略
您可以在服务网格ASM中设置
对外部服务的访问策略OutboundTrafficPolicy,用于配置对外部服务(即未在Istio的内部服务注册表中定义的服务)的访问策略。
说明 登录
ASM控制台,在左侧导航栏中单击
概览,选择目标网格实例,您可以查看Istio的内部服务注册表中定义的服务。
- 如果该选项设置为ALLOW_ANY,则Sidecar代理允许对未知服务的透传通过。优点是直接透传对外部服务的访问,缺点是失去了对外部服务流量的网格层面的监视和控制。
- 如果该选项设置为REGISTRY_ONLY,则Sidecar代理将阻止任何没有在网格中定义了HTTP服务或服务条目的主机。
- 登录ASM控制台。
- 在左侧导航栏,选择。
- 在网格管理页面,找到待配置的实例,单击实例的名称或在操作列中单击管理。
- 在网格详情页面右上角单击功能设置。
- 在功能设置更新对话框中单击展开高级选项,设置对外部服务的访问策略OutboundTrafficPolicy为ALLOW_ANY,单击确定。
在注入了Sidecar代理的应用容器中,运行
curl命令请求访问外部HTTP或HTTPS服务,可以看到正常返回结果。
- 访问外部HTTP服务
curl -I http://www.aliyun.com/
HTTP/1.1 301 Moved Permanently
server: envoy
date: Mon, 07 Sep 2020 09:28:54 GMT
content-type: text/html
content-length: 239
location: https://www.aliyun.com/
eagleeye-traceid: 0be3e0a615994709353116335ea5ea
timing-allow-origin: *
x-envoy-upstream-service-time: 67
- 访问外部HTTPS服务
curl -I https://www.aliyun.com/
HTTP/2 200
server: Tengine
date: Mon, 07 Sep 2020 09:16:31 GMT
content-type: text/html; charset=utf-8
vary: Accept-Encoding
vary: Accept-Encoding
strict-transport-security: max-age=31536000
x-download-options: noopen
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
x-readtime: 0
eagleeye-traceid: 0b57ff8715994701916963132ec7ad
strict-transport-security: max-age=0
timing-allow-origin: *
配置ServiceEntry
如果设置
对外部服务的访问策略OutboundTrafficPolicy为
REGISTRY_ONLY,在注入了Sidecar代理的应用容器中,运行
curl命令请求访问外部HTTP或HTTPS服务,可以看到不能正常返回结果。
- 访问外部HTTP服务。
curl -I http://www.aliyun.com/
HTTP/1.1 502 Bad Gateway
date: Mon, 07 Sep 2020 09:25:58 GMT
server: envoy
transfer-encoding: chunked
- 访问外部HTTPS服务。
curl -I https://www.aliyun.com/
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to www.aliyun.com:443
您可以使用ServiceEntry配置,从网格内访问网格外部的可公开访问的服务,同时保留Istio的流量监视和控制功能,实现对外部服务的受控访问。
- 按以下示例创建ServiceEntry,详细介绍请参见管理ServiceEntry。
根据实际需求设置hosts,在本示例中hosts为www.aliyun.com。
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: aliyun-com-ext
spec:
hosts:
- 'www.aliyun.com'
ports:
- number: 80
name: http
protocol: HTTP
- number: 443
name: https
protocol: HTTPS
resolution: DNS
location: MESH_EXTERNAL
- 访问外部HTTP或HTTPS服务,可以看到正常返回结果。
- 访问外部HTTP服务。
curl -I http://www.aliyun.com/
HTTP/1.1 301 Moved Permanently
server: envoy
date: Mon, 07 Sep 2020 09:49:17 GMT
content-type: text/html
content-length: 239
location: https://www.aliyun.com/
eagleeye-traceid: 0be3e0a915994721583014504e7b31
timing-allow-origin: *
x-envoy-upstream-service-time: 66
- 访问外部HTTPS服务。
curl -I https://www.aliyun.com/
HTTP/2 200
server: Tengine
date: Mon, 07 Sep 2020 09:49:31 GMT
content-type: text/html; charset=utf-8
vary: Accept-Encoding
vary: Accept-Encoding
strict-transport-security: max-age=31536000
x-download-options: noopen
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
x-readtime: 1
eagleeye-traceid: 0be3e0b115994721709577294ed9e8
strict-transport-security: max-age=0
timing-allow-origin: *
- 按以下示例创建虚拟服务,详细介绍请参见管理VirtualService。
为使用ServiceEntry配置访问的外部服务设置路由规则。在本示例中,对www.aliyun.com服务的调用注入了延迟时间
fixedDelay的规则。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: aliyun-com-ext
spec:
hosts:
- 'www.aliyun.com'
http:
- fault:
delay:
percent: 100
fixedDelay: 5s
route:
- destination:
host: www.aliyun.com
weight: 100
- 查看定义的路由规则是否生效。
返回结果中real的时间为5.07s,说明路由规则已生效。
time curl -o /dev/null -s -w "%{http_code}\n" http://www.aliyun.com/
301
real 0m 5.07s
user 0m 0.00s
sys 0m 0.00s
设置网格拦截指定地址范围
设置指定的网格拦截IP范围,使得未被指定的其他IP范围不被网格内的Sidecar代理流量拦截,从而可以绕过Sidecar代理直接访问目标服务。
您可以在服务网格ASM中配置拦截对外访问的地址范围,设置流量被拦截的IP范围,通常设置为所管理的Kubernetes集群的Service CIDR。即访问集群内目标服务需要经过服务网格内的Sidecar代理进行流量拦截,非集群内目标则绕过服务网格内的Sidecar代理。
- 登录ASM控制台。
- 在左侧导航栏,选择。
- 在网格管理页面,找到待配置的实例,单击实例的名称或在操作列中单击管理。
- 在网格详情页面右上角单击功能设置。
- 在功能设置更新对话框中单击展开高级选项,在拦截对外访问的地址范围文本框中设置拦截的网段,单击确定。
说明 拦截对外访问的地址范围默认为*,表示对所有的IP网段进行拦截。可以根据实际需要设置拦截的IP网段,一般情况下可以设置为Kubernetes集群的Service CIDR。
在文档使用中是否遇到以下问题
更多建议
匿名提交