1、ES高可用架構圖
ES數據庫最好的高可用集群部署架構為:
三台服務器做master節點、三台服務器作為data節點(存儲資源要大)、三台做ingest節點(用於數據轉換,可以提高ES查詢效率)
2、創建ES用戶組
Elasticsearch不能在 root 用戶下啟動,我們需要在三台機器上分創建一個普通用戶# 創建elastic用戶
useradd elastic
設置用戶密碼
passwd elastic
測試服務器密碼設置的是
abc123!@#
切換到elastic用戶
su elastic
分別在三台機器上的 /home/elastic/ 目錄下分別創建data、logs文件夾。
cd /home/elastic/
mkdir data
mkdir logs在生產環境下我們要把Elasticsearch生成的索引文件數據存放到自定義的目錄下
data:存儲Elasticsearch索引文件數據
logs:存儲日志文件
3.系統設置。
使用root用戶分別在三台服務器上增加 /etc/sysctl.conf 配置
添加內容為
vm.max_map_count = 655300
接着輸入如下命令讓配置生效:
sysctl -p
解鎖文件限制,增加 /etc/security/limits.conf 配置
* soft nofile 65535
* hard nofile 65535
* soft nofile 65535
* hard nofile 65535
4、配置Elasticsearch
首先我們將下載好的elasticsearch-7.6.2-linux-x86_64.tar.gz壓縮包通過elastic普通用戶,上傳到三台服務器集群的/home/elastic目錄下,解壓
tar -zxvf elasticsearch-7.9.0-linux-x86_64.tar.gz
如果是root用戶,上傳壓縮包解壓后,需要修改elastic 目錄的擁有者
cd /home/
chown -R elastic elastic
解壓完成后ll查看目錄
su elastic一定要切換用戶,切記!切記!
分別修改elasticsearch.yml配置文件
vi /home/elastic/elasticsearch-7.9.0/config/elasticsearch.yml
cluster.name: data-cluster
node.name: "data-es-05"
#node.data: false
# Indexing & Cache config
index.number_of_shards: 5
index.number_of_replicas: 1
index.cache.field.type: soft
index.cache.field.expire: 10m
index.cache.query.enable: true
indices.cache.query.size: 2%
indices.fielddata.cache.size: 35%
indices.fielddata.cache.expire: 10m
index.search.slowlog.level: INFO
#indices.recovery.max_size_per_sec: 1gb
index.merge.scheduler.max_thread_count: 2 # Only for spinning media.
# Refresh config
index.refresh_interval: 300s
# Translog config
index.translog.flush_threshold_ops: 100000
# Paths config
path.data: /data/esData
path.plugins: /usr/share/elasticsearch/plugins
# Network And HTTP
network.bind_host: 10.0.126.203
network.publish_host: 10.0.126.203
transport.tcp.port: 9300
transport.tcp.compress: true
http.port: 9200
# Discovery
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.timeout: 10s
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["10.0.32.3:9300", "10.0.4.37:9300", "10.0.40.159:9300", "10.0.107.116:9300" , "10.0.126.203:9300"]
配置文件位於%ES_HOME%/config/elasticsearch.yml文件中,用Editplus打開它,你便可以進行配置。
所有的配置都可以使用環境變量,例如:
node.rack: ${RACK_ENV_VAR} 表示環境變量中有一個RACK_ENV_VAR變量。
下面列舉一下elasticsearch的可配置項:
1. 集群名稱,默認為elasticsearch:
cluster.name: elasticsearch
2. 節點名稱,es啟動時會自動創建節點名稱,但你也可進行配置:
node.name: "Franz Kafka"
3. 是否作為主節點,每個節點都可以被配置成為主節點,默認值為true:
node.master: true
4. 是否存儲數據,即存儲索引片段,默認值為true:
node.data: true
master和data同時配置會產生一些奇異的效果:
1) 當master為false,而data為true時,會對該節點產生嚴重負荷;
2) 當master為true,而data為false時,該節點作為一個協調者;
3) 當master為false,data也為false時,該節點就變成了一個負載均衡器。
你可以通過連接http://localhost:9200/_cluster/health或者http://localhost:9200/_cluster/nodes,或者使用插件http://github.com/lukas-vlcek/bigdesk或http://mobz.github.com/elasticsearch-head來查看集群狀態。
5. 每個節點都可以定義一些與之關聯的通用屬性,用於后期集群進行碎片分配時的過濾:
node.rack: rack314
6. 默認情況下,多個節點可以在同一個安裝路徑啟動,如果你想讓你的es只啟動一個節點,可以進行如下設置:
node.max_local_storage_nodes: 1
7. 設置一個索引的碎片數量,默認值為5:
index.number_of_shards: 5
8. 設置一個索引可被復制的數量,默認值為1:
index.number_of_replicas: 1
當你想要禁用公布式時,你可以進行如下設置:
index.number_of_shards: 1
index.number_of_replicas: 0
這兩個屬性的設置直接影響集群中索引和搜索操作的執行。假設你有足夠的機器來持有碎片和復制品,那么可以按如下規則設置這兩個值:
1) 擁有更多的碎片可以提升索引執行能力,並允許通過機器分發一個大型的索引;
2) 擁有更多的復制器能夠提升搜索執行能力以及集群能力。
對於一個索引來說,number_of_shards只能設置一次,而number_of_replicas可以使用索引更新設置API在任何時候被增加或者減少。
ElasticSearch關注加載均衡、遷移、從節點聚集結果等等。可以嘗試多種設計來完成這些功能。
可以連接http://localhost:9200/A/_status來檢測索引的狀態。
9. 配置文件所在的位置,即elasticsearch.yml和logging.yml所在的位置:
path.conf: /path/to/conf
10. 分配給當前節點的索引數據所在的位置:
path.data: /path/to/data
可以可選擇的包含一個以上的位置,使得數據在文件級別跨越位置,這樣在創建時就有更多的自由路徑,如:
path.data: /path/to/data1,/path/to/data2
11. 臨時文件位置:
path.work: /path/to/work
12. 日志文件所在位置:
path.logs: /path/to/logs
13. 插件安裝位置:
path.plugins: /path/to/plugins
14. 插件托管位置,若列表中的某一個插件未安裝,則節點無法啟動:
plugin.mandatory: mapper-attachments,lang-groovy
15. JVM開始交換時,ElasticSearch表現並不好:你需要保障JVM不進行交換,可以將bootstrap.mlockall設置為true禁止交換:
bootstrap.mlockall: true
請確保ES_MIN_MEM和ES_MAX_MEM的值是一樣的,並且能夠為ElasticSearch分配足夠的內在,並為系統操作保留足夠的內存。
16. 默認情況下,ElasticSearch使用0.0.0.0地址,並為http傳輸開啟9200-9300端口,為節點到節點的通信開啟9300-9400端口,也可以自行設置IP地址:
network.bind_host: 192.168.0.1
17. publish_host設置其他節點連接此節點的地址,如果不設置的話,則自動獲取,publish_host的地址必須為真實地址:
network.publish_host: 192.168.0.1
18. bind_host和publish_host可以一起設置:
network.host: 192.168.0.1
19. 可以定制該節點與其他節點交互的端口:
transport.tcp.port: 9300
20. 節點間交互時,可以設置是否壓縮,轉為為不壓縮:
transport.tcp.compress: true
21. 可以為Http傳輸監聽定制端口:
http.port: 9200
22. 設置內容的最大長度:
http.max_content_length: 100mb
23. 禁止HTTP
http.enabled: false
24. 網關允許在所有集群重啟后持有集群狀態,集群狀態的變更都會被保存下來,當第一次啟用集群時,可以從網關中讀取到狀態,默認網關類型(也是推薦的)是local:
gateway.type: local
25. 允許在N個節點啟動后恢復過程:
gateway.recover_after_nodes: 1
26. 設置初始化恢復過程的超時時間:
gateway.recover_after_time: 5m
27. 設置該集群中可存在的節點上限:
gateway.expected_nodes: 2
28. 設置一個節點的並發數量,有兩種情況,一種是在初始復蘇過程中:
cluster.routing.allocation.node_initial_primaries_recoveries: 4
另一種是在添加、刪除節點及調整時:
cluster.routing.allocation.node_concurrent_recoveries: 2
29. 設置復蘇時的吞吐量,默認情況下是無限的:
indices.recovery.max_size_per_sec: 0
30. 設置從對等節點恢復片段時打開的流的數量上限:
indices.recovery.concurrent_streams: 5
31. 設置一個集群中主節點的數量,當多於三個節點時,該值可在2-4之間:
discovery.zen.minimum_master_nodes: 1
32. 設置ping其他節點時的超時時間,網絡比較慢時可將該值設大:
discovery.zen.ping.timeout: 3s
http://elasticsearch.org/guide/reference/modules/discovery/zen.html上有更多關於discovery的設置。
33. 禁止當前節點發現多個集群節點,默認值為true:
discovery.zen.ping.multicast.enabled: false
34. 設置新節點被啟動時能夠發現的主節點列表(主要用於不同網段機器連接):
discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]
35.設置是否可以通過正則或者_all刪除或者關閉索引
3、Elasticsearch配置優化
1、skywalking的elasticsearch配置優化
#bulkActions默認1000次請求批量寫入一次改到4000次。
bulkActions: ${SW_STORAGE_ES_BULK_ACTIONS:4000} # Execute the bulk every 1000 requests
#flushInterval每10秒刷新一次堆改為每30秒刷新。
flushInterval: ${SW_STORAGE_ES_FLUSH_INTERVAL:30} # flush the bulk every 10 seconds whatever the number of requests
#concurrentRequests並發請求的數量由2改為4。
concurrentRequests: ${SW_STORAGE_ES_CONCURRENT_REQUESTS:4} # the number of concurrent requests
#metadataQueryMaxSize查詢的最大數量由5000改為8000。
metadataQueryMaxSize: ${SW_STORAGE_ES_QUERY_MAX_SIZE:8000}
2、elasticsearch內置參數優化
index.merge.scheduler.max_thread_count# 索引 merge 最大線程數
index.refresh_interval#index 刷新間隔
index.translog.durability# 這個可以異步寫硬盤,增大寫的速度
index.translog.sync_interval #translog 間隔時間
curl -H "Content-Type: application/json" -u -key elastic:elastic -X PUT 'http://192.168.40.7:9200/_all/_settings?preserve_existing=true' -d '{
"index.merge.scheduler.max_thread_count" : "1",
"index.refresh_interval" : "30s",
"index.translog.durability" : "async",
"index.translog.sync_interval" : "120s"
}'
3、ES堆內存優化(根據服務器的硬件資源配置合理修改參數,一般8C 64G 堆內存最好選用內存的二分之一,也就是32G)
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms32g
-Xmx32g
################################################################
## Expert settings
################################################################
4、ES分片數優化(ES7.2之后,分片數默認為3000,可以對max_shards_per_node進行修改)
curl -k -u elastic:abc123!@# -H "Content-Type:application/json" -X PUT -d '{"transient": {"cluster": {"max_shards_per_node":12000}}}' http://ip地址:9200/_cluster/settings
5、驗證ES高可用配置
http://localhost:9200/_all/_settings