Logstash:如何连接到带有 HTTPS 访问的集群

发布时间 2023-12-26 13:47:36作者: 技术颜良

 

https://blog.csdn.net/UbuntuTouch/article/details/126868040

前段时间,有一个开发者在评论区问了一个问题:如何运用 Logstash 采集数据,并写入到带有 HTTPS 的 Elasticsearch 集群中。我们知道,在 Elasticsearch 8.x 的安装中,SSL 的连接是最基本的配置。那么我们如何把采集的数据成功地写到 Elasticsearch 中去呢?在我之前的视频中有分享。在今天的文章中,我将采用另外一种方法来进行演示。

在今天的展示中,我将使用最新的 Elastic Stack 8.4.1 来进行展示。


Logstash:如何连接到带有 HTTPS 访问的集群

安装
Elasticsearch 及 Kibana
如果你还没有安装 Elasticsearch 及 Kibana,请参阅如下的文章来安装自己的 Elasticsearch 及 Kibana:

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

特别需要指出的是:我们需要选择自己的平台,并安装好 Elasticsearch 8.x 及 Kibana 8.x 版本。

Logstash
Logstash 的安装非常简单直接。我们可以参考文章 “如何安装 Elastic 栈中的 Logstash”。我们直接到官方网站下载和 Elasticsearch 一样的版本,并解压到相应的目录中即可。

写入数据到 Elasticsearch 中
在 Logstash 的安装根目录下,我们创建一个如下的配置文件:

logstash.conf

input {
generator {
message => "liuxg"
count => 1
}
}

output {
stdout {
codec => rubydebug
}

elasticsearch {
hosts => ["https://localhost:9200"]
index => "data-%{+YYYY.MM.dd}"
user => "elastic"
password => "ndktp2Xr2h_*Q6EZfq4t"
ssl_certificate_verification => true
truststore => "/Users/liuxg/elastic/elasticsearch-8.4.1/config/certs/http.p12"
truststore_password => "buSYG3l-R0mdN6pggG76VA"
}
}
在上面的第一个部分,我们使用 generator 来生成一个 message。我们在 output 里定义两个输出,一个是 stdout,而另外一个是 elasticsearch output。我们查一下官方文档,并对它做如上的配置。需要指出的是:

hosts:这个是 Elasticsearch 集群的终端地址。你需要根据自己的配置进行修改
index:这个在 Elasticsearch 中创建的索引的名称
user:这个是在 Elasticsearch 中创建的用户。我使用的是超级用户 elastic,尽管这个在生产环境中是不必要的。你可以根据文章 “Elasticsearch:用户安全设置” 来创建具有适当权限的用户
password:这个是上面的 user 用户对应的密码
ssl_certificate_verification:当为 true 时,启动 SSL 检验
truststore:这个是 Elasticsearch 的证书所在的地址
truststore_password:这个是 truststore 的密码


我们可以根据如下的方式来得到这个密码:

$ pwd
/Users/liuxg/elastic/elasticsearch-8.4.1
$ ./bin/elasticsearch-keystore list
keystore.seed
xpack.security.http.ssl.keystore.secure_password
xpack.security.transport.ssl.keystore.secure_password
xpack.security.transport.ssl.truststore.secure_password
$ ./bin/elasticsearch-keystore show xpack.security.http.ssl.keystore.secure_password
buSYG3l-R0mdN6pggG76VA
在上面的最后一个命令中它显示了 keystore 的密码。

$ pwd
/Users/liuxg/elastic/elasticsearch-8.4.1
$ ./bin/elasticsearch-keystore list
keystore.seed
xpack.security.http.ssl.keystore.secure_password
xpack.security.transport.ssl.keystore.secure_password
xpack.security.transport.ssl.truststore.secure_password
$ ./bin/elasticsearch-keystore show xpack.security.http.ssl.keystore.secure_password
buSYG3l-R0mdN6pggG76VA
$ ls config/certs/
http.p12 http_ca.crt transport.p12
我们在 Logstash 的安装目录下使用如下的方式来进行运行:

./bin/logstash -f logstash.conf


我们接下来到 Kibana 中进行查看:

 

很显然,我们的数据已经写入到 Elasticsearch 中了。

在上面,我们使用了 http.p12 证书来进行这样的操作。我们可以使用如下的命令来查看这个证书:

