Use ALB to load balance gRPC traffic

Updated at:
Copy as MD

Use ALB to load balance gRPC traffic and provide a unified ingress for backend service communication. ALB supports HTTPS-encrypted frontend connections with gRPC backend server groups over HTTP/2.

Background information

gRPC is a high-performance, open-source remote procedure call framework. It uses Protocol Buffers as its Interface Definition Language (IDL) and communicates over HTTP/2.

gRPC resolves cross-language communication issues between services. Its IDL provides auto-generated code for defining and calling remote services. By using binary encoding and HTTP/2 multiplexing, gRPC delivers high performance, low latency, and efficient data transfer.

gRPC is suitable for microservices architectures, cross-language communication, and large-scale distributed systems. It supports unary calls, streaming requests and responses, and bidirectional streaming.

For more information about gRPC, see the official gRPC documentation.

Scenario

A company deploys gRPC services in a virtual private cloud (VPC) in the China (Hangzhou) region. In the VPC, an ALB instance and a gRPC-enabled server group are deployed. The ALB instance is configured with an HTTPS listener with HTTP/2 enabled, and the server group is configured with gRPC health checks.

Clients need to access the gRPC services deployed in the VPC through the ALB instance.

Prerequisites

Step 1: Create a server group

  1. Log on to the ALB console.

  2. In the top navigation bar, select the region where the ALB instance is deployed.

  3. In the left-side navigation pane, choose ALB > Server Groups.

  4. On the Server Groups page, click Create Server Group.

  5. In the Create Server Group dialog box, configure the following parameters and click Create.

    This topic describes only the parameters that are relevant to this scenario. For other parameters, use the default values or configure them as needed.

    Parameter

    Description

    Server Group Type

    Select a server group type. In this example, Server is selected.

    VPC

    Select the VPC of your ALB instance. Only servers in this VPC can be added to the server group.

    Backend Server Protocol

    In this example, select gRPC.

    Health Check

    Enable this feature.

    Health Check Settings

    Click Modify and configure the health check settings:

    • Health Check Protocol: In this example, select gRPC.

    • Health Check Method: In this example, select POST.

    • Health Check Status Codes: Select the status code that indicates a healthy response. This must match the status code that is returned by your gRPC service. In this example, the status code is 12.

  6. On the Server Groups page, find the server group that you created and click its ID.

  7. Click the Backend Servers tab and then click Add Backend Server.

  8. In the Add Backend Server panel, select the ECS instances that you created and click Next.

  9. In the Ports/Weights step, set the port and weight for each ECS instance and click OK.

    Note

    The port configured for an ECS instance must match the port used by the gRPC service. You must also ensure that the security group rules for the instance allow traffic on this port.

Step 2: Configure an HTTPS listener

  1. Log on to the Application Load Balancer (ALB) console.

  2. In the top navigation bar, select the region where your instance is deployed.

  3. In the left-side navigation pane, click Instances.

  4. On the Instances page, find the instance that you want to manage and click Create Listener in the Actions column.

  5. On the Configure Listener page, configure the following parameters. Use the default values for other parameters or set them as needed. Then, click Next.

    Parameter

    Description

    Listener Protocol

    Select HTTPS.

    Listener Port

    In this example, port 443 is used.

    Note
    • If the backend protocol of a server group is set to gRPC, the listener protocol must be HTTPS.

    • gRPC communicates over the HTTP/2 protocol. When the backend protocol of a server group is set to gRPC, the HTTPS listener must have HTTP/2.0 enabled. ALB enables this feature by default. Ensure that the Enable HTTP/2 feature is enabled, and do not disable it.

  6. On the Configure SSL Certificate page, configure the following parameter. Use the default values for other parameters or set them as needed. Then, click Next.

    Parameter

    Description

    Server Certificate

    Select the prepared SSL certificate.

  7. On the Select Server Group page, configure the following parameter. Use the default values for other parameters or set them as needed. Then, click Next.

    Parameter

    Description

    Server Group

    Select the gRPC server group that you created.

  8. On the Configuration Review page, check the configuration. If the configuration is correct, click Submit and wait for the listener to be created.

Step 3: Configure DNS resolution

For production, we recommend mapping a custom domain name to the ALB instance's domain name by creating a CNAME record.

  1. In the left-side navigation pane, choose ALB > Instances.

  2. On the Instances page, copy the DNS name of the created ALB instance.

  3. Follow these steps to add a CNAME record.

    Note

    If your domain name is registered by a provider other than Alibaba Cloud, you must add the domain name to the Alibaba Cloud DNS console before you can configure DNS resolution. For more information, see Manage domain names. If your domain name is registered with Alibaba Cloud, proceed with the following steps.

    1. Log on to the Alibaba Cloud DNS console.

    2. On the Authoritative DNS Resolution page, find the target domain name, and click Settings in the Operations column.

    3. On the Settings page, click Add Record.

    4. In the Add Record panel, configure the CNAME record by setting the following parameters, and then click OK.

      Parameter

      Description

      Record Type

      Select CNAME from the drop-down list.

      Hostname

      The prefix of your domain name. This tutorial uses @.

      Note

      To use a root domain, set the hostname to @.

      Query Source

      Select Default.

      Record Value

      Enter the DNS name of the ALB instance that you copied.

      TTL

      Time to live (TTL) is the duration that a DNS record is cached on a DNS server. Use the default value.

Step 4: Verify connectivity

After completing the preceding steps, clients can access the gRPC services through ALB. Use a tool such as grpcurl to test connectivity.

Note

You cannot directly access a gRPC service from a web browser. We recommend that you use a tool such as grpcurl to test connectivity.

On the client, run the grpcurl -insecure -v <Domain name>:<Listener port> <gRPC service name>/<Method> command to access the gRPC service on the ECS instance.

A response similar to the following confirms that clients can access the gRPC service through ALB.

[root@iZbp1xxx.0Z ~]# grpcurl -insecure -v xxx.com:443 helloworld.Greeter/SayHello
Resolved method descriptor:
rpc SayHello ( .helloworld.HelloRequest ) returns ( .helloworld.HelloReply );
Request metadata to send:
(empty)
Response headers received:
accept-encoding: identity,gzip
content-type: application/grpc
date: Mon, 04 Jul 2022 08:53:01 GMT
grpc-accept-encoding: identity,deflate,gzip
vary: Accept-Encoding
Response contents:
{
  "message": "remoteip:47.xxx.xxx.195 x-forwarded-for:47.xxx.xxx.xx5 user-agent:grpcurl/v1.8.0 grpc-go/1.30.0 hostname:iZbp12xxx0kZ server addr:192.168.1.239 "
}
Response trailers received:
(empty)
Sent 0 requests and received 1 response

Related documents