Istio 中一个比较重要的概念就是VirtualService , 那么什么是VirtualService?
我们如果单纯从名字来看,感觉可能是一个和kubernetes 中service类似的概念,但实际上VirtualService 和kubernetes中的service是一个完全不同的概念,两者并不是替代关系
那么我们为什么需要VirtualService ? kubernetes中的原生功能不够用么?
其实真的不够用的,例如,我们想更新一个新的版本到我们的服务中,我们想切1%的流量过来,如果用kubernetes 我们怎么实现?
我们需要真正的创造100pod,然后其中一个是新版本,然后就实现了1%的流量的目的,但是我们不可能真的为了升级创建100个pod
VirtualService 就可以帮我们解决这个问题,我们甚至只需要两个pod,也可以控制1%的流量的细粒度控制
如上图,我们有一个service叫:fleetman-staff-service ,在default namespace下
然后我们为了测试我们的新版本,我们弄了两个deployment来创建了3个pod ,其中两个是6-placeholder (旧版本), 一个pod是6(新版本)
并且,新旧pod通过version 标签进行控制(这个标签很重要,后边istio会用这个来决定流量去哪里):
新版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
apiVersion: apps/v1 kind: Deployment metadata: name: staff-service-risky-version spec: selector: matchLabels: app: staff-service replicas: 1 template: # template for the pods metadata: labels: app: staff-service version: risky |
旧版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
apiVersion: apps/v1 kind: Deployment metadata: name: staff-service spec: selector: matchLabels: app: staff-service replicas: 1 template: # template for the pods metadata: labels: app: staff-service version: safe |
如果没有istio,我们会有33%的流量到我们的新版本,这是我们不希望的,我们希望有1%的流量到新版本,99%的流量到旧版本
我们如何通过istio实现呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
kind: VirtualService apiVersion: networking.istio.io/v1alpha3 metadata: name: a-set-of-routing-rules-we-can-call-this-anything # "just" a name for this virtualservice namespace: default spec: hosts: - fleetman-staff-service.default.svc.cluster.local # The Service DNS (ie the regular K8S Service) name that we're applying routing rules to. http: - route: - destination: host: fleetman-staff-service.default.svc.cluster.local # The Target DNS name subset: safe-group # The name defined in the DestinationRule weight: 90 - destination: host: fleetman-staff-service.default.svc.cluster.local # The Target DNS name subset: risky-group # The name defined in the DestinationRule weight: 10 --- kind: DestinationRule # Defining which pods should be part of each subset apiVersion: networking.istio.io/v1alpha3 metadata: name: grouping-rules-for-our-photograph-canary-release # This can be anything you like. namespace: default spec: host: fleetman-staff-service # Service subsets: - labels: # SELECTOR. version: safe # find pods with label "safe" name: safe-group - labels: version: risky name: risky-group |
第一部virtualservice 的定义中,关键的部分有两个:
- 这个virtaulservice 和通过哪个服务来查找对应的pod
- traffic如何发送到对应的pod(例子中是90% 和 10%)
这个地方有一个新概念subset ,类似aws 的target group, 也就是说,如果有流量要发送到fleetman-staff-service.default.svc.cluster.local 这服务对应的pod的时候,90%的发送到safe-group , 10%的发到risky-group
但是,这个时候我们并不知道谁是safe-group ,谁是risky-group
然后我们为了定义subnet,我们创建了DestinationRule
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
--- kind: DestinationRule # Defining which pods should be part of each subset apiVersion: networking.istio.io/v1alpha3 metadata: name: grouping-rules-for-our-photograph-canary-release # This can be anything you like. namespace: default spec: host: fleetman-staff-service # Service subsets: - labels: # SELECTOR. version: safe # find pods with label "safe" name: safe-group - labels: version: risky name: risky-group |
这个里边就简单了,也是两个重要的信息:
- 和哪个服务对应的pod绑定
- 如何选择subnets对应的pod
这个地方要注意,这个地方的lablels是选择器的意思,并不是打标签,也就是说,所有我们这个服务对应的pod重,如果标签中带有 version:safe 的,我们就把他划分到safe-group, 如果标签中带有version: risky ,我们就划分到risky-group中
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021