CentOS 7上安装和配置Elasticsearch

这个文章主要介绍一下如果在CentOS7上安装单节点Elasticsearch.

前置条件

首先来看一下JDK. Elasticsearch安装后自带OpenJDK.如果要使用自定义的JDK, 要确保版本号在1.8.0_131 or a later.
先来检查一下我们是否已经安装jdk,版本是否满足要求:

$ java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)
$ echo $JAVA_HOME
/usr/lib/jvm/java_latest

如果想了解怎么安装open-jdk,这篇文章可以参考。

Install Elasticsearch

We can get download url from the following link:
https://www.elastic.co/downloads/
现在开始下载安装包, 包挺大。在我的网络上下载挺慢。

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-linux-x86_64.tar.gz
$ tar -xvf elasticsearch-7.0.0-linux-x86_64.tar.gz
$ cd elasticsearch-7.0.0
$ cd jdk/bin/
$ ./java -version
openjdk version "12" 2019-03-19
OpenJDK Runtime Environment (build 12+33)
OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)

我们的确看到有jdk在Elasticsearch目录的jdk/下面. 这个我们先不管. 我们先进到elasticsearch-7.0.0/bin目录.

$ pwd
/home/ops/elasticsearch-7.0.0/bin

现在执行下面的命令:

$ ./elasticsearch
[2019-04-14T01:28:47,160][INFO ][o.e.e.NodeEnvironment    ] [elasticsearch] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [14.7gb], net total_space [16.9gb], types [rootfs]
[2019-04-14T01:28:47,167][INFO ][o.e.e.NodeEnvironment    ] [elasticsearch] heap size [1007.3mb], compressed ordinary object pointers [true]
[2019-04-14T01:28:47,171][INFO ][o.e.n.Node               ] [elasticsearch] node name [elasticsearch], node ID [z4Qpr3KpSK-Hz48idq2ICQ]
[2019-04-14T01:28:47,172][INFO ][o.e.n.Node               ] [elasticsearch] version[7.0.0], pid[5328], build[default/tar/b7e28a7/2019-04-05T22:55:32.697037Z], OS[Linux/3.10.0-957.10.1.el7.x86_64/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/1.8.0_201/25.201-b09]
[2019-04-14T01:28:47,173][INFO ][o.e.n.Node               ] [elasticsearch] JVM home [/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre]
[2019-04-14T01:28:47,174][INFO ][o.e.n.Node               ] [elasticsearch] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -Des.networkaddress.cache.ttl=60, -Des.networkaddress.cache.negative.ttl=10, -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, -Djava.io.tmpdir=/tmp/elasticsearch-5278761429780090214, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, -XX:+PrintGCDetails, -XX:+PrintGCDateStamps, -XX:+PrintTenuringDistribution, -XX:+PrintGCApplicationStoppedTime, -Xloggc:logs/gc.log, -XX:+UseGCLogFileRotation, -XX:NumberOfGCLogFiles=32, -XX:GCLogFileSize=64m, -Dio.netty.allocator.type=unpooled, -Des.path.home=/home/ops/elasticsearch-7.0.0, -Des.path.conf=/home/ops/elasticsearch-7.0.0/config, -Des.distribution.flavor=default, -Des.distribution.type=tar, -Des.bundled_jdk=true]
[2019-04-14T01:28:50,753][INFO ][o.e.p.PluginsService     ] [elasticsearch] loaded module [aggs-matrix-stats]
...
[2019-04-14T01:28:50,764][INFO ][o.e.p.PluginsService     ] [elasticsearch] loaded module [x-pack-watcher]
[2019-04-14T01:28:50,766][INFO ][o.e.p.PluginsService     ] [elasticsearch] no plugins loaded
[2019-04-14T01:28:59,514][INFO ][o.e.x.s.a.s.FileRolesStore] [elasticsearch] parsed [0] roles from file [/home/ops/elasticsearch-7.0.0/config/roles.yml]
[2019-04-14T01:29:01,108][INFO ][o.e.x.m.p.l.CppLogMessageHandler] [elasticsearch] [controller/5390] [Main.cc@109] controller (64 bit): Version 7.0.0 (Build cdaa022645f38d) Copyright (c) 2019 Elasticsearch BV
[2019-04-14T01:29:02,039][DEBUG][o.e.a.ActionModule       ] [elasticsearch] Using REST wrapper from plugin org.elasticsearch.xpack.security.Security
[2019-04-14T01:29:02,621][INFO ][o.e.d.DiscoveryModule    ] [elasticsearch] using discovery type [zen] and seed hosts providers [settings]
[2019-04-14T01:29:04,531][INFO ][o.e.n.Node               ] [elasticsearch] initialized
[2019-04-14T01:29:04,532][INFO ][o.e.n.Node               ] [elasticsearch] starting ...
[2019-04-14T01:29:04,863][INFO ][o.e.t.TransportService   ] [elasticsearch] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2019-04-14T01:29:04,896][WARN ][o.e.b.BootstrapChecks    ] [elasticsearch] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2019-04-14T01:29:04,902][WARN ][o.e.b.BootstrapChecks    ] [elasticsearch] the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
[2019-04-14T01:29:04,941][INFO ][o.e.c.c.ClusterBootstrapService] [elasticsearch] no discovery configuration found, will perform best-effort cluster bootstrapping after [3s] unless existing master is discovered
[2019-04-14T01:29:07,976][INFO ][o.e.c.c.Coordinator      ] [elasticsearch] setting initial configuration to VotingConfiguration{z4Qpr3KpSK-Hz48idq2ICQ}
[2019-04-14T01:29:08,208][INFO ][o.e.c.s.MasterService    ] [elasticsearch] elected-as-master ([1] nodes joined)[{elasticsearch}{z4Qpr3KpSK-Hz48idq2ICQ}{GlVuRuLeRBWbGCapcl7Oeg}{127.0.0.1}{127.0.0.1:9300}{ml.machine_memory=2984280064, xpack.installed=true, ml.max_open_jobs=20} elect leader, _BECOME_MASTER_TASK_, _FINISH_ELECTION_], term: 1, version: 1, reason: master node changed {previous [], current [{elasticsearch}{z4Qpr3KpSK-Hz48idq2ICQ}{GlVuRuLeRBWbGCapcl7Oeg}{127.0.0.1}{127.0.0.1:9300}{ml.machine_memory=2984280064, xpack.installed=true, ml.max_open_jobs=20}]}
[2019-04-14T01:29:08,302][INFO ][o.e.c.s.ClusterApplierService] [elasticsearch] master node changed {previous [], current [{elasticsearch}{z4Qpr3KpSK-Hz48idq2ICQ}{GlVuRuLeRBWbGCapcl7Oeg}{127.0.0.1}{127.0.0.1:9300}{ml.machine_memory=2984280064, xpack.installed=true, ml.max_open_jobs=20}]}, term: 1, version: 1, reason: Publication{term=1, version=1}
[2019-04-14T01:29:08,450][WARN ][o.e.x.s.a.s.m.NativeRoleMappingStore] [elasticsearch] Failed to clear cache for realms [[]]
[2019-04-14T01:29:08,464][INFO ][o.e.h.AbstractHttpServerTransport] [elasticsearch] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2019-04-14T01:29:08,465][INFO ][o.e.n.Node               ] [elasticsearch] started
[2019-04-14T01:29:08,646][INFO ][o.e.g.GatewayService     ] [elasticsearch] recovered [0] indices into cluster_state
[2019-04-14T01:29:09,134][INFO ][o.e.c.m.MetaDataIndexTemplateService] [elasticsearch] adding template [.triggered_watches] for index patterns [.triggered_watches*]
[2019-04-14T01:29:09,237][INFO ][o.e.c.m.MetaDataIndexTemplateService] [elasticsearch] adding template [.watches] for index patterns [.watches*]
[2019-04-14T01:29:09,325][INFO ][o.e.c.m.MetaDataIndexTemplateService] [elasticsearch] adding template [.watch-history-9] for index patterns [.watcher-history-9*]
[2019-04-14T01:29:09,406][INFO ][o.e.c.m.MetaDataIndexTemplateService] [elasticsearch] adding template [.monitoring-logstash] for index patterns [.monitoring-logstash-7-*]
[2019-04-14T01:29:09,497][INFO ][o.e.c.m.MetaDataIndexTemplateService] [elasticsearch] adding template [.monitoring-es] for index patterns [.monitoring-es-7-*]
[2019-04-14T01:29:09,575][INFO ][o.e.c.m.MetaDataIndexTemplateService] [elasticsearch] adding template [.monitoring-beats] for index patterns [.monitoring-beats-7-*]
[2019-04-14T01:29:09,636][INFO ][o.e.c.m.MetaDataIndexTemplateService] [elasticsearch] adding template [.monitoring-alerts-7] for index patterns [.monitoring-alerts-7]
[2019-04-14T01:29:09,687][INFO ][o.e.c.m.MetaDataIndexTemplateService] [elasticsearch] adding template [.monitoring-kibana] for index patterns [.monitoring-kibana-7-*]
[2019-04-14T01:29:09,734][INFO ][o.e.x.i.a.TransportPutLifecycleAction] [elasticsearch] adding index lifecycle policy [watch-history-ilm-policy]
[2019-04-14T01:29:10,033][INFO ][o.e.l.LicenseService     ] [elasticsearch] license [0049f566-7a5d-4291-a90d-354005515702] mode [basic] - valid

