Elasticsearch是一个高度可扩展的开源全文搜索和分析引擎。它允许您快速,实时地存储,搜索和分析大量数据。它通常用作为具有复杂的搜索功能和要求的应用程序提供的底层引擎技术。
Elasticsearch 安装方式如同Tomcat,开箱即用,无需安装复杂的依赖包。
先决条件:
Elasticsearch至少需要Java 8版本。以下是安装文档,所以我们在这里不再赘述。
集群部署环境及设备配置:
/台/16G/8核/500G 10.28.204.62 10.28.204.63 10.28.204.64 10.28.204.65 Elasticsearch 5.6.3 CentOS Linux release 7.4.1708 (Core) Kernel: Linux 3.10.0-693.2.2.el7.x86_64
说明:以下安装步骤,我在 10.28.204.65 服务器操作,其他机器均相同,我会标注集群重点部分。
1.创建用户及组并设置密码
$ groupadd es $ useradd -g es es $ passwd es
2.安装 Elasticsearch
下载地址 //www.elastic.co/downloads/elasticsearch#ga-release
解压:
$ cd /tmp $ tar zxvf elasticsearch-5.6.3.tar.gz移动目录并创建软连接:
$ mv elasticsearch-5.6.3 /usr/local $ cd /usr/local $ ln -s elasticsearch-5.6.3 elasticsearch设置目录用户权限:
$ chown -R es.es elasticsearch*
3.配置 jvm.options
Elasticsearch默认堆内存为2 GB,无法满足需求,需要将以下文件中的2个Xms、Xmx修改为8G即可,其他默认。
$ vim /usr/local/elasticsearch/config/jvm.options ... -Xms8g -Xmx8g ...注意:建议分配机器物理内存一半大小,最大不要超过32GB。
4.配置 elasticsearch.yml
配置后的内容如下:
$ egrep -v "(^#|^$)" /usr/local/elasticsearch/config/elasticsearch.ymlcluster.name: my-apprenwole #集群名称,任意。ES启动后会将具有相同集群名字的节点放到一个集群下。 node.name: renwolenode-1 #节点名称 任意 唯一值。 bootstrap.memory_lock: false #关闭锁定内存。 network.host: 10.28.204.65 #本机IP地址,每个节点都要修改。 http.port: 9200 #http访问端口为了安全建议修改。 discovery.zen.ping.unicast.hosts: ["10.28.204.62","10.28.204.63", "10.28.204.64","10.28.204.65"] #当新节点启动时,传递主机的初始列表以执行发现。如果端口不是默认,需加上端口。 discovery.zen.minimum_master_nodes: 3 #指定集群节点中有几个有master资格的节点。如果是多个集群可以写3个以上。 client.transport.ping_timeout: 120s #等待来自节点的ping响应的时间。默认为60s。 discovery.zen.ping_timeout: 120s #允许选举时间处理速度慢或拥塞网络的情况下的调整(较高的值保证故障少的几率)。 http.cors.enabled: true #启用或禁用跨原始资源共享,即;另一来源上的浏览器是否可以针对Elasticsearch执行请求。 http.cors.allow-origin: "*" #默认为不允许来源。如果您预先添加并附/加到该值,则将其视为正则表达式,允许您支持HTTP和HTTP。例如使用/https?:\/\/localhost(:[0-9]+)?/将在两种情况下适当返回请求头。*是一个有效的值,但被认为是一个安全风险,因为您的弹性搜索实例可以从任何地方交叉发起请求。说明:Elasticsearch默认值配置具有良好设置,并且需要很少的配置,默认经过少许配置即可用于生产。
更多配置信息,请参阅 Elasticsearch modules
注:其他三台机器除以下参数不同,其他均为相同:
node.name network.host
5.Memlock设置
在该文件中添加以下内容:
$ vim /etc/security/limits.conf es soft memlock unlimited es hard memlock unlimited es - nofile 65536如果不添加,启动的时候会报警告信息:
Unable to lock JVM Memory: error=12, reason=Cannot allocate memory This can result in part of the JVM being swapped out. Increase RLIMIT_MEMLOCK, soft limit: 65536, hard limit: 65536 These can be adjusted by modifying /etc/security/limits.conf, for example: # allow user 'es' mlockall es soft memlock unlimited es hard memlock unlimited以上错误信息还给出了解决方案。
6.服务器内存设置
$ vim /etc/sysctl.conf vm.max_map_count=262144 $ sysctl -p
7.启动 Elasticsearch
由于ES默认不允许root直接启动,这是为了安全起见,下面切换为 es 账号启动:
[root@102820465 ~]# su es [es@102820465 ~]$ cd /usr/local/elasticsearch/bin [es@102820465 bin]$ ./elasticsearch[INFO ][o.e.n.Node ] [renwolenode-1] initializing ... [INFO ][o.e.e.NodeEnvironment ] [renwolenode-1] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [4021.3mb], net total_space [15.9gb], spins? [unknown], types [rootfs] [INFO ][o.e.e.NodeEnvironment ] [renwolenode-1] heap size [7.9gb], compressed ordinary object pointers [true] [INFO ][o.e.n.Node ] [renwolenode-1] node name [renwolenode-1], node ID [vkixu3LZTPq82SAWWXyNcg] [INFO ][o.e.n.Node ] [renwolenode-1] version[5.6.3], pid[21425], build[667b497/2017-10-18T19:22:05.189Z], OS[Linux/3.10.0-514.el7.x86_64/amd64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_144/25.144-b01] [INFO ][o.e.n.Node ] [renwolenode-1] JVM arguments [-Xms8g, -Xmx8g, -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/elasticsearch] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [aggs-matrix-stats] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [ingest-common] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [lang-expression] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [lang-groovy] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [lang-mustache] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [lang-painless] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [parent-join] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [percolator] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [reindex] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [transport-netty3] [INFO ][o.e.p.PluginsService ] [renwolenode-1] loaded module [transport-netty4] [INFO ][o.e.p.PluginsService ] [renwolenode-1] no plugins loaded [INFO ][o.e.d.DiscoveryModule ] [renwolenode-1] using discovery type [zen] [INFO ][o.e.n.Node ] [renwolenode-1] initialized [INFO ][o.e.n.Node ] [renwolenode-1] starting ... [INFO ][o.e.t.TransportService ] [renwolenode-1] publish_address {10.28.204.65:9300}, bound_addresses {10.28.204.65:9300} [INFO ][o.e.b.BootstrapChecks ] [renwolenode-1] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks [WARN ][o.e.n.Node ] [renwolenode-1] timed out while waiting for initial discovery state - timeout: 30s [INFO ][o.e.h.n.Netty4HttpServerTransport] [renwolenode-1] publish_address {10.28.204.65:9200}, bound_addresses {10.28.204.65:9200} [INFO ][o.e.n.Node ] [renwolenode-1] started
节点成功启动,状态:started 启动后当前终端会一直显示 ElasticSearch 状态信息。
假如启动失败,会显示详细错误说明,根据报错解决即可。
如果退出按 Ctrl + c 同时 ElasticSearch 也会停止。
8.重开一个终端访问ES
$ curl //10.28.204.65:9200/ { "name" : "renwolenode-1", "cluster_name" : "my-apprenwole", "cluster_uuid" : "Xf_ZdW0XQum4rycQA40PfQ", "version" : { "number" : "5.6.3", "build_hash" : "667b497", "build_date" : "2017-10-18T19:22:05.189Z", "build_snapshot" : false, "lucene_version" : "6.6.1" }, "tagline" : "You Know, for Search" }返回ES的部分信息,说明ES可以正常使用。
9.创建 systemd 单元服务文件
事实上我们在生产环境中,管理ES的时候,不可能来回切换账号用 ./elasticsearch 方式启动。若Elasticsearch服务器宕机恢复时无法随机启动,就会给运维人员带来不必要的麻烦。
因此创建开机自启动文件:
$ vim /usr/lib/systemd/system/elasticsearch.service添加以下内容:
[Service] Environment=ES_HOME=/usr/local/elasticsearch Environment=CONF_DIR=/usr/local/elasticsearch/config Environment=DATA_DIR=/usr/local/elasticsearch/data Environment=LOG_DIR=/usr/local/elasticsearch/logs Environment=PID_DIR=/usr/local/elasticsearch EnvironmentFile=-/usr/local/elasticsearch/config WorkingDirectory=/usr/local/elasticsearch User=es Group=es ExecStartPre=/usr/local/elasticsearch/bin/elasticsearch-systemd-pre-exec ExecStart=/usr/local/elasticsearch/bin/elasticsearch \ -p ${PID_DIR}/elasticsearch.pid \ --quiet \ -Edefault.path.logs=${LOG_DIR} \ -Edefault.path.data=${DATA_DIR} \ -Edefault.path.conf=${CONF_DIR} # StandardOutput is configured to redirect to journalctl since # some error messages may be logged in standard output before # elasticsearch logging system is initialized. Elasticsearch # stores its logs in /var/log/elasticsearch and does not use # journalctl by default. If you also want to enable journalctl # logging, you can simply remove the "quiet" option from ExecStart. StandardOutput=journal StandardError=inherit # Specifies the maximum file descriptor number that can be opened by this process LimitNOFILE=65536 # Specifies the maximum number of processes LimitNPROC=2048 # Specifies the maximum size of virtual memory LimitAS=infinity # Specifies the maximum file size LimitFSIZE=infinity # Disable timeout logic and wait until process is stopped TimeoutStopSec=0 # SIGTERM signal is used to stop the Java process KillSignal=SIGTERM # Send the signal only to the JVM rather than its control group KillMode=process # Java process is never killed SendSIGKILL=no # When a JVM receives a SIGTERM signal it exits with code 143 SuccessExitStatus=143 [Install] WantedBy=multi-user.target # Built for distribution-5.6.3 (distribution)
10.重启 elasticsearch
$ systemctl restart elasticsearch注:重启ES后不会立即运行,它有一个启动过程,约1min左右,即可通过 ss -ntlp 查看 9200、9300是否运行,运行后再查看集群状态。
11.设置 Firewalld 防火墙
$ firewall-cmd --permanent --add-port={9200/tcp,9300/tcp} $ firewall-cmd --reload $ firewall-cmd --list-all
12.查看集群状态
在集群中输入以下URL即可获取集群健康状态信息:
$ curl //10.28.204.65:9200/_cluster/health?pretty { "cluster_name" : "my-apprenwole", //集群名称 "status" : "green", //集群的状态分为:红/绿/灯,绿:健康,黄:亚健康,红:病态 "timed_out" : false, "number_of_nodes" : 4, //节点数 "number_of_data_nodes" : 4, //数据节点 "active_primary_shards" : 6, //主分片总数 "active_shards" : 22, //集群内所有索引的分片总数 "relocating_shards" : 0, //正在迁移中的分片数 "initializing_shards" : 0, //正在初始化的分片数 "unassigned_shards" : 0, //未分配到具体节点上的分片数 "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 //活动碎片百分比 }我们采用4台ES实例,所以集群也显示了具体数据,说明集群运行正常。
ES集群已经安装完成。此文为原创内容,可直接用于生产。ES有很多插件,后期我会写一些相关文档,例如:Kibana、Logstash、X-Pack 这些插件都是官方插件,相当实用。