寫在前邊
昨天晚上就已經完成這篇博客了,就是在測試這塊是否正常跑起來,晚上沒搞完,上班前把電腦關機帶着,結果沒保存!基本上昨天寫的東西都丟了,好在博客園的圖片url還在。
為了讓大家都輕松些,我輕松寫,你輕松看。打算把文章的篇幅縮小,拆分成多個部分,這樣更新頻率會提高,寫起來看起來也不會那么累,也不會再出現一次性丟那么多稿的問題……
本文記述Elasticsearch集群部分,下邊會有說明具體的結構
部署架構
整體圖
本文部分結構圖
node 1~3為集群的數據節點,同時競爭master,tribe-node為部落節點,負責Logstash與Kibana的連接
好處是無需指明master節點,不用多啟動一個只負責協調的節點,減少資源浪費。
環境准備
- GNU/Debian Stretch 9.9 linux-4.19
- elasticsearch-7.1.1-linux-x86_64.tar.gz
本文為了模擬,使用Docker的centos7,文中不會出現Docker操作部分,與正常主機無異
開始搭建
1.root權限編輯/etc/security/limits.conf sudo vim /etc/security/limits.conf
添加如下內容:
* soft memlock unlimited
* hard memlock unlimited
其中
*
可替換為啟動es的linux用戶名
保存退出. 生效需要重啟
2.[可選] 禁用swap分區 # echo "vm.swappiness=1" >> /etc/sysctl.conf
,配置性能大大提高
3.重啟系統
不重啟有些配置無法生效,啟動es后報錯依舊
4.為各主機添加用戶和組
sudo groupadd elasticsearch #添加elasticsearch組
sudo usermod -aG elasticsearch 用戶名 #添加elasticsearch用戶
5.解壓elasticsearch-7.1.1-linux-x86_64.tar.gz
,復制到各主機/home/elasticsearch
下
6.分別為每個主機的/home/elasticsearch/elasticsearch-7.1.1/config/elasticsearch.yml
內容最后追加配置
es-node-1
# ======================== Elasticsearch Configuration =========================
cluster.name: es-cluster
node.name: node-1
node.attr.rack: r1
bootstrap.memory_lock: true
http.port: 9200
network.host: 172.17.0.2
transport.tcp.port: 9300
discovery.seed_hosts: ["172.17.0.3:9300","172.17.0.4:9300","172.17.0.5:9300"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
gateway.recover_after_nodes: 2
es-node-2
# ======================== Elasticsearch Configuration =========================
cluster.name: es-cluster
node.name: node-2
node.attr.rack: r1
bootstrap.memory_lock: true
http.port: 9200
network.host: 172.17.0.3
transport.tcp.port: 9300
discovery.seed_hosts: ["172.17.0.2:9300","172.17.0.4:9300","172.17.0.5:9300"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
gateway.recover_after_nodes: 2
es-node-3
# ======================== Elasticsearch Configuration =========================
cluster.name: es-cluster
node.name: node-3
node.attr.rack: r1
bootstrap.memory_lock: true
http.port: 9200
network.host: 172.17.0.4
transport.tcp.port: 9300
discovery.seed_hosts: ["172.17.0.3:9300","172.17.0.2:9300","172.17.0.5:9300"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
gateway.recover_after_nodes: 2
es-tribe-node
# ======================== Elasticsearch Configuration =========================
cluster.name: es-cluster
node.name: tribe-node
node.master: false
node.data: false
node.attr.rack: r1
bootstrap.memory_lock: true
http.port: 9200
network.host: 172.17.0.5
transport.tcp.port: 9300
discovery.seed_hosts: ["172.17.0.3:9300","172.17.0.4:9300","172.17.0.2:9300"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]
gateway.recover_after_nodes: 2
各參數說明放到文末,請自行參考
7.使用命令啟動各節點ES_JAVA_OPTS="-Xms512m -Xmx512m" bin/elasticsearch
注意:
- 這里只能用非root用戶,即文章最開始部分的創建的賬號
- 本命令相對es的解壓目錄的路徑
- JVM參數堆大小可自行調節
查看效果
這里使用瀏覽器查看
此圖中的tribe-node節點不是mdi,是我之前忘加
node.data: false
的圖,現在是i
可以看出node-3
是master節點,最近發現了個好用的elasticsearch查看工具cerebro
使用cerebro查看 cerebro github
圖片看不清可以右鍵新標簽頁打開看大圖
點nodes,查看各節點狀態
還可以通過more來修改集群設置,功能好強大
elasticsearch.yml參數配置的解釋
cluster.name: es-cluster #指定es集群名
node.name: xxxx #指定當前es節點名
node.data: false #非數據節點
node.master: false #非master節點
node.attr.rack: r1 #自定義的屬性,這是官方文檔中自帶的
bootstrap.memory_lock: true #開啟啟動es時鎖定內存
network.host: 172.17.0.5 #當前節點的ip地址
http.port: 9200 #設置當前節點占用的端口號,默認9200
discovery.seed_hosts: ["172.17.0.3:9300","172.17.0.4:9300","172.17.0.2:9300"] #啟動當前es節點時會去這個ip列表中去發現其他節點,此處不需配置自己節點的ip,這里支持ip和ip:port形式,不加端口號默認使用ip:9300去發現節點
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"] #可作為master節點初始的節點名稱,tribe-node不在此列
gateway.recover_after_nodes: 2 #設置集群中N個節點啟動時進行數據恢復,默認為1。可選
path.data: /path/to/path #數據保存目錄
path.logs: /path/to/path #日志保存目錄
transport.tcp.port: 9300 #設置集群節點發現的端口
這里的
discovery.seed_hosts
在之前幾個版本中叫discovery.zen.ping.unicast.hosts
配置我發現個比較全的,只是有些現在已經不用了,還是很有借鑒價值, elasticsearch配置文件詳解
遺留問題
- 最后就是本次測試的時候,沒有考慮腦裂問題,如有需要請自行添加修改,比如用tribe-node當master,不存數據;又比如加一個
node.master: true
的節點,修改cluster.initial_master_nodes
只有這一個master節點 - 文中為了簡單,沒有的把數據存儲目錄掛載出去,生產環境請勿必掛載出去
本文系原創文章,禁止轉載