具有Amazon CloudWatch的Elastic Beanstalk Docker

时间:2022-09-09 23:18:43

I have an Elastic Beanstalk application deployed with a Docker container. The application itself is a Java Application.

我有一个部署了Docker容器的Elastic Beanstalk应用程序。应用程序本身是一个Java应用程序。

My goal is to get the logs to Cloudwatch. In particular I would like to get the stdouterr.log file to Cloudwatch. The file can be found under /var/log/eb-docker/containers/eb-current-app/*

我的目标是将日志记录到Cloudwatch。特别是我想将stdouterr.log文件导入Cloudwatch。该文件可以在/ var / log / eb-docker / containers / eb-current-app / *下找到

I followed the official AWS documentation here. Based on the example configuration files I managed to get the nginx Webrequest to Cloudwatch.

我在这里遵循官方AWS文档。根据示例配置文件,我设法将nginx Webrequest发送到Cloudwatch。

For the EB docker stdouterr log I adapted the cwl-log-setup.config file to the following:

对于EB docker stdouterr日志,我将cwl-log-setup.config文件修改为以下内容:

Mappings:
  CWLogs:
    ApplicationLogGroup:
      LogFile: "/var/log/eb-docker/containers/eb-current-app/*"
      TimestampFormat: "%d/%b/%Y:%H:%M:%S %z"

Outputs:
  ApplicationLogGroup:
    Description: "The name of the Cloudwatch Logs Log Group created for this environments web server access logs. You can specify this by setting the value for the environment variable: WebRequestCWLogGroup. Please note: if you update this value, then you will need to go and clear out the old cloudwatch logs group and delete it through Cloudwatch Logs."
    Value: { "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup"}


Resources :
  AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup:    ## Must have prefix:  AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0
    Type: "AWS::Logs::LogGroup"
    DependsOn: AWSEBBeanstalkMetadata
    DeletionPolicy: Retain     ## this is required
    Properties:
      LogGroupName:
        "Fn::GetOptionSetting":
          Namespace: "aws:elasticbeanstalk:application:environment"
          OptionName: ApplicationLogGroup
          DefaultValue: {"Fn::Join":["-", [{ "Ref":"AWSEBEnvironmentName" }, "webrequests"]]}
      RetentionInDays: 14

The cloudwatch log group is created but no logs arrive. What steps am I missing or what is wrong in my configuration file?

已创建cloudwatch日志组,但没有日志到达。我错过了哪些步骤或配置文件中出了什么问题?

2 个解决方案

#1


5  

I've just encountered the same issue - I managed to get the log files by changing the LogFile configuration to

我刚遇到同样的问题 - 我设法通过将LogFile配置更改为来获取日志文件

Mappings:
  CWLogs:
    WebRequestLogGroup:
      LogFile: "/var/log/eb-docker/containers/eb-current-app/*.log"
      TimestampFormat: "%d/%b/%Y:%H:%M:%S %z"

Note this only works if there is a single log file, if you re-deploy a container or apply a configuration change which results in multiple logs in this directory then only the events from the log file with the most current modified time will be processed by the awslogs agent

请注意,这仅适用于存在单个日志文件的情况,如果重新部署容器或应用配置更改导致此目录中存在多个日志,则只有具有最新修改时间的日志文件中的事件才会被处理awslogs代理

Also by default the agent will compare the first line of the log file to determine if it is a different file, if the first line is the same it will ignore it. You can specify the lines the agent uses to fingerprint the file by adding file_fingerprint_lines configuration,

此外,默认情况下,代理将比较日志文件的第一行以确定它是否是不同的文件,如果第一行是相同的,它将忽略它。您可以通过添加file_fingerprint_lines配置来指定代理用于指纹文件的行,

for example to use lines 1 - 20 to identify the file:

例如,使用第1 - 20行来标识文件:

AWSEBAutoScalingGroup:
    Metadata:
      "AWS::CloudFormation::Init":
        CWLogsAgentConfigSetup:
          files:
            ## any .conf file put into /tmp/cwlogs/conf.d will be added to the cwlogs config (see cwl-agent.config)
            "/tmp/cwlogs/conf.d/apache-access.conf":
              content : |
            [    apache-access_log]
                file = `{"Fn::FindInMap":["CWLogs", "WebRequestLogGroup", "LogFile"]}`
                log_group_name = `{ "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup" }`
                file_fingerprint_lines = 1-20
                log_stream_name = {instance_id}
                datetime_format = `{"Fn::FindInMap":["CWLogs", "WebRequestLogGroup", "TimestampFormat"]}`
              mode  : "000400"
              owner : root
              group : root

#2


0  

I create Dockerrun file for each ElasticBeanstalk application. https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_image.html

我为每个ElasticBeanstalk应用程序创建了Dockerrun文件。 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_image.html

There you can specify a logging configuration:

在那里,您可以指定日志记录配置:

           "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "${you-log-group-name}",
                    "awslogs-region": "${region}",
                    "awslogs-stream-prefix": "${serviceName}"
                }
            }

This sets up Docker's logging driver configuration more details: https://docs.docker.com/config/containers/logging/configure/

这将设置Docker的日志驱动程序配置更多详细信息:https://docs.docker.com/config/containers/logging/configure/

#1


5  

I've just encountered the same issue - I managed to get the log files by changing the LogFile configuration to

我刚遇到同样的问题 - 我设法通过将LogFile配置更改为来获取日志文件

Mappings:
  CWLogs:
    WebRequestLogGroup:
      LogFile: "/var/log/eb-docker/containers/eb-current-app/*.log"
      TimestampFormat: "%d/%b/%Y:%H:%M:%S %z"

Note this only works if there is a single log file, if you re-deploy a container or apply a configuration change which results in multiple logs in this directory then only the events from the log file with the most current modified time will be processed by the awslogs agent

请注意,这仅适用于存在单个日志文件的情况,如果重新部署容器或应用配置更改导致此目录中存在多个日志,则只有具有最新修改时间的日志文件中的事件才会被处理awslogs代理

Also by default the agent will compare the first line of the log file to determine if it is a different file, if the first line is the same it will ignore it. You can specify the lines the agent uses to fingerprint the file by adding file_fingerprint_lines configuration,

此外,默认情况下,代理将比较日志文件的第一行以确定它是否是不同的文件,如果第一行是相同的,它将忽略它。您可以通过添加file_fingerprint_lines配置来指定代理用于指纹文件的行,

for example to use lines 1 - 20 to identify the file:

例如,使用第1 - 20行来标识文件:

AWSEBAutoScalingGroup:
    Metadata:
      "AWS::CloudFormation::Init":
        CWLogsAgentConfigSetup:
          files:
            ## any .conf file put into /tmp/cwlogs/conf.d will be added to the cwlogs config (see cwl-agent.config)
            "/tmp/cwlogs/conf.d/apache-access.conf":
              content : |
            [    apache-access_log]
                file = `{"Fn::FindInMap":["CWLogs", "WebRequestLogGroup", "LogFile"]}`
                log_group_name = `{ "Ref" : "AWSEBCloudWatchLogs8832c8d3f1a54c238a40e36f31ef55a0WebRequestLogGroup" }`
                file_fingerprint_lines = 1-20
                log_stream_name = {instance_id}
                datetime_format = `{"Fn::FindInMap":["CWLogs", "WebRequestLogGroup", "TimestampFormat"]}`
              mode  : "000400"
              owner : root
              group : root

#2


0  

I create Dockerrun file for each ElasticBeanstalk application. https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_image.html

我为每个ElasticBeanstalk应用程序创建了Dockerrun文件。 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_image.html

There you can specify a logging configuration:

在那里,您可以指定日志记录配置:

           "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "${you-log-group-name}",
                    "awslogs-region": "${region}",
                    "awslogs-stream-prefix": "${serviceName}"
                }
            }

This sets up Docker's logging driver configuration more details: https://docs.docker.com/config/containers/logging/configure/

这将设置Docker的日志驱动程序配置更多详细信息:https://docs.docker.com/config/containers/logging/configure/