ELK 搭建
ELK 介绍
ELK 是三个开源项目的首字母缩写,这三个项目分别是:Elasticsearch、Logstash、Kibana。
- Elasticsearch 是一个搜索和分析引擎。
- Logstash 是服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到诸如 Elasticsearch 等“存储库”中。
- Kibana 则可以让用户在 Elasticsearch 中使用图形和图表对数据进行可视化。
ELK 安装
以 Mac 环境搭建为例:
安装ES
$ brew install elastic/tap/elasticsearch-full
$ brew info elastic/tap/elasticsearch-full
$ brew services start elastic/tap/elasticsearch-full
启动后访问: http://localhost:9200/
{
"name" : "xxxdeMacBook-Pro.local",
"cluster_name" : "elasticsearch_xxx",
"cluster_uuid" : "KP9IynxzRV602tc17cODww",
"version" : {
"number" : "7.17.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "79878662c54c886ae89206c685d9f1051a9d6411",
"build_date" : "2022-05-18T18:04:20.964345128Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
安装kibana
$ brew install elastic/tap/kibana-full
$ brew info elastic/tap/kibana-full
$ brew services start elastic/tap/kibana-full
启动后访问:http://localhost:5601/
安装Logstash
$ brew install elastic/tap/logstash-full
$ brew info elastic/tap/logstash-full
$ brew services start elastic/tap/logstash-full
设置日志配置文件
$ vi log.conf
input {
file {
# 监听日志目录
path => ["/tmp/projectname/info.log"]
}
}
output {
# logstash 对接es
elasticsearch{
hosts => ["http://localhost:9200"]
index => "projectname-log"
}
}
执行 logstash 日志同步
$ logstash log.conf
kibana 操作
- Create index pattern 创建索引
- Discover 查看同步过来的日志
ELK-linux
Linux 环境下,需要注意 ES 的集群部署、索引的mapping、索引的分片等。
Elastic Stack
随着ELK发展,日志采集组件逐渐使用 Beats 替代,如 Filebeat 它是go开发的一款轻量级采集组件。ELK 也更名为 Elastic Stack。
Filebeat
Filebeat 采集后端可以对接如 Redis、Kafka、ES、Logstash等组件,最后落到如ES等存储库中。
-
安装
-
修改 filebeat.yml配置文件
- input 日志目录
- output 外部输出到哪,如redis、kafka等
举例
- 输出到redis
output.redis:
enable: true
hosts: ["127.0.0.1:6379"]
datatype: list
keys:
- key: "filebeat-log"
- 输出到kafka
output.kafka:
hosts: ["10.0.0.1:9095", "10.0.0.1:9096", "10.0.0.1:9097"]
topic: filebeat_kafka_log
partition.round_robin:
reachable_only: true
- 启动
sudo /usr/share/filebeat/bin/filebeat -e -c /etc/filebeat/filebeat.yml