Istio 之ServiceEntry
使用服务条目资源(ServiceEntry)可以将条目添加到 Istio 内部维护的服务注册表中。添加服务条目后,Envoy 代理可以将流量发送到该服务,就好像该服务条目是网格中的服务一样。通过配置服务条目,可以管理在网格外部运行的服务的流量。
此外,可以配置虚拟服务和目标规则,以更精细的方式控制到服务条目的流量,就像为网格中的其他任何服务配置流量一样。
serviceentry样例
- client.yaml # istio 要注入的客户端资源文件
- baidu-se.yaml # baidu ServiceEntry
- baidu-dr.yaml # baidu DestinationRule
- baidu-vs.yaml # baidu VirtualService
Sidercar 注入
手动注入
# istioctl kube-inject -f xxx.yaml|kubectl apply -f -
自动注入
# kubectl label namespace default istio-injection=enabled
服务条目资源
client.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: client
spec:
replicas: 1
selector:
matchLabels:
app: client
template:
metadata:
labels:
app: client
spec:
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","sleep 3600"]
# kubectl get po
NAME READY STATUS RESTARTS AGE
client-86bc9bd5f-mj2pq 2/2 Running 0 11m
Istio 注入后,client 就处于 Istio 服务网格之中。
baidu-se.yaml
apiVersion: networking.istio.io/v1beta1
kind: ServiceEntry
metadata:
name: baidu-se
spec:
hosts:
- www.baidu.com
location: MESH_EXTERNAL # 定义网格外部还是内部,表示服务在网格外部。通常用于指示通过API使用的外部服务。
# location: MESH_INTERNAL # 表示服务是网格的一部分。通常用于指示在扩展服务网格以包括不受管理的基础架构时显式添加的服务
ports:
- name: http
number: 80
protocol: HTTP
resolution: DNS
该服务条目资源定义了一个外部网站 baidu,并将它纳入到 Istio 内部维护的服务注册表中。
# kubectl get se
NAME HOSTS LOCATION RESOLUTION AGE
baidu-se [www.baidu.com] MESH_EXTERNAL DNS 53m
- hosts:DNS名称。可以具有通配符前缀。
- ports:关联的端口。
- ports.protocol: 以下之一:HTTP,HTTPS,HTTP2,GRPC,MONGO,TCP或TLS。
- exportTo:默认情况下使用“*”,这意味着该ServiceEntry公开给每个命名空间。 “.”仅将其限制为当前命名空间。目前,exportTo值仅限于这两个。
- resolution:主机的服务发现模式
- location:从网格的角度来看,应将此服务视为内部或外部服务。
验证服务条目案例
# kubectl exec -it $(kubectl get pods | grep -i client | awk '{print $1}') -- sh
# wget -q -O - http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=truew.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_tr class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a hrp://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: bloc">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
对刚才编写的 ServiceEntry 资源做一些改动
baidu-se-gai.yaml
...
#resolution: DNS
resolution: STATIC # 静态
endpoints:
- address: 10.10.10.10 # 自定义一个内网的ip
验证
# kubectl exec -it $(kubectl get pods | grep -i client | awk '{print $1}') -- sh
Defaulting container name to busybox.
Use 'kubectl describe pod/client-86bc9bd5f-mj2pq -n default' to see all of the containers in this pod.
/ # wget -q -O - http://www.baidu.com
wget: server returned error: HTTP/1.1 503 Service Unavailable
- 出现此问题的原因是,serviceentry 一直都在发挥作用,前面没报错的原因是默认指定的域名解析是基于DNS的。而调整后,设置了静态域名解析的方式,并随意给了一个内网IP来标识baidu。kubectl apply serviceentry后此配置立刻就被应用在网格内(client)的 envoy,那么在网格内访问baidu的时候,流量就被路由到了所指定的 10.10.10.10去了。
精细的流控
使用 service entry 使用场景有哪些?这里假设一个场景,比如工作过程中需要调用外部合作方服务,该服务跟你的集群毫无关系,甚至对方服务可以布置在美国。但是通过服务条目,你可以将对方服务纳入到自己的 Istio 网格之内,就像它本身存在你的集群之内一样,就好像你做了内网拦截一样。而且服务条目可以结合虚拟服务(virtual service)、目的地规则(destination rule)做更加精细的流量控制,不仅如此,还可以做失败注入、重试等功能。
baidu-se.yaml
apiVersion: networking.istio.io/v1beta1
kind: ServiceEntry
metadata:
name: baidu-se
spec:
hosts:
- www.baidu.com
location: MESH_EXTERNAL
ports:
- name: http
number: 80
protocol: HTTP
resolution: DNS
# kubectl get se
NAME HOSTS LOCATION RESOLUTION AGE
baidu-se [www.baidu.com] MESH_EXTERNAL DNS 67m
# kubectl exec -it $(kubectl get pods | grep -i client | awk '{print $1}') -- sh
Defaulting container name to busybox.
Use 'kubectl describe pod/client-86bc9bd5f-mj2pq -n default' to see all of the containers in this pod.
/ # wget -q -O - http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=truew.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_tr class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a hrp://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: bloc">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
baidu-dr.yaml
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: baidu-dr
spec:
host: www.baidu.com
trafficPolicy: # 流量策略,包括:负载平衡策略、连接池大小、异常检测
loadBalancer: # 默认LB策略
simple: ROUND_ROBIN # ROUND_ROBIN-循环,LEAST_CONN-最小连接,RANDOM-随机,PASSTHROUGH-只连
# kubectl get dr
NAME HOST AGE
baidu-dr www.baidu.com 54m
baidu-vs.yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: baidu-vs
spec:
hosts:
- www.baidu.com
http:
- route:
- destination:
host: www.baidu.com
port:
number: 80
#subset: tls-origination
timeout: 1ms
# kubectl get vs
NAME GATEWAYS HOSTS AGE
baidu-vs [www.baidu.com] 54m
再次测试
# kubectl exec -it $(kubectl get pods | grep -i client | awk '{print $1}') -- sh
Defaulting container name to busybox.
Use 'kubectl describe pod/client-86bc9bd5f-mj2pq -n default' to see all of the containers in this pod.
/ # wget -q -O - http://www.baidu.com
wget: server returned error: HTTP/1.1 408 Request Timeout
!经过简单的测试可以对网格外部服务进行精细的流控
Istio 之ServiceEntry的更多相关文章
- 早上好,我是 Istio 1.1
1性能增强 虽然Istio1.0的目标是生产可用,但从去年7月份发布以来,在性能和稳定性上并不能让用户满意.社区的Performance and Scalability工作组在Istio v1.1中做 ...
- 深入理解Istio核心组件之Pilot
Istio作为当前服务网格(Service Mesh)领域的事实标准,流量治理(Traffic Management)是其最为基础也最为重要的功能.本文将结合源码对Istio流量治理的实现主体——组件 ...
- Istio ServiceEntry 引入外部服务
概念及示例 使用服务入口Service Entry来添加一个入口到 Istio 内部维护的服务注册中心.添加了服务入口后,Envoy 代理可以向服务发送流量,就好像它是网格内部的服务一样.配置服务入口 ...
- istio服务条目(ServiceEntry)介绍
使用服务条目资源(ServiceEntry)可以将条目添加到 Istio 内部维护的服务注册表中.添加服务条目后,Envoy 代理可以将流量发送到该服务,就好像该服务条目是网格中的服务一样.通过配置服 ...
- Istio实践(4)- 故障注入、熔断及ServiceEntry
前言:接上一篇istio多服务应用部署及调用,本文介绍通过流量管理(故障注入.请求超时等)以及ServiceEntry外部服务部署应用 1.设置服务延迟 修改springbootapp-vs-v1.y ...
- istio路由配置
istio路由配置 istio的代理配置参考文档: 中文文档: https://istio.io/zh/docs/reference/config/istio.networking.v1alpha ...
- Istio如何使用相同的端口访问网格外服务
1.1.背景 写这篇文章的目的是为了说明以下问题:如何使用TCP协议相同的端口访问网格外多个服务? 这是最近直播的时候有一个同学提出的,当时我没有完全明白,“访问多集群” 的意思.后来仔细思考了一下, ...
- idou老师教你学Istio: 如何用Istio实现K8S Egress流量管理
本文主要介绍在使用Istio时如何访问集群外服务,即对出口流量的管理. 默认安装的Istio是不能直接对集群外部服务进行访问的,如果需要将外部服务暴露给 Istio 集群中的客户端,目前有两种方案: ...
- Istio流量管理实现机制深度解析
https://zhaohuabing.com/post/2018-09-25-istio-traffic-management-impl-intro/TOC 前言 Pilot高层架构 统一的服务模型 ...
随机推荐
- Gym102012A Rikka with Minimum Spanning Trees
题意 \(T\) 组数据,每组数据给定一个 \(n\) 个点,\(m\) 条边,可能含有重边自环的图,求出最小生成树的个数与边权和的乘积,对 \(10^9+7\) 取模. \(\texttt{Data ...
- Kubernetes YAML最佳实践和策略
Kubernetes工作负载最常用YAML格式的文件来定义. YAML的问题之一就是很难描述清单文件之间的约束或关系. 如果你希望检查是否已从受信任的注册表中提取部署到群集中的所有映像,该怎么办? 如 ...
- @Transactional 注意事项、方法调用
1.同一个类中,即A与B在同一类中,A()调用B()方法,A不加 @Transactional 事务注解,B加 @Transactional 事务注解,则B中的事务不起作用,A加事务,才会起作用,B中 ...
- vue脚手架项目搭建失败
可能是ssl 的问题 解决办法: 1. npm config set strict-ssl false 2. npm install -g supervisor 以上完成后vue ui 或vue cr ...
- ThreadLocal原理大解析
今天呢,和大家聊一下ThreadLocal. 1. 是什么? JDK1.2提供的的一个线程绑定变量的类. 他的思想就是:给每一个使用到这个资源的线程都克隆一份,实现了不同线程使用不同的资源,且该资源之 ...
- 【Mycat】作为Mycat核心开发者,怎能不来一波Mycat系列文章?
写在前面 Mycat是基于阿里开源的Cobar产品而研发,Cobar的稳定性.可靠性.优秀的架构和性能以及众多成熟的使用案例使得Mycat一开始就拥有一个很好的起点,站在巨人的肩膀上,我们能看到更远. ...
- DevExpress XtraReport报表预览时可编辑的功能
设置控件的EditOptions.Enabled=true即可 注册实现PrintingSystem. EditingFieldChanged事件,可获得当前发生更改的控件的值. 另:XtraRepo ...
- [MIT6.006] 5. Binary Search Trees, BST Sort 二分搜索树,BST排序
第5节课主要讲述了二分搜索树概念和BST排序.讲师提出一个关于"跑道预订系统"的问题,假设飞机场只有一个跑道,飞机需要为未来降落时间t进行预订,如果时间集合R中,在t时间前后k分钟 ...
- 睿象云:为什么 Zabbix 告警如此火热?
每当我们谈及监控工具的时候,Zabbix 总是最惹人瞩目的那一个.如同清晨荷叶上的剔透露珠,卓尔不凡:如同巷子末头的百年酒香,让人倾心.我们都知道 Zabbix 是监控工具里当仁不让的龙头大哥,却没几 ...
- python之 socketserver模块的使用
在我们正常的使用socket模块来写一个server的程序就会显得比较的复杂通常一般流程为 1.生成socket实例对象 2.绑定地址 3.开始监听 4.接收数据 一般demo为 # 服务器 impo ...