1. 安裝:
要求至少Java 8(推薦使用Oracle JDK version 1.8.0_121)并設置$JAVA_HOME。
# 下載tar包或rpm包。
tarName="elasticsearch-5.3.2.tar.gz"
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/${tarName}
tar -xf ${tarName}
dirName=`tar -tf "${tarName}" | head -1`
dirName=${dirName%%\/*}
cd ${dirName}
#創建elastic賬號運行如果用root賬號運行則會報錯(提示"can not run elasticsearch as root" !)
useradd elastic
chown elastic:elastic `pwd`
su elastic
# 啟動 elasticsearch
./bin/elasticsearch
# 以daemon方式運行
#-p指定pid文件
./bin/elasticsearch -d -p pid
#或
nohup ./bin/elasticsearch &
# 測試是否運行
elServer="內網ip:9200"
curl -XGET "${elServer}/?pretty"
# 停止進程
kill -9 `cat pid`
2. 配置命令行參數
elasticsearch 默認從 $ES_HOME/config/elasticsearch.yml 文件中加載其配置。
所有可以在配置文件中指定的設置也可以在命令行上指定,使用 -E 語法,如下所示
./bin/elasticsearch -d -Ecluster.name=my_cluster -Enode.name=node_1
一般來說,所有cluster-wide的設置應該寫在配置文件中,所有node-wide的設置應該寫在命令行上。
3. 目錄結構說明
3.1 tar包安裝方式的目錄結構
- home目錄 :使用$ES_HOME表示
- bin/ : 位置 $ES_HOME/bin,包含了elasticsearch和elasticsearch-plugin等腳本
- conf/ :位置 $ES_HOME/config,包含了 配置文件 elasticsearch.yml 和 log4j2.properties,使用 path.conf 指定
- data/ :位置 $ES_HOME/data,包含了每個index/shard的數據文件,可以指定多個位置,使用 path.data 指定
- logs/ : 位置 $ES_HOME/logs,使用 path.logs 指定
- plguins/ : 位置$ES_HOME/plugins
- repo/ :使用 path.repo指定,沒有默認位置,表示共享文件系統repository的位置。可以指定多個位置。
- script/ :位置$ES_HOME/scripts,使用 path.scripts 指定。
3.2 RPM 安裝方式的目錄結構
https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.html
RPM方式安裝的elasticsearch 默認會從 /etc/elasticsearch/elasticsearch.yml 加載其配置
RPM方式安裝的elasticsearch 還會使用 /etc/sysconfig/elasticsearch ,允許你設置如下的參數:
- ES_USER :默認 elasticsearch ,表示指定使用哪個用戶運行elasticsearch。
- ES_GROUP:默認是 elasticsearch。
- JAVA_HOME:設置自定義的要使用的JAVA path
- MAX_OPEN_FILES:打開文件的最大數量。默認是65536
- MAX_LOCKED_MEMORY:最大locked memory size。如果你在elasticsearch.yml中使用了
- bootstrap.memory_lock,則設置為unlimited 。
- MAX_MAP_COUNT:內存map區域的最大數量。如果你使用mmapfs作為index存儲類型,要確保這個值設置的夠大。這個值是在啟動elasticsearch之前,通過sysctl設置。默認值是 262144。
- LOG_DIR :
- DATA_DIR:
- CONF_DIR:
- ES_JAVA_OPTS:
- RESTART_ON_UPGRADE:
rpm包安裝方式的目錄結構
- home目錄 默認是/usr/share/elasticsearch
- bin目錄 默認是 $ES_HOME/bin
- conf目錄 默認是 /etc/elasticsearch
- 環境變量文件 默認是 /etc/sysconfig/elasticsearch
- data/目錄 默認是 /var/lib/elasticsearch
- logs/目錄 默認是 /var/log/elasticsearch
- plugins目錄 默認是 $ES_HOME/plugins
- repo目錄 默認沒有配置。
- scripts目錄默認位置是 /etc/elasticsearch/scripts
4. 配置elasticsearch
https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html
- (1) 配置文件格式為YAML。支持 path.data的語法或 path: data: /var/lib/elasticsearch語法。
- (2) 如果你不想將配置寫在配置文件中,使用 node.name: ${prompt.text} ,這樣在啟動時會讓用戶輸入,但是這種用法不能用在服務或后臺運行方式中
- (3) 在命令行上設置默認值 -Edefault.node.name="XXX"
4.1 重要的配置項:
- path.data 和 path.logs :如果使用的是tar包模式安裝的,默認情況下,這兩個文件夾位于$ES_HOME下的子文件夾,當升級elasticsearch時這兩個文件夾很容易被刪除,所以最好將其設置到其他位置。
例如在 config/elasticsearch.yml 中設置
path.logs: /var/log/elasticsearch
path.data: /var/data/elasticsearch
- cluster.name : 在 config/elasticsearch.yml 中設置
cluster.name: logging-prod
- node.name : 在 config/elasticsearch.yml 中設置
node.name: prod-data-2
node.name: ${HOSTNAME} #使用主機名
- bootstrap.memory_lock:這個配置項對node的健康極其重要,JVM永遠不swapped out to disk,即永遠不使用交換分區將JVM交換到磁盤上。在 config/elasticsearch.yml 中設置
bootstrap.memory_lock: true
要查看該設置是否生效,通過如下的API請求,然后檢查返回內容中的mlockall的值
GET _nodes?filter_path=**.mlockall
elServer="10.162.159.24:9200"
curl -XGET "${elServer}/_nodes?filter_path=**.mlockall"
如果你看到mlockall=false,那就意味著mlockall請求失敗了。
你還可以在logs中搜索 “Unable to lock JVM Memory” 來獲得更詳細的信息。
如果你看到mlockall=false,那就意味著mlockall請求失敗了。
你還可以在logs中搜索 “Unable to lock JVM Memory” 來獲得更詳細的信息。
要使這個設置生效,需要先配置其他的系統設置。
一般來說,最可能的原因是運行elasticsearch的用戶沒有權限來lock memory,這可以通過如下方式解決:
(a) tar包方式安裝的: 在運行elasticsearch之前使用root用戶設置 ulimit -l unlimited ,或在 /etc/security/limits.conf 中設置 memlock=unlimited。( elasticsearch - memlock unlimited )
(b) rpm包安裝于使用systemd 的系統的: 在環境變量配置文件中設置 LimitMEMLOCK=infinity
另一個可能的原因是臨時文件目錄/tmp被掛載為 noexec 。
這可以通過指定一個新的臨時文件夾來解決,通過設置 ES_JAVA_OPTS,如下所示
export ES_JAVA_OPTS="$ES_JAVA_OPTS -Djava.io.tmpdir=/path/to/temp/dir"
或在 jvm.options配置文件中設置JVM flag。
可選的方法還有 徹底禁用swap 和 設置 vm.swappiness=1 。 見后面的詳述
- network.host: 默認時,elasticsearch只綁定loopback地址,例如 127.0.0.1 and [::1]。在生產環境中,為了和其他nodes通信并組成一個集群,你的node需要綁定到一個non-loopback地址。雖然有很多關于network的設置,但是一般你只需要設置 network.host (在 /etc/security/limits.conf 中設置):
network.host: 192.168.1.10 ,127.0.0.1
- discovery.zen.ping.unicast.hosts: elasticsearch會綁定到可用的loopback 地址,并且掃描9300到9305,嘗試連接到同一個server上的其他nodes。
這提供了一種自動集群的體驗,而不用額外的配置。
但是當真的集群來臨時,你需要提供集群中一組其他種子節點(這些節點可能是live且可連接的),如下所示:(在 /etc/security/limits.conf 中設置)
discovery.zen.ping.unicast.hosts:
192.168.1.10:9300
#默認使用 transport.profiles.default.port,并且失敗后使用 transport.tcp.port
192.168.1.11
#會解析成IP地址
- seeds.mydomain.com
上面的數據格式在命令行怎么寫?
discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]
-
discovery.zen.minimum_master_nodes : 為了放棄數據丟失,這個參數極其重要。
在 /etc/security/limits.conf 中設置discovery.zen.minimum_master_nodes: 2 #集群master節點數量/2+1
4.2 啟動檢查
我們有許多關于用戶忍受不可預期的問題的體驗,因為他們沒有配置上述重要的設置。
這些Bootstrap檢查會檢閱elasticsearch和操作系的設置,并看看是否安全。
如果elasticsearch是在開發模式,Bootstrap檢查只會warning 于log中。
如果是在生產模式下,任何的Bootstrap檢查都會導致啟動fail。
heap size check:
如果JVM啟動時,initial和max heap size不一致,在JVM heap被resized時可能會導致暫停。
要通過heap size check,你必須配置heap size。
在 config/jvm.options中設置
-Xms2g
-Xmx2g
一般這個值有兩個原則,
- (a)不要超過物理內存的50%
- (b)不要超過26G
file descriptor check:
打開文件數量的檢查,elasticsearch需要很多的file descriptor。
要通過檢查,你必須配置 file descriptor ,確保將其設置為至少65536。
-
如果是用tar包的安裝方式:
先通過使用root用戶 ulimit -n 65536 然后啟動elasticsearch。
或者 通過 /etc/security/limits.conf 文件設置 nofile 為65536。elasticsearch - nofile 65536
RPM包安裝方式的默認設置已經是65536。
你可以通過如下API檢查 max_file_descriptors 配置
GET _nodes/stats/process?filter_path=**.max_file_descriptors
curl -XGET "${elServer}/_nodes/stats/process?filter_path=**.max_file_descriptors"
memory lock check:
在 config/elasticsearch.yml 中設置
bootstrap.memory_lock: true
要查看該設置是否生效,通過如下的API請求,然后檢查返回內容中的mlockall的值
GET _nodes?filter_path=**.mlockall
elServer="10.162.159.24:9200"
curl -XGET "${elServer}/_nodes?filter_path=**.mlockall"
如果你看到mlockall=false,那就意味著mlockall請求失敗了。
你還可以在logs中搜索 “Unable to lock JVM Memory” 來獲得更詳細的信息。
要使這個設置生效,需要先配置其他的系統設置。
一般來說,最可能的原因是運行elasticsearch的用戶沒有權限來lock memory,這可以通過如下方式解決:
-
tar包方式安裝的:
在運行elasticsearch之前使用root用戶設置 ulimit -l unlimited ,
或在 /etc/security/limits.conf 中設置 memlock=unlimited。elasticsearch - memlock unlimited
rpm包安裝于使用systemd 的系統的:
在環境變量配置文件中設置 LimitMEMLOCK=infinity
另一個可能的原因是臨時文件目錄/tmp被掛載為 noexec 。
這可以通過指定一個新的臨時文件夾來解決,通過設置 ES_JAVA_OPTS,如下所示
export ES_JAVA_OPTS="$ES_JAVA_OPTS -Djava.io.tmpdir=/path/to/temp/dir"
或在jvm.options配置文件中設置JVM flag。
maximum number of threads check:
elasticsearch執行是通過將查詢分解成不同的階段,然后在不同的階段使用不同的thread pool Executors。
所以elasticsearch需要大量的線程。
你必須至少讓elasticsearch能創建2048個線程。
這可以通過 /etc/security/limits.conf 的 nproc 設置來做到。
elasticsearch - nproc 2048
maximum size virtual memory check:
elasticsearch和Lucene使用 mmap 來增強影響map index到elasticsearch的地址空間。
這會保持特定的index data off the JVM heap但是在內存中來燃燒快速的access。
要使它生效,elasticsearch需要擁有unlimited address space。
maximum size virtual memory check強制elasticsearch進程擁有unlimited地址空間。
要通過這個檢查,你必須配置你的系統允許elasticsearch進程用擁有unlimited地址空間的能力。
這可以通過 /etc/security/limits.conf 的 as = unlimited 來做到。
elasticsearch - as unlimited
maximum map count check:
和上面一個設置一樣,使用 mmap,elasticsearch還要求有創建很多內存map區域的能力。
maximum map count check檢查內核允許一個進程至少有 262144個memory內存區域。
要通過這個檢查,你必須配置 sysctl vm.max_map_count >= 262144
client JVM check:
OpenJDK-derived JVMs提供了兩種不同的JVMs:client JVM 和 server JVM。
這些JVMs使用不同的compilers來從Java bytecode產生不同的可執行maching code。
client JVM 被調優于啟動速度和內存占用,server JVM 被調優于 性能最大化。
性能是兩種JVM的本質區別。
client JVM check保證了elasticsearch沒有運行于client JVM。
要通過這個檢查,你必須使用server VM來啟動elasticsearch。
在現代的操作系統中,使用server VM是默認選項。
另外,elasticsearch默認被配置為強制使用server VM。
在 jvm.options 中默認有 -server 選項。
use serial collector check:
openjdk-derived JVMs有很多種垃圾收集器,用于不同的workloads。
serial collector 非常適用于 single logical CPU 機器 或 非常小的heaps,但這些都不適用于elasticsearch。
使用這種collector會極大地破壞elasticsearch的性能。
要通過這個檢查,你必須不能使用這種collector,默認情況下,JVM配置為使用CMS collector。
在 jvm.options 中默認有 -XX:+UseConcMarkSweepGC 選項。
system call filter check:
elasticsearch根據不同的操作系統安裝不同風格的system call filters,例如在linux上是seccomp。
這些system call filters被安裝,是用于阻止執行system calls來forking的防御機制,防御任意代碼執行于elasticsearch之上。
system call filter check保證了如果system call filter被啟用了,它們被成功安裝。
要通過這個檢查,你必須修復那些任何阻止system call filter被安裝的Configuration errors(通過檢查logs),或者禁用system call filters(自負風險),通過在 /etc/security/limits.conf 中設置
bootstrap.system_call_filter: false
OnError and OnOutOfMemoryError checks:
JVM的選項 OnError 和 OnOutOfMemoryError 使得 如果 JVM遭遇了一個fatal error(OnError)或一個OutOfMemoryError(OnOutOfMemoryError)時可以執行任意代碼。
但是默認情況下,elasticsearch的system call filter(seccomp)是啟用的,并且這些filters會阻止forking。
因此,使用OnError或OnOutOfMemoryError和system call filters是矛盾的。
這個檢查阻止elasticsearch啟動,如果system call filter被啟用的同時這兩個選項也開啟。
這個check總是強制的。要通過這個檢查,就不要啟用OnError或OnOutOfMemoryError
替代方案是升級到Java 8u92然后使用 ExitOnOutOfMemoryError
G1GC check:
JDK8提供的早期版本的HotSpot JVM 已知確認有問題,當G1GC collector被啟用時會導致 index corruption 。
G1GC check會檢測這些早期版本的HotSpot JVM。
4.3 重要的操作系統配置
(1) 理想狀態下,elasticsearch應該霸占整個server。
要達到這個目的,你應該配置elasticsearch盡可能多地使用操作系統的資源。
(2) 默認情況下,elasticsearch假設你工作于開發模式下,這些重要的配置只是會記錄為log中的warning。
當你設置了類似 network.host的配置項之后,elasticsearch會假設你已經在生產環境下,并且會將warning升級為Exception。
這會導致elasticsearch啟動失敗。
(3) 在哪里配置操作系統設置基于你的安裝方式和你使用的操作系統。
如果你是通過tar包安裝的,system settings可以通過如下方式配置:
(a) 通過 ulimit 進行臨時配置
(b) 通過 /etc/security/limits.conf 進行永久配置如果你是通過systemd方式安裝,則可以通過systemd配置文件來設置。
使用ulimit設置:
(必須使用root,而且limit的設置只應用于當前session)
sudo su
ulimit -n 65536
su elasticsearch
通過 /etc/security/limits.conf 設置:
(可以為特定用戶永久設置),在這個文件中增加:
elasticsearch - nofile 65536
通過 /etc/sysconfig/elasticsearch 文件設置:
當使用RPM包安裝時,可以通過這個文件進行設置環境變量和system settings。
通過 systemd Configuration 設置:
當使用RPM包安裝于使用systemd的系統時,limits必須通過systemd配置。
systemd的服務文件 /usr/lib/systemd/system/elasticsearch.service 包含了默認的配置。
要覆蓋這個默認配置,可以增加一個文件于 /etc/systemd/system/elasticsearch.service.d/elasticsearch.conf 并配置一些更改,例如:
[Service]
LimitMEMLOCK=infinity
設置JVM選項:
設置JVM選項(包括系統屬性和JVM選項)的推薦方法是通過 jvm.options 配置文件。
該文件默認位置為 config/jvm.options。
這個文件的每一行都必須是以 - 開頭,你可以在其中增加自己的JVM選項。
另一種方式是通過 ES_JAVA_OPTS 環境變量,例如:
export ES_JAVA_OPTS="$ES_JAVA_OPTS -Djava.io.tmpdir=/path/to/temp/dir"
這個環境變量可以在 /etc/sysconfig/elasticsearch 中指定。
設置 JVM heap size:
默認情況下,elasticsearch告訴JVM使用最小heap size和最大heap size為2GB。
當在生產環境中時,需要增加這個值。
在 jvm.options 中通過 -Xms 和 -Xmx 來設置這個值。
這個值的大小取決于 可用內存的大小。好的規則如下:
(a) 將 Xms 和 Xmx 設置成一樣的值
(b) 越多的heap設置,elasticsearch就能緩存更多的數據。
但是注意太大的heap會導致你長時間的垃圾回收中斷。(c) 設置Xmx不要超過你的物理內存的50%,要為內核文件系統留有足夠的內存。
(d) 不要設置Xmx 超過JVM 用于 壓縮 object pointers的臨界點,這個臨界點大約為32GB。你可以通過查看日志中的一行(如下所示)來驗證它:
heap size [1.9gb], compressed ordinary object pointers [true]
(e) 最好是嘗試停留在below the threshold for zero-based compressed oops;精確的臨界值是變化的,但是26GB一般而言是安全的,但是在有些系統上可以達到30GB。你可以驗證它,通過使用JVM選項
-XX:+UnlockDiagnosticVMOptions -XX:+PrintCompressedOopsMode
啟動elasticsearch,并查找像下面的行:
heap address: 0x000000011be00000, size: 27648 MB, zero based Compressed Oops
這顯示了 zero-based compressed oops are enabled,而不是
heap address: 0x0000000118400000, size: 28672 MB, Compressed Oops with base: 0x00000001183ff000
還可以通過環境變量配置JAVA 選項:
ES_JAVA_OPTS="-Xms2g -Xmx2g" ./bin/elasticsearch
ES_JAVA_OPTS="-Xms4000m -Xmx4000m" ./bin/elasticsearch
(5) 禁用swapping :
大部分操作系統都嘗試使用盡可能多的文件系統緩存并急切地swap out未使用的應用程序緩存。
這會導致JVM heap被swapped out to disk。
swapping對于性能和node的穩定性來說是很壞的,應該盡可能阻止它發生。
它會導致垃圾回收達數分鐘而不是毫秒級別。
啟用 bootstrap.memory_lock :
第一個選項是使用 mlockall 來嘗試lock進程地址到RAM中,阻止任何的elasticsearch 內存被swapped out。
在配置文件中設置:
bootstrap.memory_lock: true
注意:mlockall可能會導致JVM或shell session退出,如果它嘗試分配了超過了可用的內存!
在elasticsearch啟動后你可以看到這個配置是否生效了,通過
GET _nodes?filter_path=**.mlockall
elServer="10.162.159.24:9200"
curl -XGET "${elServer}/_nodes?filter_path=**.mlockall"
如果你看到 mlockall=false,那就意味著mlockall請求失敗了。你還會在logs中看到一行更詳細的信息
Unable to lock JVM Memory.
一般來說,最可能的原因是運行elasticsearch的用戶沒有權限來lock memory,這可以通過如下方式解決:
-
(1) tar包方式安裝的:
在運行elasticsearch之前使用root用戶設置 ulimit -l unlimited ,或在 /etc/security/limits.conf 中設置 memlock=unlimited。elasticsearch - memlock unlimited
(2) rpm包安裝于使用systemd系統的:
在環境變量配置文件中設置 LimitMEMLOCK=infinity
另一個可能的原因是臨時文件目錄/tmp被掛載為 noexec 。
這可以通過指定一個新的臨時文件夾來解決,通過設置 ES_JAVA_OPTS,如下所示
export ES_JAVA_OPTS="$ES_JAVA_OPTS -Djava.io.tmpdir=/path/to/temp/dir"
或在jvm.options配置文件中設置JVM flag。
第二個選項是徹底禁用swap
一般來說elasticsearch獨立運行于一個box中,并且它的Memory usage通過JVM選項操縱。
那就不需要啟用swapped。
在linux操作系統中,你可以通過如下命令來來禁用它:
sudo swapoff -a
要永久禁用,就編輯 /etc/fstab 并注釋任何包含swap的行。
另一個選項是設置 vm.swappiness=1 。
這不會影響整個操作系統在緊急情況下的swap。
File Descriptors :
elasticsearch需要使用大量的文件描述符,如果用超了,會是災難性的并很可能導致數據丟失。
確保將其設置為至少65536。
-
如果是用tar包的安裝方式:
先通過使用root用戶 ulimit -n 65536 然后啟動elasticsearch。
或者 通過 /etc/security/limits.conf 文件設置 nofile 為65536。elasticsearch - nofile 65536
RPM包安裝方式的默認設置已經是65536。
你可以通過如下API檢查 max_file_descriptors 配置
GET _nodes/stats/process?filter_path=**.max_file_descriptors
elServer="10.162.159.24:9200"
curl -XGET "${elServer}/_nodes/stats/process?filter_path=**.max_file_descriptors"
Virutal memory:
elasticsearch默認使用 hybrid mmapfs / niofs 文件夾存儲它的indices。
操作系統默認的mmap counts limits一般都太小了,可能會導致內存溢出。
你可以使用root執行下面的命令來增加這個limits:
sysctl -w vm.max_map_count=262144
要永久設置,就更新 /etc/sysctl.conf 中的 vm.max_map_count 設置。
RPM包安裝方式會自動設置,不需要額外的配置。
number of threads:
elasticsearch使用很多的thread pools來執行不同類型的操作。
要確保elasticsearch至少可以創建2048個線程。
可以通過使用root用戶執行 ulimit -u 2048 來臨時設置,或通過 /etc/security/limits.conf 設置 nproc=2048來永久設置。
elasticsearch - nproc 2048
2.6 升級 elasticsearch
https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-upgrade.html
2.7 停止elasticsearch
- (1) 一個常規的shutdown可以讓elasticsearch有機會cleanup并close outstanding resources。例如一個node會將其自身從cluster中移除,sync translogs到磁盤上,執行其他的相關cleanup activities。
- (2) 如果你使用service的方式運行elasticsearch,你可以通過systemctl stop的方式
- (3) 如果你使用直接運行的方式運行elasticsearch,你可以通過Ctrl+C或發送SIGTERM信號。
- (4) 在elasticsearch VM的生命周期內,可能會發生fatal errors,這會導致VM處于一個有問題的狀態。這些erros包括內存溢出、VM的內部錯誤、嚴重的I/O錯誤。
當elasticsearch檢測到VM遇到這樣的錯誤時,它會嘗試log error并halt VM。
當elasticsearch開始了這樣的一個shutdown,它不會饞鬼的shutdown,其進程也會返回一個特定的狀態碼來指示發生了什么錯誤。
3. API 約定
elasticsearch REST APIs使用HTTP返回JSON數據。
3.1 Multiple Indices:
所有的index參數都支持多個,可以簡單的 test1,test2,...,_all表示所有Indices,
支持通配符,test* 或 test 或 tet 或 test ,
還支持 + 和 -,例如 +test* , -test3 。
3.2 date math support in index names:
例子: index的名稱使用了date math
GET /<logstash-{now/d}>/_search
GET /%3Clogstash-%7Bnow%2Fd%7D%3E/_search
elServer="10.162.159.24:9200"
curl -XGET "${elServer}/%3Clogstash-%7Bnow%2Fd%7D%3E/_search" -H 'Content-Type: application/json' -d'
{
"query" : {
"match": {
"test": "data"
}
}
}
注意 < > 需要urlcode
格式為 <static_name{date_math_expr{date_format|time_zone}}>
3.3 通用選項
(1) ?pretty=true 表示返回的JSON有縮進,只在debugging時使用它!
(2) Date math
-
(3) response filtering: filter_path, 例子:
GET /_search?q=elasticsearch&filter_path=took,hits.hits._id,hits.hits._score elServer="10.162.159.24:9200" curl -XGET "${elServer}/_search?q=elasticsearch&filter_path=took,hits.hits._id,hits.hits._score"
(4) flat settings: 當其設置為true時,返回的是flat格式的數據。
(5) Parameters: false是指 false、0、no、off 。其他的值都是 true。
時間單位:2d = days h=hours m=minutes micros=microseconds nanos=nanoseconds
byte size units:b=Bytes kb mb gb tb pb
沒有單位的quantities:k m g t p
長度單位: mi或miles、yd或yards、ft或feet、in或inch、km或kilometers、m或meters、cm或centimiters、mm或millimeters、NM或nmi或nauticalmiles(海里)
Fuzziness(模糊不清的):0,1,2=匹配任意一個 0..2 、3..5 、 >5
啟用Stack traces:默認情況下,當一個請求返回錯誤時,elasticsearch不包含stacktrace。你可以啟用它,通過設置 error_trace=true。Request body In query string: 對于一些非POST請求不接受Request body的庫來說,你可以通過將Request body作為source的參數來代替。
3.4 URL-based access control:
(1) 很多用戶使用with URL-based access control的代理來安全訪問到elasticsearch indices。
(2) 對于 multi-search、multi-get和bulk請求,用戶可以在URL中選擇指定一個index,和在每個individual Request的Request body中。這會導致URL-based access control面臨挑戰。
-
(3) 要阻止用戶覆蓋URL中已經指定的index,可以在 config.yml 中設置
rest.action.multi.allow_explicit_index: false
這個值默認是true。當設置為false時,elasticsearch會拒絕這樣的請求,
什么樣的請求?就是在Request body中顯式指定了index 的請求。
4. Modules
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules.html
這個章節包含各個modules,它們負責elasticsearch功能的各個方面。
每個module都可能包含靜態配置(在每個node上通過 elasticsearch.yml 文件配置)和動態配置(通過 Cluster APIs中的cluster-update-settings 進行動態配置)。
這個章節包含的模塊有:
4.1 Cluster-level routing and shard allocation
(控制 where、when、how shards被分配到nodes上) :
master的一個主要職能就是決定哪些shards被分配到哪些nodes上,和什么時候在nodes之間移動shards以達到rebalance集群。
4.1.1 Cluster level shard Allocation:
shard Allocation是分配shards到nodes上的過程。
這可以發生在 initial recovery時、replica Allocation時、rebalancing時、或當nodes被增加或移除時。
這部分設置主要是動態設置,關于shard Allocation settings、shard rebalancing settings、shard balancing heuristics(啟發法)。
4.1.2 Disk-based Shard Allocation:
elasticsearch在決定是否分配新的shards到那個節點之前,或將shards從那個節點重新分配到別處之前,會將可用的磁盤空間作為考慮的因素。 這部分設置可以是靜態配置,也可以動態更改(使用cluster-update-settings API)。
4.1.3 Shard Allocation Awareness:
當在同一個物理主機上運行多個VMs時,或在多個機架上運行、或在多個可awareness的zones上運行時,很有可能同一臺server上、同一個機架上、或同一個zone上會同時發生crash。
如果elasticsearch可以識別你的硬件的物理配置,它就可以確保primary shards和它的replica shards分開存放。
這部分設置可以是靜態配置,也可以動態更改(使用cluster-update-settings API)。
4.1.4 Shard Allocation Filtering:
雖然 Index Shard Allocation APIs提供了per-index級別的設置來控制shards到nodes的Allocation,cluster-level shard Allocation Filtering還能允許你允許或禁止任意index的shards到特定nodes的Allocation。
經典使用案例是當你想decommission(使退役) 一個節點時,并且你想將shards從那個節點移到其他節點。
使用動態 APIs 設置
4.1.5 Miscellaneous(混雜的) cluster settings:
包括Metadata、index tombstones(墓碑)、Logger的設置。
4.2 Discovery
(nodes如何彼此發現來組成集群) :
負責發現集群中的節點,并選舉master node。
注意,elasticsearch是一個基于點到點的系統,nodes之間直接互相通信。
所有的main APIs(index、delete、search)并不和master節點通信。
master節點的職責是維護整個cluster的狀態,還有當節點加入或離開集群時重新分配shards。
集群的狀態發生更改時,集群的狀態會通知到集群中其他節點(傳播方式基于實際的Discovery實現方式)。
Zen Discovery是elasticsearch內置的Discovery module,也是默認值。
Zen Discovery和其他module集成在一起使用,例如,節點之間所有的通信使用transport module完成。
它被分成了幾個子modules,包括ping、unicast、master election、fault detection、cluster state updates、no master block
4.3 Gateway
(在recovery可以開始之前需要多少nodes加入到集群中):
Local gateway module存儲在整個集群重啟時的集群狀態和shards data。
這部分設置主要是靜態配置,必須配置好于master node
4.4 HTTP
(控制HTTP REST Interface的設置):
http module允許你通過HTTP暴露elasticsearch APIs。
這部分配置通過 elasticsearch.yml 配置。
包括
- http.port :(默認是9200-9300)、會綁定第一個可用的端口。
- http.publish_port : HTTP客戶端用來跟node通信的端口。用于當節點位于proxy或防火墻后面,并且 http.port 不能直接從外面訪問。默認值和通過 http.port 的設置值相同。
- http.bind_host: 默認值同 http.host 或 network.bind_host
- http.publish_host: 默認值同 http.host 或 network.bind_host
- http.host : 用于設置 http.bind_host 和 http.publish_host 。默認值同 network.host
- ...
還可以禁用HTTP, ( http.enabled=false )。
elasticsearch節點(還有Java clients)之間的內部通信使用 transport interface通信,不是HTTP。所以在那些不準備提供HTTP APIs的節點上禁用HTTP APIs也是有用的。
4.5 Indices
(全局的index相關的設置):
indices module控制index相關的設置,這些設置全局管理所有的indices,而不是per-index級別。
可用的設置包括:
- Circuit breaker: 設置內存使用的限制來避免內存溢出。
- Fielddata cache:設置用于fielddata cache的in-memory的heap的使用量限制。
- node query cache:
- indexing buffer:
- shard request cache:
- recovery:
4.6 Network
(默認的network設置) :
elasticsearch默認只綁定到localhost。
常用的network設置有:
- network.host : 節點會綁定到這個hostname或IP地址,并發布這個host給其他節點。默認是 local ,其他可選的還有 [networkInterface] 、 site 、 global ,分別表示特定網卡節點綁定的IP地址、site-local地址(例如 192.168.0.1)、全局地址(例如8.8.8.8)
- discovery.zen.ping.unicast.hosts : 為了加入集群,節點需要知道集群中其他一些節點的IP地址。
- http.port :
- transport.tcp.port : 綁定的用于節點之間通信使用的端口。默認是9300-9400。
- ...
4.7 Node client
(加入到集群但是不扮演master node的角色) :
集群中的每個節點默認都可以處理HTTP和transport traffic。
transport layer只用于節點(還有Java TransportClient)之間的通信;
HTTP layer只用于外部REST client的訪問。
集群中的所有節點都知道其他所有的節點,并轉發client請求到相應的節點,每個節點都服務于一個或多個用途:
- master-eligible node: 設置 node.master:true ,也是默認值,這使得節點有資格被選舉為master node,master node控制集群。
- data node:設置 node.data:true ,也是默認值,data節點持有數據并執行數據相關的操作,例如CRUD、search和Aggregations。
- ingest(攝取,吸收) node :設置 node.ingest:true ,也是默認值。ingest node可以應用一個 ingest pipeline到一個document,以便在index它之前轉換并充實該document。這個工作很重,所以使用專門的ingest node,并且在master node和data node上設置 ingest.node:false
- tribe node: 通過設置 tribe.* 相關設置,這些設置可以讓節點可以連接多個集群并可以跨集群 執行search和其他操作 。
默認情況下,節點是 master-eligible和data node,而且可以當作 ingest node(雖然不建議)。
隨著集群的增長,建議master node和data node也分開。
4.8 Painless
(elasticsearch使用的內置的腳本語言,被設計為讓elasticsearch盡可能地安全)
4.9 Plugins
(使用擴展的插件) :
4.10 Scripting
(使用Lucene表達式、Groovy、Python、Javascript的自定義的腳本。你也可以使用內置的腳本語言Painless來編寫腳本) : https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
scripting module讓你能夠使用腳本來evaluate自定義的表達式。
例如,你可以使用腳本來返回一個“script fields”作為搜索請求的一部分。
默認的腳本語言是 Painless。
額外的 lang 插件讓你能夠運行其他語言的腳本。
一般用途的語言有 painless、groovy、javascript、python,特定用途的語言(不靈活,但對特定任務有很好的性能)有 expression、mustache、java。
如何使用?
"script":{"lang":"...","inline"|"sorted"|"filter":"...","params":{ ... } }
4.11 Snapshot/Restore
(使用 snapshot/restore 備份你的data) :
snapshot and restore module 允許你創建單獨Indices或整個集群的snapshots到一個遠端repository,像共享文件系統、S3、HDFS。
這些snapshots不是檔案文件,因為它們只能被可以識別index的elasticsearch版本恢復。
這意味著 在2.x版本創建的snapshots可以被恢復到5.x,在1.x版本創建的snapshots可以被恢復到2.x,但是1.x版本創建的snapshots不能被恢復到5.x。
- 在snapshot或restore操作之前,你必須先注冊一個snapshot repository到elasticsearch中。通過一個HTTP API
- 一個repository可以包含同一個集群的多個snapshots。snapshots通過唯一的名稱標識自己。
4.12 Thread pools
(elasticsearch使用的專用的thread pools的信息) :
一個節點持有幾個thread pools以達到改善threads的內存消耗。
許多這樣的pools一般還有一個queues與它們關聯,這些queues允許pending requests而不是丟棄他們。
一些重要的thread pools有:
- generic :通用操作(例如 后臺的node recovery)使用。threadpool類型為scaling
- index: index/delete操作使用。threadpool類型為fixed,其size是 # of available processors,queue size of 200.這個pool的最大size是 1 + # of available processors
- search: count/search/suggest操作使用。threadpool類型為fixed,其size是 int((# of available_processors * 3) / 2) + 1, queue_size of 1000 。
- get : get操作使用。threadpool類型為fixed,其size是 # of available processors, queue_size of 1000.
- bulk : bulk操作使用。 threadpool類型為fixed,其size是 # of available processors, queue_size of 50 。這個pool的最大size是 1 + # of available processors
- ...
4.13 Transport
(配置transport networking layer,用于Elasticsearch在內部nodes之間通信用的使用) :
Transport機制是完全異步的,這意味著沒有任何一個thread會阻塞等待一個response。
這樣做的好處是首先解決了C10k problem,同時也是scatter(broadcast)/gather操作的理想解決方案,例如在elasticsearch中的search操作。
TCP Transport是Transport module的一個實現。它有如下設置:
transport.tcp.port : 默認是 9300-9400
transport.host :
...
4.14 Tribe nodes
(加入一個或多個clusters的tribe node,并扮演它們之間聯邦 client的角色) :
tribe node 會檢索其連接的所有集群的集群狀態,并將它們合并成一個全局的集群狀態。
有了這些信息在手上,它能夠對所有集群的節點執行讀寫操作,就像它們在本地一樣。
要想讓一個節點稱為tribe node,只需要簡單的在 elasticsearch.yml 中配置:
tribe:
t1:
cluster.name: cluster_one
t2:
cluster.name: cluster_two
t1和t2是任意的名稱,表示要連接到的集群。
5. Index Modules
https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html
Index Modules are modules created per index,并控制一個index相關的方方面面。
index level settings可以被設置為per-index。可以是動態設置,也可以靜態設置。
6. Ingest Node
https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html
ingest=提取
你可以使用ingest node在實際的indexing 發生之前 來預處理documents,這些預處理操作會攔截bulk和index請求,應用transformations,然后將documents傳回給index或bulk APIs。
你可以讓任意的節點具有ingest功能,也可以設置專門的ingest node(推薦)。
ingest默認是在所有節點上啟用的。
要禁用它就在 elasticsearch.yml 中設置:
node.ingest: false
7. How to
https://www.elastic.co/guide/en/elasticsearch/reference/current/how-to.html
7.1 常規推薦
- 不要返回大的結果集:返回top documents是非常好的。如果你非要返回大的結果集,請使用Scroll API。
- 避免大的documents: 默認的 http.max_context_length=100MB ,elasticsearch會拒絕index超過這個值得documents。你可以決定增大這個值,但是Lucene仍然有最大值為大約2GB。
- 避免sparsity(數據稀疏):在Lucene后面的數據結構,是elasticsearch為了index和存儲數據所依賴的,最好工作于抽魔的數據,例如所有的documents都擁有相同的fields。
- 避免在相同的index中存放不相關的數據
- 標準化document structures
- 避免types:types可能聽起來是一個不錯的方式來存儲多個tenants到一個單獨的index中。但是它們不是:如果你的types不是有非常類似的mappings,你需要考慮將他們移動到專用的index中
- 禁用 norms 和 doc_values on sparse(稀疏的、缺少的) fields:如果上面的建議都不符合你的情況,你可能要檢查在你的sparse fields上你是否真的需要 norms 和 doc_values 。norms 可以被禁用,如果producing scores不是必須的,doc_values可以被禁用如果不使用sorting和Aggregations。注意不要輕易下這個決定,因為這些參數不能動態更改,所以你需要reindex,如果你意識到你需要 norms 或 doc_values。
7.2 Recipes(食譜)
- mixing exact search with stemming(詞干提取):
- getting consistent scoring :
(a) scores are not reproducible:
(b) relevancy looks wrong:
7.3 調優 indexing speed
- 使用bulk requests
- 使用multiple workers/threads 來發送數據給elasticsearch:
- 增加refresh間隔
- 禁用refresh和replicas for initial loads
- 禁用swapping
- 為filesystem cache留出內存
- 使用auto-generated ids
- 使用更快的hardware
- indexing buffer size
7.4 調優 search speed
- 為filesystem cache留出內存
- 使用更快的hardware
- document 建模
- Pre-index data
- Mappings :一些數據是數字并不意味著它總是應該被mapped as a numeric field。有時應該被mapped as keyword 而不是integer 或 long。
- avoid scripts:如果一定要使用,你應該選擇 painless 和 expressions 引擎。
- search rounded date: 在 日期類型的fields上的查詢使用 now 一般不能cacheable。然而使用一個rounded date經常是可以接受的,并且可以使用緩存。
- 強制合并 read-only indices:
- warm up gloal ordinals:
- warm up the filesystem cache
7.5 調優 disk usage
- 禁用你不需要的features
- 不要使用默認的動態string mappings
- 禁用 _all
- 使用 best_compression :
- 使用smallest numeric type that is sufficient
作者:堅持到底v2
鏈接:http://www.lxweimin.com/p/6a700cc61913