大資料學習初級入門教程(八) —— Elasticsearch 7.6.2 單節點的安裝、啟動和測試

孟郎郎發表於2020-04-04

本篇文章主要介紹 Elasticsearch 7.6.2 版本的單節點安裝、啟動及測試步驟,供初學者參考。測試系統為 CentOS 6.9,JDK 版本為 1.8.0_172。

第一步:下載安裝包並上傳到測試伺服器

下載地址為官網地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-linux-x86_64.tar.gz

下載後上傳到測試伺服器,這裡上傳到路徑:/opt/es下。

第二步:解壓安裝包

# tar -zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz

第三步:啟動 ES 服務

# cd elasticsearch-7.6.2

# ./bin/elasticsearch

報錯資訊如下:

[2018-07-31T04:25:46,553][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [test242] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.6.2.jar:7.6.2]
        at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.6.2.jar:7.6.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.6.2.jar:7.6.2]
        ... 6 more

可以看到錯誤原因為:不能用 root 執行 Elasticsearch!!!

那為什麼不能用 root 賬號執行呢?這是出於系統安全考慮設定的條件。由於 es 可以接收使用者輸入的指令碼並且執行,為了系統安全考慮,es5 之後的都不能使用新增啟動引數或者修改配置檔案等方法啟動了,不允許使用 root 使用者啟動 es,建議建立一個單獨的使用者用來執行。

第四步:建立單獨系統使用者

# adduser elastic

# passwd elastic

第五步:修改目錄許可權

對於第二步解壓縮的包,目錄許可權目前是 root,需要修改為 elastic。

# cd /opt/es/

# chown -R elastic elasticsearch-7.6.2

# chgrp -R elastic elasticsearch-7.6.2

第六步:切換使用者並啟動服務

切換新建的使用者 elastic,並在此啟動 es 服務。

# su elastic

$ cd elasticsearch-7.6.2

$ ./bin/elasticsearch

如果出現如下錯誤:

[2018-07-31T05:14:58,860][WARN ][o.e.b.JNANatives         ] [test242] 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:342) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.SystemCallFilter.init(SystemCallFilter.java:617) ~[elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.JNANatives.tryInstallSystemCallFilter(JNANatives.java:260) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Natives.tryInstallSystemCallFilter(Natives.java:113) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:110) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) [elasticsearch-cli-7.6.2.jar:7.6.2]
        at org.elasticsearch.cli.Command.main(Command.java:90) [elasticsearch-cli-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) [elasticsearch-7.6.2.jar:7.6.2]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) [elasticsearch-7.6.2.jar:7.6.2]

原因是:Centos6 不支援 SecComp,而 ES 預設 bootstrap.system_call_filter 為 true 進行檢測,導致檢測失敗,失敗後直接導致ES 不能啟動。E3.5 版本以上,直接禁用這個外掛即可。

解決方式:修改 elasticsearch.yml。

$ vi config/elasticsearch.yml

新增如下配置:

# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
bootstrap.system_call_filter: false
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#

再次啟動服務,可以看到又出現一些錯誤導致啟動不了,詳細如下:

[4] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [1024] for user [elastic] 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]: 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

【1】錯誤原因是:每個程式最大同時開啟檔案數太小。可通過下面以下命令檢視當前數量:

[root@test242 logs]# ulimit -Hn
4096
[root@test242 logs]# ulimit -Sn
1024
[root@test242 logs]# 

解決方法:修改 /etc/security/limits.conf 檔案,增加配置:

*               soft    nofile          65535
*               hard    nofile          65535

使用者退出後重新登入生效。 

[root@test242 ~]# ulimit -Hn
65535
[root@test242 ~]# ulimit -Sn
65535
[root@test242 ~]# 

【2】錯誤原因是:最大執行緒個數太低。可通過下面以下命令檢視當前數量:

[root@test242 ~]# ulimit -Hu
3838
[root@test242 ~]# ulimit -Su
3838
[root@test242 ~]# 

解決方法:修改 /etc/security/limits.conf 檔案,增加配置:

*               soft    nproc           4096
*               hard    nproc           4096

修改 vi /etc/security/limits.d/90-nproc.conf 檔案:

*          soft    nproc     4096
root       soft    nproc     unlimited

使用者退出後重新登入生效。 

[root@test242 ~]# ulimit -Hu
4096
[root@test242 ~]# ulimit -Su
4096
[root@test242 ~]# 

【3】錯誤原因是:JVM 執行緒數限制數量太低,需要調高。

解決方法:修改 /etc/sysctl.conf 檔案,增加配置 vm.max_map_count=262144

執行命令 sysctl -p 使之配置生效。

[root@test242 ~]# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
vm.max_map_count = 262144

【4】錯誤原因是:沒有指定初始化節點。

解決方法:修改 es 的配置檔案 elasticsearch-7.6.2/config/elasticsearch.yml,把 #cluster.initial_master_nodes: ["node-1", "node-2"] 去掉註釋後,只保留 node-1。修改後配置如下:

# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["node-1"]
#
# For more information, consult the discovery and cluster formation module documentation.
#

登出系統使用者,重新登入後再次啟動 ES:

可以看到服務啟動成功了,通過瀏覽器訪問 http://192.168.220.242:9200/ 進行測試,結果頁面提示 ERR_CONNECTION_REFUSED:

原因是沒有指定 es 網路配置,需要修改 Elasticsearch 的配置檔案 elasticsearch.yml,詳細如下:

$ vi config/elasticsearch.yml

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

再次重啟服務,通過瀏覽器訪問 http://192.168.220.242:9200/ 進行測試,如果出現如下頁面結果,說明服務部署成功並啟動成功了。

目前啟動時前臺啟動方式,如果要後臺啟動服務,啟動時新增 -d 引數即可。

後臺啟動:$ ./bin/elasticsearch -d

檢視程式:$ ps -ef |grep java

程式資訊如下:

[elastic@test242 elasticsearch-7.6.2]$ ps -ef |grep java   
elastic    3944      1 25 11:04 pts/0    00:00:40 /opt/es/elasticsearch-7.6.2/jdk/bin/java -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 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.locale.providers=COMPAT -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Djava.io.tmpdir=/tmp/elasticsearch-17990411953466674273 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m -XX:MaxDirectMemorySize=536870912 -Des.path.home=/opt/es/elasticsearch-7.6.2 -Des.path.conf=/opt/es/elasticsearch-7.6.2/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp /opt/es/elasticsearch-7.6.2/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
elastic    4023   3170  0 11:07 pts/0    00:00:00 grep java
[elastic@test242 elasticsearch-7.6.2]$ 

相關文章