Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

Flask application: Using gunicorn on ECS Fargate , How to

Info:

  • I created a flask app and on my Dockerfile last command CMD gunicorn -b 0.0.0.0:5000 --access-logfile - "app:create_app()"
  • I build,tag and upload image on ECR
  • I used this docker image to create an ECS Fargate instance with the following configs (just posting the one needed for the question):
ECSTaskDefinition: 
  Type: AWS::ECS::TaskDefinition  
  Properties:  
    Cpu: "256"  
    Memory: "1024"  
    RequiresCompatibilities:
      - FARGATE
    ContainerDefinitions:
      - Name: contained_above
            .
            .
            .
ECSService:
  Type: AWS::ECS::Service
  DependsOn: ListenerRule
  Properties:
    Cluster: !Sub "${EnvName}-ECScluster"
    DesiredCount: 1
    LaunchType: FARGATE
    DeploymentConfiguration:
      MaximumPercent: 200
      MinimumHealthyPercent: 50
    NetworkConfiguration:
      AwsvpcConfiguration:
        AssignPublicIp: ENABLED
        Subnets:
          - Fn::ImportValue: !Sub "${EnvName}-PUBLIC-SUBNET-1"
          - Fn::ImportValue: !Sub "${EnvName}-PUBLIC-SUBNET-2"
        SecurityGroups:
          - Fn::ImportValue: !Sub "${EnvName}-CONTAINER-SECURITY-GROUP"
      ServiceName: !Sub "${EnvName}-ECS-SERVICE"
      TaskDefinition: !Ref ECSTaskDefinition
      LoadBalancers:
        - ContainerName: contained_above
          ContainerPort: 5000
          TargetGroupArn: !Ref TargetGroup

(App is working normally)

Question

Now my question is what number should be the workers on gunicorn command (my last command in dockerfile)?

On gunicorn design it is stated to use Generally we recommend (2 x $num_cores) + 1 as the number of workers to start off with.

So whats the number of cores on a fargate? Does actually make sense to combine gunicorn with Fargate like the above process? Is there 'compatibility' between loadbalancers and gunicorn workers? What is the connection between DesiredCount of ECS Service and the gunicorn -w workers value? Am I missing or miss-understanding something?

Possible solution(?)

One way that I could call it is the following:

CMD gunicorn -b 0.0.0.0:5000 -w $(( 2 * `cat /proc/cpuinfo | grep 'core id' | wc -l` + 1 )) --access-logfile - "app:create_app()"

But I am not sure if that would be a good solution. Any insights? Thanks

question from:https://stackoverflow.com/questions/65905818/flask-application-using-gunicorn-on-ecs-fargate-how-to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...