DevOps

GRPC Configuration Guide for ECS


Problem

To configure the gRPC framework for a single microservice, we found that exposing two different ports for a single ECS container is a problem. The gRPC framework and the application have to be run on individual ports, both ports must be exposed for proper functionality in ECS and ALB.

 

Need

They must be exposed on different ports in the same ECS container for them to work to allow the application and the gRPC framework to operate correctly. This means updating ECS container task definition to identify two ALB target groups, routing both frameworks to the ELB target groups based on the request.

 

Solution

1. Update Dockerfile

  • Modify Dockerfile and expose both Ports, an application port and gRPC framework port.
  • Example Dockerfile snippet:
FROM python:alpineWORKDIR /appCOPY requirements.txt .RUN pip install -r requirements.txtCOPY . /appEXPOSE <application_port> <gRPC_port>CMD [ "python", "server.py" ]

 

  • Make sure that both of the ports you have mentioned in your application settings are exposed and have access.

 

2. Update ECS Task Definition

  • Update the ECS task definition to include both the ports (e.g., 80 and 81).
  • Example JSON snippet for ECS task definition:
"containerDefinitions": [   {     "name": "Container_Name",     "image": "Container_Image_URI",     "portMappings": [       {         "containerPort": 80,         "protocol": "tcp"       },       {         "containerPort": 81         "protocol": "tcp"       }     ],     ...   } ]

 

3. Create Two Target Groups in ALB

  • Create two target groups:

    • Target Group 1: Using HTTP protocol for a python application.

    • Target Group 2: Using HTTP/2 protocol for a gRPC framework.

  • Make sure the second target group has HTTP/2 set as a ProtocolVersion for gRPC compatibility.

4. Set up Listeners and Listener Rules

  1. Listener 1: Create a listener for the HTTP traffic on the application port (80) and associate it with the first target group.
  2. Listener 2: Create another listener for the gRPC framework port (81) and associate it with the second target group. Ensure that this listener is configured for HTTP/2 traffic.

5. Attach Target Groups to ECS Service

The one ECS service that runs the container is attached to both target groups. This lets users route traffic correctly to the python application and gRPC framework.

6. HTTP/2 Configuration for gRPC framework

For the gRPC framework related second target group, the ProtocolVersion  should be set to HTTP2. gRPC target group example CloudFormation configuration:

 

TargetGroup:   Type: "AWS::ElasticLoadBalancingV2::TargetGroup"   Properties:     Name: grpc-target-group     Protocol: HTTP     Port: 81     VpcId: <your-vpc-id>     ProtocolVersion: HTTP2     HealthCheckProtocol: HTTP

 

  Ready to transform your business with our technology solutions? Contact Us  today to Leverage Our DevOps Expertise. 

0

Devops

Related Center Of Excellence