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

这篇文章是继文章 "Beats:安装及配置 Metricbeat (一)- 8.x" 的续篇。你可以先阅读之前的那篇文章再继续阅读这篇文章。我们在这篇文章中继续之前的探讨。

使用 fingerprint 来代替证书

在实际的使用中,我们需要从 Elasticsearch 的安装目录中拷贝证书来配置 metricbeat。这样有时觉得并不是很方便。相反,我们可以直接使用 fingerprint 来对 metricbeat 的证书来进行配置。我们可以参考文章 "Beats:使用 fingerprint 来连接 Beats/Logstash 和 Elasticsearch"。我们对 metricbeat.yml 进行如下的修改:

/etc/metricbeat/metricbeat.yml

bash 复制代码
1.  output.elasticsearch:
2.    # Array of hosts to connect to.
3.    hosts: ["192.168.0.3:9200"]

5.    # Protocol - either `http` (default) or `https`.
6.    protocol: "https"

8.    # Authentication credentials - either API key or username/password.
9.    #api_key: "id:api_key"
10.    username: "${ES_USER}"
11.    password: "${ES_PASSWORD}"
12.    # ssl.certificate_authorities: ["/etc/metricbeat/http_ca.crt"]
13.    ssl.ca_trusted_fingerprint: "633bf7f6e4bf264e6a05d488af3c686b858fa63592dc83999a0d77f7e9fe5940"

在上面,我们使用 ssl.ca_trusted_fingerprint 的配置来代替 ssl.certificate_authorities 的配置。显然这个比较方便,因为我们不需要拷贝证书,而且配置是不需要含有证书路径。

等修改完上面的配置后,我们重新运行如下的命令来测试这个输出是否已经成功:

bash 复制代码
metricbeat test output
markdown 复制代码
1.  oot@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

从输出的结果中来可能,我们的证书配置是成功的。

使用 API key 来代替用户名及密码

在很多的情况下,我们并不希望使用 elastic 这个超级用户来对 Beats 进行配置。我们可以使用 API key 来进行配置。使用 API 可以的好处是它可以定义 API key 的使用时效,比如1一个月。当然它也可以定义相应的权限。在下面我们来展示如何创建 API key:

我们拷贝上面的 API key,并在 metricbeat.yml 中进行配置:

/etc/metricbeat/metricbeat.yml

yaml 复制代码
1.  output.elasticsearch:
2.    # Array of hosts to connect to.
3.    hosts: ["192.168.0.3:9200"]

5.    # Protocol - either `http` (default) or `https`.
6.    protocol: "https"

8.    # Authentication credentials - either API key or username/password.
9.    #api_key: "id:api_key"
10.    # username: "${ES_USER}"
11.    # password: "${ES_PASSWORD}"
12.    api_key: sFizXYoBxPLM4LwrKywe:NMOjRbUvT7ykunWDsVG4uQ

14.    # ssl.certificate_authorities: ["/etc/metricbeat/http_ca.crt"]
15.    ssl.ca_trusted_fingerprint: "633bf7f6e4bf264e6a05d488af3c686b858fa63592dc83999a0d77f7e9fe5940"

在上面,我们使用 api_key 来代替 username/password 的配置。我们使用如下的命令来测试配置是否成功:

bash 复制代码
metricbeat test output
markdown 复制代码
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

上面显示我们的配置是成功的。

创建新用户并使用新用户信息来进行配置

在上面,我们已经说明了使用 elastic 超级用户是非常不好的一个习惯,因为一旦 elastic 这个超级用户的信息被泄露,那么它可能造成灾难性的后果。在实际的使用中,我们尽量避免使用 elastic 这个超级用户。我们可以参考之前的文章 "Beats:最佳实践" 来创建一个针对 Beats 数据采集的用户。这个用户有较少的权限。即便泄露也可能不会造成特别大的损失。更多信息,可以参阅官方文档 Grant privileges and roles needed for publishing | Filebeat Reference [8.9] | Elastic

我们参考之前的文章 "Elasticsearch:用户安全设置" 来创建用户及 roles。

我们接下来创建用户:

如上所示,我创建了一个叫做 user 的用户,而它的密码为 password。

接下来,我们使用如下的命令来创建一个属于 mertic 用户的 API key:

bash 复制代码
1.  POST _security/api_key/grant
2.  {
3.    "grant_type": "password",
4.    "username": "metric",
5.    "password": "password",
6.    "api_key": {
7.      "name": "mertic"
8.    }
9.  }

我们可以使用如下的方法来对 Metricbeat 进行配置:

vbnet 复制代码
api_key: "id:api_key"

/etc/metricbeat/metricbeat.yml

yaml 复制代码
1.  output.elasticsearch:
2.    # Array of hosts to connect to.
3.    hosts: ["192.168.0.3:9200"]

