ES系列十六、集群配置和维护管理
一、修改配置文件
1.節(jié)點配置
1.vim elasticsearch.yml
# ======================== Elasticsearch Configuration ========================= # # NOTE: Elasticsearch comes with reasonable defaults for most settings. # Before you set out to tweak and tune the configuration, make sure you # understand what are you trying to accomplish and the consequences. # # The primary way of configuring a node is via this file. This template lists # the most important settings you may want to configure for a production cluster. # # Please consult the documentation for further information on configuration options: # https://www.elastic.co/guide/en/elasticsearch/reference/index.html # # ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # 集群名稱 cluster.name: es-test # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # 節(jié)點名稱 node.name: node-1 # # Add custom attributes to the node: # node.attr.rack: r1 # # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): # path.data: /home/esdata/data # # Path to log files: # path.logs: /home/esdata/log # # ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # bootstrap.memory_lock: false 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. # # ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # network.host: 0.0.0.0 # # Set a custom port for HTTP: # http.port: 9200 # # For more information, consult the network module documentation. # # --------------------------------- Discovery ---------------------------------- # # Pass an initial list of hosts to perform discovery when new node is started: # The default list of hosts is ["127.0.0.1", "[::1]"] # 節(jié)點主機 discovery.zen.ping.unicast.hosts: ["192.168.1.102","192.168.1.103","192.168.1.104"] # # Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1): # 主節(jié)點數(shù)量 discovery.zen.minimum_master_nodes: 1 # # For more information, consult the zen discovery module documentation. # # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: # 節(jié)點發(fā)現(xiàn)多少開始恢復(fù) gateway.recover_after_nodes: 3 # # For more information, consult the gateway module documentation. # # ---------------------------------- Various ----------------------------------- # # Require explicit names when deleting indices: # action.destructive_requires_name: true http.cors.enabled: true http.cors.allow-origin: "*"
其他節(jié)點文件除了節(jié)點名稱不一樣,其他都一樣。
2.驗證
1.啟動三臺機器ES和head
2.訪問head地址查看
集群一共兩個三個節(jié)點,test有5分片,每個分片兩個副本,停掉節(jié)點二,集群副本0,2,4不見了
二、集群規(guī)劃
搭建一個集群我們需要考慮如下幾個問題:
1. 我們需要多大規(guī)模的集群?
2. 集群中的節(jié)點角色如何分配?
3. 如何避免腦裂問題?
4. 索引應(yīng)該設(shè)置多少個分片?
5. 分片應(yīng)該設(shè)置幾個副本?
下面我們就來分析和回答這幾個問題
1、我們需要多大規(guī)模的集群?
需要從以下兩個方面考慮:
1.1 當前的數(shù)據(jù)量有多大?數(shù)據(jù)增長情況如何?
1.2 你的機器配置如何?cpu、多大內(nèi)存、多大硬盤容量?
推算的依據(jù):
ES JVM heap 最大可以設(shè)置32G 。
30G heap 大概能處理的數(shù)據(jù)量 10 T。如果內(nèi)存很大如128G,可在一臺機器上運行多個ES節(jié)點實例。
備注:集群規(guī)劃滿足當前數(shù)據(jù)規(guī)模+適量增長規(guī)模即可,后續(xù)可按需擴展。
兩類應(yīng)用場景:
A. 用于構(gòu)建業(yè)務(wù)搜索功能模塊,且多是垂直領(lǐng)域的搜索。數(shù)據(jù)量級幾千萬到數(shù)十億級別。一般2-4臺機器的規(guī)模。
B. 用于大規(guī)模數(shù)據(jù)的實時OLAP(聯(lián)機處理分析),經(jīng)典的如ELK Stack,數(shù)據(jù)規(guī)模可能達到千億或更多。幾十到上百節(jié)點的規(guī)模。
2、集群中的節(jié)點角色如何分配?
2.1 節(jié)點角色:
Master
node.master: true 節(jié)點可以作為主節(jié)點
DataNode
node.data: true 默認是數(shù)據(jù)節(jié)點。
Coordinate node 協(xié)調(diào)節(jié)點
如果僅擔任協(xié)調(diào)節(jié)點,將上兩個配置設(shè)為false。
說明:
一個節(jié)點可以充當一個或多個角色,默認三個角色都有
協(xié)調(diào)節(jié)點:一個節(jié)點只作為接收請求、轉(zhuǎn)發(fā)請求到其他節(jié)點、匯總各個節(jié)點返回數(shù)據(jù)等功能的節(jié)點。就叫協(xié)調(diào)節(jié)點
2.2 如何分配:
A. 小規(guī)模集群,不需嚴格區(qū)分。
B. 中大規(guī)模集群(十個以上節(jié)點),應(yīng)考慮單獨的角色充當。特別并發(fā)查詢量大,查詢的合并量大,可以增加獨立的協(xié)調(diào)節(jié)點。角色分開的好處是分工分開,不互影響。如不會因協(xié)調(diào)角色負載過高而影響數(shù)據(jù)節(jié)點的能力。
3、如何避免腦裂問題?
3.1 腦裂問題:
一個集群中只有一個A主節(jié)點,A主節(jié)點因為需要處理的東西太多或者網(wǎng)絡(luò)過于繁忙,從而導(dǎo)致其他從節(jié)點ping不通A主節(jié)點,這樣其他從節(jié)點就會認為A主節(jié)點不可用了,就會重新選出一個新的主節(jié)點B。過了一會A主節(jié)點恢復(fù)正常了,這樣就出現(xiàn)了兩個主節(jié)點,導(dǎo)致一部分數(shù)據(jù)來源于A主節(jié)點,另外一部分數(shù)據(jù)來源于B主節(jié)點,出現(xiàn)數(shù)據(jù)不一致問題,這就是腦裂。
3.2 盡量避免腦裂,需要添加最小數(shù)量的主節(jié)點配置:
discovery.zen.minimum_master_nodes: (有master資格節(jié)點數(shù)/2) + 1
這個參數(shù)控制的是,選舉主節(jié)點時需要看到最少多少個具有master資格的活節(jié)點,才能進行選舉。官方的推薦值是(N/2)+1,其中N是具有master資格的節(jié)點的數(shù)量。
3.3 常用做法(中大規(guī)模集群):
1. Master 和 dataNode 角色分開,配置奇數(shù)個master,如3
2. 單播發(fā)現(xiàn)機制,配置master資格節(jié)點:
discovery.zen.ping.multicast.enabled:false —— 關(guān)閉多播發(fā)現(xiàn)機制,默認是關(guān)閉的
discovery.zen.ping.unicast.hosts:["master1","master2","master3"] —— 配置單播發(fā)現(xiàn)的主節(jié)點ip地址,其他從節(jié)點要加入進來,就得去詢問單播發(fā)現(xiàn)機制里面配置的主節(jié)點我要加入到集群里面了,主節(jié)點同意以后才能加入,然后主節(jié)點再通知集群中的其他節(jié)點有新節(jié)點加入
3. 配置選舉發(fā)現(xiàn)數(shù),及延長ping master的等待時長
discovery.zen.ping_timeout: 30(默認值是3秒)——其他節(jié)點ping主節(jié)點多久時間沒有響應(yīng)就認為主節(jié)點不可用了
discovery.zen.minimum_master_nodes: 2 ——選舉主節(jié)點時需要看到最少多少個具有master資格的活節(jié)點,才能進行選舉
4、索引應(yīng)該設(shè)置多少個分片?
說明:分片數(shù)指定后不可變,除非重索引。
思考:
分片對應(yīng)的存儲實體是什么?
存儲的實體是索引
分片是不是越多越好?
不是
分片多有什么影響?
分片多浪費存儲空間、占用資源、影響性能
4.1 分片過多的影響:
每個分片本質(zhì)上就是一個Lucene索引, 因此會消耗相應(yīng)的文件句柄, 內(nèi)存和CPU資源。
每個搜索請求會調(diào)度到索引的每個分片中. 如果分片分散在不同的節(jié)點倒是問題不太. 但當分片開始競爭相同的硬件資源時, 性能便會逐步下降。
ES使用詞頻統(tǒng)計來計算相關(guān)性. 當然這些統(tǒng)計也會分配到各個分片上. 如果在大量分片上只維護了很少的數(shù)據(jù), 則將導(dǎo)致最終的文檔相關(guān)性較差。
4.2 分片設(shè)置的可參考原則:
ElasticSearch推薦的最大JVM堆空間是30~32G, 所以把你的分片最大容量限制為30GB, 然后再對分片數(shù)量做合理估算. 例如, 你認為你的數(shù)據(jù)能達到200GB, 推薦你最多分配7到8個分片。
在開始階段, 一個好的方案是根據(jù)你的節(jié)點數(shù)量按照1.5~3倍的原則來創(chuàng)建分片. 例如,如果你有3個節(jié)點, 則推薦你創(chuàng)建的分片數(shù)最多不超過9(3x3)個。當性能下降時,增加節(jié)點,ES會平衡分片的放置。
對于基于日期的索引需求, 并且對索引數(shù)據(jù)的搜索場景非常少. 也許這些索引量將達到成百上千, 但每個索引的數(shù)據(jù)量只有1GB甚至更小. 對于這種類似場景, 建議只需要為索引分配1個分片。如日志管理就是一個日期的索引需求,日期索引會很多,但每個索引存放的日志數(shù)據(jù)量就很少。
5、分片應(yīng)該設(shè)置幾個副本?
說明:副本數(shù)是可以隨時調(diào)整的!
思考:
副本的用途是什么?
備份數(shù)據(jù)保證高可用數(shù)據(jù)不丟失,高并發(fā)的時候參與數(shù)據(jù)查詢
針對它的用途,我們該如何設(shè)置它的副本數(shù)?
一般一個分片有1-2個副本即可保證高可用
集群規(guī)模沒變的情況下副本過多會有什么影響?
副本多浪費存儲空間、占用資源、影響性能
5.1 副本設(shè)置基本原則:
為保證高可用,副本數(shù)設(shè)置為2即可。要求集群至少要有3個節(jié)點,來分開存放主分片、副本。
如發(fā)現(xiàn)并發(fā)量大時,查詢性能會下降,可增加副本數(shù),來提升并發(fā)查詢能力。
注意:新增副本時主節(jié)點會自動協(xié)調(diào),然后拷貝數(shù)據(jù)到新增的副本節(jié)點
三、集群管理
1. 監(jiān)控API
http://localhost:9200/_cat
GET /_cat
/_cat/health
/_cat/nodes
/_cat/master
/_cat/indices
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/thread_pool
/_cat/segments
/_cat/segments/{index}
2. x-pack
為集群提供安全防護、監(jiān)控、告警、報告等功能的收費組件;
部分免費:https://www.elastic.co/subscriptions
6.3開始已開源,并并入了elasticsearch核心中。
官網(wǎng)安裝介紹:
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/installing-xpack-es.html
ES安裝參考:
ES系列一、CentOS7安裝ES 6.3.1
ES系列二、CentOS7安裝ES head6.3.1
總結(jié)
以上是生活随笔為你收集整理的ES系列十六、集群配置和维护管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开发者说 | Apollo控制算法之汽车
- 下一篇: chipmunk 物理引擎的基本概念和基