来看一下JVM的参数配置

[2019-04-14T01:28:47,174][INFO ][o.e.n.Node               ] [elasticsearch] JVM arguments [
-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, 
-XX:+UseCMSInitiatingOccupancyOnly, 
-Des.networkaddress.cache.ttl=60, -Des.networkaddress.cache.negative.ttl=10, -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, -Djava.io.tmpdir=/tmp/elasticsearch-5278761429780090214, 
-XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data,
 -XX:ErrorFile=logs/hs_err_pid%p.log, -XX:+PrintGCDetails, -XX:+PrintGCDateStamps, 
 -XX:+PrintTenuringDistribution, -XX:+PrintGCApplicationStoppedTime, -Xloggc:logs/gc.log, 
 -XX:+UseGCLogFileRotation, -XX:NumberOfGCLogFiles=32, -XX:GCLogFileSize=64m, 
 -Dio.netty.allocator.type=unpooled, -Des.path.home=/home/ops/elasticsearch-7.0.0, 
 -Des.path.conf=/home/ops/elasticsearch-7.0.0/config, -Des.distribution.flavor=default, 
 -Des.distribution.type=tar, -Des.bundled_jdk=true]

内存配置1G

-Xms1g, -Xmx1g,

再看这一行:

[2019-04-14T01:29:08,464][INFO ][o.e.h.AbstractHttpServerTransport] [elasticsearch] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2019-04-14T01:29:08,465][INFO ][o.e.n.Node               ] [elasticsearch] started

