日志在Elastic Beanstalk Tomcat环境中无法正常工作。

huangapple go评论67阅读模式
英文:

Logs not working on Elastic Beanstalk Tomcat environment

问题

以下是翻译的内容:

我们一直在公司使用Elastic Beanstalk在Tomcat上运行我们的Java(8)应用程序,没有任何问题。现在我们决定升级到Java 11。我们像之前为Java 8应用程序做过的那样,使用CloudFormation设置了我们的环境,但现在我们正在使用这个解决方案栈:'64位Amazon Linux 2 v4.1.1运行Tomcat 8.5 Corretto 11'(也尝试过v4.1.2)。一切都运行正常,但似乎请求日志功能在Elastic Beanstalk中不再起作用(最后100行和完整日志)。我还向应用程序服务器添加了一个密钥对,以便在ec2服务器上的/var/log/tomcat/中检查日志,但我们找不到catalina.out文件(只有带有日期的catalina文件,如:catalina.2020-10-14.log)。这些文件只包含我们使用的库的日志,而不包括我们发送到标准系统输出的日志。我们偶尔合作的另一家公司在这个解决方案栈上遇到了相同的问题。

有没有人在这个解决方案栈上遇到过相同的问题并找到解决方法?

这是我们的CloudFormation配置(YAML):

Application:
  Type: AWS::ElasticBeanstalk::Application
  Properties:
    Description: Application backend

ApplicationVersion:
  Type: AWS::ElasticBeanstalk::ApplicationVersion
  Properties:
    ApplicationName:
      Ref: Application
    Description: !Ref AppVersion
    SourceBundle:
      S3Bucket: !Ref BeanstalkSourceS3
      S3Key: !Ref BuildFileName

AppEC2ServiceRole:
  Type: AWS::IAM::Role
  Properties:
    AssumeRolePolicyDocument:
      Version: '2012-10-17'
      Statement:
        - Effect: Allow
          Principal:
            Service:
              - elasticbeanstalk.amazonaws.com
          Action:
            - sts:AssumeRole
      ManagedPolicyArns:
        - "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth"
        - "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkService"

