ELKStack实战之Elasticsearch [ 一 ]

ELKStack简介

ELK StackElasticsearchLogstashKibana 三个开源软件的组合。在实时数据检索和分析场合,三者通常是配合共用,而且又都先后归于 Elastic.co 公司名下,故有此简称。

ELK Stack 在最近两年迅速崛起,成为机器数据分析,或者说实时日志处理领域,开源界的第一选择。和传统的日志处理方案相比,ELK Stack 具有如下几个优点:

  • 处理方式灵活。Elasticsearch 是实时全文索引,不需要像 storm 那样预先编程才能使用;
  • 配置简易上手。Elasticsearch 全部采用 JSON 接口,Logstash 是 Ruby DSL 设计,都是目前业界最通用的配置语法设计;
  • 检索性能高效。虽然每次查询都是实时计算,但是优秀的设计和实现基本可以达到全天数据查询的秒级响应;
  • 集群线性扩展。不管是 Elasticsearch 集群还是 Logstash 集群都是可以线性扩展的;
  • 前端操作炫丽。Kibana 界面上,只需要点击鼠标,就可以完成搜索、聚合功能,生成炫丽的仪表板

ELK 地址:https://www.elastic.co/
Logstash 最佳实践:http://udn.yyuap.com/doc/logstash-best-practice-cn/index.html
Elasticsearch 权威指南:http://www.learnes.net/index.html
ELKStack 中文社区:https://kibana.logstash.es/content/

Elasticsearch部署

Elasticsearch首先需要Java环境,所以需要提前安装好JDK,可以直接使用yum安装。也可以从Oracle官网下载JDK进行安装。开始之前要确保JDK正常安装并且环境变量也配置正确

安装JDK

1
$ yum install -y java

安装ElasticSearch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 下载并安装GPG key
$ rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
// 添加yum仓库
$ cat /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
// 安装elasticsearch
$ yum install -y elasticsearch

配置elasticsearch

1
2
3
4
5
6
7
8
$ vim /etc/elasticsearch/elasticsearch.yml
cluster.name: myes   #ES集群名称
node.name: carey-node-1  #节点名称
path.data: /data/es-data #数据存储的目录(多个目录使用逗号分隔)
path.logs: /var/log/elasticsearch #日志格式
bootstrap.memory_lock: true #锁住es内存,保证内存不分配至交换分区
network.host: 192.168.56.101 #设置本机IP地址
http.port: 9200 #端口默认9200

设置数据目录权限

1
2
$ mkdir -p /data/es-data
$ chown -R elasticsearch:elasticsearch /data/es-data/

启动

1
2
3
4
$ systemctl start elasticsearch.service
$ netstat -anpt
tcp6 0 0 127.0.0.1:9200 :::* LISTEN 32490/java
tcp6 0 0 127.0.0.1:9300 :::* LISTEN 32490/java

访问测试

访问地址:192.168.56.101:9200

Elasticsearch插件介绍

Haed插件

插件作用:主要是做es集群管理的插件
Github下载地址:https://github.com/mobz/elasticsearch-head

1
2
3
4
5
6
7
8
//安装head
$ /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ...............DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /usr/share/elasticsearch/plugins/head

访问地址:http://192.168.56.101:9200/_plugin/head/

这里看到域名是因为我服务器在家里,通过frp内网穿透出来的必须通过域名去访问,这个地址可以忽略。

Kopf插件

插件作用:kopf是一个简单的网络管理工具
Kopf不再维护。已经开发了替代品 cerebro,目前维护在https://github.com/lmenezes/cerebro。在这一点上,cerebro应该有相当于kopf的功能,顶部有一些新的功能。

Github地址:https://github.com/lmenezes/elasticsearch-kopf

1
2
3
4
5
6
7
8
//安装kopf
$ /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
-> Installing lmenezes/elasticsearch-kopf...
Trying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip ...
Downloading ..................DONE
Verifying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed kopf into /usr/share/elasticsearch/plugins/kopf

访问地址:http://192.168.56.101:9200/_plugin/kopf/

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器