目前生產在用的索引模板。索引調優只是es調優的一部分,還包括硬件,jvm ,集羣調優,系統級別的調優等。
索引模版
注意:對於已經創建的索引不會生效,只有新建索引並且索引以logstash-kafka 開頭的時候纔會使用你的模版。order字段爲優先級,數字越大優先級越高。
PUT _template/logstash-kafka
{
"order": 6,
"template": "logstash-kafka*",
"settings": {
"index.merge.policy.segments_per_tier": "25",
"index.warmer.enabled": "false",
"index.refresh_interval": "60s",
"index.number_of_shards": "7",
"index.translog.durability": "async",
"index.store.type": "mmapfs",
"index.merge.policy.floor_segment": "50mb",
"index.merge.scheduler.max_thread_count": "1",
"index.translog.sync_interval": "60s",
"index.routing.allocation.total_shards_per_node": "2",
"index.number_of_replicas": "1"
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"path_match": "message",
"mapping": {
"norms": false,
"type": "text"
},
"match_mapping_type": "string"
}
},
{
"string_fields": {
"mapping": {
"norms": false,
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"match_mapping_type": "string",
"match": "*"
}
}
],
"_all": {
"norms": false,
"enabled": true
},
"properties": {
"@timestamp": {
"include_in_all": false,
"type": "date"
},
"geoip": {
"dynamic": true,
"properties": {
"ip": {
"type": "ip"
},
"latitude": {
"type": "half_float"
},
"location": {
"type": "geo_point"
},
"longitude": {
"type": "half_float"
}
}
},
"@version": {
"include_in_all": false,
"type": "keyword"
}
}
}
},
"aliases": {}
}
}
replica數目
爲了讓創建的es index在每臺datanode上均勻分佈,同一個datanode上同一個index的shard數目不應超過3個。
計算公式: (number_of_shard * (1+number_of_replicas)) < 3*number_of_datanodes
每臺機器上分配的shard數目
"index.routing.allocation.total_shards_per_node": "2",
refresh時間間隔
默認的刷新時間間隔是1s,對於寫入量很大的場景,這樣的配置會導致寫入吞吐量很低,適當提高刷新間隔,可以提升寫入量,代價就是讓新寫入的數據在60s之後可以被搜索,新數據可見的及時性有所下降。
"index.refresh_interval": "60s"
translog
降低數據flush到磁盤的頻率。如果對數據丟失有一定的容忍,可以打開async模式。
"index.translog.flush_threshold_ops": "1000000",
"index.translog.durability": "async",
merge相關參數
"index.merge.policy.floor_segment": "100mb",
"index.merge.scheduler.max_thread_count": "1",
"index.merge.policy.min_merge_size": "10mb"
mapping設置
對於不參與搜索的字段(fields), 將其index方法設置爲no, 如果對分詞沒有需求,對參與搜索的字段,其index方法設置爲not_analyzed。
index分爲3種模式(簡單的說):
1.not_analyzed 進行整個字段的搜索,打個比方對整個單詞可以搜索,單詞的一部分是搜索不到的。
2.analyzed 對正文可以關鍵詞搜索,只要有你查詢的關鍵字就可以搜索到
3.no 不能進行搜索,也就是關閉搜索功能
不過作爲elk查看日誌最好要打開分詞並且可以進行搜索
"index": "analyzed",
硬件
當然是ssd啦,反正是加上各種資源。
系統級別
文件句柄
Linux中,每個進程默認打開的最大文件句柄數是1000,對於服務器進程來說,顯然太小,通過修改/etc/security/limits.conf來增大打開最大句柄數
* - nofile 65535
虛擬內存設置
max_map_count定義了進程能擁有的最多內存區域
sysctl -w vm.max_map_count=262144
修改/etc/elasticsearch/elasticsearch.yml
bootstrap.mlockall: true
修改/etc/security/limits.conf, 在limits.conf中添加如下內容
* soft memlock unlimited
* hard memlock unlimited
TCP全連接隊列參數設置, 這樣設置的目的是防止節點數較多(比如超過100)的ES集羣中,節點異常重啓時全連接隊列在啓動瞬間打滿,造成節點hang住,整個集羣響應遲滯的情況
echo "net.ipv4.tcp_abort_on_overflow = 1" >>/etc/sysctl.conf
echo "net.core.somaxconn = 2048" >>/etc/sysctl.conf
降低tcp alive time,防止無效鏈接佔用鏈接數
echo 300 >/proc/sys/net/ipv4/tcp_keepalive_time
磁盤緩存相關參數
vm.dirty_background_ratio 這個參數指定了當文件系統緩存髒頁數量達到系統內存百分之多少時(如5%)就會觸發pdflush/flush/kdmflush等後臺回寫進程運行,將一定緩存的髒頁異步地刷入外存;
vm.dirty_ratio
該參數則指定了當文件系統緩存髒頁數量達到系統內存百分之多少時(如10%),系統不得不開始處理緩存髒頁(因爲此時髒頁數量已經比較多,爲了避免數據丟失需要將一定髒頁刷入外存);在此過程中很多應用進程可能會因爲系統轉而處理文件IO而阻塞。
把該參數適當調小,原理通(1)類似。如果cached的髒數據所佔比例(這裏是佔MemTotal的比例)超過這個設置,系統會停止所有的應用層的IO寫操作,等待刷完數據後恢復IO。所以萬一觸發了系統的這個操作,對於用戶來說影響非常大的。
sysctl -w vm.dirty_ratio=10
sysctl -w vm.dirty_background_ratio=5
swap調優
swap空間是一塊磁盤空間,操作系統使用這塊空間保存從內存中換出的操作系統不常用page數據,這樣可以分配出更多的內存做page cache。這樣通常會提升系統的吞吐量和IO性能,但同樣會產生很多問題。頁面頻繁換入換出會產生IO讀寫、操作系統中斷,這些都很影響系統的性能。這個值越大操作系統就會更加積極的使用swap空間。
調節swappniess方法如下
sudo sh -c 'echo "0">/proc/sys/vm/swappiness'
io sched
如果集羣中使用的是SSD磁盤,那麼可以將默認的io sched由cfq設置爲noop
sudo sh -c 'echo "noop">/sys/block/sda/queue/scheduler
JVM參數設置
1.jvm.options
-Xms和-Xmx設置爲相同的值,推薦設置爲機器內存的一半左右,剩餘一半留給系統cache使用。
jvm內存建議不要低於2G,否則有可能因爲內存不足導致ES無法正常啓動或OOM
jvm建議不要超過32G,否則jvm會禁用內存對象指針壓縮技術,造成內存浪費
2.elasticsearch.yml
設置內存熔斷參數,防止寫入或查詢壓力過高導致OOM,具體數值可根據使用場景調整。
indices.breaker.total.limit: 30%
indices.breaker.request.limit: 6%
indices.breaker.fielddata.limit: 3%
調小查詢使用的cache,避免cache佔用過多的jvm內存,具體數值可根據使用場景調整。
indices.queries.cache.count: 500
indices.queries.cache.size: 5%
集羣調優
{
"persistent": {
"cluster": {
"routing": {
"allocation": {
"allow_rebalance": "indices_primaries_active",
"cluster_concurrent_rebalance": "8",
"node_concurrent_recoveries": "8",
"enable": "new_primaries"
}
}
},
"indices": {
"store": {
"throttle": {
"max_bytes_per_sec": "50mb"
}
}
}
},
"transient": {
"cluster": {
"routing": {
"rebalance": {
"enable": "all"
},
"allocation": {
"disk": {
"include_relocations": "true",
"threshold_enabled": "true",
"watermark": {
"low": "85%",
"high": "90%"
}
}
}
},
"info": {
"update": {
"interval": "30s"
}
}
},
"indices": {
"breaker": {
"fielddata": {
"limit": "30%"
},
"request": {
"limit": "30%"
}
}
}
}
}
Disk-based Shard Allocation
“cluster.routing.allocation.disk.threshold_enabled”:true,//是否開啓基於硬盤的分發策略
“cluster.routing.allocation.disk.watermark.low”:“85%”,//不會分配分片到硬盤使用率高於這個值的節點
“cluster.routing.allocation.disk.watermark.high”:“90%”,//如果硬盤使用率高於這個值,則會重新分片該節點的分片到別的節點
“cluster.info.update.interval”:“30s”,//當前硬盤使用率的查詢頻率
“cluster.routing.allocation.disk.include_relocations”:true,//計算硬盤使用率時,是否加上正在重新分配給其他節點的分片的大小
new_primaries,僅僅對新建索引的primary shard允許分配
cluster.routing.allocation.node_concurrent_recoveries: 2添加刪除節點或負載均衡時併發恢復線程的個數,默認爲4
indices_primaries_active,僅僅只有在所有的primary shard都被分配之後才允許rebalance
indices_all_active,默認,僅僅允許所有的primary shard和replica shard都被分配之後,才能rebalance
關閉不必要的index
open的索引的shard會加載到內存中,close不必要的index可以減少內存佔用。
curl -XPOST http://node01:1 9200/myshard/_close
集羣啓動數據恢復相關參數配置
集羣維護後重啓。比如一共5個節點,其中3個節點啓動很快,2個節點啓動很慢。
當3個節點啓動後,另外兩個節點還沒啓動,這個時候集羣找不到沒啓動服務的節點上的shard,這個時候集羣就會嘗試做分片的重新分配。等另外兩個節點啓動後,集羣再次對集羣的分片重新分配。這樣就導致了不必要的IO操作,對性能影響非常大。
gateway.recover_after_nodes: 3 當集羣中至少啓動3個節點,纔開始做分片的分配。默認爲1
副本、分片數量的規劃
分片的數量(建議3~4個)需要根據實際數據量測試,確定最合適的分片數量。
分片數過少,併發少。分片數過多,消耗的系統資源多(打開文件句柄、內存),影響性能。
一個分片最多能存儲20億條記錄,根據實際性能規劃。
副本一般推薦設置2~3個(大量數據情況)。
副本數量能提升數據檢索的性能。
副本需要從主分片同步數據,爲了避免服務器壓力,副本數量不能過多。
參考: https://blog.csdn.net/ZYC88888/article/details/83184473