Elasticsearch高可用集羣搭建部署調優生產可用

Elasticsearch基本概念:
1) elasticsearch 介紹
Elasticsearch是一個基於Lucene的搜索服務器。它提供了一個分佈式多用戶能力的全文搜索引擎,基於 RESTful web 接口。Elasticsearch 是用 Java 開發的,並作爲 Apache 許可條款下的開放源碼發佈,是第二流行的企業搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。

Elasticsearch 的基礎核心概念:
接近實時(NRT)
elasticsearch 是一個接近實時的搜索平臺,這意味着,從索引一個文檔直到這個文檔能夠被搜索到有一個輕微的延遲(通常是 1 秒)

集羣(cluster)
一個集羣就是由一個或多個節點組織在一起,它們共同持有你整個的數據,並一起提供索引和搜索功能。其中一個節點爲主節點,這個主節點是可以通過選舉產生的,並提供跨節點的聯合索引和搜索的功能。集羣有一個唯一性標示的名字,默認是 elasticsearch,集羣名字很重要,每個節點是基於集羣名字加入到其集羣中的。因此,確保在不同環境中使用不同的集羣名字。一個集羣可以只有一個節點。強烈建議在配置 elasticsearch 時,配置成集羣模式。

節點(node)
節點就是一臺單一的服務器,是集羣的一部分,存儲數據並參與集羣的索引和搜索功能。像集羣一樣,節點也是通過名字來標識,默認是在節點啓動時隨機分配的字符名。當然,你可以自己定義。該名字也很重要,在集羣中用於識別服務器對應的節點。
節點可以通過指定集羣名字來加入到集羣中。默認情況,每個節點被設置成加入到 elasticsearch集羣。如果啓動了多個節點,假設能自動發現對方,他們將會自動組建一個名爲 elasticsearch 的集羣。

索引(index)
一個索引就是一個擁有幾分相似特徵的文檔的集合。比如說,你可以有一個客戶數據的索引,另一個產品目錄的索引,還有一個訂單數據的索引。一個索引由一個名字來標識(必須全部是小寫字母的),並且當我們要對對應於這個索引中的文檔進行索引、搜索、更新和刪除的時候,都要使用到這個名字。在一個集羣中,如果你想,可以定義任意多的索引。
索引相對於關係型數據庫的庫。
類型(type)
在一個索引中,你可以定義一種或多種類型。一個類型是你的索引的一個邏輯上的分類/分區,其語義完全由你來定。通常,會爲具有一組共同字段的文檔定義一個類型。比如說,我們假設你運營一個博客平臺並且將你所有的數據存儲到一個索引中。在這個索引中,你可以爲用戶數據定義一個類型,爲博客數據定義另一個類型,當然,也可以爲評論數據定義另一個類型。
類型相對於關係型數據庫的表。

文檔(document)
一個文檔是一個可被索引的基礎信息單元。比如,你可以擁有某一個客戶的文檔,某一個產品的一個文檔,當然,也可以擁有某個訂單的一個文檔。文檔以 JSON(Javascript Object Notation)格式來表示,而 JSON 是一個到處存在的互聯網數據交互格式。 在一個 index/type 裏面,只要你想,你可以存儲任意多的文檔。注意,雖然一個文檔在物理上位於一個索引中,實際上一個文檔必須在一個索引內被索引和分配一個類型。
文檔相對於關係型數據庫的列。

分片和副本(shards & replicas)
在實際情況下,索引存儲的數據可能超過單個節點的硬件限制。如一個 10 億文檔需 1TB 空間可能不適合存儲在單個節點的磁盤上,或者從單個節點搜索請求太慢了。爲了解決這個問題,elasticsearch 提供將索引分成多個分片的功能。當在創建索引時,可以定義想要分片的數量。每一個分片就是一個全功能的獨立的索引,可以位於集羣中任何節點上。
分片的兩個最主要原因:
a. 水平分割擴展,增大存儲量
b. 分佈式並行跨分片操作,提高性能和吞吐量
分佈式分片的機制和搜索請求的文檔如何彙總完全是有 elasticsearch 控制的,這些對用戶而言是透明的。

網絡問題等等其它問題可以在任何時候不期而至,爲了健壯性,強烈建議要有一個故障切換機制,無論何種故障以防止分片或者節點不可用。 爲此,elasticsearch 讓我們將索引分片複製一份或多份,稱之爲分片副本或副本。
副本也有兩個最主要原因:
a. 高可用性,以應對分片或者節點故障。出於這個原因,分片副本要在不同的節點上。
b. 提供性能,增大吞吐量,搜索可以並行在所有副本上執行。
總之,每個索引可以被分成多個分片。一個索引也可以被複制 0 次(意思是沒有複製)或多次。

