All Products
Search
Document Center

Container Service for Kubernetes:Deploy a gRPC Service in Knative

Last Updated:Jul 04, 2024

Knative supports the HTTP and HTTP/2 protocols, including gRPC. This topic describes how to deploy a gRPC Service in Knative.

Prerequisites

Step 1: Depoy a gRPC Service

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Applications > Knative in the left-side navigation pane.

  3. 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
  4. 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

  1. Visit grpcbin and install a BloomRPC that is suitable for your OS.

  2. 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 to grpcbin.GRPCBin/Index if you use gRPC to access GRPCBin Index over HTTP/2.

  3. Use the BloomRPC to verify the availability of the gRPC Service. The gRPC Service is available if the server returns responses as normal.

    1. Import the gRPC.proto file to the BloomRPC.

    2. 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.

    3. Click the green button and check whether the responses returned by the server are normal.