AWS 统一 CloudWatch 代理和 Beanstalk

AWS 统一 CloudWatch 代理和 Beanstalk

AWS 提供了新开发的日志收集器和 CloudWatch 上传器,如下所述:https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/UseCloudWatchUnifiedAgent.html

我的问题是如何使用 Beanstalk 使用这个新代理?目前我们正在使用 EBS 解决方案:64bit Amazon Linux 2018.03 v2.7.1 running Java 8 由于这是 EBS,我不想手动迁移我们的 EC2 机器,因为它们经常被丢弃并由 EBS 重新创建(使手动迁移毫无意义),所以我的理解是,我需要一个具有这个统一代理的新解决方案/AMI 版本,而不是旧版本。我们的动机是旧版本存在错误,有时它会错过上传随机的轮换日志块,导致 1 小时的日志完全丢失。这个错误已由 AWS 验证,他们没有修复此问题,而是编写了一个全新的应用程序,而没有修复旧版本。

本文提到了一个“旧”代理(awslogs),它包含在所提到的 AMI 中,作为内置代理。我尝试阅读 AWS 文档,但没有找到任何提供新版本的官方 AMI 版本。

我真的希望官方 AMI 能提供此上传器,如果是,那么这个 AMI 是什么?谢谢!

附言:我不会为此目的创建自定义 AMI,因为我们希望坚持使用官方的 Amazon Linux AMI。

答案1

您需要通过提供脚本.ebextensions并按照统一代理的命令行安装说明中概述的步骤进行安装:https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html

    files:
      "/opt/elasticbeanstalk/hooks/appdeploy/post/install_cw_unified_agent.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
          # download agent
          wget https://s3.us-east-1.amazonaws.com/amazoncloudwatch-agent-us-east-1/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm

          # install CW agent
          sudo rpm -U ./amazon-cloudwatch-agent.rpm

          # run the agent
          sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-config.json -s


您还需要一个配置文件,您可以在以下文档中找到:https://docs.aws.amazon.com/en_pv/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html

以下是来自文档的示例配置:

    {
      "agent": {
        "metrics_collection_interval": 10,
        "logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
      },
      "metrics": {
        "metrics_collected": {
          "cpu": {
            "resources": [
              "*"
            ],
            "measurement": [
              {"name": "cpu_usage_idle", "rename": "CPU_USAGE_IDLE", "unit": "Percent"},
              {"name": "cpu_usage_nice", "unit": "Percent"},
              "cpu_usage_guest"
            ],
            "totalcpu": false,
            "metrics_collection_interval": 10,
            "append_dimensions": {
              "customized_dimension_key_1": "customized_dimension_value_1",
              "customized_dimension_key_2": "customized_dimension_value_2"
            }
          },
          "disk": {
            "resources": [
              "/",
              "/tmp"
            ],
            "measurement": [
              {"name": "free", "rename": "DISK_FREE", "unit": "Gigabytes"},
              "total",
              "used"
            ],
             "ignore_file_system_types": [
              "sysfs", "devtmpfs"
            ],
            "metrics_collection_interval": 60,
            "append_dimensions": {
              "customized_dimension_key_3": "customized_dimension_value_3",
              "customized_dimension_key_4": "customized_dimension_value_4"
            }
          },
          "diskio": {
            "resources": [
              "*"
            ],
            "measurement": [
              "reads",
              "writes",
              "read_time",
              "write_time",
              "io_time"
            ],
            "metrics_collection_interval": 60
          },
          "swap": {
            "measurement": [
              "swap_used",
              "swap_free",
              "swap_used_percent"
            ]
          },
          "mem": {
            "measurement": [
              "mem_used",
              "mem_cached",
              "mem_total"
            ],
            "metrics_collection_interval": 1
          },
          "net": {
            "resources": [
              "eth0"
            ],
            "measurement": [
              "bytes_sent",
              "bytes_recv",
              "drop_in",
              "drop_out"
            ]
          },
          "netstat": {
            "measurement": [
              "tcp_established",
              "tcp_syn_sent",
              "tcp_close"
            ],
            "metrics_collection_interval": 60
          },
          "processes": {
            "measurement": [
              "running",
              "sleeping",
              "dead"
            ]
          }
        },
        "append_dimensions": {
          "ImageId": "${aws:ImageId}",
          "InstanceId": "${aws:InstanceId}",
          "InstanceType": "${aws:InstanceType}",
          "AutoScalingGroupName": "${aws:AutoScalingGroupName}"
        },
        "aggregation_dimensions" : [["ImageId"], ["InstanceId", "InstanceType"], ["d1"],[]],
        "force_flush_interval" : 30
      },
      "logs": {
        "logs_collected": {
          "files": {
            "collect_list": [
              {
                "file_path": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
                "log_group_name": "amazon-cloudwatch-agent.log",
                "log_stream_name": "amazon-cloudwatch-agent.log",
                "timezone": "UTC"
              },
              {
                "file_path": "/opt/aws/amazon-cloudwatch-agent/logs/test.log",
                "log_group_name": "test.log",
                "log_stream_name": "test.log",
                "timezone": "Local"
              }
            ]
          }
        },
        "log_stream_name": "my_log_stream_name",
        "force_flush_interval" : 15
      }
    }

相关内容