$ pwd
/Users/liuxg/elastic/elasticsearch-8.4.1
$ ./bin/elasticsearch-keystore list
keystore.seed
xpack.security.http.ssl.keystore.secure_password
xpack.security.transport.ssl.keystore.secure_password
xpack.security.transport.ssl.truststore.secure_password
$ ./bin/elasticsearch-keystore show xpack.security.http.ssl.keystore.secure_password
Jn1YI7FMSNaiAXjUsdp4Mw
$ cd config/certs/
$ keytool -keystore http.p12 -list
Enter keystore password:
Keystore type: PKCS12
Keystore provider: SUN

Your keystore contains 2 entries

http, Sep 26, 2022, PrivateKeyEntry,
Certificate fingerprint (SHA-256): 97:D0:50:B0:31:73:42:67:D1:00:7C:71:F2:4D:D4:9E:AC:73:99:39:AC:E8:34:EB:DA:2F:0B:5E:F6:65:D3:CD
http_ca, Sep 26, 2022, PrivateKeyEntry,
Certificate fingerprint (SHA-256): B4:8C:3E:33:FE:E5:AA:42:CF:1F:36:AC:94:D2:47:B2:3E:A3:B1:27:52:67:C5:6F:7B:CB:FF:9F:30:47:56:99

 

从上面的输出中,我们可以看出来,在 http.p12 中有两个 entry,其中一个是私钥。考虑到我们并不便于发送这个给客户端。我们可以使用如下的方法来创建 truststore:

keytool -import -file http_ca.crt -keystore truststore.p12 -storepass password -noprompt -storetype pkcs12
如上所示,它在当前的目录下创建一个叫做 truststore.p12 的文件。它的密码是 password。我们可以使用如下的命令来查看这个 truststore 的内容:

keytool -keystore truststore.p12 -list
$ pwd
/Users/liuxg/elastic/elasticsearch-8.4.1/config/certs
$ ls
http.p12 http_ca.crt transport.p12
$ keytool -import -file http_ca.crt -keystore truststore.p12 -storepass password -noprompt -storetype pkcs12
Certificate was added to keystore
$ ls
http.p12 http_ca.crt transport.p12 truststore.p12
$ keytool -keystore truststore.p12 -list
Enter keystore password:
Keystore type: PKCS12
Keystore provider: SUN

Your keystore contains 1 entry

mykey, Sep 30, 2022, trustedCertEntry,
Certificate fingerprint (SHA-256): B4:8C:3E:33:FE:E5:AA:42:CF:1F:36:AC:94:D2:47:B2:3E:A3:B1:27:52:67:C5:6F:7B:CB:FF:9F:30:47:56:99

 

如上所示,我们可以看到在生成的 truststore.p12 中只有一个项目。它的别名叫做 mykey。

根据上面所生成的 truststore.p12 及其访问密码 password,我们可以修改上面的 elasticsearch 配置如下:

elasticsearch {
hosts => ["https://localhost:9200"]
index => "data-%{+YYYY.MM.dd}"
user => "elastic"
password => "ndktp2Xr2h_*Q6EZfq4t"
ssl_certificate_verification => true
truststore => "/Users/liuxg/elastic/elasticsearch-8.4.1/config/certs/truststore.p12"
truststore_password => "password"
}
我们再次重新运行之前的命令即可。

运用 API key 来进行连接
我们也可以试验 API Key 来连接到 Elasticsearch。首先,我们来创建一个 API Key:

 

 

 

这样我们就创建了一个 API key。点击上面右边的文档小图标,并拷贝 APK key。这个 key 将在下面的配置中使用。

我们接着创建如下的一个 Logstash 的配置文件:

logstash.conf

nput {
generator {
message => "liuxg"
count => 1
}
}

output {
stdout {
codec => rubydebug
}

elasticsearch {
hosts => ["https://localhost:9200"]
index => "data-%{+YYYY.MM.dd}"
ssl => true
ilm_enabled => true
ssl_certificate_verification => false
api_key => "GMl1S4QBW_e6QIS4FQXK:KtC_17NAQt6Ef_7xuqzUCA"
}
}

我们把上面的文件保存于 Logstash 安装的根目录下,并使用如下的命令来进行运行:

./bin/logstash -f logstash.conf

 


我们接下来在 Kibana 中进行查看:

GET _cat/indices


我们可以看到 数据已经写入到 Elasticsearch 中了。

使用 fingerprint 来进行连接
我们可以使用 fingerprint 来进行连接。Fingerprint 的好处是,我们不必把把证书拷贝到 Logstash 运行的服务器中。请参考 “Beats:使用 fingerprint 来连接 Beats/Logstash 和 Elasticsearch”
————————————————
版权声明:本文为CSDN博主「Elastic 中国社区官方博客」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/UbuntuTouch/article/details/126868040