AWS Elastic Beanstalk Worker - 根据可用队列消息的数量进行扩展

Posted

技术标签:

【中文标题】AWS Elastic Beanstalk Worker - 根据可用队列消息的数量进行扩展【英文标题】:AWS Elastic Beanstalk Worker - Scale Based On Number Of Available Queue Messages 【发布时间】:2015-05-01 18:46:19 【问题描述】:

我目前正在为我的队列使用 AWS 的 Elastic Beanstalk 工作线程,可用于触发自动缩放的指标非常通用(CPU、Net in、Net out 等)。

我很想知道是否可以根据附加到工作人员的队列的状态使用触发器 - 特别是根据过去 X 分钟内队列中可用消息的平均数量添加或删除实例?

【问题讨论】:

【参考方案1】:

如果您使用的是 SQS,这是很有可能的,建议您这样做,如果我没记错的话,这是架构课程中的示例之一。

这里有关于示例的更多信息 https://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/as-using-sqs-queue.html

【讨论】:

您是否设法对此进行了排序?该 SQS 链接似乎没有任何关于专门为 Elastic Beanstalk 工作人员层进行设置的详细信息...【参考方案2】:

由于这是此类搜索的第一次点击,我想更新一个答案。 AWS 现在有一个 .ebextensions 配置示例,它完全符合您的要求:

https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/environment-configuration/workertier-scaleonqueuesize.config

转载于此:

Resources:
  AWSEBCloudwatchAlarmHigh:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmActions: []

  AWSEBCloudwatchAlarmLow:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmActions: []

  QueueDepthAlarmHigh:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: "Alarm if queue depth grows beyond 20 messages"
      Namespace: "AWS/SQS"
      MetricName: ApproximateNumberOfMessagesVisible
      Dimensions:
        - Name: QueueName
          Value:  "Fn::GetAtt": ["AWSEBWorkerQueue", "QueueName"] 
      Statistic: Sum
      Period: 300
      EvaluationPeriods: 1
      Threshold: 20
      ComparisonOperator: GreaterThanThreshold
      AlarmActions:
        - Ref: AWSEBAutoScalingScaleUpPolicy

  QueueDepthAlarmLow:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: "Alarm if queue depth is less than 5 messages"
      Namespace: "AWS/SQS"
      MetricName: ApproximateNumberOfMessagesVisible
      Dimensions:
        - Name: QueueName
          Value:  "Fn::GetAtt": ["AWSEBWorkerQueue", "QueueName"] 
      Statistic: Sum
      Period: 300
      EvaluationPeriods: 1
      Threshold: 5
      ComparisonOperator: LessThanThreshold
      AlarmActions:
        - Ref: AWSEBAutoScalingScaleDownPolicy

基本上它会删除默认警报并根据消息大小制作新警报。我已经测试了此配置,如果您使用 ElasticBeanstalk 中的自动生成队列,它会按原样工作,但如果您指定预先存在的队列,则不会。您可以在此脚本中对队列名称进行硬编码,也可以检索 URL 并对其进行操作以获取名称。我不是 CloudFormation 方面的专家,所以可能有更好的方法来做到这一点,但这是我想出的:

Resources:

  # make the default alarms do nothing (I believe)
  AWSEBCloudwatchAlarmHigh:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmActions: []

  AWSEBCloudwatchAlarmLow:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmActions: []

  # set the High alarm
  QueueDepthAlarmHigh:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: "Alarm if queue depth grows beyond 50 messages"
      Namespace: "AWS/SQS"
      MetricName: ApproximateNumberOfMessagesVisible
      Dimensions:
        - Name: QueueName
          Value:
            "Fn::Select":
              - 4
              - "Fn::Split":
                - '/'
                - "Fn::GetOptionSetting":
                    Namespace: "aws:elasticbeanstalk:sqsd"
                    OptionName: "WorkerQueueURL"
      Statistic: Sum
      Period: 300
      EvaluationPeriods: 1
      Threshold: 50
      ComparisonOperator: GreaterThanThreshold
      AlarmActions:
        - Ref: AWSEBAutoScalingScaleUpPolicy

  # set the Low alarm
  QueueDepthAlarmLow:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: "Alarm if queue depth is less than 5 messages"
      Namespace: "AWS/SQS"
      MetricName: ApproximateNumberOfMessagesVisible
      Dimensions:
        - Name: QueueName
          Value:
            "Fn::Select":
              - 4
              - "Fn::Split":
                - '/'
                - "Fn::GetOptionSetting":
                    Namespace: "aws:elasticbeanstalk:sqsd"
                    OptionName: "WorkerQueueURL"
      Statistic: Sum
      Period: 300
      EvaluationPeriods: 1
      Threshold: 5
      ComparisonOperator: LessThanThreshold
      AlarmActions:
        - Ref: AWSEBAutoScalingScaleDownPolicy

【讨论】:

你能用 cloudformation 代替配置吗? @Eric 你知道一种方法可以让 ebextension 文件只适用于工作环境吗?我显然不希望这些更改也适用于非工作环境。

以上是关于AWS Elastic Beanstalk Worker - 根据可用队列消息的数量进行扩展的主要内容,如果未能解决你的问题,请参考以下文章

AWS Elastic Beanstalk CLI 安装错误

text AWS Elastic Beanstalk

HTTPS Elastic Beanstalk (AWS) 到 Android

AWS Elastic Beanstalk 无法担任角色

AWS Elastic Beanstalk

AWS Elastic Beanstalk chown PythonPath 错误