Use an ASM ingress gateway to securely access gRPC services within your mesh. This enables precise access control, improved service governance, and secure service-to-service communication. This topic shows how to access a gRPC service through an ingress gateway and shift traffic between two versions of the service.
Before you begin
Step 1: Deploy the sample application
Deploy two versions of a sample application: istio-grpc-server-v1 and istio-grpc-server-v2.
-
Create a file named
app.yaml. -
Deploy the application.
kubectl apply -f app.yaml
Step 2: Set the routing rules
Create a gateway, a virtual service, and a destination rule to route all traffic to the istio-grpc-server-v1 service.
-
Create a file named
rules.yaml.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: 0 -
Apply the rules.
kubectl apply -f rules.yaml
Step 3: Deploy or use an ingress gateway
New gateway
Create an ingress gateway and add port 8080 during the Port Mapping configuration.
Existing 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 target gateway. On the Gateway Details page, in the Basic options section, click the
icon next to Port. In the Port Mapping dialog box, click Add Port, set Protocol to TCP and Service Port to 8080, and then click Submit.
Step 4: Run the gRPC client
-
Install the grpcurl command-line tool and run the following command:
grpcurl -d '{"name": "Jack"}' -plaintext {IP address of the ingress gateway}:8080 helloworld.Greeter/SayHello -
The output shows that all requests are routed to
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: Shift traffic with weighted routing
Update the virtual service to route 40% of the traffic to v2 and the remaining 60% to v1.
-
Edit the virtual service.
kubectl edit VirtualService grpc-vsModify the
routesection as follows and save the changes..... route: - destination: host: grpc-helloworld-py port: number: 50051 subset: v1 weight: 60 - destination: host: grpc-helloworld-py port: number: 50051 subset: v2 weight: 40 -
Run the
grpcurlcommand again to access the gRPC service in the mesh.grpcurl -d '{"name": "Jack"}' -plaintext {IP address of the ingress gateway}:8080 helloworld.Greeter/SayHelloThe output shows that approximately 40% of the traffic is routed to
v2.NoteYour results may not show an exact 40/60 split, but the overall traffic distribution will approach this ratio over time.
"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!"