Beats:安装及配置 Metricbeat (一) 8.x

2023年 8月 16日 79.0k 0

在我之前的文章:

  • Beats:Beats 入门教程 (一)
  • Beats:Beats 入门教程 (二)

我详细描述了如何在 Elastic Stack 7.x 安装及配置 Beats。在那里的安装,它通常不带有安全及 Elasticsearch 在默认的情况下,不含有 HTTPS 访问。在最新的 Elastic Stack 8.x 中,Elasticsearch 集群在默认的情况下是带有安全访问的。这样针对 Metricbeat 访问 Elasticsearch 它和之前的情形是不同的。我在之前文章 “Elastic Stack 8.0 安装 - 保护你的 Elastic Stack 现在比以往任何时候都简单” 有描述 Beats 如何连接到 Elasticsearch。在今天的文章里,我们来详细地 Beats 描述如何安全地连接到 Elasticsearch。

在今天的展示中,我将使用最新的 Elastic Stack 8.9.0 来进行展示。我将使用如下的架构:

安装

Elasticsearch 及 Kibana

如果你还没有安装好自己的 Elasticsearch 及 Kibana,请参考我之前的文章:

  • 如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch
  • Kibana:如何在 Linux,MacOS 及 Windows 上安装 Elastic 栈中的 Kibana

在安装的时候,请选择使用 Elastic Stack 8.x 的安装指南来进行安装。在安装的时候,我们需要记录下它的 elastic 超级用户的密码,fingerprint 信息,以备后面的配置所使用:



1.  ✅ Elasticsearch security features have been automatically configured!
2.  ✅ Authentication is enabled and cluster connections are encrypted.

4.  ℹ️  Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
5.    p1k6cT4a4bF+pFYf37Xx

7.  ℹ️  HTTP CA certificate SHA-256 fingerprint:
8.    633bf7f6e4bf264e6a05d488af3c686b858fa63592dc83999a0d77f7e9fe5940

10.  ℹ️  Configure Kibana to use this cluster:
11.  • Run Kibana and click the configuration link in the terminal when Kibana starts.
12.  • Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
13.    eyJ2ZXIiOiI4LjkuMCIsImFkciI6WyIxOTIuMTY4LjAuMzo5MjAwIl0sImZnciI6IjYzM2JmN2Y2ZTRiZjI2NGU2YTA1ZDQ4OGFmM2M2ODZiODU4ZmE2MzU5MmRjODM5OTlhMGQ3N2Y3ZTlmZTU5NDAiLCJrZXkiOiJ3WEE3MDRrQkxxWTFWWGY0QWRHbDpCa0VZVXZmaFFidWNPOFUxdXJwXzZnIn0=

15.  ℹ️  Configure other nodes to join this cluster:
16.  • On this node:
17.    ⁃ Create an enrollment token with `bin/elasticsearch-create-enrollment-token -s node`.
18.    ⁃ Uncomment the transport.host setting at the end of config/elasticsearch.yml.
19.    ⁃ Restart Elasticsearch.
20.  • On other nodes:
21.    ⁃ Start Elasticsearch with `bin/elasticsearch --enrollment-token `, using the enrollment token that you generated.


为了能够使得我们的 Kibana 能被其他的电脑所访问,我们必须针对 kibana.yml 文件进行一项配置:

server.host: "0.0.0.0"

修改完毕后,我们必须重新启动 Kibana。

Metricbeat

有关 Metricbeat 的安装,我们可以参考官方文档 Metricbeat quick start: installation and configuration | Metricbeat Reference [8.9] | Elastic 来进行安装。针对大多数的应用场景,我们可以使用仓库进行安装。我们可以参考链接 Repositories for APT and YUM | Metricbeat Reference [8.9] | Elastic 来进行安装。首先,我们创建如下的一个文档:

install.sh



1.  #!/bin/bash
2.  wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -;
3.  sudo apt-get -y install apt-transport-https;
4.  echo "deb https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-8.x.list;
5.  apt-get update;
6.  apt-get install -y metricbeat;


chmod +x install.sh

我们打入上面的命令来把上面的脚步变为可执行文件,并打入如下的命令:

sudo ./install.sh

这样我们就完成了我们的 Metricbeat 的安装。

配置 Metricbeat

在完成 Metricbeat 的配置后,我们进入到 Metricbeat 的配置目录中,并对它的配置文件 metricbeat.yml 进行配置:

/etc/metricbeat/metricbeat.yml



1.  parallels@ubuntu2004:/etc/metricbeat$ pwd
2.  /etc/metricbeat
3.  parallels@ubuntu2004:/etc/metricbeat$ ls
4.  fields.yml  http_ca.crt  metricbeat.reference.yml  metricbeat.yml  modules.d


我们需要针对 metricbeat.yml 文件做如下的修改:

/etc/metricbeat/metricbeat.yml

把上面的标志设置为 true,那么每次修改 metricbeat.yml 后,会自动装载 metricbeat.yml 而不用重新启动 metricbeat。

我们需要把 Elasticsearch 的证书拷贝到当前的 Metricbeat 配置目录下。我们可以在 macOS 机器的 Elasticsearch 的安装目录中找到这个证书文件 http_ca.crt 文件:



1.  $ pwd
2.  /Users/liuxg/elastic/elasticsearch-8.9.0/config/certs
3.  $ ls
4.  http.p12      http_ca.crt   transport.p12


我们把上面的证书拷贝到 Metricbeat 的安装目录中:



