Elastic Certified Engineer複習記錄-集羣配置-題目篇

前言

對應考綱裏的
Installation and Configuration
Configure the nodes of a cluster to satisfy a given set of requirements

Secure a cluster using Elasticsearch Security

集羣組建

這裏最基本的就是網段中的節點能通過什麼方式發現彼此,認爲大家是一個集羣的,還不會出現腦裂的問題。

1. 集羣的發現和單一節點的配置

Deploy the cluster `eoc-01-cluster`, so that it satisfies the following requirements:
(i)   has three nodes, named `node1`, `node2`, and `node3`,
(ii)  all nodes are eligible master nodes

題目裏說我們需要部署一個叫eoc-01-cluster的集羣,那我們只需要在$ES_HOME/config/elasticsearch.yml裏填上cluster.name: eoc-01-cluster就好。因爲在同一個局域網中,ES的節點們會嘗試用cluster name來進行第一步的彼此發現。

然後有兩條要求:
第一個是需要有仨節點,分別叫node1, node2, and node3,我們就需要在每個節點的elasticsearch.yml裏填上cluster.name之外,給他們每個人一個名字,node.name: node1,這個節點名稱並沒有強制要求唯一,但是爲了後續的使用和運維的方便,我們在真正使用的時候還是建議每個節點給出一個好用的名字,比如:${產線名稱}-${產品名稱}-${集羣名稱}-${環境}-${IP地址中的最後一位} =》 fin-earning-text-prod-192之類的。

第二個是所有節點都有master資格,那我們只需要再添一行node.master: true就完成了。一般情況,我們在配置集羣節點身份的時候需要考慮的是node.masternode.datanode.ingest這仨屬性,分別代表了是否有資格成爲master節點、data節點和ingest節點。

那麼在這一題中,我們的配置文件如下(以node1爲例,node2、node3類似,下同):

cluster.name: eoc-01-cluster
node.name: node1
node.master: true

2. 地址綁定、端口監聽及節點彼此發現的配置

Bind `node1` to the IP address “151.101.2.217” and port “9201”
Bind `node2` to the IP address “151.101.2.218” and port “9202”
Bind `node3` to the IP address “151.101.2.219” and port “9203”
Configure the cluster discovery module of `node2` and `node3` so as to use `node1` as seed host

題目中說我們需要把node1綁定IP地址151.101.2.217,以及端口9201,這就用到了配置中的http.hosthttp.port兩個配置了,他們分別代表了監聽/綁定的地址和端口信息。在實際使用中,特別是在單節點多實例的使用中,這兩個屬性十分常見。

那麼在這一題中,我們的配置文件如下:

http.host: 151.101.2.217
http:port: 9201

3. 防止腦裂,設置初始推薦master節點

Configure the nodes to avoid the split brain scenario

在集羣啓動過程中,我們無法保證所有節點都同時啓動,所以當出現滿足集羣啓動但是存在偶數個master節點的候選節點在進行選舉的過程的時候,很容易出現腦裂的問題,這時ES通過initial_master_nodes這個參數來指定初始化master節點,以避免這種情況出現。

那麼在這一題中,我們的配置文件如下:

initial_master_nodes: ["node1"]

4. 節點身份設置

Configure `node1` to be a data node but not an ingest node
Configure `node2` and `node3` to be both an ingest and data node

在1.裏面我們曾經對節點的master資格進行過設置,在這一題中我們需要對他們重新進行一些定義。

這題分兩部分,第一部分是設置node1作爲一個data node,但是不能是ingest node,那我們就需要在node1的配置文件中指定三個屬性(因爲他是data node的話就不能是master node了),node.master: falsenode.data: truenode.ingest: false

第二部分是設置node2和node3可以同時作爲ingest和data node,一樣的,他們也不能成爲master node,我們就需要將上面三個屬性設置成:node.master: falsenode.data: truenode.ingest: true

那麼這題中,我們的配置文件就會成這樣:
node1:

node.master: false 
node.data: true 
node.ingest: false 

node2:

node.master: false 
node.data: true 
node.ingest: false 

5. 關閉系統交換區

Configure `node1` to disallow swapping on its host

對於承載ES節點的無論物理機還是虛擬機,他們都默認會開啓交換區,因爲他們都會儘可能完全使用文件系統緩存,並嘗試把不用的應用內存換出去,但是這樣一來,有可能會影響一部分JVM的堆棧區甚至導致它的運行頁被換到硬盤上去。因此,這種內存交換可能會影響到ES的響應性能,比如GC的過程變得很長(STW的時間也會相應變長)所帶來的節點響應慢,集羣狀態上報超時以至於節點被標記爲離線,在一個分佈式的系統中,這樣的節點可能會被標記爲不可用或進入重啓流程等。

#官網資料的描述如下:

Most operating systems try to use as much memory as possible for file system caches and eagerly swap out unused application memory. This can result in parts of the JVM heap or even its executable pages being swapped out to disk.

Swapping is very bad for performance, for node stability, and should be avoided at all costs. It can cause garbage collections to last for minutes instead of milliseconds and can cause nodes to respond slowly or even to disconnect from the cluster. In a resilient distributed system, it’s more effective to let the operating system kill the node.

在這題中,明確要求在node1中關閉交換區,所以我們需要在命令行中執行以下命令:sudo swapoff -a

6. 配置JVM參數

Configure the JVM settings of each node so that it uses a minimum and maximum of 8 GB for the heap

