对于64位的Amazon Linux 2,设置略有不同。
用于传递日志的AWS CloudWatch Agent被安装在/opt/aws/amazon-cloudwatch-agent
中,而Elastic Beanstalk配置则在/opt/aws/amazon-cloudwatch-agent/etc/beanstalk.json
中。它被设置为记录容器的输出,假设存在名为stdouterr.log
的文件,以下是配置的一部分:
{
"file_path": "/var/log/eb-docker/containers/eb-current-app/stdouterr.log",
"log_group_name": "/aws/elasticbeanstalk/EB-ENV-NAME/var/log/eb-docker/containers/eb-current-app/stdouterr.log",
"log_stream_name": "{instance_id}"
}
然而当我查找file_path
时,它并不存在,相反我有一个文件路径编码为当前 Docker 容器 ID /var/log/eb-docker/containers/eb-current-app/eb-e4e26c0bc464-stdouterr.log
。
这个日志文件是由脚本/opt/elasticbeanstalk/config/private/eb-docker-log-start
创建的,该脚本由eb-docker-log
服务启动,此文件的默认内容如下:
EB_CONFIG_DOCKER_CURRENT_APP=`cat /opt/elasticbeanstalk/deployment/.aws_beanstalk.current-container-id | cut -c 1-12`
mkdir -p /var/log/eb-docker/containers/eb-current-app/
docker logs -f $EB_CONFIG_DOCKER_CURRENT_APP >> /var/log/eb-docker/containers/eb-current-app/eb-$EB_CONFIG_DOCKER_CURRENT_APP-stdouterr.log 2>&1
为了暂时修复日志问题,您可以手动运行以下命令(替换docker ID),然后日志将开始在CloudWatch中出现:
ln -sf /var/log/eb-docker/containers/eb-current-app/eb-e4e26c0bc464-stdouterr.log /var/log/eb-docker/containers/eb-current-app/stdouterr.log
为使此更改永久生效,我添加了一个
.ebextension
来修复
eb-docker-log
服务,以便重新创建此链接。在您的源代码中创建一个名为
fix-cloudwatch-logging.config
的文件,并将其放置在
.ebextensions
目录下,然后设置其内容为:
files:
"/opt/elasticbeanstalk/config/private/eb-docker-log-start" :
mode: "000755"
owner: root
group: root
content: |
EB_CONFIG_DOCKER_CURRENT_APP=`cat /opt/elasticbeanstalk/deployment/.aws_beanstalk.current-container-id | cut -c 1-12`
mkdir -p /var/log/eb-docker/containers/eb-current-app/
ln -sf /var/log/eb-docker/containers/eb-current-app/eb-$EB_CONFIG_DOCKER_CURRENT_APP-stdouterr.log /var/log/eb-docker/containers/eb-current-app/stdouterr.log
docker logs -f $EB_CONFIG_DOCKER_CURRENT_APP >> /var/log/eb-docker/containers/eb-current-app/eb-$EB_CONFIG_DOCKER_CURRENT_APP-stdouterr.log 2>&1
commands:
fix_logging:
command: systemctl restart eb-docker-log.service
cwd: /home/ec2-user
test: "[ ! -L /var/log/eb-docker/containers/eb-current-app/stdouterr.log ] && systemctl is-active --quiet eb-docker-log"