引言
Istio的服务模型是怎么样的?通过什么方式进行流量治理
一、Istio服务模型
服务(Service)与版本(Version):Istio中的服务在kubernetes中以service形式存在,可定义不同的服务版本。通过Deployment创建工作负载,通过Service关联这些负载,域名或者虚拟IP访问后端Pod。
服务实例(ServiceInstance): 一个服务可以包含一组实例,在Kubernetes中用Endpoints实现,一组域名或者IP地址。
服务(Service)示例:
apiVersion: v1
kind: Service
metadata:
name: helloworld
labels:
app: helloworld
service: helloworld
spec:
ports:
- port: 5000
name: http
selector:
app: helloworld
备注:创建一个名称为helloworld的Service,指向“app: helloworld”的Pods,Kubernetes会自动创建一个和Service同名的Endpoints对象,Selector会持续跟踪映射属于helloworld的Pods。
工作负载示例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld-v1
labels:
app: helloworld
version: v1
spec:
replicas: 1
selector:
matchLabels:
app: helloworld
version: v1
template:
metadata:
labels:
app: helloworld
version: v1
spec:
containers:
- name: helloworld
image: docker.io/istio/examples-helloworld-v1
resources:
requests:
cpu: "100m"
imagePullPolicy: IfNotPresent #Always
ports:
- containerPort: 5000
备注:以Deployment方式创建工作负载,关联的版本与镜像。
二、Istio流量治理
通过Isito中VirtualService、DestinationRule、ServiceEntry等配置实现流量治理,即Istio将流量配置通过xDS下发给Enovy,通过拦截Inbound和Outbound流量,在流量经过时执行规则,实现流量治理。
通常流量治理有:动态变更负载均衡策略、不同版本灰度发布、服务治理限流熔断和故障注入演练等。
含义:形式上为虚拟服务,将流量转发到对应的后端服务。
1.1 重要参数说明
备注:VirtualService规则是一个数组,当第一个规则生效后将会跳出,不再检查后面的规则
1.2 VirtualService典型应用
1.3 示例
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: helloworld
spec:
hosts:
- "*"
gateways:
- helloworld-gateway
http:
- match:
- uri:
exact: /hello
route:
- destination:
host: helloworld
port:
number: 5000
含义:通常和VirtualService结合使用,VirtualService描述满足什么条件将流量转发到后端服务,DestinationRule描述到达后端了如何处理,类似于方法内部逻辑。
2.1 重要参数说明
2.2 DestinationRule典型应用
2.3 示例
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: bookinfo-ratings
spec:
host: ratings.prod.svc.cluster.local
trafficPolicy:
loadBalancer:
simple: LEAST_CONN
subsets:
- name: testversion
labels:
version: v3
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN
含义:将网格外服务纳入istio网格。
3.1 重要参数说明
3.2 ServiceEntry典型应用
3.3 示例
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: baidu-external
spec:
hosts:
- www.baidu.com
ports:
- number: 80
name: HTTP
protocol: HTTP
resolution: DNS
location: MESH_EXTERNAL