说明我们已经成功地安装了一个单节点集群: node name [elasticsearch], node ID [z4Qpr3KpSK-Hz48idq2ICQ] (在第三行). 下面来访问一下(需要另起一个shell):

ops@elasticsearch:~/elasticsearch-7.0.0
$ curl localhost:9200
{
  "name" : "elasticsearch",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "V-VBklMlTROOm51le51QUQ",
  "version" : {
    "number" : "7.0.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "b7e28a7",
    "build_date" : "2019-04-05T22:55:32.697037Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.7.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

ES server IP address 是192.168.3.182.默认的端口号9200. 为了测试, 直接将ES server防火关闭:

$ sudo systemctl stop firewalld

咱换一台机器访问一下.

$ curl 192.168.3.182:9200
curl: (7) Failed to connect to 192.168.3.182 port 9200: No route to host

不能访问.

修改配置文件

下一步呢就是修改一些配置项啦.
ES的配置文件主要有三个:

  • elasticsearch.yml
  • jvm.options
  • log4j2.properties

在$ES_HOME/config

ops@elasticsearch:~/elasticsearch-7.0.0
$ tree .
.
├── bin
...
├── config
│   ├── elasticsearch.keystore
│   ├── elasticsearch.yml
│   ├── index.html
│   ├── jvm.options
│   ├── log4j2.properties
│   ├── role_mapping.yml
│   ├── roles.yml
│   ├── users
│   └── users_roles

一看文件名就应该知道它们大概是什么意思啦. 先打开elasticsearch.yml, 找到network这一节:

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.

取消network.host的注释,将其值改成0.0.0.0

network.host: 0.0.0.0

因在ES 7.0中,在安全性方面做了些修改,所以我们还需要修改下面的配置项,在Discovery这一节

cluster.initial_master_nodes: ["elasticsearch"]

'elasticsearch’是ES server hostname.
在刚才启动的shell那按Ctrl+c, 然后./elasticsearch重启

[2019-04-14T13:23:15,679][INFO ][o.e.n.Node               ] [elasticsearch] stopping ...
[2019-04-14T13:23:15,703][INFO ][o.e.x.w.WatcherService   ] [elasticsearch] stopping watch service, reason [shutdown initiated]
[2019-04-14T13:23:16,063][INFO ][o.e.n.Node               ] [elasticsearch] stopped
[2019-04-14T13:23:16,063][INFO ][o.e.n.Node               ] [elasticsearch] closing ...
[2019-04-14T13:23:16,109][INFO ][o.e.n.Node               ] [elasticsearch] closed
[ops@elasticsearch bin]$ ./elasticsearch
...
ERROR: [2] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
[2019-04-14T13:27:05,436][INFO ][o.e.n.Node               ] [elasticsearch] stopping ...
[2019-04-14T13:27:05,480][INFO ][o.e.n.Node               ] [elasticsearch] stopped
[2019-04-14T13:27:05,481][INFO ][o.e.n.Node               ] [elasticsearch] closing ...
[2019-04-14T13:27:05,526][INFO ][o.e.n.Node               ] [elasticsearch] closed

重启的时候报错啦, 根据提示是 vm.max_map_count [65530] is too low,调整一个这个值:

[ops@elasticsearch bin]$ sudo sysctl -w vm.max_map_count=524288
vm.max_map_count = 524288
[ops@elasticsearch bin]$ ./elasticsearch

重启后成功. 现在我们从另一台机器来访问

$ curl 192.168.3.182:9200
{
  "name" : "elasticsearch",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "V-VBklMlTROOm51le51QUQ",
  "version" : {
    "number" : "7.0.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "b7e28a7",
    "build_date" : "2019-04-05T22:55:32.697037Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.7.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

检查集群状态:

$ curl -X GET '192.168.3.182:9200/_cat/indices?v'
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   weather S8dR4TrVTSidb4U-KzongA   1   1          0            0       283b           283b

查看集群节点

$ curl -X GET 192.168.3.182:9200/_cat/nodes?v
ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.3.182            9          94   0    0.00    0.01     0.05 mdi       *      elasticsearch

创建索引

$ curl -X PUT 192.168.3.182:9200/customer?pretty
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}

List indices

$ curl -X GET 192.168.3.182:9200/_cat/indices?v
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer qOLFhGwoT6eAB4hYbAgcJg   1   1          0            0       283b           283b

Delete indices

$ curl -X DELETE 192.168.3.182:9200/customer
{"acknowledged":true}

你可能感兴趣的:(Elasticsearch)