【问题标题】:How to start the cloudwatch agent in container?如何在容器中启动 cloudwatch 代理?
【发布时间】:2020-01-27 18:29:52
【问题描述】:

从 docker hub 有一个 image 由亚马逊维护。

任何人都知道如何配置和启动容器,因为我找不到任何文档

【问题讨论】:

    标签: docker amazon-ec2 amazon-cloudwatch


    【解决方案1】:

    你只是用log-opt来运行容器,因为日志代理是容器的主进程。

    docker run --log-driver=awslogs --log-opt awslogs-region=us-west-2 --log-opt awslogs-group=myLogGroup amazon/cloudwatch-agent
    

    您可以在herehere 找到更多详细信息。

    我不知道你为什么需要一个容器中的代理,但最好的做法是将每个容器日志直接发送到cloud watch using aws log driver.

    顺便说一句,这是容器的入口点。

      "Entrypoint": [
             "/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent"
     ],
    

    您只需拨打电话

    /opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent
    

    【讨论】:

    • 再次感谢,但您给出的示例是使用我不需要的 awslogs 驱动程序运行任何容器,由于业务需求,我需要一个 cloudwatch 代理:(
    【解决方案2】:

    试试这个dockerfile

    FROM amazonlinux:2.0.20190508
    RUN yum -y install https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
    COPY agent.json  /opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.json
    ENV RUN_IN_CONTAINER=True
    ENTRYPOINT ["/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent"]
    

    json 示例:

      {
          "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": [
                  "/",
       .
       .
       .
       .
    }
    

    【讨论】:

    • 因此错误而失败Invalid json format, please check. Reason: invalid character ':' after top-level va,我使用的示例 json 格式是这个"agent": { "metrics_collection_interval": 60, "region": "us-west-1", "logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log", "debug": false, "run_as_user": "cwagent" }
    • dint 看到那个小错误,但它仍然失败Reading json config file path: /opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.json ... Cannot access /etc/cwagentconfig: lstat /etc/cwagentconfig: no such file or directoryValid Json input schema. No csm configuration found. No log configuration found. 正式看到一些文档会很棒:(
    【解决方案3】:

    这是来自官方文档:

    sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:configuration-file-path -s
    

    这里是文档:

    https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html#start-CloudWatch-Agent-EC2-commands-fleet

    安装路径可能不同,但这就是根据文档启动代理的方式。

    【讨论】:

      【解决方案4】:

      我得到了这个工作!当你看到Reading json config file path: /opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.json ... Cannot access /etc/cwagentconfig: lstat /etc/cwagentconfig: no such file or directoryValid Json input schema.时,我和你有同样的问题

      你需要做的就是把你的配置文件放在 /etc/cwagentconfig.一个正常运行的 dockerfile:

      FROM amazon/cloudwatch-agent:1.230621.0
      COPY config.json /etc/cwagentconfig
      

      其中 config.json 是一些 cloudwatch 代理配置,例如 LinPy 的回答。

      您可以忽略有关/opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.json 的警告,或者您也可以将 config.json 文件复制到 dockerfile 中的该位置。

      我还将分享我是如何找到这个答案的:

      我需要在 ECS 中将它作为 sidecar 运行,而我只能找到有关如何在 kubernetes 中运行它的文档。按照这个文档:https://docs.aws.amazon.com/en_pv/AmazonCloudWatch/latest/monitoring/Container-Insights-setup-StatsD.html 当我看到这个时,我决定下载所有示例 k8s 清单:

      apiVersion: v1
      kind: Pod
      metadata:
        namespace: default
        name: amazonlinux
      spec:
        containers:
          - name: amazonlinux
            image: amazonlinux
            command: ["/bin/sh"]
            args: ["-c", "sleep 300"]
          - name: cloudwatch-agent
            image: amazon/cloudwatch-agent
            imagePullPolicy: Always
            resources:
              limits:
                cpu:  200m
                memory: 100Mi
              requests:
                cpu: 200m
                memory: 100Mi
            volumeMounts:
              - name: cwagentconfig
                mountPath: /etc/cwagentconfig
        volumes:
          - name: cwagentconfig
            configMap:
              name: cwagentstatsdconfig
        terminationGracePeriodSeconds: 60
      

      所以我看到卷挂载 cwagentconfig 挂载到 /etc/cwagentconfig 并且来自 cwagentstatsdconfig 配置映射,那是只是 json 文件。

      【讨论】:

      • 在 sidecar 容器中运行 CW 代理有什么运气吗?
      【解决方案5】:

      Here 是我在没有systemctl 或 System V init 的情况下在我们的 Docker 容器中工作的方式。

      【讨论】:

        猜你喜欢
        • 2019-12-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 2020-08-29
        • 1970-01-01
        • 2019-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多