ES系列十6、集羣配置和維護管理

1、修改配置文件

1.節點配置

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:
# 節點名稱
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]"]
# 節點主機
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):
# 主節點數量
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:
# 節點發現多少開始恢復
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: "*"

其餘節點文件除了節點名稱不同,其餘都同樣。html

2.驗證

1.啓動三臺機器ES和head

2.訪問head地址查看

集羣一共兩個三個節點,test有5分片,每一個分片兩個副本,停掉節點二,集羣副本0,2,4不見了node

 

2、集羣規劃

搭建一個集羣咱們須要考慮以下幾個問題:bootstrap

1. 咱們須要多大規模的集羣?vim

2. 集羣中的節點角色如何分配?安全

3. 如何避免腦裂問題?bash

4. 索引應該設置多少個分片?網絡

5. 分片應該設置幾個副本?併發

下面咱們就來分析和回答這幾個問題app

一、咱們須要多大規模的集羣?

須要從如下兩個方面考慮:cors

1.1 當前的數據量有多大?數據增加狀況如何?
1.2 你的機器配置如何?cpu、多大內存、多大硬盤容量?

推算的依據:

ES JVM heap 最大能夠設置32G 。
30G heap 大概能處理的數據量 10 T。若是內存很大如128G,可在一臺機器上運行多個ES節點實例。

備註:集羣規劃知足當前數據規模+適量增加規模便可,後續可按需擴展。

兩類應用場景:

A. 用於構建業務搜索功能模塊,且可能是垂直領域的搜索。數據量級幾千萬到數十億級別。通常2-4臺機器的規模。
B. 用於大規模數據的實時OLAP(聯機處理分析),經典的如ELK Stack,數據規模可能達到千億或更多。幾十到上百節點的規模。

二、集羣中的節點角色如何分配?

2.1 節點角色:

Master
node.master: true 節點能夠做爲主節點
DataNode
node.data: true 默認是數據節點。
Coordinate node 協調節點
若是僅擔任協調節點,將上兩個配置設爲false。

說明:

一個節點能夠充當一個或多個角色,默認三個角色都有

協調節點:一個節點只做爲接收請求、轉發請求到其餘節點、彙總各個節點返回數據等功能的節點。就叫協調節點

2.2 如何分配:

A. 小規模集羣,不需嚴格區分。
B. 中大規模集羣(十個以上節點),應考慮單獨的角色充當。特別併發查詢量大,查詢的合併量大,能夠增長獨立的協調節點。角色分開的好處是分工分開,不互影響。如不會因協調角色負載太高而影響數據節點的能力。

三、如何避免腦裂問題?

3.1 腦裂問題

一個集羣中只有一個A主節點,A主節點由於須要處理的東西太多或者網絡過於繁忙,從而致使其餘從節點ping不通A主節點,這樣其餘從節點就會認爲A主節點不可用了,就會從新選出一個新的主節點B。過了一會A主節點恢復正常了,這樣就出現了兩個主節點,致使一部分數據來源於A主節點,另一部分數據來源於B主節點,出現數據不一致問題,這就是腦裂

3.2 儘可能避免腦裂,須要添加最小數量的主節點配置:

discovery.zen.minimum_master_nodes: (有master資格節點數/2) + 1

這個參數控制的是,選舉主節點時須要看到最少多少個具備master資格的活節點,才能進行選舉。官方的推薦值是(N/2)+1,其中N是具備master資格的節點的數量。

3.3 經常使用作法(中大規模集羣):

1. Master 和 dataNode 角色分開,配置奇數個master,如3
2. 單播發現機制,配置master資格節點:

discovery.zen.ping.multicast.enabled: false —— 關閉多播發現機制,默認是關閉的

discovery.zen.ping.unicast.hosts: ["master1", "master2", "master3"] —— 配置單播發現的主節點ip地址,其餘從節點要加入進來,就得去詢問單播發現機制裏面配置的主節點我要加入到集羣裏面了,主節點贊成之後才能加入,而後主節點再通知集羣中的其餘節點有新節點加入

3. 配置選舉發現數,及延長ping master的等待時長

discovery.zen.ping_timeout: 30(默認值是3秒)——其餘節點ping主節點多久時間沒有響應就認爲主節點不可用了
discovery.zen.minimum_master_nodes: 2 —— 選舉主節點時須要看到最少多少個具備master資格的活節點,才能進行選舉

四、索引應該設置多少個分片?

說明:分片數指定後不可變,除非重索引。

思考:

分片對應的存儲實體是什麼?

  存儲的實體是索引

分片是否是越多越好?

  不是

分片多有什麼影響?

  分片多浪費存儲空間、佔用資源、影響性能

4.1 分片過多的影響:

每一個分片本質上就是一個Lucene索引, 所以會消耗相應的文件句柄, 內存和CPU資源。
每一個搜索請求會調度到索引的每一個分片中. 若是分片分散在不一樣的節點卻是問題不太. 但當分片開始競爭相同的硬件資源時, 性能便會逐步降低。
ES使用詞頻統計來計算相關性. 固然這些統計也會分配到各個分片上. 若是在大量分片上只維護了不多的數據, 則將致使最終的文檔相關性較差。

4.2 分片設置的可參考原則:

ElasticSearch推薦的最大JVM堆空間是30~32G, 因此把你的分片最大容量限制爲30GB, 而後再對分片數量作合理估算. 例如, 你認爲你的數據能達到200GB, 推薦你最多分配7到8個分片。
在開始階段, 一個好的方案是根據你的節點數量按照1.5~3倍的原則來建立分片. 例如,若是你有3個節點, 則推薦你建立的分片數最多不超過9(3x3)個。當性能降低時,增長節點,ES會平衡分片的放置。
對於基於日期的索引需求, 而且對索引數據的搜索場景很是少. 也許這些索引量將達到成百上千, 但每一個索引的數據量只有1GB甚至更小. 對於這種相似場景, 建議只須要爲索引分配1個分片。如日誌管理就是一個日期的索引需求,日期索引會不少,但每一個索引存放的日誌數據量就不多。

五、分片應該設置幾個副本?

說明:副本數是能夠隨時調整的!

思考:

副本的用途是什麼?

  備份數據保證高可用數據不丟失,高併發的時候參與數據查詢
針對它的用途,咱們該如何設置它的副本數?

  通常一個分片有1-2個副本便可保證高可用
集羣規模沒變的狀況下副本過多會有什麼影響?

  副本多浪費存儲空間、佔用資源、影響性能

5.1 副本設置基本原則:

爲保證高可用,副本數設置爲2便可。要求集羣至少要有3個節點,來分開存放主分片、副本。
如發現併發量大時,查詢性能會降低,可增長副本數,來提高併發查詢能力。

注意:新增副本時主節點會自動協調,而後拷貝數據到新增的副本節點

 

3、集羣管理

1. 監控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

爲集羣提供安全防禦、監控、告警、報告等功能的收費組件;
部分免費:https://www.elastic.co/subscriptions
6.3開始已開源,並併入了elasticsearch核心中。

官網安裝介紹:

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/installing-xpack-es.html

 

ES安裝參考:

ES系列1、CentOS7安裝ES 6.3.1

ES系列2、CentOS7安裝ES head6.3.1

相關文章
相關標籤/搜索