5.    # Protocol - either `http` (default) or `https`.
6.    protocol: "https"

8.    # Authentication credentials - either API key or username/password.
9.    api_key: "s1jWXYoBxPLM4LwrZSzu:rL-6POdnQDSAjWWHao9Ybw"
10.    # username: "${ES_USER}"
11.    # password: "${ES_PASSWORD}"

13.    # ssl.certificate_authorities: ["/etc/metricbeat/http_ca.crt"]
14.    ssl.ca_trusted_fingerprint: "633bf7f6e4bf264e6a05d488af3c686b858fa63592dc83999a0d77f7e9fe5940"

配置完毕后,我们再进行如下的测试:

bash 复制代码
metricbeat test output
markdown 复制代码
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

很显然我们的配置是成功的。我们可以使用和之前文章 "Beats:安装及配置 Metricbeat (一)- 8.x" 中所述的 keystore 来保存这些信息。

/etc/mertricbeat/metrcibeat.yml

yaml 复制代码
1.  # ---------------------------- Elasticsearch Output ----------------------------
2.  output.elasticsearch:
3.    # Array of hosts to connect to.
4.    hosts: ["192.168.0.3:9200"]

6.    # Protocol - either `http` (default) or `https`.
7.    protocol: "https"

9.    # Authentication credentials - either API key or username/password.
10.    api_key: "${ES_API_KEY}"
11.    # username: "${ES_USER}"
12.    # password: "${ES_PASSWORD}"

14.    # ssl.certificate_authorities: ["/etc/metricbeat/http_ca.crt"]
15.    ssl.ca_trusted_fingerprint: "633bf7f6e4bf264e6a05d488af3c686b858fa63592dc83999a0d77f7e9fe5940"
csharp 复制代码
metricbeat keystore add ES_API_KEY
ruby 复制代码
1.  root@ubuntu2004:/etc/metricbeat# metricbeat keystore add ES_API_KEY
2.  Enter value for ES_API_KEY: 
3.  Successfully updated the keystore

在上面,我们使用 s1jWXYoBxPLM4LwrZSzu:rL-6POdnQDSAjWWHao9Ybw 作为输入。它就是 id:api_key 的组合。我们再次运行如下的命令来检查配置是否成功:

markdown 复制代码
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

当然针对有些用户喜欢使用 username 及 password 来配置而不使用 API key,你也可以是使用如下的配置:

/etc/metricbeat/metricbeat.yml

yaml 复制代码
1.  output.elasticsearch:
2.    # Array of hosts to connect to.
3.    hosts: ["192.168.0.3:9200"]

5.    # Protocol - either `http` (default) or `https`.
6.    protocol: "https"

8.    # Authentication credentials - either API key or username/password.
9.    # api_key: "${ES_API_KEY}"
10.    username: "metric"
11.    password: "password"

13.    # ssl.certificate_authorities: ["/etc/metricbeat/http_ca.crt"]
14.    ssl.ca_trusted_fingerprint: "633bf7f6e4bf264e6a05d488af3c686b858fa63592dc83999a0d77f7e9fe5940"

这样我们就完成了这个部分的展示。在接下来的文章中,我将介绍如何使用 Alerts 来对 Metricbeat 进行通知。

相关推荐
hengzhepa7 小时前
ElasticSearch备考 -- Async search
大数据·学习·elasticsearch·搜索引擎·es
bubble小拾15 小时前
ElasticSearch高级功能详解与读写性能调优
大数据·elasticsearch·搜索引擎
不能放弃治疗16 小时前
重生之我们在ES顶端相遇第 18 章 - Script 使用(进阶)
elasticsearch
hengzhepa16 小时前
ElasticSearch备考 -- Search across cluster
学习·elasticsearch·搜索引擎·全文检索·es
Elastic 中国社区官方博客18 小时前
Elasticsearch:使用 LLM 实现传统搜索自动化
大数据·人工智能·elasticsearch·搜索引擎·ai·自动化·全文检索
慕雪华年19 小时前
【WSL】wsl中ubuntu无法通过useradd添加用户
linux·ubuntu·elasticsearch
Elastic 中国社区官方博客21 小时前
使用 Vertex AI Gemini 模型和 Elasticsearch Playground 快速创建 RAG 应用程序
大数据·人工智能·elasticsearch·搜索引擎·全文检索
alfiy1 天前
Elasticsearch学习笔记(四) Elasticsearch集群安全配置一
笔记·学习·elasticsearch
alfiy1 天前
Elasticsearch学习笔记(五)Elastic stack安全配置二
笔记·学习·elasticsearch
丶21362 天前
【大数据】Elasticsearch 实战应用总结
大数据·elasticsearch·搜索引擎