Istio VirtualService 结合 DestinationRule 基础使用

ps:istio 的官方提供的 bookinfo 例子已经很齐全了,但我很讨厌拿 bookinfo 当作示例。因为是别人已经提供好的,包括 yaml 文件你自己根本没有从中学习到什么,所以想自己写一些 demoyaml 来使用

step1: 前提

在这里默认你已经自己提供了 demo,才可以往下进行。demo 可以如下图所示:
在这里插入图片描述在这里插入图片描述
中间三个 pod 是我自己提供的 demo 其他的请无视。java-demo-client 是客户端,会调用 java-demo-service 服务端,服务端我提供了两个版本 v1、v2 版本。现在访问一下确定一下部署是否成功了

[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v2
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v2
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v2
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1

这个时候已经是默认的轮询了。

step2: 创建 DestinationRule

如果想要控制路由就必须创建 VirtualServiceVirtualService 实现如何匹配一个流量的路由,DestinationRule 则是定义了收到流量之后做什么?
如果结合使用的话 VirtualService 中的 subset 也需要在 DestinationRule 中定义。重要的内容已经在 yaml 中使用注释表明了。

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: demo-default
  namespace: default
spec:
  # 注意: 如果创建 DestinationRule 没有指定负载均衡算法,此时就是默认的轮询算法,其他还有随机、最少连接、还有一个我忘了
  # 规则的目标
  host: java-demo-service
  subsets:
  - name: v1
    labels:
      # DestinationRule 将匹配下面这个标签的 pod 定义为 v1 版本
      version: v1
  - name: v2
    labels:
      # DestinationRule 将匹配下面这个标签的 pod 定义为 v2 版本
      version: v2
kubectl apply -f destinationrule.yaml
kubectl get dr -n default

在这里插入图片描述
好的,下发成功。

step3: 创建 VirtualService

VirtualService 来更细粒度的匹配请求 DestinationRule 来处理这些请求,下面是定义路由。
下面就是一个最基本的路由,将所有的请求转发到 java-demo-service 的 v1 版本。同样比较重要的提供已经注释提示。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: demo-default
  namespace: default
spec:
  hosts:
  # 规则的目标
  - java-demo-service
  http:
  - route:
    - destination:
        host: java-demo-service
        # DestinationRule 同样需要定义 v1
        subset: v1
kubectl apply -f virtualservice-all-v1.yaml

在这里插入图片描述

step4: 测试

[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1
[root@node4 simple]# curl 10.96.87.54:8083/demo/client/hello?name=zhangsan
张三小企业一v1

没有问题,此时所有的流量都已经转发到 v1 版本。如果需要转发到 v2 的话,更新一下 yaml 文件然后 apply 即可,直接使用 edit 编辑 virtualservice 也行,没了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章