elasticsearch經常使用命令node
檢查es版本信息linux
curl IP:9200json
查看集羣是否健康bootstrap
http://IP:9200/_cat/health?vapi
curl 'IP:9200/_cat/health?v'安全
查看集羣狀態網絡
curl -XGET ip:9200/_cluster/stateapp
curl -XGET ip:9200/_cluster/health?prettycurl
查看節點列表異步
http://IP:9200/_cat/nodes?v
curl 'IP:9200/_cat/nodes?v'
curl 'ip:9200/_nodes/stats'
列出全部索引及存儲大小
http://IP:9200/_cat/indices?v
curl 'IP:9200/_cat/indices?v'--查詢全部索引及數據大小
查看線程狀態
GET /_cat/thread_pool
查看線程狀態
http://172.21.0.5:9200/_nodes/thread_pool
線程池優化
更改配置文件elasticsearch.yml增長以下內容:
thread_pool.search.queue_size: 500
#queue_size容許控制沒有線程執行它們的掛起請求隊列的初始大小。
thread_pool.search.size: 200
#size參數控制線程數,默認爲核心數乘以5。
thread_pool.search.min_queue_size:10
#min_queue_size設置控制queue_size能夠調整到的最小量。
thread_pool.search.max_queue_size: 1000
#max_queue_size設置控制queue_size能夠調整到的最大量。
thread_pool.search.auto_queue_frame_size: 2000
#auto_queue_frame_size設置控制在調整隊列以前進行測量的操做數。它應該足夠大,以便單個操做不會過分偏向計算。
thread_pool.search.target_response_time: 6s
#target_response_time是時間值設置,指示線程池隊列中任務的目標平均響應時間。若是任務一般超過此時間,則將調低線程池隊列以拒絕任務。
獲取集羣主ip
curl -s localhost:9200/_cat/nodes?v|awk -F":" '{print $1;print $9}'|grep "*"|awk '{print $1}'
建立索引
建立索引名爲XX,默認會有5個分片,1個索引
curl -XPUT 'IP:9200/XX?pretty'
添加一個類型
curl -XPUT 'IP:9200/XX/external/2?pretty' -d '
{
"gwyy": "John"
}'
更新一個類型
curl -XPOST 'IP:9200/XX/external/1/_update?pretty' -d '
{
"doc": {"name": "Jaf"}
}'
刪除指定索引
curl -XDELETE 'IP:9200/_index?pretty'
分片處理
過濾查看全部未分配索引的方式,c知道哪一個索引的哪一個分片就開始手動修復,經過reroute的allocate分配
curl -s 「http://10.19.22.142:9200/_cat/shards」 | grep UNASSIGNED結果以下,第一列表示索引名,第二列表示分片編號,第三列p是主分片,r是副本
知道哪一個索引的哪一個分片就開始手動修復,經過reroute的allocate分配
curl -XPOST '{ESIP}:9200/_cluster/reroute' -d '{
"commands" : [ {
"allocate_replica" : {
"index" : "eslog1",
"shard" : 4,
"node" : "es1",
"allow_primary" : true
}
}
]
}'
分配時可能遇到的坑,須要注意的地方
分配副本時必需要帶參數」allow_primary」 : true, 否則會報錯
當集羣中es版本不一樣時,若是這個未分配的分片是高版本生成的,不能分配到低版本節點上,反過來低版本的分片能夠分配給高版本,若是遇到了,只要升級低版本節點的ES版本便可
案例
經過/_cat/shards 查看,發現有從分配處於一個未分配的狀態,該命令該出的數據奇怪的地方是,個人集羣明明有三臺機器,可是shareds裏面只給出了兩臺。
data 2 r STARTED 449516 1.6gb 100.73.22.5 22-5
data 2 p STARTED 449516 1.6gb 100.73.22.6 22-6
data 2 r UNASSIGNED
而後經過查閱官方手冊,查詢UNASSIGNED 的緣由,而後發現不少種可能性的,可是官方的api 的case裏面,shards 是直接給出了緣由的,可是個人es卻沒有給出緣由。怎麼辦呢?在api文檔裏面找答案,發現了/_cluster/reroute 的命令,一看就知道是救星,既然狀態是UNASSIGNED, 我手動指定 該切片到特定的節點,不就能夠了麼。
執行命令
curl -XPOST ‘100.73.22.5:9200/_cluster/reroute’ -d ‘{
「commands」 : [{
"move" : {
"index" : "data",
"shard" : 0,
"from_node" : "22-6" ,
"to_node": "22-5"
}
}]
}’
4.手動移動分片和副本:
移動分片:
$curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
"commands":[{
"move":{
"index":"filebeat-ali-hk-fd-tss1",
"shard":1,
"from_node":"ali-hk-ops-elk1",
"to_node":"ali-hk-ops-elk2"
}
}]
}'
取消分片:
$curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
"commands":[{
"cancel":{
"index":"filebeat-ali-hk-fd-tss1",
"shard":1,
"node":"ali-hk-ops-elk1"
}
}]
}'
分配分片:
$curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
"commands":[{
"allocate":{
"index":"filebeat-ali-hk-fd-tss1",
"shard":1,
"node":"ali-hk-ops-elk2"
}
}]
}'
將某個未分配的索引手動分配到某個節點上.
$curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
"commands":[
{
"move":{
"index":"filebeat-ali-hk-fd-tss1",
"shard":1,
"from_node":"ali-hk-ops-elk1",
"to_node":"ali-hk-ops-elk2"
}
},
{
"cancel":{
"index":"filebeat-ali-hk-fd-tss1",
"shard":1,
"node":"ali-hk-ops-elk1"
}
},
{
"allocate":{
"index":"filebeat-ali-hk-fd-tss1",
"shard":1,
"node":"ali-hk-ops-elk1"
}
}]
}'
curl -XPOST '10.9.86.252:9200/_cluster/reroute' -d '{ "commands": [{"allocate_replica": {"index": "esuser117db1","shard": 5,"nodes": "in-searchN-A2"}}]}'
未分配副本解決方法(通過實戰有效可行):
若是手動分配無效的話,可採用以下針對未分配索引的副本數目設置操做處理
設置索引副本數目爲0
curl -XPUT '192.168.12.131:9200/vip_active223/_settings' -d '{"index": {"number_of_replicas":0}}'
設置索引副本數目爲1
curl -XPUT '192.168.12.131:9200/vip_active223/_settings' -d '{"index": {"number_of_replicas":1}}'
#elasticsearch集羣安全重啓方法
咱們須要的是,告訴 Elasticsearch 推遲再平衡,由於對外部因子影響下的集羣狀態,咱們本身更瞭解。操做流程以下:
可能的話,中止索引新的數據。雖然不是每次都能真的作到,可是這一步能夠幫助提升恢復速度。
禁止分片分配。這一步阻止 Elasticsearch 再平衡缺失的分片,直到你告訴它能夠進行了。若是你知道維護窗口會很短,這個主意棒極了。你能夠像下面這樣禁止分配:
查看:curl -XGET ip:9200/_cluster/settings
修改:curl -XPUT ip:9200/_cluster/settings {}
PUT /_cluster/settings
{
"transient" : {
"cluster.routing.allocation.enable" : "none"
}
}
關閉單個節點。
執行維護/升級。
重啓節點,而後確認它加入到集羣了。
用以下命令重啓分片分配:
PUT /_cluster/settings
{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}
分片再平衡會花一些時間。一直等到集羣變成 綠色 狀態後再繼續。
重複第 2 到 6 步操做剩餘節點。
到這步你能夠安全的恢復索引了(若是你以前中止了的話),不過等待集羣徹底均衡後再恢復索引,也會有助於提升處理速度。
防止內存溢出(生產環境建議設置鎖住內存)
你須要打開配置文件中的mlockall開關,它的做用就是運行JVM鎖住內存,禁止OS交換出去。在elasticsearch.yml配置以下:
bootstrap.memory_lock: true
置爲true來鎖住內存不進行swapping。由於當jvm開始swapping時es的效率 會下降,因此要保證它不swap,能夠把ES_MIN_MEM和ES_MAX_MEM兩個環境變量設置成同一個值,而且保證機器有足夠的內存分配給es。 同時也要容許elasticsearch的進程能夠鎖住內存,linux下啓動es以前能夠經過`ulimit -l unlimited`命令設置
防止出現內存溢出可是進程沒有退出
jvm.options
-XX:+ExitOnOutOfMemoryErrorsss
elasticsearch集羣內部節點超時解決
默認配置爲:節點每隔1s同master發送1次心跳,超時時間爲30s,測試次數爲3次,超過3次,則認爲該節點同master已經脫離了。以上爲elasticsearch的默認配置。在實際生產環境中,每隔1s,太頻繁了,會產生太多網絡流量。咱們能夠在elasticsearch.yml以下修改。
discovery.zen.fd.ping_timeout: 120s
discovery.zen.fd.ping_retries: 6
discovery.zen.fd.ping_interval: 30s
如何加快個人index速度
Elasticsearch預先配置了許多設置,嘗試確保您保留足夠的資源來搜索和索引數據。可是,若是您使用Elasticsearch嚴重偏向寫入,則可能會發現調整某些設置以提升索引性能是有意義的,即便這意味着丟失一些搜索性能或副本數據。 下面,咱們將探討一些方法來優化用例來進行索引,而不是搜索數據。(預設值是兼顧搜索和索引)
分片分配: 若是你要建立一個更新頻繁的索引,請確保指定足夠的主分片,以便您能夠在全部節點間均勻分佈索引負載。通常建議是每一個集羣中的節點分配一個主分片。若是你的CPU和磁盤夠用,2個或者更多的主分片是可行的(注意:這個是對單個索引來描述的)。可是,請記住,分片過分分配會增長開銷,並可能會對搜索產生負面影響,由於搜索請求須要打到索引中的每一個分片。另外一方面,若是分配的分片數少於節點數,you may create hotspots?由於包含這些分片的節點須要處理比不包含任何索引分片的節點更多的索引請求。
禁止 merge throttling: merge throttling是elasticsearch的一個自動機制:當es檢測到合併速度落後於索引速度時,es會throttle 索引請求。 若是要優化索引性能,而不是搜索性能,能夠經過更新集羣設置來禁止掉merge throttling(經過將indices.store.throttle.type設置爲「none」)。You can make this change persistent (meaning it will persist after a cluster restart) or transient (resets back to default upon restart), based on your use case.
增長indexing buffer的大小: 這個index-level 設置(indices.memory.index_buffer_size)決定了在將文檔寫入到磁盤上的segment以前buffer的總大小。默認爲總heap的10%,以便爲索引請求預留更多的heap,which doesn’t help you if you’re using Elasticsearch primarily for indexing.??
先index,後replicate: 初始化索引時,在索引設置中指定零個複本分片,並在索引完成後添加副本。 這將提升索引性能。可是有必定的風險。
刷新間隔拉長: 增長Index Settings API中的刷新間隔。 默認狀況下,索引刷新過程每秒都會發生一次,可是在較大的索引期間,下降刷新頻率能夠幫助減輕部分工做量。
調整translog設置: 從版本2.0開始,Elasticsearch將在每次請求後將Translog數據刷新到磁盤,從而下降硬件故障時數據丟失的風險。若是你中索引性能,而且不太擔憂潛在的數據丟失的可能,您能夠將index.translog.durability更改成異步。 有了這一點,索引只會在每一個sync_interval上提交寫入磁盤,而不是在每一個請求以後提交寫入磁盤,從而使其更多的富餘資源能夠提供索引請求。
es集羣節點數據遷移場景:集羣中須要替換一臺新機器,須要把集羣中的老的機器的數據遷移到集羣其餘的機器中,下面的命令是排除掉10.0.0.1,把這臺機器的數據遷移到器羣其餘機器中。
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"transient" : {
"cluster.routing.allocation.exclude._ip" : "10.0.0.1"
}
}
'