出现问题,无法从.eextensions/logs.config 创建自定义cloudwatch日志组
以下是我尝试过的不同文件。
1
---
files:
/opt/elasticbeanstalk/tasks/bundlelogs.d/celery_logs.conf:
content: |-
/var/log/celery_beat.stdout.log
/var/log/celery_flower.stdout.log
/var/log/celery_worker.stdout.log
/var/log/faust_worker.stdout.log
group: root
mode: "000755"
owner: root
2
---
files:
"/opt/elasticbeanstalk/config/private/logtasks/bundle/applogs.conf" :
mode: "000755"
owner: root
group: root
content: |
/var/log/celery_beat.stdout.log
/var/log/celery_flower.stdout.log
/var/log/celery_worker.stdout.log
/var/log/faust_worker.stdout.log
3
packages:
yum:
awslogs: []
files:
"/etc/awslogs/awscli.conf" :
mode: "000600"
owner: root
group: root
content: |
[plugins]
cwlogs = cwlogs
[default]
region = `{"Ref":"AWS::Region"}`
"/etc/awslogs/config/logs.conf" :
mode: "000600"
owner: root
group: root
content: |
[/var/log/celery_beat.stdout.log]
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "/var/log/celery_beat.stdout.log"]]}`
log_stream_name = {instance_id}
file = /var/log/celery_beat.stdout.log
[/var/log/celery_flower.stdout.log]
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "/var/log/celery_flower.stdout.log"]]}`
log_stream_name = {instance_id}
file = /var/log/celery_flower.stdout.log
[/var/log/celery_worker.stdout.log]
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "/var/log/celery_worker.stdout.log"]]}`
log_stream_name = {instance_id}
file = /var/log/celery_worker.stdout.log
commands:
"01":
command: systemctl enable awslogsd.service
"02":
command: systemctl restart awslogsd
日志正确显示在文件中:
/var/log/celery_beat.stdout.log
/var/log/celery_flower.stdout.log
/var/log/celery_worker.stdout.log
/var/log/faust_worker.stdout.log
但是没有创建日志组,也没有向其传输日志。
我尝试过15种或更多类似的配置,但运气不佳。
有人留下了答案,但它被删除了,所以我继续,把它留在这里给你们。
我认为OP和我不能让它工作的原因是,我们按照本教程走错了路:
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.logging.html
在本教程中,他们教您在/opt/elasticbeanstalk/tasks/
中使用conf
文件,但这仅用于将日志导出到Elastic Beanstalk web控制台,而不是CloudWatch日志。
要将自定义日志导出到CloudWatch日志中,您必须使用本教程(这是OP问题中的选项#3(
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/AWSHowTo.cloudwatchlogs.html
它特别地具有用于";自定义日志文件流"--这是通过使用CCD_ 3安装名为CCD_。这个过程比简单地放入conf
文件要复杂得多,但这就是必须要做的。
请确保还检查Elastic Beanstalk角色,以确保它具有与CloudWatch日志交互的权限。
最新的EC 2不再使用锥子。SSH到您的实例中,并通过键入检查您的实例是否使用最新的cloudwatch代理
sudo amazon-cloudwatch-agent-ctl -a status
如果它正在运行,请将以下配置文件添加到您的ebextensions中。
container_commands: 01_append_logs_config:
command: |
amazon-cloudwatch-agent-ctl -a append-config -m ec2 -c file:/tmp/custom-cloudwatch-config.json -s
files: "/tmp/custom-cloudwatch-config.json":
mode: "000600"
owner: root
group: root
content: |
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/messages",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/messages"]]}`",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/cfn-init.log",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/cfn-init.log"]]}`",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/cfn-init-cmd.log",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/cfn-init-cmd.log"]]}`",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/dnf.log",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/dnf.log"]]}`",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/eb-cfn-init.log",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/eb-cfn-init.log"]]}`",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/celery_beat.stdout.log",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/celery_beat.stdout.log"]]}`",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/celery_worker.stdout.log",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/celery_worker.stdout.log"]]}`",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/secure",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/secure"]]}`",
"log_stream_name": "{instance_id}"
},
{
"file_path": "/var/log/cron",
"log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/cron"]]}`",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}
检查https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html用于附加将添加自定义日志文件的自定义配置文件。
中解释了配置文件的生成https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file-wizard.html.您可以通过运行向导生成适合您的情况的配置文件,并根据结果更改下面文件的内容。
https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/UseCloudWatchUnifiedAgent.html
最后,确保你的elasticbeanstalliam配置文件有权在你的iam配置中写入cloudwatch日志
Amazon Linux 2不再支持/opt/elasticbeanstall/和awslogs
要配置自定义日志,请遵循以下方法。
- 在应用程序的根目录中创建一个.platform/hooks/postdeploy/
- 根据需要创建一个带有日志路径的shell脚本
- 将shell脚本放在扩展名为.sh的/postdeploy目录中
- 在Elasticbeanstalk控制台配置中设置了一个环境变量,我们稍后将在脚本中获取该变量
- 您应该看到您的自定义日志在cloudwatch中进行流式传输
#!/bin/bash
ENV_NAME=$(/opt/elasticbeanstalk/bin/get-config environment | jq -r '.ENV_NAME')
cat > /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_beanstalk.json <<EOF
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/eb-engine.log",
"log_group_name": "/aws/elasticbeanstalk/${ENV_NAME}/var/log/eb-engine.log",
"log_stream_name": "{instance_id}"
}
{
"file_path": "/var/log/messages",
"log_group_name": "/aws/elasticbeanstalk/${ENV_NAME}/var/log/messages",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}
EOF
sudo sudo systemctl restart amazon-cloudwatch-agent