注意:在独立模式(standalone)下运行 Elastic Agent 是一个高级用例。 文档不完整,还不成熟。 如果可能,我们建议使用 fleet 管理的代理而不是独立模式。
要在独立模式下运行 Elastic Agent,请在你要监控的每台主机上安装该代理,并在安装它的系统上本地手动配置该代理。 你负责管理和升级代理。 仅建议高级用户使用此方法。
我们建议尽可能使用 Fleet-managed Elastic Agents,因为它使你的代理程序的管理和升级变得相当容易。你可以阅读我之前的文章 “Observability:使用 Elastic Agent 来摄入日志及指标 – Elastic Stack 8.0”。
重要:独立代理无法自动升级到新的集成包版本。 在 Kibana 中升级集成时,你需要手动更新独立策略。
在下面的展示中,我将使用 Elastic Stack 8.0 来进行展示。
安装
我们首先需要在我们的主机上安装并运行 Elastic Agent。针对不同的操作系统,我们需要不同的安装方法。我们需要访问网站 Download Elastic Agent Free | Elastic 来下载 Elastic Agent。
- macOS
1. curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-8.0.1-darwin-x86_64.tar.gz
2. tar xzvf elastic-agent-8.0.1-darwin-x86_64.tar.gz
复制代码
- Linux
1. curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-8.0.1-linux-x86_64.tar.gz
2. tar xzvf elastic-agent-8.0.1-linux-x86_64.tar.gz
复制代码
- Windows
1. # PowerShell 5.0+
2. wget https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-8.0.1-windows-x86_64.zip -OutFile elastic-agent-8.0.1-windows-x86_64.zip
3. Expand-Archive .\elastic-agent-8.0.1-windows-x86_64.zip
复制代码
- DEB
1. curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-8.0.1-amd64.deb
2. sudo dpkg -i elastic-agent-8.0.1-amd64.deb
复制代码
为了简化升级到 Elastic Agent 的未来版本,我们建议你使用 tarball 分发版而不是 DEB 分发版。
- RPM
1. curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-8.0.1-x86_64.rpm
2. sudo rpm -vi elastic-agent-8.0.1-x86_64.rpm
复制代码
为了简化升级到 Elastic Agent 的未来版本,我们建议你使用 tarball 分发而不是 RPM 分发。
在下面的展示中,我将使用 maoOS 来进行展示。在进行展示之前,你需要安装文章 “Elastic Stack 8.0 安装 – 保护你的 Elastic Stack 现在比以往任何时候都简单” 来安装好自己的 Elasticsearch 及 Kibana。Elasticsearch,Kibana 及 Elastic Agent 都将被安装于 macOS 机器上。
配置
为了能够使得 standalone Elastic Agent 能够正常工作,我们必须配置 elastic-agent.yml 文件。这个文件可以在 Elastic Agent 的安装目录中找到:
1. $ pwd
2. /Users/liuxg/elastic/fleet/elastic-agent-8.0.1-darwin-x86_64
3. $ ls
4. LICENSE.txt data elastic-agent.yml
5. NOTICE.txt elastic-agent fleet.yml
6. README.md elastic-agent.reference.yml fleet.yml.lock
复制代码
要快速入门并避免错误,请使用 Kibana 创建和下载独立的配置文件,而不是尝试手动构建它。
创建 standalone Elastic Agent 策略
要快速入门,请使用 Kibana 将集成添加到代理策略,然后下载该策略以用作独立 Elastic Agent 策略的起点。 这种方法可以节省时间,不易出错,并使用大量手动添加繁琐的细节来填充策略。 此外,在启动 Elastic Agent 之前,在 Kibana 中添加集成会加载所需的资源,例如索引模板和摄取管道。
我们首先来创建一个叫做 Standalone 的策略。这个名称可以是你喜欢的任何名称。
这样我们就创建了一个叫做 Standalone 的 policy。点击上面的链接:
我们接着为这个 policy 添加想要的 integration:
我们首先找到在 macOS 上 access.log 及 error.log 的位置:
`
1. $ nginx -help
2. nginx version: nginx/1.21.6
3. Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
4. [-e filename] [-c filename] [-g directives]
6. Options:
7. -?,-h : this help
8. -v : show version and exit
9. -V : show version and configure options then exit
10. -t : test configuration and exit
11. -T : test configuration, dump it and exit
12. -q : suppress non-error messages during configuration testing
13. -s signal : send signal to a master process: stop, quit, reopen, reload
14. -p prefix : set prefix path (default: /usr/local/Cellar/nginx/1.21.6/)
15. -e filename : set error log file (default: /usr/local/var/log/nginx/error.log)
16. -c filename : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
17. -g directives : set global directives out of configuration file
`
复制代码
上面显示了 nginx 的安装目录。我们进入到该文件所在的目录:
1. $ pwd
2. /usr/local/Cellar/nginx/1.21.6
3. $ ls
4. CHANGES bin logs
5. INSTALL_RECEIPT.json homebrew.mxcl.nginx.plist share
6. LICENSE homebrew.nginx.service
7. README html
8. $ cd logs
9. $ ls
10. access.log error.log host.access.log nginx.pid
复制代码
从上面,我们可以看到 host.access.log 及 error.log 这两个文件。接下来我们将使用这些信息来配置 Nginx 的 integration。点击上面图中的 Add Nginx:
我们需要修改上面的默认的配置。把 host.access.log 及 error.log 的位置信息填入,并保存。
上面的 Go to download page 就是我们在上面安装一节中显示下载 Elastic Agent 的页面。我们接着向下滚动:
点击上面的 Download Policy:
上面显示下载的 policy。它的名字和之前 Elastic Agent 安装目录中一个配置文件 elastic-agent.yml 是一样的。我们把上面的 elastic-agent.yml 文件拷贝到 Elastic Agent 的安装目录中,并覆盖在 Elastic Agent 安装目录中原有的 elastic-agent.yml 文件:
1. $ pwd
2. /Users/liuxg/elastic/fleet/elastic-agent-8.0.1-darwin-x86_64
3. $ ls
4. LICENSE.txt data elastic-agent.yml
5. NOTICE.txt elastic-agent fleet.yml
6. README.md elastic-agent.reference.yml fleet.yml.lock
复制代码
请注意上面的 elastic-agent.yml 文件是我们下载的文件,而不是之前在安装 Elastic Agent 时留下来的文件。
授予独立 Elastic Agents 访问 Elasticsearch
你可以使用 API 密钥或用户凭证来授予独立 Elastic Agent 访问 Elasticsearch 资源的权限。 将日志、指标、跟踪和合成数据发送到 Elasticsearch 需要以下最低权限:
- monitor 集群权限
- 对 logs-–、metrics-–、traces-– 和 synthetics-– 的 auto_configure 和 create_doc 索引权限。
建议你使用 API 密钥来避免在配置文件中暴露用户名和密码。
你可以将 API 密钥设置为在特定时间过期,并且可以显式地使它们失效。 任何具有 manage_api_key 或 manage_own_api_key 集群权限的用户都可以创建 API 密钥。
出于安全原因,我们建议每个 Elastic Agent 使用唯一的 API 密钥。 你可以根据需要为每个用户创建任意数量的 API 密钥。
要为 Elastic Agent 创建 API 密钥:
我们在上面的 Restricted privileges 的编辑框中输入如下的内容:
`
1. {
2. "standalone_agent": {
3. "cluster": [
4. "monitor"
5. ],
6. "indices": [
7. {
8. "names": [
9. "logs-*-*", "metrics-*-*", "traces-*-*", "synthetics-*-*"
10. ],
11. "privileges": [
12. "auto_configure", "create_doc"
13. ]
14. }
15. ]
16. }
17. }
`
复制代码
我们需要根据自己的应用场景来调整上面的 names 列表。如果你不使用 APM 或者 synthetics,那么你可以从列表中删除 “traces-–” 及 “synthetics-–“。
要设置 API 密钥的过期日期,请选择过期时间并输入 API 密钥的生命周期(以天为单位)。针对我们的情况,我们不设置这个参数。
点击上面的 Create API key 按钮:
你将看到一条消息,指示该密钥已创建,以及已编码的密钥。 默认情况下,API 密钥是 Base64 编码的,但这不适用于 Elastic Agent。 点击上面的选择框,把 Base64 修改为 Beats:
我们把上面的 API key 拷贝下来,这是因为以后我们在这个页面中再也看不到这个 key 了。
我们接下来修改 Elastic Agent 安装目录中的 elastic-agent.yml 文件:
这样我们就完成了 policy 的配置。
创建 standalone 角色
尽管建议你使用 API 密钥而不是用户名和密码来访问 Elasticsearch,但吧可以创建具有所需权限的角色,将其分配给用户,并在 elastic-agent.yml 文件中指定用户的凭据。
- 在 Kibana 中,转到 Stack Management > Roles。
- 单击 Create role 并输入角色的名称。
- 在 Cluster privileges 中,输入 monitor。
- 在 Index privileges 中,输入:
- 在 indices 项中,输入
logs-*-*
,metrics-*-*
, nginx-*-*,traces-*-*
andsynthetics-*-*
- 在 Privileges 项中 填入 auto_configure 及 create_doc
- 创建角色并将其分配给用户。
- 要使用这些凭据,请在 elastic-agent.yml 文件中设置用户名和密码:
1. [...]
2. outputs:
3. default:
4. type: elasticsearch
5. hosts:
6. - 'https://da4e3a6298c14a6683e6064ebfve9ace.us-central1.gcp.cloud.es.io:443'
7. username: ES_USERNAME
8. password: ES_PASSWORD
9. [...]
复制代码
注意:出于安全原因,请指定具有此处描述的最低权限的用户。 建议您不要使用 elastic 超级用户
创建 assets
这些 assets 包括 dashboard 及 ingest pipelines。我们可以通过 Kibana 来创建这些 assets。如果你使用 Kibana 生成独立配置,则会自动设置 assets。 否则,你需要安装它们。 有关更多信息,请参阅查看 Elastic Agent 集成 asset 和安装集成 assets。
安装并启动 Elastic Agent 作为一个服务
在 Elastic Agent 安装目录中,运行以下命令来安装 Elastic Agent 并将其作为服务启动。
注意:在 macOS、Linux(tar 包)和 Windows 上,运行 install 命令将 Elastic Agent 安装为托管服务并启动该服务。 DEB 和 RPM 包包含一个用于 Linux 系统的服务单元 systemd,因此只需启用然后启动该服务。
- macOS
sudo ./elastic-agent install
复制代码
- Linux
sudo ./elastic-agent install
复制代码
- Windows
.\elastic-agent.exe install
复制代码
以管理员身份打开 PowerShell 提示符(右键单击 PowerShell 图标并选择以管理员身份运行)。
- DEB
1. sudo systemctl enable elastic-agent
2. sudo systemctl start elastic-agent
复制代码
- RPM
1. sudo systemctl enable elastic-agent
2. sudo systemctl start elastic-agent
复制代码
针对我的情况,我在 macOS 下运行:
1. $ sudo ./elastic-agent install -i
2. Elastic Agent will be installed at /Library/Elastic/Agent and will run as a service. Do you want to continue? [Y/n]:Y
3. Do you want to enroll this Agent into Fleet? [Y/n]:n
复制代码
在上面我们使用 –insecure 或 -i 选项,这是因为我们的 Elasticsearch 使用自签名证书。
我们可以查看 Elastic Agent 服务的状态:
1. $ sudo elastic-agent status
2. Password:
3. Status: HEALTHY
4. Message: (no message)
5. Applications:
6. * filebeat (HEALTHY)
7. Running
8. * metricbeat (HEALTHY)
9. Running
10. * filebeat_monitoring (HEALTHY)
11. Running
12. * metricbeat_monitoring (HEALTHY)
13. Running
复制代码
从上面的输出中,我们可以看出来 elastic-agent 目前正常运行。
我们接着回到 Fleet 界面的 data stream 进行查看:
我们可以看到好几个有关 nginx 的 dataset。
当然,我们也可以选择上面的 Metrics Nginx。这里就不再累述了。