我們知道,ES是一個運行在JVM上的Java項目,所以它的調優中很重要的一環就是JVM的調優。

所以這一題中,我們需要在$ES_HOME/config/jvm.options設置XmxXms這倆參數:

-Xms8g
-Xmx8g

我在文章 集羣配置 裏提到過兩個JVM參數的配置建議、理由等,這裏不再贅述。

7.配置log參數

Configure the logging settings of each node so that
	(i)  the logs directory is not the default one,
	(ii) the log level for transport-related events is "debug"

ES的log用的是log4j的,所以當我們遇到需要自定義一些日誌輸出的時候,只需要修改$ES_HOME/config/log4j2.properties裏相關的內容就好。

在這一題中,首先提到了讓logs目錄不在默認位置,那麼我們首先想到的是之前elasticsearch.yml文件裏的path.logs,那麼這裏我們只需要將這個值的註釋放開,然後在後面填上需要的路徑就好,但是要注意,這個路徑需要存在而且啓動ES實例的用戶要有寫入權限,否則會報錯。

題目第二部分說到,對於transport相關行爲的日誌設置成debug level的。這裏其實會有很多種修改方式:

  1. 在我們修改elasticsearch.yml的時候,可以直接在後面加一行logger.org.elasticsearch.transport: debug,只要相關的日誌key是正確的,那麼在節點生效的時候就可以直接將這個配置進行加載和生效。
  2. 修改剛纔提到的log4j2.properties,添加transport相關的屬性:
logger.transport.name = org.elasticsearch.transport
logger.transport.level = debug
  1. 在啓動ES實例的命令中通過-E ${params}的方式將這個配置傳進去,比如:
bin/elasticsearch -E logger.org.elasticsearch.transport=debug
  1. 通過集羣配置接口_cluster/settings修改集羣配置,比如:
PUT /_cluster/settings
{
  "transient": {
    "logger.org.elasticsearch.transport": "debug"
  }
}

這裏需要highlight一下的是:

  1. 我們一般使用的時候,推薦修改log4j2.properties的方式,其次是修改elasticsearch.yml的方式,因爲這樣一來在我們下次的節點重啓時,這些日誌的配置也能生效,而且當有其他同學需要參與相關配置的修改的時候,可以很方便的找到我們當時的配置。
  2. 其次是通過修改集羣配置的方式進行修改,雖然這個修改可以設置成永久的(persistent),但是沒有相關的文檔、知識的傳承,可能很難保證以後的使用中能知道這個配置的存在。
  3. 最不推薦的是通過啓動參數的方式進行修改,因爲如果不是單節點調試的情況,我們可能會把這個參數寫在啓動腳本里,可能對大多數人來說根本不可能想到這種額外參數的存在,因爲這種方式十分冷門,甚至說幾乎沒人知道和使用。

8.禁止通過模糊匹配刪除索引

Configure the nodes so as to disable the possibility to delete indices using wildcards

我們知道,ES的各種針對索引名稱的命令基本上都支持包括[?]、[*]等通配符進行模糊匹配的方式進行指定,對於刪除索引的接口也一樣,但是對於刪除索引的操作,ES的接口包括Kibana裏面都是可以直接進行刪除不需要確認的,而且,索引的刪除操作基本上可以說是不可逆的。那麼這樣一來就存在如果通配符編寫失誤造成某些索引的誤刪除的風險。
這題裏提到,希望我們通過配置將模糊匹配索引名稱進行刪除的功能禁用掉。那麼就需要用到ES中關於索引刪除的一個配置action.destructive_requires_name了,這個配置字面意思是對於破壞性的操作(比如刪除索引),需要指定名字。那麼還是一樣,我們有幾種方式可以禁止:

  1. 修改elasticsearch.yml文件,在裏面加入action.destructive_requires_name: true
  2. 通過集羣配置接口_cluster/settings修改集羣配置,比如:
PUT /_cluster/settings
{
  "transient": {
    "action.destructive_requires_name": "false"
  }
}

這裏也是,我們會首先推薦第一種方式,其次是第二種。

9.總結

在本文中,我們以一組複習大綱爲主線,瞭解了ECE考試中集羣配置方面的基礎操作,包括了:

  1. 集羣中節點的啓動
  2. 集羣組網
  3. 節點身份設置
  4. 防止腦裂
  5. 交換區禁止
  6. 配置JVM參數
  7. 配置log參數
  8. 禁止通過模糊匹配方式刪除索引

最終我們的配置文件們可能成爲的樣子:
elasticsearch.yml:

# 集羣組網
cluster.name: eoc-01-cluster
#節點身份設置
node.name: node1
node.master: true
#節點ip、端口綁定
http.host: 151.101.2.217
http:port: 9201
#防止腦裂
initial_master_nodes: ["node1"]
#自定義日誌輸出
path.logs: /usr/local/logs/
logger.org.elasticsearch.transport: debug
#禁止模糊匹配刪除索引
action.destructive_requires_name: true

jvm.options

-Xms8g
-Xmx8g

大家可以對照上面的題目自己設置和練習一下。

10.參考文獻

節點配置
=》 [7.2] » Modules » Node
禁用交換區
=》[7.2] » Set up Elasticsearch » Important System Configuration » Disable swapping
日誌配置
=》[7.2] » Set up Elasticsearch » Configuring Elasticsearch » Logging configuration

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章