If your Knative Service adopts a distributed system or microservices architecture, you can use the gRPC protocol to enhance communication efficiency between the client and the server. ACK Knative supports the HTTP and HTTP/2 protocols, including gRPC. You can deploy a gRPC Service in the YAML file of a Knative Service, and the Knative gateway can automatically implement gRPC routing.
Prerequisites
Knative has been deployed in your cluster. For more information, see Deploy Knative.
Step 1: Depoy a gRPC Service
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the name of the cluster that you want to manage and choose in the left-side navigation pane.
On the Knative page, click the Services tab. Set Namespace to default, click Create from Template, and then select Custom from the Sample Template drop-down list. Copy the following content to the template editor and click Create to create a Knative Service named helloworld-grpc.
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: helloworld-grpc spec: template: metadata: annotations: autoscaling.knative.dev/class: mpa.autoscaling.knative.dev spec: containers: - image: docker.io/moul/grpcbin # The image contains a tool that is used to test gRPC by providing a gRPC Service to respond to requests. env: - name: TARGET value: "Knative" ports: - containerPort: 9000 name: h2c # Set the name of the gRPC Service in the Knative port section to h2c. protocol: TCP
Add a mapping to the Hosts file. Map the default domain name in the Default Domain column to the gateway address in the Gateway column on the Services tab.
Find the Hosts file on your machine and add a mapping to the Hosts file. The mapping must be in the Gateway address + Space character + Default domain name format. Then, use the default domain name to access the gRPC Service.
Example:
121.xx.xxx.xx helloworld-grpc.default.example.com
Step 2: Verify the availability of the gRPC Service
Visit grpcbin and install a BloomRPC that is suitable for your OS.
Copy the following content to the
gRPC.proto
file and save the file to the on-premises machine:syntax = "proto3"; package grpcbin; service GRPCBin { rpc Index(EmptyMessage) returns (IndexReply) {} // Define an RPC method that takes an empty message as the input and returns an empty message as the output. rpc Empty(EmptyMessage) returns (EmptyMessage) {} // Define an RPC method that takes a dummy message as the input and returns a dummy message as the output. rpc DummyUnary(DummyMessage) returns (DummyMessage) {} // Define an RPC method that takes a dummy message as the input and returns a stream of dummy messages (10 messages) as the output. rpc DummyServerStream(DummyMessage) returns (stream DummyMessage) {} // Define an RPC method that takes a stream of dummy messages (10 messages) as the input and returns a dummy message as the output. rpc DummyClientStream(stream DummyMessage) returns (DummyMessage) {} // Define an RPC method that takes a stream of dummy messages as the input and returns a stream of dummy messages as the output. rpc DummyBidirectionalStreamStream(stream DummyMessage) returns (stream DummyMessage) {} // Define an RPC method that returns a message of the specified gRPC error. rpc SpecificError(SpecificErrorRequest) returns (EmptyMessage) {} // Define an RPC method that returns a message of a random error. rpc RandomError(EmptyMessage) returns (EmptyMessage) {} // Define an RPC method that returns a header. rpc HeadersUnary(EmptyMessage) returns (HeadersMessage) {} // Define an RPC method that does not return any messages. rpc NoResponseUnary(EmptyMessage) returns (EmptyMessage) {} } message HeadersMessage { message Values { repeated string values = 1; } map<string, Values> Metadata = 1; } message SpecificErrorRequest { uint32 code = 1; string reason = 2; } message EmptyMessage {} message DummyMessage { message Sub { string f_string = 1; } enum Enum { ENUM_0 = 0; ENUM_1 = 1; ENUM_2 = 2; } string f_string = 1; repeated string f_strings = 2; int32 f_int32 = 3; repeated int32 f_int32s = 4; Enum f_enum = 5; repeated Enum f_enums = 6; Sub f_sub = 7; repeated Sub f_subs = 8; bool f_bool = 9; repeated bool f_bools = 10; int64 f_int64 = 11; repeated int64 f_int64s= 12; bytes f_bytes = 13; repeated bytes f_bytess = 14; float f_float = 15; repeated float f_floats = 16; } message IndexReply { message Endpoint { string path = 1; string description = 2; } string description = 1; repeated Endpoint endpoints = 2; }
The gRPC Service uses HTTP/2. The path of the gRPC Service is
{Packet name}.{Service name}/{Method name}
. You can configure the Knative gateway to route gRPC packets by setting the Path field to the preceding path. For example, set the Path field togrpcbin.GRPCBin/Index
if you use gRPC to access GRPCBin Index over HTTP/2.Use the BloomRPC to verify the availability of the gRPC Service. The gRPC Service is available if the server returns responses as normal.
Import the
gRPC.proto
file to the BloomRPC.In the left-side navigation pane, click the DummyUnary method and specify
Domain name:Port
in the upper part of the page. Example:helloworld-grpc.default.example.com:80
.Click the green button and check whether the responses returned by the server are normal.
References
You can configure a certificate to access Services over HTTPS through a custom domain name. For more information, see Configure a certificate to access Services over HTTPS.