AppEnvironment:
  Type: AWS::ElasticBeanstalk::Environment
  Properties:
    ApplicationName:
      Ref: Application
    OptionSettings:
      - Namespace: aws:autoscaling:launchconfiguration
        OptionName: InstanceType
        Value: !Ref EC2InstanceSize
      - Namespace: aws:elasticbeanstalk:environment
        OptionName: EnvironmentType
        Value: SingleInstance
      - Namespace: aws:autoscaling:launchconfiguration
        OptionName: IamInstanceProfile
        Value: !Ref AppInstanceProfile
      - Namespace: aws:autoscaling:launchconfiguration
        OptionName: EC2KeyName
        Value: CompanyTestKey
      - Namespace: aws:ec2:vpc
        OptionName: VPCId
        Value: !Ref Vpc
      - Namespace: aws:ec2:vpc
        OptionName: Subnets
        Value: !Join [",",https://forums.aws.amazon.com/
      - Namespace: aws:ec2:vpc
        OptionName: AssociatePublicIpAddress
        Value: true
      - Namespace: aws:elasticbeanstalk:environment
        OptionName: ServiceRole
        Value: !Ref AppEC2ServiceRole
      - Namespace: aws:elasticbeanstalk:cloudwatch:logs
        OptionName: StreamLogs
        Value: true
      - Namespace: aws:elasticbeanstalk:cloudwatch:logs
        OptionName: RetentionInDays
        Value: 14
    SolutionStackName: 64bit Amazon Linux 2 v4.1.1 running Tomcat 8.5 Corretto 11
    VersionLabel:
      Ref: ApplicationVersion
    Tags:
      - Key: group
        Value: !Ref ResourceGroupTagValue
英文:

We've been using Elastic Beanstalk to run our Java (8) applications on Tomcat in our company without any issues. Now we have decided to move on to Java 11. We set up our environments with CloudFormation as we did before for the Java 8 applications but now we are using this solution stack: '64bit Amazon Linux 2 v4.1.1 running Tomcat 8.5 Corretto 11' (also tried v4.1.2). Everything works fine but it looks like the request logs feature isn't working anymore in elastic beanstalk (Last 100 lines and full logs). I also added a keypair to the application server to check the logs on the ec2 server at /var/log/tomcat/ but we can't find the catalina.out file (only catalina files with a date like: catalina.2020-10-14.log). Those files contains only logs of a library we used but not the logs we send to the Standard system output. Another company we sometimes work with experiences the same issue on this solution stack.

Has anyone experienced the same issue on this solution stack and found a fix?

This is our CloudFormation configuration (YAML):

Application:
Type: AWS::ElasticBeanstalk::Application
Properties:
Description: Application backend
ApplicationVersion:
Type: AWS::ElasticBeanstalk::ApplicationVersion
Properties:
ApplicationName:
Ref: Application
Description: !Ref AppVersion
SourceBundle:
S3Bucket: !Ref BeanstalkSourceS3
S3Key: !Ref BuildFileName
AppEC2ServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- elasticbeanstalk.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth"
- "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkService"
AppEnvironment:
Type: AWS::ElasticBeanstalk::Environment
Properties:
ApplicationName:
Ref: Application
OptionSettings:
- Namespace: aws:autoscaling:launchconfiguration
OptionName: InstanceType
Value: !Ref EC2InstanceSize
- Namespace: aws:elasticbeanstalk:environment
OptionName: EnvironmentType
Value: SingleInstance
- Namespace: aws:autoscaling:launchconfiguration
OptionName: IamInstanceProfile
Value: !Ref AppInstanceProfile
- Namespace: aws:autoscaling:launchconfiguration
OptionName: EC2KeyName
Value: CompanyTestKey
- Namespace: aws:ec2:vpc
OptionName: VPCId
Value: !Ref Vpc
- Namespace: aws:ec2:vpc
OptionName: Subnets
Value: !Join [",",https://forums.aws.amazon.com/
- Namespace: aws:ec2:vpc
OptionName: AssociatePublicIpAddress
Value: true
- Namespace: aws:elasticbeanstalk:environment
OptionName: ServiceRole
Value: !Ref AppEC2ServiceRole
- Namespace: aws:elasticbeanstalk:cloudwatch:logs
OptionName: StreamLogs
Value: true
- Namespace: aws:elasticbeanstalk:cloudwatch:logs
OptionName: RetentionInDays
Value: 14
SolutionStackName: 64bit Amazon Linux 2 v4.1.1 running Tomcat 8.5 Corretto 11
VersionLabel:
Ref: ApplicationVersion
Tags:
- Key: group
Value: !Ref ResourceGroupTagValue

答案1

得分: 1

我在一个新的Medium博客中详细说明了Tomcat的所有工作原理,我遇到了类似的问题,尤其是由于S3日志轮转导致的零长度文件。

以下是一个摘录,你可能可以使用,文章解释了如何确定正确的文件夹/文件来进行流式传输。

在这里,我正在流式传输三个不同的文件(它会自动处理你提到的日期),请查看logs.conf 'content' 部分中针对每个需要传输的文件的内容。

不要忘记设置策略和角色,使Elastic Beanstalk EC2实例能够与CloudWatch通信。

packages:
  yum:
    awslogs: []

option_settings:
  - namespace: aws:elasticbeanstalk:cloudwatch:logs
    option_name: StreamLogs
    value: true
  - namespace: aws:elasticbeanstalk:cloudwatch:logs
    option_name: DeleteOnTerminate
    value: false
  - namespace: aws:elasticbeanstalk:cloudwatch:logs
    option_name: RetentionInDays
    value: 90

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/tomcat/localhost.log]
      log_group_name = {"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/tomcat/localhost.log"]]}
      log_stream_name = {instance_id}
      file = /var/log/tomcat/localhost.*

      [/var/log/tomcat/catalina.log]
      log_group_name = {"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/tomcat/catalina.log"]]}
      log_stream_name = {instance_id}
      file = /var/log/tomcat/catalina.*

      [/var/log/tomcat/localhost_access_log.txt]
      log_group_name = {"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/tomcat/access_log"]]}
      log_stream_name = {instance_id}
      file = /var/log/tomcat/access_log.*      

commands:
  "01":
    command: systemctl enable awslogsd.service
  "02":
    command: systemctl restart awslogsd
英文:

I've detailed in a new Medium blog how this all works for Tomcat, I hit similar issues, particularly zero-length files due to S3 Log Rotation.

Below is an excerpt that you might be able to use, the article explains how to determine the right folder/file(s) to stream.

Here I'm streaming three different files (it deals with the dates you mention automatically), review what's under the logs.conf 'content' section for each file that you need to stream.

Don't forget to also setup your policy and role that enables the Elastic Beanstalk EC2 instance to communicate with CloudWatch.

packages:
yum:
awslogs: []
option_settings:
- namespace: aws:elasticbeanstalk:cloudwatch:logs
option_name: StreamLogs
value: true
- namespace: aws:elasticbeanstalk:cloudwatch:logs
option_name: DeleteOnTerminate
value: false
- namespace: aws:elasticbeanstalk:cloudwatch:logs
option_name: RetentionInDays
value: 90
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/tomcat/localhost.log]
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/tomcat/localhost.log"]]}`
log_stream_name = {instance_id}
file = /var/log/tomcat/localhost.*
[/var/log/tomcat/catalina.log]
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/tomcat/catalina.log"]]}`
log_stream_name = {instance_id}
file = /var/log/tomcat/catalina.*
[/var/log/tomcat/localhost_access_log.txt]
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/tomcat/access_log"]]}`
log_stream_name = {instance_id}
file = /var/log/tomcat/access_log.*
commands:
"01":
command: systemctl enable awslogsd.service
"02":
command: systemctl restart awslogsd

huangapple
  • 本文由 发表于 2020年10月22日 19:56:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64481718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定