分布式跟踪ELK
一、为什么要用ELK
1.1、小系统或单机系统的日志搜索现状?
直接在日志文件中 grep、awk 就可以获得自己想要的信息。
1.2、大系统或分布式系统的日志面临的问题?
一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上;问题出现时,大部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块;一般是采用grep,awk,效率非常低下;如果有一套集中式日志系统,可以提高定位问题的效率;
大规模的分布式面临问题如下:
- 集群环境下的日志查询如何查询?
- 日志量太大如何归档?
- 文本搜索太慢怎么办?
- 如何多维度查询?
1.3、分布式系统的日志搜索的解决方案
需要集中化的日志管理,所有服务器上的日志收集汇总。常见解决思路是建立集中式日志收集系统,将所有节点上的日志统一收集,管理,访问。
一个完整的集中式日志系统,需要包含以下几个主要特点:
- 收集-能够采集多种来源的日志数据
- 传输-能够稳定的把日志数据传输到中央系统
- 存储-如何存储日志数据
- 分析-可以支持 UI 分析
- 警告-能够提供错误报告,监控机制
1.4、ELK介绍
ELK是目前主流的一种日志系统;提供了一整套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用。
ELK是三个开源软件的缩写,分别表示:ElasticSearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。
ElasticSearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式(支持以 TCP/UDP/HTTP 多种方式收集数据)。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作,再一并 发往elasticsearch上去。
Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
Filebeat隶属于Beats。目前Beats包含四种工具:
- Packetbeat(搜集网络流量数据)
- Topbeat(搜集系统、进程和文件系统级别的 CPU 和内存使用情况等数据)
- Filebeat(搜集文件数据)
- Winlogbeat(搜集 Windows 事件日志数据)
1.5、ELK架构原理
二、ELK搭建
2.1、ElasticSearch搭建
2.1.1、查看JDK版本
java -version
2.1.2、创建用户
从5.0开始,ElasticSearch 安全级别提高了,不允许采用root帐号启动,所以我们要添加一个用户。
2.1.2.1、创建elk 用户组
groupadd elk
2.1.2.2、创建用户qixin
useradd qixin
passwd qixin
2.1.2.3、将agan用户添加到elk组
usermod -G elk qixin
2.1.2.4、设置sudo权限
visudo
找到root ALL=(ALL) ALL一行,添加qixin用户,如下。
##Allow root to run any commands anywhere
root ALL=(ALL) ALL
qixin ALL=(ALL) ALL.
2.1.2.5、切换用户
su qixin
2.1.3、安装elasticsearch
2.1.3.1、下载压缩
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz
tar -zxvf elasticsearch-6.2.3.tar.gz
2.1.3.2、 修改目录权限
mv elasticsearch-6.2.3/ elasticsearch
sudo chown -R agan:elk ./
2.1.3.3、ElasticSearch 配置
2.1.3.3.1、修改elasticsearch.yml
修改network.host和http.port
vi config/elasticsearch.yml
network.host: 0.0.0.0
http.port: 9200
2.1.3.3.2、切换用户 root
su root
2.1.3.3.3、修改/etc/sysctl.conf
vi /etc/sysctl.conf
添加内容如下:
vm.max_map_count=262144
sysctl -p
2.1.3.3.4、修改文件/etc/security/limits.conf
vi /etc/security/limits.conf
添加如下内容
*soft nofile 65536
*hard nofile 65536
*soft nofile 65536
*hard nofile 65536
2.1.3.4、ElasticSearch启动与停止
2.1.3.4.1、切换用户 qixin
su qixin
2.1.3.4.2、启动
./elasticsearch -d
2.1.3.4.3、验证
curl http://47.106.128.80:9200
http://47.106.128.80:9200/
2.1.4、安装Head插件
2.1.4.1、Head插件简介
ElasticSearch-head是一个H5编写的ElasticSearch集群操作和管理工具,可以对集群进行傻瓜式操作。
它提供了:
- 索引和节点级别操作
- 搜索接口能够查询集群中原始json或表格格式的检索数据
- 能够快速访问并显示集群的状态
2.1.4.2、安装NodeJS
su root
#要求在root下执行
curl -sL https://rpm.nodesource.com/setup_8.x | bash -
yum install -y nodejs
2.1.4.3、安装npm
npm install -g cnpm --registry=https://registry.npm.taobao.org
2.1.4.4、使用npm安装grunt
npm install -g grunt
npm install -g grunt-cli --registry=https://registry.npm.taobao.org --no-proxy
2.1.4.5、查看以上版本
su qixin
node -v
npm -v
grunt -version
2.1.4.6、下载head插件源码
cd /home/agan
mkdir es
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip master.zip
2.1.4.7、国内镜像安装
cd elasticsearch-head-master
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
sudo cnpm install
2.1.5、配置
2.1.5.1、配置 ElasticSearch,使得HTTP对外提供服务
vi config/elasticsearch.yml
添加如下内容
#增加新的参数,这样head插件可以访问es。设置参数的时候:后面要有空格
http.cors.enabled: true
http.cors.allow-origin: “*”
2.1.5.2、修改Head插件配置文件
vi Gruntfile.js
找到connect:server,添加hostname一项,如下:
connect: {
server: {
options: {
hostname: '0.0.0.0',
port: 9100,
base: '.',
keepalive: true
}
}
}
2.1.6、启动
2.1.6.1、重启elasticsearch
./elasticsearch -d
2.1.6.2、启动head
elasticsearch-head-master目录下:
grunt server
或 npm run start
2.1.6.3、访问9100端口
2.1.7、简单应用
2.1.7.1、创建索引
curl -XPUT http://47.106.128.80:9200/applog
2.1.7.2、查看head变化
http://47.106.128.80:9100/
2.2、logstash搭建
2.2.1、下载压缩
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.3.tar.gz
tar zxvf logstash-6.2.3.tar.gz
mv logstash-6.2.3/ logstash
2.2.2、测试
./bin/logstash -e ‘input { stdin { } } output { stdout {} }’
2.2.3、修改配置
在 logstash的主目录下
vim config/log_to_es.conf
内容如下:
# For detail structure of this file
# Set: https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
input {
# For detail config for log4j as input,
# See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html
tcp {
mode => "server"
host => "47.106.128.80"
port => 9250
}
}
filter {
#Only matched data are send to output.
}
output {
# For detail config for elasticsearch as output,
# See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html
elasticsearch {
action => "index" #The operation on ES
hosts => "47.106.128.80:9200" #ElasticSearch host, can be array.
index => "applog" #The index to write data to.
}
}
2.2.4、启动
./bin/logstash -f config/log_to_es.conf
或 后台运行守护进程 ./bin/logstash -f config/log_to_es.conf &
2.2.5、测试
curl ‘http://47.106.128.80:9200/_search?pretty’
2.3、kibana搭建
2.3.1、下载压缩
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.3-linux-x86_64.tar.gz
tar zxvf kibana-6.2.3-linux-x86_64.tar.gz
mv kibana-6.2.3-linux-x86_64.tar.gz/ kibana
2.3.2、修改配置
vim config/kibana.yml
把以下注释放开,使配置起作用。
server.port: 5601
server.host: “192.168.1.245”
elasticsearch.url: http://192.168.48.35:9200
kibana.index: “.kibana”
2.3.3、启动
./bin/kibana
2.3.4、测试
http://192.168.48.35:5601/app/kibana
三、SpringCloud与ELK集成
3.1、添加jar
<dependency> <!-- sleuth链路跟踪 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency> <!-- springcloud与ELK集成 -->
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.0</version>
</dependency>
3.2、修改logback.xml
配置日志输出logstash信息