如何监控适用于在 Amazon EC2 实例上运行的 Amazon Linux AMI 的 Elastic Beanstalk 的内存使用量?
上次更新时间:2020 年 10 月 21 日
我的 Amazon Linux Amazon 系统映像 (AMI) 在 AWS Elastic Beanstalk 中的 Amazon Elastic Compute Cloud (Amazon EC2) 实例上运行,但其内存使用量太高且不断增长。我想要监控内存随着时间的推移的使用量。
简短描述
要监控内存 (RAM) 使用量,您必须使用 .ebextensions 文件添加一个自定义的 Amazon CloudWatch 指标。.ebextensions 文件在您的 Amazon EC2 实例环境中运行一个脚本。该脚本会定期发出内存指标和其他的自定义 CloudWatch 指标。
重要提示:以下解决方案中使用的 .ebextension 文件仅适用于 Amazon Linux AMI 实例,不适用于 Elastic Beanstalk 中的 Windows 或自定义 Ubuntu AMI 实例。
解决方法
1. 基于以下示例创建 .ebextensions 文件(如 .ebextensions/cloudwatch.config):
packages:
yum:
perl-DateTime: []
perl-Sys-Syslog: []
perl-LWP-Protocol-https: []
perl-Switch: []
perl-URI: []
perl-Bundle-LWP: []
sources:
/opt/cloudwatch: https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip
container_commands:
01-setupcron:
command: |
echo '*/5 * * * * root perl /opt/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl `{"Fn::GetOptionSetting" : { "OptionName" : "CloudWatchMetrics", "DefaultValue" : "--mem-util --disk-space-util --disk-path=/" }}` >> /var/log/cwpump.log 2>&1' > /etc/cron.d/cwpump
02-changeperm:
command: chmod 644 /etc/cron.d/cwpump
03-changeperm:
command: chmod u+x /opt/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl
option_settings:
"aws:autoscaling:launchconfiguration" :
IamInstanceProfile : "aws-elasticbeanstalk-ec2-role"
"aws:elasticbeanstalk:customoption" :
CloudWatchMetrics : "--mem-util --mem-used --mem-avail --disk-space-util --disk-space-used --disk-space-avail --disk-path=/ --auto-scaling"
注意:对于基于 Amazon Linux 2 的平台,请将前面配置中的 perl-Bundle-LWP yum 程序包替换为 perl-Digest-SHA.x86_64。
2. 为 Elastic Beanstalk 环境的实例配置文件添加以下权限:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"cloudwatch:PutMetricData",
"ec2:DescribeTags"
],
"Effect": "Allow",
"Resource": [
"*"
]
}
]
}
注意:第 2 步中的权限将为您环境的实例提供将第 1 步中的指标发布至 CloudWatch 所需的权限。
3. 创建一个包含步骤 1 中的 .ebextensions 文件的应用程序源包。
4. 部署已更新的 Elastic Beanstalk 应用程序。
5. 在 Linux 系统命名空间下的 Cloudwatch 控制台中查看指标。
注意:关于包含内存使用指标、警报和扩展的配置文件的示例,请参阅 AWS GitHub 上的示例文件。