在Ubuntu系统中为Filebeat启用SSL/TLS加密传输,整体操作并不复杂,但细节处理往往决定成败。许多新手在证书生成与路径配置环节容易遇到问题。本文将完整拆解整个流程,确保每一步都能顺利实施。
核心思路如下:Filebeat采集日志后,需通过HTTPS加密通道安全发送至Elasticsearch,这就要求双方相互验证证书。你需要为Filebeat配置一套客户端证书,同时在Elasticsearch端配置好服务端证书及CA证书。下面从安装到验证,逐步讲解。
第一步:安装Filebeat
若尚未安装Filebeat,直接执行两条命令即可:
sudo apt-get update
sudo apt-get install filebeat
安装完成后,请勿立即启动,因为还需修改配置文件。
第二步:配置Filebeat
编辑 /etc/filebeat/filebeat.yml,明确指定输入源与输出目标。假设你需要监控 /var/log/*.log 这类日志,并将数据输出到Elasticsearch,参考配置如下:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["https://your_elasticsearch_host:9200"]
ssl.verification_mode: certificate
ssl.certificate_authorities: ["/etc/filebeat/certs/ca.crt"]
ssl.certificate: "/etc/filebeat/certs/client.crt"
ssl.key: "/etc/filebeat/certs/client.key"
此处需特别注意:ssl.verification_mode: certificate 表示既要验证服务器证书,同时Filebeat自身也要提供客户端证书。其中 ssl.certificate_authorities 指向CA证书,用于验证Elasticsearch服务端证书的真实性;ssl.certificate 和 ssl.key 则是Filebeat的身份凭证。
第三步:生成SSL证书
证书部分最容易令人困惑。我们可以使用OpenSSL自签发一套CA和客户端证书。首先创建CA:
openssl req -x509 -newkey rsa:4096 -keyout ca.key -out ca.crt -days 3650 -nodes -subj "/C=US/ST=YourState/L=YourCity/O=YourOrganization/CN=YourCA"
接着为Filebeat生成客户端证书请求与正式证书:
openssl req -newkey rsa:2048 -keyout filebeat.key -out filebeat.csr -nodes -subj "/C=US/ST=YourState/L=YourCity/O=YourOrganization/CN=filebeat"
openssl x509 -req -in filebeat.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out filebeat.crt -days 365
生成完成后,将证书移动到Filebeat配置中指定的目录:
sudo mv filebeat.crt /etc/filebeat/certs/
sudo mv filebeat.key /etc/filebeat/certs/
sudo chmod 600 /etc/filebeat/certs/*.crt
sudo chmod 600 /etc/filebeat/certs/*.key
务必设置权限为600,否则Filebeat会因私钥权限过于开放而报错。
第四步:重启Filebeat
完成配置修改与证书部署后,重启服务使更改生效:
sudo systemctl restart filebeat
若未出现报错,说明配置基本成功。若想确认Filebeat是否正常运行,可通过日志查看:
journalctl -u filebeat -f
第五步:验证加密传输
使用curl模拟一次请求,测试Filebeat能否与Elasticsearch通过HTTPS完成握手:
curl -k --cacert /etc/filebeat/certs/ca.crt --cert /etc/filebeat/certs/client.crt --key /etc/filebeat/certs/client.key https://your_elasticsearch_host:9200/_cat/nodes?v
参数 -k 表示允许自签名证书;若你使用的是正规CA签发的证书,可去掉此选项。若成功返回节点信息,说明加密通道已打通。
最后,请确保你的Elasticsearch集群也已开启SSL/TLS支持。在 elasticsearch.yml 中配置 xpack.security.http.ssl.* 相关参数,使Elasticsearch能够正确验证Filebeat发来的客户端证书。两端配置完成后,数据在网络中将以密文传输,安全无忧。
