ELK搭建文档
ELKB在5.0开始统一版本,本文主要以安装 Elasticsearch-5.x kibana5.x为例,亲测5.0.0到5.1.1的安装方式未变,所以此处以5.0.0版本为例安装。
官网下载5.x:https://www.elastic.co/downloads
Elasticsearch-5.x依赖于jdk-1.8,请确保需要安装的机器jdk版本不低于1.8.0_73
查看:java -version
下载java-1.8:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Elasticsearch解压后主要修改配置文件config/elasticsearch.yml,严格按照原来的格式修改,否则不识别,修改下面几项:
# cluster.name: es_cluster #集群名,依此识别并组成集群
# node.name: node-1 #节点名,集群内每个节点名不同
# network.host: 192.168.0.1 #配置本机ip,以便外网访问
我的data节点配置(.yml的文件冒号后面必须加空格)
cluster.name: es_cluster
node.name: cdh3
network.host: cdh3
添加防脑裂配置(因为elc默认是一个局域网内能搜索到的就是一个集群,一些原因会导致分成几个集群)
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s
discovery.zen.ping.unicast.hosts: ["cdh2","cdh1","cdh4"]
设置该节点仅仅为数据节点
node.master:false #设置不充当master节点,默认为true
node.data:true #设置充当data节点,默认为true
elasticsearch配置的cluster.name相同,都启动可以自动组成集群,这里如果不改cluster.name则默认是cluster.name=elasticsearch,nodename随意取但是集群内的各节点不能相同,host设置自己的ip
另一个master节点的配置如下:
cluster.name: es_cluster
node.name: cdh4
node.master:true#设置充当master节点,默认为true
node.data:false #设置不充当data节点,默认为true
network.host: cdh4
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s
discovery.zen.ping.unicast.hosts: ["cdh2","cdh4","cdh3"]
注: discovery设置自动发现集群,discovery.zen.ping.unicast.hosts 设置具有master权限的es节点ip,防止es的master节点因某些原因脑裂。不设置node.master和node.data则默认每个节点都有master和data权限。
master和data同时配置会产生一些奇异的效果:
1) 当master为false,而data为true时,会对该节点产生严重负荷;
2) 当master为true,而data为false时,该节点作为一个协调者;
3) 当master为false,data也为false时,该节点就变成了一个负载均衡器。
你可以通过连接http://localhost:9200/_cluster/health或者http://localhost:9200/_cluster/nodes
解压安装,修改配置文件viconfig/kibana.yml
Server.host: cdh4 默认localhost,只能本机访问kibana
Server.name: cdh4 此kibana服务的名称
Elasticsearch.url: "http://cdh4:9200" es地址
cd elasticsearch-5.0.0
官网上下载zip包: x-pack-5.0.0.zip
到每个节点$ES_HOME,执行命令:
./bin/plugin install file:/opt/sxt/soft/x-pack-5.0.0.zip
到$KIBANA_HOME(为kibana的主目录,下同),执行命令
bin/kibana-plugin install file:/opt/sxt/soft/x-pack-5.0.0.zip
不启用x-pack安全机制
分别在kibana.yml 和elasticsearch.yml中加入下行
xpack.security.enabled: false
启用x-pack安全机制
分别在kibana.yml 和elasticsearch.yml中配置
xpack.security.enabled: true #默认为true,可不配置
在kibana.yml中配置es的帐号密码
elasticsearch.username: "elastic"
elasticsearch.password: "changeme"
kibana安装xpack后用elastic用户登录
x-pack安装之后有一个超级用户elastic ,其默认的密码是changeme,拥有对所有索引和数据的控制权,可以使用该用户创建和修改其他用户,当然这里可以通过kibana的web界面进行用户和用户组的管理
也可以使用shell 终端进行管理:
修改elastic用户的密码:
curl -XPUT -u elastic:changeme 'localhost:9200/_xpack/security/user/elastic/_password'-d '{
"password" : "123456"
}'
修改kibana用户的密码:
curl -XPUT -u elastic:changeme 'localhost:9200/_xpack/security/user/kibana/_password'-d '{
"password" : "123456"
}'
后台启动,要装插件,前台启动全部./bin/elasticsearch, 如果想在后台以守护进程模式运行,添加-d参数: ./bin/elasticsearch -d。
注:es不能用root用户启动,先更改elasticsearch文件夹的所有者
成功页面访问:http://cdh4:9200
启动kibana bin/kibana
停止kibana:ss -lntp |grep 5601 kill -9端口
http://cdh4:5601/app/monitoring
http://cdh4:5601/app/kibana
Logstash的功能如下:
其实它就是一个收集器而已,我们需要为它指定Input和Output(当然Input和Output可以为多个)。由于我们需要把Java代码中Log4j的日志输出到ElasticSearch中,因此这里的Input就是Log4j,而Output就是ElasticSearch。
配置Logstash:
tar -zxvf logstash-2.4.0.tar.gz
cd logstash-2.4.0
测试:
到logstash目录下:
mkdir config
vi config/stdin_to_es.conf
# For detail structureof 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/
stdin {
}
}
filter {
#Only matched data are send to output.
}
output {
# For detail config for elasticsearch asoutput,
# See:https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html
elasticsearch {
action => "index" #The operation on ES
hosts => "cdh1:9200" #ElasticSearch host, can be array.
index => "applog" #The index to write data to.
}
}
:
./bin/logstash -f config/stdin_to_es.conf
后台运行:nohup ./bin/logstashagent -f config/stdin_to_es.conf &
输入测试数据:
hello logstash
{
"message" => "hello logstash",
"@version" => "1",
"@timestamp" => "2016-11-02T10:40:00.450Z",
"host" => "noc.vfast.com"
}
使用kibana查看:http://cdh4:5601/app/kibana
elasticsearch 5.0 安装过程中遇到了一些问题,这里提供解决的方法。
问题一:警告提示
[2016-11-06T16:27:21,712][WARN][o.e.b.JNANatives ] unable to install syscall filter:
java.lang.UnsupportedOperationException:seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP andCONFIG_SECCOMP_FILTER compiled in
at org.elasticsearch.bootstrap.Seccomp.linuxImpl(Seccomp.java:349)~[elasticsearch-5.0.0.jar:5.0.0]
at org.elasticsearch.bootstrap.Seccomp.init(Seccomp.java:630)~[elasticsearch-5.0.0.jar:5.0.0]
报了一大串错误,其实只是一个警告。
解决:使用新的linux版本,就不会出现此类问题了。
问题二:ERROR: bootstrap checks failed
max file descriptors[4096] for elasticsearch process likely too low, increase to at least [65536]
max number of threads [1024] for user [lishang] likely too low, increase to atleast [2048]
解决:切换到root用户,编辑limits.conf 添加类似如下内容
vi/etc/security/limits.conf
添加如下内容:
*soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
问题三:max number of threads [1024] for user [lish] likely toolow, increase to at least [2048]
解决:切换到root用户,进入limits.d目录下修改配置文件。
vi/etc/security/limits.d/90-nproc.conf
修改如下内容:
* soft nproc 1024
#修改为
* soft nproc 2048
问题四:max virtual memory areas vm.max_map_count [65530] likelytoo low, increase to at least [262144]
解决:切换到root用户修改配置sysctl.conf
vi/etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并执行命令:
sysctl -p
然后,重新启动elasticsearch,即可启动成功。
下载已编译的对应版本zip包:https://github.com/medcl/elasticsearch-analysis-ik/releases/elasticsearch-analysis-ik-5.0.0.zip
也可以自己编译安装,详情看:https://github.com/medcl/elasticsearch-analysis-ik
在安装好的elasticsearch中在plugins目录下新建ik目录,将此zip包拷贝到ik目录下
将权限修改为elasticsearch启动用户的权限,通过unzip命令解压缩
例如在plugins/ik目录下执行unzip elasticsearch-analysis-ik-5.0.0.zip
解压后查看 得到解压后的结果
在每个节点安装,重启elasticsearch集群即可
(一)Beats是什么?
Beats是elasticsearch公司开源的一款采集系统监控数据的代理agent,它可以发送不同类型的数据到elasticsearch中,也可以行将采集完的数据发送到logstash中转,然后在推送到elasticsearch中,目前还在发展中,与成熟的监控系统zabbix和ganglia相比就界面看起来爽了点,系统功能还是有点弱,不过与elasticsearch全文搜索框架集成后,数据查询过滤功能非常强悍,还是非常有前途
的,在ELKB中,各个框架角色如下:
Beats:负责收集系统数据,可以直接发送到es中,也可以通过logstash中转
logstash:收集日志,为beats提供中转功能
Elasticsearch:提供数据存储,服务端聚合计算功能
Kibana:提供炫丽的可视化图形展示并且作为elasticsearch的搜索的小清新客户端
(二)Beats-5.0的组成:
到目前elasticsearch已经提供的有:
(1)Packetbeat 网络流量监控采集
(2)metricbeat类似linux top的监控采集
(3)Filebeat文件log的监控采集
(4)WinlogBeat windows系统的log监控采集
(5)自定义beat ,如果上面的指标不能满足需求,elasticsarch公司鼓励开发者
使用go语言,扩展实现自定义的beats指标,只需要按照模板,实现监控的输入,日志,输出等即可
(三)Beats的基本拓扑:
Elk已安装完毕
Metricbeat定期收集系统信息如每个进程信息、负载、内存、磁盘等等,然后将数据发送到elasticsearch进行索引。
Metricbeat收集的指标有:
系统统计信息
系统负载: 最后1分钟、最后5分钟、最后15分钟
CPU使用情况: user (和百分比), system, idle, IOWait等等
内存使用情况: 总共, 已用 (和百分比), 剩余等等
swap使用情况:总共, 已用 (和百分比), 剩余等等
每个进程的统计信息
进程名
进程PID
进程状态
进程ID
进程使用CPU情况: 用户 (和百分比), 系统, 总数 和 启动时间
进程使用内存情况: 虚拟内存,常驻内存(和百分比) 和 共享内存
文件系统统计信息
可用磁盘列表
每个磁盘、名称、类型和挂载目录
每个磁盘总大小、已用(和百分比)、剩余和可用空间
Metricbeat可以将这些收集到的指标直接插入到elasticsearch或者是使用logstash。
要使用这个你要安装elasticsearch,kibana,参见前文。我这里直接存储到elasticsearch。
官网下载Metricbeat-5.0.0的tar包:
解压分发配置:vi /opt/sxt/soft/beats/metricbeat-5.0.0-linux-x86_64/metricbeat.yml
input:
period: 10
procs: [".*"]
stats:
system: true
proc: true
filesystem: true
output:
elasticsearch:
hosts: ["cdh4:9200"]
shipper:
logging:
files:
period 选项定义收集信息的频率,默认是10秒。
procs 选项定义正则表达式,以匹配你所要监控的进程。默认是所有正在运行的进程都进行监控。
如果不监控进程,可以这么做:
input:
period: 10
procs: ["^$"]
导入elasticsearch索引模板
将topbeat提供的索引模板导入到elasticsearch,以便elasticsearch知道哪些字段以哪种方式进行分析。
# curl -XPUT'http://cdh4:9200/_template/metricbeat' -d@/opt/sxt/soft/beats/metricbeat-5.0.0-linux-x86_64/metricbeat.template.json
运行metricbeat:# ./metricbeat start
后台运行:#nohup ./metricbeat start &
加载kibana Dashboards
下载Beats-dashboards:https://artifacts.elastic.co/downloads/beats/beats-dashboards/beats-dashboards-5.0.0.zip
加载方法如下:
# unzip beats-dashboards-5.0.0.zip
#cd metricbeat -5.0.0-linux-x86_64
# ./scripts/import_dashboards -dir/opt/sxt/soft/beats/beats-dashboards-5.0.0/ metricbeat -es http://cdh4:9200
选择仪表盘
选择Metribeat监控指标,就可以看见监控的走势图了,最上面有个搜索框,可以任意搜索你监控的机器节点, 如图
Packetbeat 是一个实时网络数据包分析工具,与elasticsearch一体来提供应用程序的监控和分析系统。
Packetbeat通过嗅探应用服务器之间的网络通讯,来解码应用层协议类型如HTTP、MySQL、redis等等,关联请求与响应,并记录每个事务有意义的字段。
Packetbeat可以帮助我们快速发现后端应用程序的问题,如bug或性能问题等等,修复排除故障也很快捷。
Packetbeat目前支持的协议有:
HTTP、MySQL、PostgreSQL、Redis、Thrift-RPC、MongoDB、DNS、Memcache
Packetbeat可以将相关事务直接插入到elasticsearch或redis(不推荐)或logstash。
Packetbeat可以运行在应用服务器上或者独自的服务器。当运行在独自服务器上时,需要从交换机的镜像端口或者窃听设备上获取网络流量。
对第七层信息解码后,Packetbeat关联与请求相关的响应,称之为事务。每个事务,Packetbeat插入一个json格式文档到elasticsearch。然后可通过kibana进行分析展示。
安装:
官网下载tar包解压:
配置:
# vi/opt/sxt/soft/beats/packetbeat-5.0.0-linux-x86_64/packetbeat.yml
选择要从哪个网卡嗅探网络通讯,默认是所有的网络接口。
interfaces:
# Select on which network interfaces tosniff. You can use the "any"
# keyword to sniff on all connectedinterfaces.
device: any
在协议部分,配置端口以便Packetbeat找到每个端口对应的协议。如果使用非标准端口,需要添加上。多个端口以逗号分隔。
protocols:
# Configure which protocols to monitor and onwhich ports are they
# running. You can disable a given protocolby commenting out its
# configuration.
http:
ports: [80, 8080, 8081, 5000, 8002]
memcache:
ports: [11211]
mysql:
ports: [3306]
redis:
ports: [6379]
pgsql:
ports: [5432]
thrift:
ports: [9090]
定义elasticsearch服务
output:
elasticsearch:
# Uncomment out this option if you want tooutput to Elasticsearch. The
# default is false.
enabled: true
# Set the host and port where to find Elasticsearch.
hosts: ["cdh4:9200"]
# Uncomment this option and set it to trueif you want to store the topology in
# Elasticsearch. Default behavior if thissetting is left out of the
# config file is equivalent to setting"save_topology" to "false"
#save_topology: false
加载elasticsearch索引模板
加载索引模板,以便elasticsearch知道哪些字段该以何种方式进行分析。
# curl -XPUT'http://cdh4:9200/_template/packetbeat'-d@/opt/sxt/soft/beats/packetbeat-5.0.0-linux-x86_64/packetbeat.template.json
启动服务
# ./ packetbeat-5.0.0-linux-x86_64/packetbeatstart
后台启动:
# nohup ./packetbeat-5.0.0-linux-x86_64/packetbeatstart &
加载kibana Dashboards (见Metricbeat安装最后)
# cd packetbeat-5.0.0-linux-x86_64
# ./scripts/import_dashboards -dir /opt/sxt/soft/beats/beats-dashboards-5.0.0/packetbeat-es http://cdh4:9200
Web页面查看参照metricbeat仪表盘加载
Filebeat是一个以logstash-forwarder的源码为基础的日志收集器,以客户端的形式安装在要被监控日志的服务器上,监控日志目录或日志文件(以查看文件尾的形式),然后将日志数据转发给Logstash解析或者Elasticsearch建立索引。这样就可以在多台待收集日志的机器上部署Filebeat,然后在另一台服务器上部署Logstash或者Elasticsearch收集各个Filebeat发过来的日志,方便扩展和维护。
Filebeat的工作原理如下:Filebeat启动后,会开启若干个“prospector”搜索配置路径下的日志,针对每一个日志会开启一个“harvester”不停地监控、收集日志中的新增部分,然后把收集到的日志发送给”spooler”,“spooler”负责整合数据信息然后将信息发送到特定的位置(如Logstash或者Elasticsearch)。
安装:官网下载filebeat-5.0.0的tar包解压即可
配置:vi/usr/lib/beats/filebeat-5.0.0-linux-x86_64/filebeat.yml
prospectors:
behaviour.
paths:
- /var/log/*.log
#- c:\programdata\elasticsearch\logs\*
input_type: log
output:
### Elasticsearch as output
elasticsearch:
hosts: ["cdh4:9200"]
导入elasticsearch索引模板
curl -XPUT'http://cdh4:9200/_template/filebeat' -d@/opt/sxt/soft/beats/filebeat-5.0.0-linux-x86_64/filebeat.template.json
启动filebeat:./filebeat-5.0.0-linux-x86_64/filebeatstart
后台启动:nohup ./filebeat-5.0.0-linux-x86_64/filebeatstart &
加载kibana Dashboards
# cd filebeat-5.0.0-linux-x86_64
# ./scripts/import_dashboards -dir/opt/sxt/soft/beats/beats-dashboards-5.0.0/filebeat -es http://cdh4:9200
在每一台需要监控的节点上安装Beats
转载请注明出处:http://blog.csdn.net/qq_21835703/article/details/53183645