Elasticsearch是一个高度可伸缩的开源全文搜索和分析引擎。它允许您快速、实时地存储、搜索和分析大量数据。它通常用作底层引擎/技术,为具有复杂搜索特性和需求的应用程序提供支持。
核心概念:NRT、Cluster、Node、Index、Type、Document、Shards & Replicas、
1. Near Realtime(NRT) 近实时
Elasticsearch是一个近实时的搜索平台,这意味着从索引文档到可搜索文档有轻微的延迟(通常是一秒)
2. Cluster 集群
一个集群是一个或多个节点(服务器)的集合,这些节点一起保存你的全部数据,并提供跨所有节点的联邦索引和搜索功能,一个集群由一个唯一的默认为“elasticsearch”的名称标识,这个名称很重要,因为只有在节点被设置为通过其名称加入集群时,节点才能成为集群的一部分。
确保在不同的环境你没有重复使用同样的集群的名称,否则你可能以让节点加入到错误的集群而告终,
3. Node 节点
一个节点是属于集群的单个服务器,存储数据,并参与集群的索引和搜索功能,就像集群一样,节点由一个名称标识,该名称在默认情况下是在启动时分配给节点的随机全局惟一标识符(UUID),如果你不想要这个默认节点名称,可以定义任意节点名称。
此名称对于管理目的非常重要,在管理目的中,您希望确定网络中的哪些服务器对应于Elasticsearch集群中的哪些节点。
可以将节点配置为按集群名称连接特定的集群,默认情况下,每个节点都被设置为加入一个名为elasticsearch的集群,这意味着如果您在网络上启动多个节点并假设它们可以发现彼此,它们将自动形成并加入一个名为elasticsearch的集群。
4. index 、type、document、shards&replicas
参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/_basic_concepts.html
1. 安装前的准备
Elasticsearch需要JDK的版本至少是Java 8. 推荐使用Oracle JDK 的版本是1.8.0_131,所有,首先查看你linux机器上安装的java版本,
[root@zzf ~]# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
[root@zzf ~]# echo $JAVA_HOME
/usr/lib/java/jdk1.8.0_161
2. 安装,通过zip/tar.gz包安装
通过下载elasticsearch的zip包进行安装,安装方式很多,rpm、docker等
Elasticsearch提供了.zip和.tar.gz包,这些包可以被用来在任何系统中安装,Elasticsearch的最新稳定版本下载地址
https://www.elastic.co/downloads/elasticsearch, 其他版本下载地址 https://www.elastic.co/downloads/past-releases
# 下载elasticsearch
[root@zzf software]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.14.zip
# 解压
[root@zzf software]# unzip elasticsearch-5.6.14.zip
# 将解压后的elasticsearch-5.6.14 移动到 /usr/local/elk目录
[root@zzf software]# mv elasticsearch-5.6.14 /usr/local/elk
通过命令行启动Elasticsearch
# cd 到/usr/local/elk/elasticsearch-5.6.14目录,并启动elasticsearch
[root@zzf elasticsearch-5.6.14]# pwd
/usr/local/elk/elasticsearch-5.6.14
[root@zzf elasticsearch-5.6.14]# ./bin/elasticsearch
此时为报错,因为当前用户为root,elasticsearch为了安全,不允许使用root用户启动elasticsearch。
所有需要新建用户,使用root用户新建es用户 及es用户组,并将/usr/local/elk权限赋予es用户,操作如下
# 创建用户组 es
[root@zzf elasticsearch-5.6.14]# groupadd es
# 创建用户es 并指定用户所属的群组为es
[root@zzf elasticsearch-5.6.14]# useradd -g es es
#给es用户赋权限
[root@zzf elasticsearch-5.6.14]# chown -R es:es /usr/local/elk/
# 设置es用户密码
[root@zzf elasticsearch-5.6.14]# passwd es
# 切换至es用户
[root@zzf elasticsearch-5.6.14]# su es
# 启动es
[root@zzf elasticsearch-5.6.14]# ./bin/elasticsearch
这样就启动成功,
[es@zzf elasticsearch-5.6.14]$ ./bin/elasticsearch
[2019-01-07T16:54:21,911][INFO ][o.e.n.Node ] [] initializing ...
[2019-01-07T16:54:22,097][INFO ][o.e.e.NodeEnvironment ] [sPw1Vhx] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [13.5gb], net total_space [16.9gb], spins? [unknown], types [rootfs]
[2019-01-07T16:54:22,097][INFO ][o.e.e.NodeEnvironment ] [sPw1Vhx] heap size [1.9gb], compressed ordinary object pointers [true]
[2019-01-07T16:54:22,101][INFO ][o.e.n.Node ] node name [sPw1Vhx] derived from node ID [sPw1VhxBTW6jo2sYFB4V1Q]; set [node.name] to override
[2019-01-07T16:54:22,102][INFO ][o.e.n.Node ] version[5.6.14], pid[1662], build[f310fe9/2018-12-05T21:20:16.416Z], OS[Linux/3.10.0-862.el7.x86_64/amd64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_161/25.161-b12]
[2019-01-07T16:54:22,102][INFO ][o.e.n.Node ] JVM arguments [-Xms2g, -Xmx2g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/usr/local/elk/elasticsearch-5.6.14]
[2019-01-07T16:54:24,128][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [aggs-matrix-stats]
[2019-01-07T16:54:24,128][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [ingest-common]
[2019-01-07T16:54:24,128][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [lang-expression]
[2019-01-07T16:54:24,128][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [lang-groovy]
[2019-01-07T16:54:24,128][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [lang-mustache]
[2019-01-07T16:54:24,128][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [lang-painless]
[2019-01-07T16:54:24,128][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [parent-join]
[2019-01-07T16:54:24,129][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [percolator]
[2019-01-07T16:54:24,129][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [reindex]
[2019-01-07T16:54:24,129][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [transport-netty3]
[2019-01-07T16:54:24,129][INFO ][o.e.p.PluginsService ] [sPw1Vhx] loaded module [transport-netty4]
[2019-01-07T16:54:24,129][INFO ][o.e.p.PluginsService ] [sPw1Vhx] no plugins loaded
[2019-01-07T16:54:28,679][INFO ][o.e.d.DiscoveryModule ] [sPw1Vhx] using discovery type [zen]
[2019-01-07T16:54:30,168][INFO ][o.e.n.Node ] initialized
[2019-01-07T16:54:30,168][INFO ][o.e.n.Node ] [sPw1Vhx] starting ...
[2019-01-07T16:54:30,744][INFO ][o.e.t.TransportService ] [sPw1Vhx] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2019-01-07T16:54:30,760][WARN ][o.e.b.BootstrapChecks ] [sPw1Vhx] max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2019-01-07T16:54:30,760][WARN ][o.e.b.BootstrapChecks ] [sPw1Vhx] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2019-01-07T16:54:33,968][INFO ][o.e.c.s.ClusterService ] [sPw1Vhx] new_master {sPw1Vhx}{sPw1VhxBTW6jo2sYFB4V1Q}{edktK-_4QFq147_64LQ5qA}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2019-01-07T16:54:34,028][INFO ][o.e.g.GatewayService ] [sPw1Vhx] recovered [0] indices into cluster_state
[2019-01-07T16:54:34,047][INFO ][o.e.h.n.Netty4HttpServerTransport] [sPw1Vhx] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2019-01-07T16:54:34,047][INFO ][o.e.n.Node ] [sPw1Vhx] started
默认,Elasticsearch在前端运行,会将日志打印到标准输出中,可以通过Ctrl-C 使其停止。
3. 检查Elasticsearch是否正在运行
你可以通过发送http请求测试刚才启动Elasticsearch节点 是否正常运行。
curl -X GET "localhost:9200/"
[root@zzf ~]# curl -X GET "localhost:9200"
{
"name" : "sPw1Vhx",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "6VtrjLY4SsGcu2Li2rwk4w",
"version" : {
"number" : "5.6.14",
"build_hash" : "f310fe9",
"build_date" : "2018-12-05T21:20:16.416Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
[root@zzf ~]# curl http://localhost:9200
{
"name" : "sPw1Vhx",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "6VtrjLY4SsGcu2Li2rwk4w",
"version" : {
"number" : "5.6.14",
"build_hash" : "f310fe9",
"build_date" : "2018-12-05T21:20:16.416Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
响应内容内容如上就说明elasticsearch正常运行。
4. 作为守护进程后端运行elasticsearch
作为守护进程运行elasticsearch,需要在命令行指定 -d 并且使用-p 选项 将进程ID 记录到文件中
[es@zzf elasticsearch-5.6.14]$ ./bin/elasticsearch -d -p pid
此时,日志就不会在打印出来了,日志信息在/usr/loca/elk/elasticsearch-5.6.14/logs目录下可以查看
[es@zzf elasticsearch-5.6.14]$ cd logs
[es@zzf logs]$ pwd
/usr/local/elk/elasticsearch-5.6.14/logs
[es@zzf logs]$ ls
elasticsearch_deprecation.log elasticsearch_index_indexing_slowlog.log elasticsearch_index_search_slowlog.log elasticsearch.log
[es@zzf logs]$ tail -f elasticsearch.log
在elasticsearch文件夹就会多出一个pid文件
# 通过cat 或more 命令查看pid文件内容, 就可以得到elasticsearch的进程号
[es@zzf elasticsearch-5.6.14]$ more pid
1916
要关闭Elasticsearch, kill 被记录到pid文件中的进程ID即可,命令如下:
[es@zzf elasticsearch-5.6.14]$ kill `cat pid`
# 此时查询 elasticsearch的进程ID 已经不存在了
[es@zzf elasticsearch-5.6.14]$ ps -ef|grep elasticsearch
es 1999 1647 0 17:25 pts/0 00:00:00 grep --color=auto elasticsearch
5. 通过命令行配置Elasticsearch
默认Elasticsearch从/usr/local/elk/elasticsearch-5.6.14/config/elasticsearch.yml文件中加载配置信息, 同时,任何在配置文件能指定的参数同样能通过命令行方式指定,通过使用 -E ,如下
[es@zzf elasticsearch-5.6.14]$ ./bin/elasticsearch -d -Ecluster.name=my_cluster -Enode.name=node_1
官网上这么说:
Typically, any cluster-wide settings (like cluster.name
) should be added to the elasticsearch.yml
config file, while any node-specific settings such as node.name
could be specified on the command line(通常,所有集群范围的设置(如cluster.name)都应该添加到elasticsearch.yml中。而任何特定于节点的设置,如node.name,都可以在命令行中指定)