總之一旦複製了,每個索引就有了主分片(作爲複製源的原來的分片)和複製分片(主分片的拷貝)之別。分片和副本的數量可以在索引創建的時候指定。在索引創建之後,你可以在任何時候動態地改變副本的數量,但是你事後不能改變分片的數量。

默認情況下,Elasticsearch 中的每個索引被分片 5 個主分片和 1 個副本,這意味着,如果你的集羣中至少有兩個節點,你的索引將會有 5 個主分片和另外 5 個副本分片(1 個完全拷貝),這樣的話每個索引總共就有 10 個分片

2)Elasticsearch集羣部署
搭建Elasticsearch集羣要注意集羣中的Master數量因是"候選master/2+1".
架構圖:
在這裏插入圖片描述
2.1 生產環境系統配置,其餘節點配置相同。
安裝基本軟件包
yum install net-tools wget lrzsz lsof epel-release vim bash-completion make cmake gcc gcc-c++ screen -y
安裝JAVA環境
JDK安裝包:https://pan.baidu.com/s/1T6QpR7tpwiAGKLGpAKSPDw
在這裏插入圖片描述
運行”java -version“命令檢查java環境是否安裝成功。

設置JVM堆大小
Elasticsearch將 通過Xms(最小堆大小)和Xmx(最大堆大小)設置分配jvm.options中指定的整個堆。

cat >>/etc/profile <<HERE
export ES_MIN_MEM=1g
export ES_MAX_MEM=1g
HERE
source /etc/profile

禁止交換
swapoff -a
永久禁止修改/etc/fstab文件。
註釋掉有"swap"的所有行
增加文件描述符

		vi /etc/security/limits.conf 
		* soft nofile 65536
		* hard nofile 65536
		* soft nproc 2048
		* hard nproc 4096

確保有足夠的虛擬內存

		vi /etc/sysctl.conf 
		vm.max_map_count=655360
		執行命令:
		sysctl -p
		永久修改
		編輯/etc/sysctl.conf
		添加vm.max_map_count=655360,然後重啓
		運行sysctl vm.max_map_count

確保足夠的線程
保證Elasticsearch用戶可以創建的線程數至少爲4096。
可以通過ulimit -u 4096在啓動Elasticsearch之前設置爲root或設置nproc爲4096 in來完成/etc/security/limits.conf。

2.2 名稱解析
編輯/etc/hosts文件.

	vim /etc/hosts
		127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
		::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
		192.168.2.17    ES1
		192.168.3.25    ES2
		192.168.2.21    ES3
		192.168.2.15    Management
		192.168.2.16    es5
		192.168.2.22    es6

scp拷貝到其餘ES上去。

2.3 創建elasticsearch賬戶

    groupadd elasticsearch
	useradd -g elasticsearch elasticsearch -s /bin/bash

