You can securely access gRPC services in a service mesh through an ASM ingress gateway. This approach enables precise access control for gRPC services, enhances service administration capabilities, and ensures secure inter-service communication. This topic describes how to access gRPC services in a service mesh through an ingress gateway and how to switch traffic between two versions of a gRPC service.
Prerequisites
Step 1: Deploy an application
Deploy sample applications named istio-grpc-server-v1 and istio-grpc-server-v2.
Create app.yaml.
Deploy the application.
kubectl apply -f app.yaml
Step 2: Configure routing rules
Create gateway rules, virtual services, and destination rules to route all traffic to istio-grpc-server-v1.
Create 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: 0Deploy the rules.
kubectl apply -f rules.yaml
Step 3: Deploy a new ingress gateway or reuse an existing one
Create a new ingress gateway
Create an ingress gateway, and add port 8080 when configuring Port Mapping.
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 target gateway. On the Gateway Overview page, in the Basic Options section, click the Port Configuration
icon next to Port Mapping. In the Add Port dialog box, set Protocol to TCP, set Service Port to 8080, and then click Confirm.
Step 4: Run the gRPC client
Install the grpcurl command-line tool and run the following command:
grpcurl -d '{"name": "Jack"}' -plaintext {Ingress gateway IP address}:8080 helloworld.Greeter/SayHelloThe following 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: Transfer a specific ratio of the traffic to Istio-grpc-server-v2
Update the virtual service to route 40% of the traffic to istio-grpc-server-v2 and the remaining 60% to istio-grpc-server-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: 40Continue to use grpcurl to execute the following command to access the gRPC service in the service mesh.
grpcurl -d '{"name": "Jack"}' -plaintext {Ingress gateway IP address}:8080 helloworld.Greeter/SayHelloThe output indicates that 40% traffic is routed to istio-grpc-server-v2.
NoteYour test results may not always show exactly 40 out of 100 requests routed to istio-grpc-server-v2, but the overall ratio will be close to 40%.
"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!"