You can use a Service Mesh (ASM) ingress gateway to access Google Remote Procedure Call (gRPC) services in an ASM instance. This allows you to implement accurate access control on gRPC services, improve service governance, and ensure the security of service-to-service communication. This topic describes how to use an ingress gateway to access a gRPC service in an ASM instance and how to shift traffic between two versions of a gRPC service.
Prerequisites
Step 1: Deploy sample applications
Deploy version 1 and version 2 of a gRPC service: istio-grpc-server-v1 and istio-grpc-server-v2.
Create a file named app.yaml and copy the following content to the file:
Run the following command to deploy the application:
kubectl apply -f app.yaml
Step 2: Configure a routing rule
Create an Istio gateway, a virtual service, and a destination rule for the ASM instance to route all inbound traffic to istio-grpc-server-v1.
Run the following command to create a file named rules.yaml and copy the following content to the file:
apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata: name: grpc-gateway spec: selector: istio: ingressgateway servers: - port: number: 8080 name: grpc protocol: GRPC hosts: - "*" --- apiVersion: networking.istio.io/v1beta1 kind: DestinationRule metadata: name: dr-istio-grpc-server spec: host: grpc-helloworld-py trafficPolicy: loadBalancer: simple: ROUND_ROBIN subsets: - name: v1 labels: version: "v1" - name: v2 labels: version: "v2" --- apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: grpc-vs spec: hosts: - "*" gateways: - grpc-gateway http: - match: - port: 8080 route: - destination: host: grpc-helloworld-py port: number: 50051 subset: v1 weight: 100 - destination: host: grpc-helloworld-py port: number: 50051 subset: v2 weight: 0Run the following command to deploy the routing rule:
kubectl apply -f rules.yaml
Step 3: Create an ingress gateway or use an existing ingress gateway
Create an ingress gateway
Create an ingress gateway and set the Service Port of the ingress gateway to 8080.
Add port 8080 to an existing ingress gateway
Log on to the ASM console. In the left-side navigation pane, choose .
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose .
On the Ingress Gateway page, click the name of the desired gateway. In the Basic options section of the Gateway Details page, click the
icon next to Port. In the Port Mapping dialog box, click Add Port, set Protocol to TCP, set Service Port to 8080, and then click Submit.
Step 4: Start the gRPC client
Install grpcurl command-line tool to run the following command:
grpcurl -d '{"name": "Jack"}' -plaintext {IP address of the ingress gateway}:8080 helloworld.Greeter/SayHelloThe output indicates that all requests are routed to istio-grpc-server-v1.
"message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!"
Step 5: Route traffic to gRPC service v2 based on a specific ratio
Route 40% of the traffic to istio-grpc-server-v2 and 60% of the traffic to istio-grpc-server-v1.
Run the following command to edit virtual services:
kubectl edit VirtualService grpc-vsRun the following command to modify
routeand save the rule:.... route: - destination: host: grpc-helloworld-py port: number: 50051 subset: v1 weight: 60 - destination: host: grpc-helloworld-py port: number: 50051 subset: v2 weight: 40Run the following gRPCurl command to access the gRPC service that you deployed in the ASM instance:
grpcurl -d '{"name": "Jack"}' -plaintext {IP address of the ingress gateway}:8080 helloworld.Greeter/SayHelloThe output indicates that 40% of the traffic is routed to istio-grpc-server-v2.
NoteThe traffic may not be routed to istio-grpc-server-v1 and istio-grpc-server-v2 at an exact ratio of 60:40. However, the overall traffic distribution is always close to this ratio.
"message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v2-7f56b49b7f-9vvr7!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v2-7f56b49b7f-9vvr7!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v2-7f56b49b7f-9vvr7!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v2-7f56b49b7f-9vvr7!" "message": "Hello, Jack! I'm from grpc-helloworld-py-v1-79b5dc9654-cg4dq!"