1.  root@ubuntu2004:/etc/metricbeat# pwd
2.  /etc/metricbeat
3.  root@ubuntu2004:/etc/metricbeat# ls
4.  fields.yml  http_ca.crt  metricbeat.reference.yml  metricbeat.yml  modules.d


我们接下来继续配置 metricbeat.ym 文件:

我们需要手动修改上面的部分。把 elastic 超级用户的密码填入,并配置证书。

为了验证我们的配置是否有语法错误,我们可以使用如下的命令来进行检测:

metricbeat test config


1.  root@ubuntu2004:~# metricbeat test config
2.  Config OK


我们接下来测试 output。这个部分是为了测试能够正确地连接到 Elasticsearch。我们使用如下的命令:

metricbeat test output


1.  root@ubuntu2004:~# metricbeat test output
2.  elasticsearch: https://192.168.0.3:9200...
3.    parse url... OK
4.    connection...
5.      parse host... OK
6.      dns lookup... OK
7.      addresses: 192.168.0.3
8.      dial up... OK
9.    TLS...
10.      security: server's certificate chain verification is enabled
11.      handshake... OK
12.      TLS version: TLSv1.3
13.      dial up... OK
14.    talk to server... OK
15.    version: 8.9.0


很显然,我们的测试是成功的。它能够正确地连接到 Elasticsearch。这样我们的配置是完全成功的。

在上面,我们发现在 metricbeat 里使用硬编码把用户名及密码写入到文件中,这个是一个不好的习惯,因为有人可以查看这个文件就会发现你使用的密码等信息。我们可以使用 keystore 来保护这些信息。



1.  root@ubuntu2004:/etc/metricbeat# metricbeat keystore create
2.  Created metricbeat keystore
3.  root@ubuntu2004:/etc/metricbeat# metricbeat keystore add ES_USER
4.  Enter value for ES_USER: 
5.  Successfully updated the keystore
6.  root@ubuntu2004:/etc/metricbeat# metricbeat keystore add ES_PASSWORD
7.  Enter value for ES_PASSWORD: 
8.  Successfully updated the keystore
9.  root@ubuntu2004:/etc/metricbeat# metricbeat keystore list
10.  ES_PASSWORD
11.  ES_USER


有了这些配置过后,我们可以重新来修改 metricbeat.yml 文件:

修改完毕后,我们使用如下的命令重新来验证输出:



1.  root@ubuntu2004:/etc/metricbeat# metricbeat test output
2.  elasticsearch: https://192.168.0.3:9200...
3.    parse url... OK
4.    connection...
5.      parse host... OK
6.      dns lookup... OK
7.      addresses: 192.168.0.3
8.      dial up... OK
9.    TLS...
10.      security: server's certificate chain verification is enabled
11.      handshake... OK
12.      TLS version: TLSv1.3
13.      dial up... OK
14.    talk to server... OK
15.    version: 8.9.0


很显然我们的输出是成功的。这样我们的用户名及密码就可以不暴露了,即便配置文件被别人所看到。

启动模块

在默认的情况下,system 模块是自动启动的。我们可以通过如下的命令来进行查看:

metricbeat modules list


1.  root@ubuntu2004:~# metricbeat modules list
2.  Enabled:
3.  system

5.  Disabled:
6.  activemq
7.  aerospike
8.  airflow
9.  apache
10.  aws
11.  awsfargate
12.  azure
13.  beat
14.  beat-xpack
15.  ceph
16.  ceph-mgr
17.  cloudfoundry
18.  cockroachdb
19.  ...


 从上面的显示结果中,我们可以看出来 system 模块是被启动的。我们也可以通过查看当前 metricbeat 下的 modules.d 目录来看出来:

从上面的输出中,我们可以看出来,只有 system.yml 文件的后缀不含有 disabled。它表明这个模块是被启动的。事实上,我们可以在这个目录下直接把某个模块的扩展名 disabled 去掉,就可以启动该模块。我们也可以通过如下的命令来禁止模块,比如:

metricbeat modules disable system

上面的命令禁止 system 模块。我们可以通过如下的命令来进行查看:

metricbeat modules list

我们可以使用如下的命令来重新启动 system 模块:

metricbeat modules enable system


1.  root@ubuntu2004:/etc/metricbeat# metricbeat modules enable system
2.  Enabled system


启动 Metricbeat

在上面,我们已经成功地配置了 Metricbeat。我们接下来运行如下的 setup 命令来加载 Dashboards 及创建相应的 pipeline,index template 等:

metricbeat setup

上述命令将运行一会儿时间。我们可以看到:



1.  root@ubuntu2004:~# metricbeat setup
2.  Overwriting ILM policy is disabled. Set `setup.ilm.overwrite: true` for enabling.

4.  Index setup finished.
5.  Loading dashboards (Kibana must be running and reachable)
6.  Loaded dashboards


针对所有的 Metricbeat 模块来说,我们只需要运行上面的命令一次就够了,即便当前的模块没有被启动。等上面的命令完成后,我们可以到 Kibana 中进行查看:

 

我们接下来运行如下的命令来采集数据:

service metricbeat start

 

从上面,我们可以看出来运行状态正常。我们到 Kibana 中去查看数据:

 

 

 我们可以在 index management 里查看到:

 

显然这个数据是以 data stream 的形式采集进来的。 

好了,今天的文章就先介绍到这里。在后面的文章里,我将继续介绍如何创建一个具有较少权限的用户来配置 Metricbeat。目前的 elastic 用户被用来配置,在很少的情况会这么做。另外,我还将探讨使用其他形式的证书来配置 output。

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论