在本地部署服务器上安装 CloudWatch 代理 - Amazon CloudWatch

在本地部署服务器上安装 CloudWatch 代理

如果已在一台电脑上下载了 CloudWatch 代理并创建了所需的代理配置文件,则可以使用该配置文件在其他本地部署服务器上安装该代理。

在本地部署服务器上下载 CloudWatch 代理

您可以使用 Systems Manager Run Command 或 Amazon S3 下载链接下载 CloudWatch 代理程序包。有关使用 Amazon S3 下载链接的信息,请参阅 下载 CloudWatch 代理软件包

使用 Systems Manager 下载

要使用 Systems Manager Run Command,您必须通过 Amazon EC2 Systems Manager 注册您的本地部署服务器。有关更多信息,请参阅 AWS Systems Manager 用户指南中的在混合环境中设置 Systems Manager

如果已注册您的服务器,请将 SSM Agent 更新为最新版本。

有关在运行 Linux 的服务器上更新 SSM Agent 的信息,请参阅 AWS Systems Manager 用户指南中的为混合环境安装 SSM Agent (Linux)

有关在运行 Windows Server 的服务器上更新 SSM Agent 的信息,请参阅 AWS Systems Manager 用户指南中的为混合环境安装 SSM Agent (Windows)

使用 SSM Agent 在本地部署服务器上下载 CloudWatch 代理软件包
  1. 通过 https://console.aws.amazon.com/systems-manager/ 打开 Systems Manager 控制台。

  2. 在导航窗格中,选择 Run Command

    –或者–

    如果打开了 AWS Systems Manager 主页,请向下滚动并选择 Explore Run Command(浏览 Run Command)

  3. 选择 Run command(运行命令)

  4. Command document(命令文档)列表中,选择 AWS-ConfigureAWSPackage 旁边的按钮。

  5. Targets(目标)区域中,选择要在其上安装 CloudWatch 代理的服务器。如果未看到特定的服务器,则可能没有为 Run Command 配置该服务器。有关更多信息,请参阅 AWS Systems Manager 用户指南中的为混合环境设置 AWS Systems Manager

  6. 操作列表中,选择安装

  7. Name (名称) 框中,输入 AmazonCloudWatchAgent

  8. Version (版本) 保留空白以安装最新版本的代理。

  9. 选择运行

    将下载代理软件包,后续步骤是配置并启动该代理。

(安装在本地部署服务器上)指定 IAM 凭证和 AWS 区域

要使 CloudWatch 代理能够从本地部署服务器发送数据,您必须指定先前创建的 IAM 用户的访问密钥和私有密钥。有关创建此用户的更多信息,请参阅创建 IAM 角色和用户以用于 CloudWatch 代理

您还必须使用 region 字段指定要将指标发送到的 AWS 区域。

以下为该文件的示例。

[AmazonCloudWatchAgent] aws_access_key_id=my_access_key aws_secret_access_key=my_secret_key region = us-west-1

对于 my_access_keymy_secret_key,请使用来自没有写入到 Systems Manager Parameter Store 权限的 IAM 用户的密钥。有关 CloudWatch 代理所需的 IAM 用户的更多信息,请参阅 创建 IAM 用户以用于本地部署服务器上的 CloudWatch 代理

如果您将此配置文件命名为 AmazonCloudWatchAgent,则无需执行任何操作。(可选)您可以为其指定一个不同的名称,并将该名称指定为 common-config.toml 文件中的 shared_credential_profile 值(将在下一节中介绍)。

下面是使用 aws configure 命令为 CloudWatch 代理创建命名配置文件的示例。该示例假设您使用默认配置文件名称 AmazonCloudWatchAgent

为 CloudWatch 代理创建 AmazonCloudWatchAgent 配置文件
  1. 如果您还没有这样做,请在服务器上安装 AWS Command Line Interface。有关更多信息,请参阅安装 AWS CLI

  2. 在 Linux 服务器上,输入以下命令并按照提示进行操作:

    sudo aws configure --profile AmazonCloudWatchAgent

    在 Windows Server 上,以管理员身份打开 PowerShell,输入以下命令并按照提示进行操作。

    aws configure --profile AmazonCloudWatchAgent

(可选)修改 CloudWatch 代理的通用配置和命名配置文件

CloudWatch 代理包含一个名为 common-config.toml 的配置文件。您可以(可选)使用该文件指定代理和区域信息。

在运行 Linux 的服务器上,该文件位于 /opt/aws/amazon-cloudwatch-agent/etc 目录中。在运行 Windows Server 的服务器上,该文件位于 C:\ProgramData\Amazon\AmazonCloudWatchAgent 目录中。

默认 common-config.toml 如下所示:

# This common-config is used to configure items used for both ssm and cloudwatch access ## Configuration for shared credential. ## Default credential strategy will be used if it is absent here: ## Instance role is used for EC2 case by default. ## AmazonCloudWatchAgent profile is used for onPremise case by default. # [credentials] # shared_credential_profile = "{profile_name}" # shared_credential_file= "{file_name}" ## Configuration for proxy. ## System-wide environment-variable will be read if it is absent here. ## i.e. HTTP_PROXY/http_proxy; HTTPS_PROXY/https_proxy; NO_PROXY/no_proxy ## Note: system-wide environment-variable is not accessible when using ssm run-command. ## Absent in both here and environment-variable means no proxy will be used. # [proxy] # http_proxy = "{http_url}" # https_proxy = "{https_url}" # no_proxy = "{domain}"

最初将注释所有行。要设置凭证配置文件或代理设置,请从该行中删除 # 并指定一个值。您可以手动编辑该文件,或者使用 Systems Manager 中的 RunShellScript Run Command 执行该操作:

  • shared_credential_profile – 对于本地部署服务器,此行指定 IAM 用户凭证配置文件,以用于将数据发送到 CloudWatch。如果您将此行注释掉,则会使用 AmazonCloudWatchAgent。有关创建此配置文件的更多信息,请参阅(安装在本地部署服务器上)指定 IAM 凭证和 AWS 区域

    在 EC2 实例上,您可以使用此行让 CloudWatch 代理将数据从该实例发送到不同 AWS 区域中的 CloudWatch。要执行此操作,请指定一个包含 region 字段的命名配置文件,该字段指定要发送到的区域的名称。

    如果指定 shared_credential_profile,您还必须从 [credentials] 行开头删除 #

  • shared_credential_file – 要让代理在位于默认路径以外的路径中的文件中查找凭证,请在此处指定完整的路径和文件名。在 Linux 上,默认路径为 /root/.aws;在 Windows Server 上,默认路径为 C:\\Users\\Administrator\\.aws

    下面的第一个示例显示对 Linux 服务器有效的 shared_credential_file 行的语法,第二个示例对 Windows Server 有效。在 Windows Server 上,您必须转义 \ 字符。

    shared_credential_file= "/usr/username/credentials"
    shared_credential_file= "C:\\Documents and Settings\\username\\.aws\\credentials"

    如果指定 shared_credential_file,您还必须从 [credentials] 行开头删除 #

  • 代理设置 – 如果您的服务器使用 HTTP 或 HTTPS 代理联系 AWS 服务,请在 http_proxyhttps_proxy 字段中指定这些代理。如果应从代理中排除某些 URL,请在 no_proxy 字段中指定这些 URL 并以逗号分隔。

启动 CloudWatch 代理

您可以使用 Systems Manager Run Command 或命令行启动 CloudWatch 代理。

使用 SSM Agent 在本地部署服务器上启动 CloudWatch 代理
  1. 通过 https://console.aws.amazon.com/systems-manager/ 打开 Systems Manager 控制台。

  2. 在导航窗格中,选择 Run Command

    –或者–

    如果打开了 AWS Systems Manager 主页,请向下滚动并选择 Explore Run Command(浏览 Run Command)

  3. 选择 Run command(运行命令)

  4. 命令文档列表中,选择 AmazonCloudWatch-ManageAgent 旁边的按钮。

  5. 目标区域,选择安装了该代理的实例。

  6. 操作列表中,选择配置

  7. 模式列表中,选择 onPremise

  8. Optional Configuration Location(可选的配置位置)框中,输入您使用向导创建并存储在 Parameter Store 中的代理配置文件的名称。

  9. 选择运行

    该代理使用在配置文件中指定的配置启动。

使用命令行在本地部署服务器上启动 CloudWatch 代理
  • 此命令中,-a fetch-config 会使代理加载最新版本的 CloudWatch 代理配置文件,-s 则会启动该代理。

    Linux:如果在 Systems Manager Parameter Store 中保存了配置文件,请输入以下命令:

    sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m onPremise -s -c ssm:configuration-parameter-store-name

    Linux:如果在本地计算机上保存了配置文件,请输入以下命令。将 configuration-file-path 替换为指向代理配置文件的路径。如果您使用向导创建此文件,则此文件命名为 config.json;如果您手动创建该文件,则该文件可能命名为 amazon-cloudwatch-agent.json

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

    Windows Server:如果在 Systems Manager Parameter Store 中保存代理配置文件,请从 PowerShell 控制台中输入以下命令:

    & "C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m onPremise -s -c ssm:configuration-parameter-store-name

    Windows Server:如果在本地计算机上保存了代理配置文件,请从 PowerShell 控制台中输入以下命令。将 configuration-file-path 替换为指向代理配置文件的路径。如果您使用向导创建此文件,则此文件命名为 config.json;如果您手动创建该文件,则该文件可能命名为 amazon-cloudwatch-agent.json

    & "C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m onPremise -s -c file:configuration-file-path