• istio 访问网格内的服务(路由管理)


    本文实验访问网络内的应用,并根据路由规则访问不同版本。
    访问链路:istio-springboot-demo-a > istio-springboot-demo-b

    一、应用准备

    istio-springboot-demo-a 的代码如下:

    @RestController
    @RequestMapping("/test")
    public class TestEndpoint {
    
        private RestTemplate restTemplate = new RestTemplate();
    
        @GetMapping("/request-b")
        public String requestb(@RequestParam("a") String a, @RequestParam(value = "forward", required = false) String forward) {
            String result = restTemplate.getForObject("http://" + forward + "/test/get?a=" + a, String.class);
            return PREFIX + " demo-a request " + forward + ": parameter a:" + a + "\n demo-b response:" + result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    istio-springboot-demo-b 分v1版本和latest版本,代码分别如下。
    istio-springboot-demo-b:v1

    @RestController
    @RequestMapping("/test")
    public class TestEndpoint {
        private static final String PREFIX = "Latest";
    
        @GetMapping("/get")
        public String get(@RequestParam("a") String a) {
            return PREFIX + " demo-b: test get and the parameter a is : " + a;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    istio-springboot-demo-b:latest

    @RestController
    @RequestMapping("/test")
    public class TestEndpoint {
        private static final String PREFIX = "V";
    
        @GetMapping("/get")
        public String get(@RequestParam("a") String a) {
            return PREFIX + " demo-b: test get and the parameter a is : " + a;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    打包成镜像,具体方法参考:Java应用打包成Docker镜像

    二、通过kubernetes Service访问

    istio-springboot-demo-a 的部署过程参考 istio gateway入口流量路由管控

    部署 istio-springbbot-demo-b

    关于istio-springbbot-demo-b的部署过程也是类似的,创建部署istio-springbbot-demo-b需要的yaml文件,内含Service和Deployment。

    demo-b-vault.yaml:

    apiVersion: v1
    kind: Service
    metadata:
      name: istio-springboot-demo-b
      labels:
        app: istio-springboot-demo-b
        service: istio-springboot-demo-b
    spec:
      ports:
      - name: http
        port: 80
        targetPort: 8081
      selector:
        app: istio-springboot-demo-b
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: istio-springboot-demo-b-v1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: istio-springboot-demo-b
          version: v1
      template:
        metadata:
          labels:
            app: istio-springboot-demo-b
            version: v1
        spec:
          serviceAccountName: istio-demo    
          containers:
          - image: demo-istio-springboot-demo-b:1.0-SNAPSHOT
            imagePullPolicy: IfNotPresent
            name: istio-springboot-demo-b
            ports:
            - containerPort: 8081
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: istio-springboot-demo-b-latest
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: istio-springboot-demo-b
          version: latest
      template:
        metadata:
          labels:
            app: istio-springboot-demo-b
            version: latest
        spec:
          serviceAccountName: istio-demo    
          containers:
          - image: demo-istio-springboot-demo-b:latest
            imagePullPolicy: IfNotPresent
            name: istio-springboot-demo-b
            ports:
            - containerPort: 8081
    
    • 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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    执行kubectl 命令进行部署

    kubectl apply -f demo-b-vault.yaml -n istio-demos
    
    • 1

    访问 istio-springbbot-demo-b

    本次实验通过istio-springboot-demo-a 访问网格内的istio-springboot-demo-b,没有配置istio的VirtualService,所以istio-springboot-demo-a 在访问istio-springboot-demo-b时,是随机路由istio-springboot-demo-b的两个版本。

    访问url: http://demoa.istiodemos.com/test/request-b?forward=istio-springboot-demo-b.istio-demos.svc.cluster.local&a=av356
    在这里插入图片描述

    istio-springboot-demo-a和istio-springboot-demo-b是在同一个集群同一个namespace中,以下两个url也是一样可以访问的:

    http://demoa.istiodemos.com/test/request-b?forward=istio-springboot-demo-b.istio-demos&a=av356
    
    http://demoa.istiodemos.com/test/request-b?forward=istio-springboot-demo-b&a=av356
    
    • 1
    • 2
    • 3

    三、制定路由规则

    创建VirtualService,管理istio-springboot-demo-b的路由规则

    istio的VirtualService是在kubernetes 的Service 之上新增的一层,主要负责路由、流控、降级等。

    demo-b-route-virtualservice.yaml 的配置如下 :

    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: istio-springboot-demo-b-dr
    spec:
      host: istio-springboot-demo-b
      trafficPolicy:
        loadBalancer:
            simple: LEAST_CONN
      subsets:
      - name: v1
        labels:
          version: v1
        trafficPolicy:
          loadBalancer:
            simple: ROUND_ROBIN
      - name: latest
        labels: 
          version: latest
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: istio-springboot-demo-b-vs
    spec:
      hosts:
      - "istio-springboot-demo-b"
      - "istio-springboot-demo-b.istio-demos"
      - "istio-springboot-demo-b.istio-demos.svc.cluster.local"
      http:
      - match:
        - queryParams:
            a:
              # exact: ad
              regex: ae\d+$
        route:
        - destination:
            host: istio-springboot-demo-b
            subset: v1
      - route:
        - destination:
            host: istio-springboot-demo-b
            subset: latest
    
    • 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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    路由规则定义如下:

    当queryString a 以ae加数字结尾(例如:ae32, fade32)时,路由到v1版本
    否则都路由到latest版本
    
    • 1
    • 2

    执行kubectl 命令在kubernetes集群内创建VirtualService。

    kubectl apply -f demo-b-route-virtualservice.yaml -n istio-demos
    
    • 1

    访问 istio-springboot-demo-b 的不同版本

    根据上面对istio-springboot-demo-b的路由规则定义:

    当queryString a 以ae加数字结尾(例如:ae32, fade32)时,路由到v1版本
    否则都路由到latest版本
    
    • 1
    • 2

    请求URL : http://demoa.istiodemos.com/test/request-b?forward=istio-springboot-demo-b&a=ae356 必定会被路由到istio-springboot-demo-b的v1版本。

    请求URL : http://demoa.istiodemos.com/test/request-b?forward=istio-springboot-demo-b&a=ae356f 必定会被路由到istio-springboot-demo-b的latest版本。

    接下来的实验也确实如下
    http://demoa.istiodemos.com/test/request-b?forward=istio-springboot-demo-b&a=ae356:
    在这里插入图片描述
    http://demoa.istiodemos.com/test/request-b?forward=istio-springboot-demo-b&a=ae356f:
    在这里插入图片描述

  • 相关阅读:
    产教融合共发展 | 开源网安高校合作战略再下一城
    《设计模式Java版》读书笔记
    超好用的手机开源自动化测试工具分享
    Python网络编程(OSI Socket)
    PowerQuery 多级目录数据合并,并将目录转化为字段
    Fiddler截包后代理转发
    spring 事务源码剖析
    机器学习基础
    uniapp中人脸识别图片并圈起人脸
    互联网技术大佬独立博客推荐
  • 原文地址:https://blog.csdn.net/Mr_rain/article/details/126029873