Elastic Stack and Product Introduction
Elastic Stack 产品说明,重要指数 ★★☆
Elastic Stack 链接目录,包含各种产品的使用手册,例如:ElasticSearch权威指南(英文)、ElasticSearch使用手册、Kibana使用手册等等。重要指数 ★★★★★
ELK简介
- E: ElasticSearch 基于Luence构建的开源、分布式、RESTful接口全文搜索引擎,它还是一个分布式文档数据库,其中的每个字段都是被索引的数据,并且可被搜索,能够扩展到数以百计的服务器存储以及处理PB级别的数据。
- L: Logicstash 是一个灵活、开源的数据收集、处理、传输的工具。它可以处理日志、事件、非结构化的数据,并把他们输出出来,比如输出到ElasticSearch中
- K: Kibana 能够可视化 Elasticsearch 中的数据并操作 Elastic Stack,例如,雨水会对季度数据造成怎样的影响。
业界一般将ELK组合专门用来处理、存储、展示日志。
ElasticSearch
- 送一本书(中文): 《Elasticsearch 权威指南》,基于
ElasticSearch 2.0
,集群原理可以在这里面找到,其他的最好参考最新版本的使用手册,因为新版本改动很大,尤其是6.0+
,重要指数 ★★★☆ - 使用手册: https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
- 安装版本: elasticsearch-6.0.0
- 操作系统: CenOS 6.7
安装与配置
tar -zxvf elasticsearch-6.0.0.tar.gz -C /opt/elasticsearch/
cd /opt/elasticsearch/elasticsearch-6.0.0/config/
# 其中包含三个文件 elasticsearch.yml jvm.options log4j2.properties
vim elasticsearch.yml
# 索引存储位置,默认在$ES_HOME/下的data文件夹,如果需要配置在其他路径,修改此属性。
path.data: /opt/elasticsearch/data
# 日志存储位置,默认在$ES_HOME/下的logs文件夹,如果需要配置在其他路径,修改此属性。
path.logs: /opt/elasticsearch/logs
# 内存分配模式
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
# 绑定IP
network.host: 10.4.121.79
# 端口号
http.port: 9200
# 当删除索引时,必须指定索引名称,禁止使用通配符或者_all。这样做的目的是为了防止误删。
action.destructive_requires_name: true
修改完配置文件之后,cd /opt/elasticsearch/elasticsearch-6.0.0/bin
,然后执行./elasticsearch
启动es。
如果启动成功了,在浏览器访问http://10.4.121.79:9200
可以看到
{
"name" : "irLY0__",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "djFq3wkhQz-GVOIJckvIgw",
"version" : {
"number" : "6.0.0",
"build_hash" : "8f0685b",
"build_date" : "2017-11-10T18:41:22.859Z",
"build_snapshot" : false,
"lucene_version" : "7.0.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
启动异常
[2017-11-20T11:02:27,267][WARN ][o.e.b.JNANatives ] unable to install syscall filter:
java.lang.UnsupportedOperationException: seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed
at org.elasticsearch.bootstrap.SystemCallFilter.linuxImpl(SystemCallFilter.java:341) ~[elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.SystemCallFilter.init(SystemCallFilter.java:616) ~[elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.JNANatives.tryInstallSystemCallFilter(JNANatives.java:258) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.Natives.tryInstallSystemCallFilter(Natives.java:113) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:109) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:322) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:130) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:121) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:69) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.cli.Command.main(Command.java:90) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) [elasticsearch-6.0.0.jar:6.0.0]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:85) [elasticsearch-6.0.0.jar:6.0.0]
[2017-11-20T11:02:27,544][INFO ][o.e.n.Node ] [node-1] initializing ...
[2017-11-20T11:02:27,730][INFO ][o.e.e.NodeEnvironment ] [node-1] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [22.8gb], net total_space [49gb], types [rootfs]
[2017-11-20T11:02:27,731][INFO ][o.e.e.NodeEnvironment ] [node-1] heap size [990.7mb], compressed ordinary object pointers [true]
[2017-11-20T11:02:27,734][INFO ][o.e.n.Node ] [node-1] node name [node-1], node ID [irLY0__cTcSCke-RWHyl3w]
[2017-11-20T11:02:27,734][INFO ][o.e.n.Node ] [node-1] version[6.0.0], pid[1644], build[8f0685b/2017-11-10T18:41:22.859Z], OS[Linux/2.6.32-573.el6.x86_64/amd64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_111/25.111-b14]
[2017-11-20T11:02:27,735][INFO ][o.e.n.Node ] [node-1] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/opt/elasticsearch/elasticsearch-6.0.0, -Des.path.conf=/opt/elasticsearch/elasticsearch-6.0.0/config]
[2017-11-20T11:02:29,299][INFO ][o.e.p.PluginsService ] [node-1] loaded module [aggs-matrix-stats]
[2017-11-20T11:02:29,300][INFO ][o.e.p.PluginsService ] [node-1] loaded module [analysis-common]
[2017-11-20T11:02:29,300][INFO ][o.e.p.PluginsService ] [node-1] loaded module [ingest-common]
[2017-11-20T11:02:29,300][INFO ][o.e.p.PluginsService ] [node-1] loaded module [lang-expression]
[2017-11-20T11:02:29,300][INFO ][o.e.p.PluginsService ] [node-1] loaded module [lang-mustache]
[2017-11-20T11:02:29,300][INFO ][o.e.p.PluginsService ] [node-1] loaded module [lang-painless]
[2017-11-20T11:02:29,301][INFO ][o.e.p.PluginsService ] [node-1] loaded module [parent-join]
[2017-11-20T11:02:29,301][INFO ][o.e.p.PluginsService ] [node-1] loaded module [percolator]
[2017-11-20T11:02:29,301][INFO ][o.e.p.PluginsService ] [node-1] loaded module [reindex]
[2017-11-20T11:02:29,301][INFO ][o.e.p.PluginsService ] [node-1] loaded module [repository-url]
[2017-11-20T11:02:29,301][INFO ][o.e.p.PluginsService ] [node-1] loaded module [transport-netty4]
[2017-11-20T11:02:29,301][INFO ][o.e.p.PluginsService ] [node-1] loaded module [tribe]
[2017-11-20T11:02:29,302][INFO ][o.e.p.PluginsService ] [node-1] no plugins loaded
[2017-11-20T11:02:31,866][INFO ][o.e.d.DiscoveryModule ] [node-1] using discovery type [zen]
[2017-11-20T11:02:32,984][INFO ][o.e.n.Node ] [node-1] initialized
[2017-11-20T11:02:32,984][INFO ][o.e.n.Node ] [node-1] starting ...
[2017-11-20T11:02:33,421][INFO ][o.e.t.TransportService ] [node-1] publish_address {10.4.121.79:9300}, bound_addresses {10.4.121.79:9300}
[2017-11-20T11:02:33,450][INFO ][o.e.b.BootstrapChecks ] [node-1] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
ERROR: [4] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [1024] for user [elasticsearch] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[4]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[2017-11-20T11:02:33,477][INFO ][o.e.n.Node ] [node-1] stopping ...
[2017-11-20T11:02:33,547][INFO ][o.e.n.Node ] [node-1] stopped
[2017-11-20T11:02:33,547][INFO ][o.e.n.Node ] [node-1] closing ...
[2017-11-20T11:02:33,569][INFO ][o.e.n.Node ] [node-1] closed
对于最上面的异常unable to install syscall filter:
,只是警告(WARN),使用新的linux版本系统就好了,不处理也没关系。
对于Error [2017-11-20T11:02:33,450][INFO ][o.e.b.BootstrapChecks ] [node-1] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks ERROR: [4] bootstrap checks failed
,可以看出一共有4个错误。
# 问题[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
# 原因: 无法创建本地文件问题,用户最大可创建文件数太小
# 解决方案:
sudo vim /etc/security/limits.conf
# 添加如下内容: 其中 * 代表Linux所有用户的名称(比如 elasticsearch、oracle、hadoop)
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
# 保存、退出当前用户、重新登陆生效
# 问题[2]: max number of threads [1024] for user [elasticsearch] is too low, increase to at least [4096]
# 原因: 无法创建本地线程问题,用户最大可创建线程数太小
# 解决方案:
sudo vim /etc/security/limits.d/90-nproc.conf
# 将
* soft nproc 1024
# 修改为
* soft nproc 4096
# 保存、退出当前用户、重新登陆生效
# 问题[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
# 原因: 最大虚拟内存太小
# 解决方案:
sudo vim /etc/sysctl.conf
# 添加下面配置:
vm.max_map_count=262144
# 然后执行命令:
sudo sysctl -p
# 或者,直接执行下面的命令
sudo sysctl -w vm.max_map_count=262144
# 问题[4]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
# 原因: Centos6不支持SecComp,而ES默认bootstrap.system_call_filter为true进行检测,所以导致检测失败,失败后直接导致ES不能启动。
# 详见: https://github.com/elastic/elasticsearch/issues/22899
# 解决方案:
# 在elasticsearch.yml中新增配置bootstrap.system_call_filter,设为false,注意要在Memory下面:
vim $ES_HOME/config/elasticsearch.yml
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
# 最后,重新启动elasticsearch,即可启动成功。
cd $ES_HOME/bin
# -d, --daemonize Starts Elasticsearch in the background
./elasticsearch -d
以上问题解决后,es启动成功了,但又遇到了新的问题,本地机器无法访问虚拟机的服务,两个原因:
- 9200被限制为本机访问,需要在es的配置文件elasticsearch.yml中新增配置:
network.bind_host:0.0.0.0
- 关闭防火墙
Kibana
- 产品介绍: https://www.elastic.co/cn/products/kibana
- 下载地址: https://www.elastic.co/cn/downloads/kibana
- 使用手册: https://www.elastic.co/guide/en/kibana/current/index.html
- 操作系统: CentOS 6.7
- 使用版本: kibana-6.0.0-linux-x86_64.tar.gz
Kibana 安装与配置
解压之后,进入目录 $KIBANA_HOME/config
,修改配置文件kibana.yml
,Kibana配置项详细说明
# 指定 kibana server host
server.host: "10.4.121.79"
# 指定 kibana server port,默认5601
server.port: 5601
# es 地址
elasticsearch.url: "http://10.4.121.79:9200"
# 指定 kibana日志文件路径,默认stdout控制台输出,不保存文件。
logging.dest: "/opt/elasticsearch/kibana-6.0.0-linux-x86_64/logs/kibana.log"
然后进入目录 $KIBANA_HOME\bin
,执行./kibana
使用 Kibana
启动成功之后就可以访问 http://10.4.121.79:5601
了。
首次使用的时候,需要添加一个index pattern
,用来告诉kibana我们需要从es里面搜索哪些索引,当然可以添加多个index pattern
切换。
停止 Kibana
当后台启动Kibana。即./kibana &
。关闭shell窗口之后,就无法使用jobs
命令去查看进程号了。但奇怪的是在别的shell窗口使用ps aux|grep kibana
没有任何信息(正常应该有的)。尝试使用端口号查看进口号。
# 查看端口占用信息可以看到进程号是 6833
netstat -anp | grep 5601
# 显示信息如下
tcp 0 0 10.4.121.79:5601 0.0.0.0:* LISTEN 6833/./../node/bin/
# 或者使用 fuser 命令,可以查看文件或者TCP、UDP端口号的进程号
fuser -n tcp 5601
# 显示信息如下
5601/tcp: 6833
知道进程号后kill即可。
Secure Elasticsearch Cluster (6.0+)
- x-pack 产品介绍:https://www.elastic.co/products/x-pack
- x-pack 安装说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/installing-xpack-es.html
x-pack 集合了诸多强大的功能:security(旧称shield)、alerting(通过watcher实现)、monitoring(旧称marvel)、reporting、graph、machine-learning。
安装x-pack的时候需要先停止es服务。 由于是在内网环境下搭建,无法通过外网直接安装X-Pack,所以需要下载X-Pack安装包。这个安装包包含了elasticsearch、logstash、kibana三个组件的x-pack安装包,安装时复用此文件即可。
在elasticsearch中安装X-Pack插件
- 安装插件。执行
bin/elasticsearch-plugin install file:///opt/elasticsearch/x-pack-6.0.0.zip
。x-pack需要一些权限,全部y
就可以了。或者在install的时候使用option--batch
自动授权,bin/elasticsearch-plugin install --batch file:///opt/elasticsearch/x-pack-6.0.0.zip
。 - 启动es。执行
bin/elasticsearch -d
- 设置ELK的初始密码 (每次重启集群都要重新设置)。模式有两个
auto
(自动生成密码)和interactive
(手动指定密码)。当为elastic用户设置密码后,The Elastic Bootstrap Password就会失效,而且再也不能执行setup-passwords
命令,除非重新启动集群。 如果想修改密码可以去kibana页面中修改:Management -> Security -> Users。
bin/x-pack/setup-passwords auto
# 记下自动生成的密码
Changed password for user kibana
PASSWORD kibana = Ey1OBGFHBp90aGXngnk2
Changed password for user logstash_system
PASSWORD logstash_system = @GpB%bjCP?X@4sJ2j?MP
Changed password for user elastic
PASSWORD elastic = WcY+J+nM0xi40S858cqw
在kibana中安装X-Pack插件
- 在
$KIBANA_HOME
目录下执行bin/kibana-plugin install file:///opt/elasticsearch/x-pack-6.0.0.zip
。优化和缓存浏览器的过程会稍微长一点,耐心等一下。 - 修改
$KIBANA_HOME/config/kibana.yml
elasticsearch.username: "kibana" # <pwd> 是上面生成的 kibana 的密码。 elasticsearch.password: "<pwd>"
- 后台启动。
bin/kibana &
- 在浏览器访问kibana,进入
http://10.4.121.79:5601
。用户名 elastic,密码是上面生成的 elastic 的密码
X-Pack 的功能的启用与禁止
X-Pack的功能默认是都启用的,可以在$ES_HOME/config/elasticsearch.yml
完全禁用某些功能。
- Machine Learning:
xpack.ml.enabled: false
。 - Monitoring:
xpack.monitoring.enabled: false
。 - Security:
xpack.security.enabled: false
。 - Watcher:
xpack.watcher.enabled: false
。
下面这些功能可以在$ES_HOME/config/kibana.yml
中禁止,kibana页面中不会显示这些功能,但并没有真正禁用。
- Reporting:
xpack.reporting.enabled: false
。 - Security:
xpack.security.enabled: false
。 - Development Tools
- Graph:
xpack.graph.enabled: false
- Machine Learning:
xpack.ml.enabled: false
Secure Elasticsearch Cluster (7.0+)
Note: elasticsearch-oss 版本是不带 x-pack 的,下载的时候需要注意,不要下载 oss 版本的
configuring-security
对于 7.0+ 的 Elasticsearch 自带 X-pack(非 OSS 版本),所以直接配置即可
# rpm 默认安装目录
$ cd /usr/share/elasticsearch
# 为集群创建 CA(certificate authority),生成文件 elastic-stack-ca.p12
$ bin/elasticsearch-certutil ca
# 颁发证书,生成文件 elastic-certificates.p12,密码可以不写直接 enter
# 然后将证书 elastic-stack-ca.p12 放到集群每个节点的配置文件夹下 /etc/elasticsearch
$ bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
# 配置 yml
$ vim /etc/elasticsearch/elasticsearch.yml
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
# 交互式设置用户名和密码
$ bin/elasticsearch-setup-passwords interactive
ES Hadoop (6.0)
- 开发文档:https://www.elastic.co/guide/en/elasticsearch/hadoop/current/spark.html
- API:https://github.com/elastic/elasticsearch-hadoop/
- configuration”https://www.elastic.co/guide/en/elasticsearch/hadoop/current/configuration.html
Using Elasticsearch for Apache Hadoop with Shield(Security in X-Pack)
https://www.elastic.co/guide/en/shield/current/hadoop.html
val conf = new SparkConf conf.set("es.index.auto.create", "true") // 写配置 conf.set("es.nodes", "10.4.121.79,10.4.121.80,10.4.121.81,10.4.121.91,10.4.121.92") conf.set("es.port", "9200") conf.set("es.net.http.auth.user","elastic") conf.set("es.net.http.auth.pass","123456")