2.4 下載源碼包

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch`-6.0.1.tar.gz

2.5 解壓並賦權

	tar zxf elasticsearch-6.0.1.tar.gz -C /usr/local
	chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-6.0.1

編輯 /usr/local/elasticsearch-6.0.1/config/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: my-application
#集羣名稱,若在相同一個集羣內,集羣名稱需一致
cluster.name: my-es-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#節點名稱,不同的節點修改爲自己的hostname主機名
node.name: Management
#
# 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: /path/to/data
#數據目錄存放位置
path.data: /data/es_data
#
# Path to log files:
#
#path.logs: /path/to/logs
#日誌存放路徑
path.logs: /data/es_logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#不在啓動的時候鎖定內存
bootstrap.memory_lock: 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: 192.168.0.1
#提供服務綁定的IP地址,0.0.0.0代表所有地址
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: ["host1", "host2"]
#集羣發現通過單播實現
discovery.zen.ping.unicast.hosts: ["ES1", "ES2", 'ES3', "Management", "es5", "es6"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#設置了最少有多少個備選主節點參加選舉,同時也設置了一個主節點需要控制最少多少個備選主節點才能繼續保持主節點身份。
#如果控制的備選主節點少於discovery.zen.minimum_master_nodes個,那麼當前主節點下臺,重新開始選舉。
discovery.zen.minimum_master_nodes: 3
#
# 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
#開啓跨域訪問支持,默認爲 false
http.cors.enabled: true
#跨域訪問允許的域名地址
http.cors.allow-origin: "*"
#該節點沒有資格成爲候選master
node.master: false
#該節點允許存儲數據
node.data: true

註釋:
http.port
//設置客戶端的http監聽端口,若是存在多臺集羣機器,可以不去做設置,默認值爲9200,若是在一臺機器上啓動了多個es,則需要修改此端口號,防止端口衝突和被佔用。
transport.tcp.port
//設置節點與節點之間相互通信的客戶端端口,默認值爲9300,同上,同一臺機器需要修改此端口號
discovery.zen.ping.timeout
//設置發現其他節點並得到響應的等待時間,在網絡慢的情況下,設置更高的等待值。

創建數據存放路徑並授權

mkdir -p /data/es_data
mkdir /data/es_logs
chown -R elasticsearch:elasticsearch /data

創建新的終端會話並切換用戶

	screen -S es
	su - elasticsearch
	cd /usr/local/elasticsearch-6.0.1/bin/
	./elasticsearch
快捷鍵:Alt+A+D後臺運行。

查看端口:

[root@es6 opt]# netstat -anput | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      53278/java          
[root@es6 opt]# netstat -anput | grep 9300
tcp6       0      0 :::9300                 :::*                    LISTEN      53278/java 

WEB頁面查看節點情況:
通過瀏覽器中輸入 http://IP:9200/_cluster/health?pretty 查看集羣的健康情況,可以看到 status 爲 green 綠色。
在這裏插入圖片描述
通過瀏覽器中輸入 http://IP:9200/_cluster/state?pretty 查看集羣的狀態信息
在這裏插入圖片描述
以上方式查看集羣狀態都不是很直觀,可以安裝 elasticsearch-head 插件,以便方便管理集羣。

安裝 elasticsearch-head 插件,其餘節點相同,這裏我只在master節點上安裝了。
注意:需要翻牆
安裝基本命令:

yum install git npm
下載elasticsearch-head
安裝 docker 鏡像或者通過 github 下載 elasticsearch-head 項目都是可以的,1 或 者 2 兩種方式選擇一種安裝使用即可
1. 使用 docker 的集成好的 elasticsearch-head
	 # docker run -p 9100:9100 mobz/elasticsearch-head:5
	 docker 容器下載成功並啓動以後,運行瀏覽器打開 http://localhost:9100/
2.git安裝
		git clone git://github.com/mobz/elasticsearch-head.git
		cd elasticsearch-head/
		npm install
		npm run start
檢查端口是否起來
	 netstat -antp |grep 9100

瀏覽器訪問測試是否正常
http://IP:9100/
在這裏插入圖片描述
在這裏插入圖片描述
插入測試數據
[root@ES1 ~]# curl -XPUT ‘192.168.2.21:9200/index-demo/test/1?pretty&pretty’ -H ‘Content-Type: application/json’ -d ‘{ “user”: “zhangsan”,“mesg”:“hello world” }’
{
“_index” : “index-demo”,
“_type” : “test”,
“_id” : “1”,
“_version” : 1,
“result” : “created”,
“_shards” : {
“total” : 2,
“successful” : 2,
“failed” : 0
},
“_seq_no” : 0,
“_primary_term” : 1
}
在這裏插入圖片描述
可以明確的看到索引存儲在了數據節點上。

使用postman Post elasticsearch數據
在這裏插入圖片描述
【總結】
Elasticsearch節點角色類型
在生產中,高併發的場景容易出現腦裂問題,而在Elasticsearch集羣亦是如此,Elasticsearch集羣中的每個節點都可以成爲主節點,存儲數據,提供查詢服務
而這些事由兩個屬性參數控制的也就是node,master和node.data
1:這兩種組合表示這個節點有資格成爲主節點,又要存儲數據,如果該節點真的成爲主節點的話,那麼自己還要存儲數據,壓力是相當大的,生產不建議這樣配置
node.master: true
node.data:true
2:這種組合是數據存儲節點,既不參與選舉,也不會成爲成爲主節點,因爲沒有成爲主節點的資格,只能是數據存儲節點
node.master:false
node.date: true
3:擁有參與選舉主節點的資格,不會存儲節點數據,該節點我們稱之爲master主節點
node.master: true
node.data: false
4:該節點爲客戶端節點,數據存儲和主節點資格兩項都沒有,只能處理客戶端請求,對海量的請求進行負載均衡
node.master: false
node.date: false

在一個生產集羣中我們可以對這些節點的職責進行劃分。
建議集羣中設置3臺以上的節點作爲master節點【node.master: true node.data: false】這些節點只負責成爲主節點,維護整個集羣的狀態。
再根據數據量設置一批data節點【node.master: false node.data: true】
這些節點只負責存儲數據,後期提供建立索引和查詢索引的服務,這樣的話如果用戶請求比較頻繁,這些節點的壓力也會比較大所以在集羣中建議再設置一批client節點【node.master: false node.data: true】這些節點只負責處理用戶請求,實現請求轉發,負載均衡等功能。通過client node導入可以實現數據分片負載。

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