服务网格ASM的流量管理功能支持通过入口网关访问内部的gRPC服务。本文通过示例介绍如何通过ASM入口网关访问内部gRPC服务,并在gRPC的两个版本之间进行流量切换。
前提条件
- 已部署入口网关服务。具体操作,请参见创建入口网关服务。
- 已部署应用到ASM企业版或旗舰版实例,且实例为最新版本。具体操作,请参见部署应用到ASM实例。
步骤一:部署示例应用
部署名为istio-grpc-server-v1和istio-grpc-server-v2的示例应用。
- 登录容器服务管理控制台,在左侧导航栏中选择集群。
- 在集群列表页面中,单击目标集群名称,然后在左侧导航栏中,选择 。
- 在无状态页面上方,选择目标命名空间,然后在右上角单击使用YAML创建资源。说明 当前选中的命名空间应当已标注自动注入Sidecar,即包含istio-system=enabled标签。具体操作,请参见升级Sidecar代理。
- 在创建页面,选择自定义示例模板,配置以下YAML,然后单击创建。
apiVersion: apps/v1 kind: Deployment metadata: name: istio-grpc-server-v1 labels: app: istio-grpc-server version: v1 spec: replicas: 1 selector: matchLabels: app: istio-grpc-server version: v1 template: metadata: labels: app: istio-grpc-server version: v1 spec: containers: - args: - --address=0.0.0.0:8080 image: registry.cn-hangzhou.aliyuncs.com/aliacs-app-catalog/istio-grpc-server imagePullPolicy: Always livenessProbe: exec: command: - /bin/grpc_health_probe - -addr=:8080 initialDelaySeconds: 2 name: istio-grpc-server ports: - containerPort: 8080 readinessProbe: exec: command: - /bin/grpc_health_probe - -addr=:8080 initialDelaySeconds: 2 --- apiVersion: apps/v1 kind: Deployment metadata: name: istio-grpc-server-v2 labels: app: istio-grpc-server version: v2 spec: replicas: 1 selector: matchLabels: app: istio-grpc-server version: v2 template: metadata: labels: app: istio-grpc-server version: v2 spec: containers: - args: - --address=0.0.0.0:8080 image: registry.cn-hangzhou.aliyuncs.com/aliacs-app-catalog/istio-grpc-server imagePullPolicy: Always livenessProbe: exec: command: - /bin/grpc_health_probe - -addr=:8080 initialDelaySeconds: 2 name: istio-grpc-server ports: - containerPort: 8080 readinessProbe: exec: command: - /bin/grpc_health_probe - -addr=:8080 initialDelaySeconds: 2 --- apiVersion: v1 kind: Service metadata: name: istio-grpc-server labels: app: istio-grpc-server spec: ports: - name: grpc-backend port: 8080 protocol: TCP selector: app: istio-grpc-server type: ClusterIP ---
步骤二:设置服务网格ASM的路由规则
设置服务网格的服务网关、虚拟服务和目标规则,将流量全部指向istio-grpc-server-v1。
- 登录ASM控制台,在左侧导航栏,选择 。
- 在网格管理页面,找到待配置的实例,单击实例的名称或在操作列中单击管理。
- 创建网关规则。
- 创建目标规则。
- 创建虚拟服务。
步骤三:部署入口网关
在入口网关中,添加端口8080,并指向服务网关的8080端口。
- 登录ASM控制台,在左侧导航栏,选择 。
- 在网格管理页面,单击目标实例名称,然后在左侧导航栏,选择 。
- 在入口网关页面,单击创建。在创建页面,进行相关配置,然后单击创建。部分配置项说明如下。关于配置项的更多信息,请参见入口网关配置项说明。
配置项 说明 部署集群 选择要部署入口网关的集群。 负载均衡类型 此处指定负载均衡的类型为公网访问。 负载均衡 选择负载均衡。 - 使用已有负载均衡:从已有负载均衡列表中选择。
- 新建负载均衡:单击新建负载均衡,从下拉列表中选择所需的负载均衡规格。
说明 建议您为每个Kubernetes服务分配一个SLB。如果多个Kubernetes服务复用同一个SLB,存在以下风险和限制:- 使用已有的SLB会强制覆盖已有监听,可能会导致您的应用不可访问。
- Kubernetes通过Service创建的SLB不能复用,只能复用您手动在控制台(或调用OpenAPI)创建的SLB。
- 复用同一个SLB的多个Service不能有相同的前端监听端口,否则会造成端口冲突。
- 复用SLB时,监听的名字以及虚拟服务器组的名字被Kubernetes作为唯一标识符。请勿修改监听和虚拟服务器组的名字。
- 不支持跨集群复用SLB。
端口映射 单击添加端口,设置名称为tcp,服务端口为8080。
步骤四:运行gRPC客户端
- 启动gRPC客户端。
docker run -d --name grpc-client registry.cn-hangzhou.aliyuncs.com/aliacs-app-catalog/istio-grpc-client 365d
- 登录到容器。
docker exec -it grpc-client sh
- 访问网格内的gRPC服务。
/bin/greeter-client --insecure=true --address=<入口网关的IP地址>:8080 --repeat=100
返回以下结果,可以看到所有的请求都指向了istio-grpc-server-v1。
2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:18:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw
步骤五:按比例将流量路由到v2
将40%的流量指向istio-grpc-server-v2,其余60%的流量仍然指向istio-grpc-server-v1。
- 登录ASM控制台,在左侧导航栏,选择 。
- 在网格管理页面,单击目标实例名称,然后在左侧导航栏,选择 。
- 在虚拟服务页面,在grpc-vs虚拟服务右侧的操作列下,单击查看YAML。
- 在编辑对话框,更新以下YAML内容,单击确定。
.... route: - destination: host: istio-grpc-server subset: v1 weight: 60 - destination: host: istio-grpc-server subset: v2 weight: 40
- 登录到容器,执行以下命令,访问网格内的gRPC服务。
/bin/greeter-client --insecure=true --address=<入口网关的IP地址>:8080 --repeat=100
返回以下结果,可以看到40%的流量指向了istio-grpc-server-v2。说明 您的测试结果不一定总是100次中有40次指向istio-grpc-server-v2,但从总体比例来看,将接近40%。2020/09/11 03:34:51 Hello world from istio-grpc-server-v2-665c4cf57d-h74lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v2-665c4cf57d-h74lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v1-dbdd97cc-n85lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v2-665c4cf57d-h74lw 2020/09/11 03:34:51 Hello world from istio-grpc-server-v2-665c4cf57d-h74lw