对于这个问题,我对 Docker 和 AWS 还比较陌生。
目标是创建一个使用基本 Laravel 应用程序运行 Apache 和 PHP 的单个 AWS ECS 实例。我想运行 CloudWatch 代理将所有日志发送到 CloudWatch(Apache 的访问和错误日志、PHP 的错误日志和 Laravel 日志)。
我知道这可能不是“最佳实践”(欢迎提供提示),但我现在的理念是“首先让它工作,然后让它变得漂亮”:-)
我的 Dockerfile:
FROM amazonlinux:latest
# Update/Install
RUN yum update -y && \
# Install PHP & epel
amazon-linux-extras install -y php7.3 epel && \
# Install
yum install -y \
# Install apache
httpd \
# Install tools for CloudWatch
collectd statsd \
# Install supervisor
supervisor \
# Install cloudwatch agent
https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm && \
# Clean install data
yum clean metadata && \
yum -y clean all && \
rm -rf /var/cache/yum
# PHP Settings
RUN sed -i \
'/<Directory \"\/var\/www\/html\">/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' \
/etc/httpd/conf/httpd.conf
# Remove default html folder
RUN rm -rf /var/www/html
# Configure supervisor
COPY supervisord.conf /etc/supervisord.conf
# Configure CloudWatch agent
COPY amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
# Add source to image
ADD . /var/www/aws
RUN chown -R apache:apache /var/www && ln -s /var/www/aws/public /var/www/html
# Expose port 80
EXPOSE 80
# Start supervisor
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]
我的主管.conf
[supervisord]
nodaemon=true
[program:httpd]
priority=1
command=/usr/sbin/apachectl -D FOREGROUND
autorestart=true
username=apache
[program:php]
priority=2
command=/usr/sbin/php-fpm
autorestart=true
[program:cloudformation]
priority=10
command=/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent
autorestart=true
我的云观察配置:
{
"agent": {
"metrics_collection_interval": 60,
"region": "eu-europe-1",
"logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
"debug": false,
"run_as_user": "cwagent"
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/php-fpm/www-error.log",
"log_group_name": "aws-docker",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}
基本上,这作为运行 Laravel 应用程序的 docker 镜像运行良好。我现在遇到的唯一问题是 CloudWatch 代理。它在 ECS 中的容器上启动,但无法运行并显示以下消息:
2020/02/22 13:39:28 I! 2020/02/22 13:39:28 E! ec2metadata is not available
I! Detected the instance is OnPrem
2020/02/22 13:39:28 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json ...
Valid Json input schema.
I! Detecting runasuser...
2020/02/22 13:39:28 E! Credentials path is not set while runasuser is not root
2020/02/22 13:39:28 I! AmazonCloudWatchAgent Version 1.237768.0.
2020/02/22 13:39:28 Configuration validation first phase failed. Agent version: 1.237768.0. Verify the JSON input is only using features supported by this version.
2020/02/22 13:39:28 I! Return exit error: exit code=1
2020/02/22 13:39:28 E! Cannot translate JSON config into TOML, ERROR is exit status 1
首先我不明白为什么会出现这个消息ec2metadata is not available
正在显示。容器在ECS上运行,所以它应该是可用的(据我了解)。
第二条消息Configuration validation first phase failed. Agent version: 1.237768.0. Verify the JSON input is only using features supported by this version.
。据我所知配置应该没问题。
我认为我的角色也可以,因为容器确实将日志发送到CloudWatch。
我究竟做错了什么?