Elasticsearch叢集管理
阿新 • • 發佈:2020-08-18
1.叢集搭建
1.時間同步 yum install -y ntpdate ntpdate time1.aliyun.com 2.安裝Java環境 rz jdk-8u181-linux-x64.rpm rpm -ivh jdk-8u181-linux-x64.rpm 3.安裝ES rz elasticsearch-6.6.0.rpm #下載地址:https://www.elastic.co/downloads/elasticsearch rpm -ivh elasticsearch-6.6.0.rpm # 根據提示繼續操作 systemctl daemon-reload systemctl enable elasticsearch.service systemctl start elasticsearch.service 4.配置ES # 配置檔案 vim /etc/elasticsearch/elasticsearch.yml cluster.name: escluster # 叢集名稱 node.name: es1 /es2 es3# 節點名稱 path.data: /service/es/data path.logs: /service/es/logs bootstrap.memory_lock: true bootstrap.system_call_filter: false http.port: 9200 transport.tcp.port: 9300 transport.tcp.compress: true network.host: 10.0.0.21,127.0.0.1 discovery.zen.minimum_master_nodes: 2 discovery.zen.ping.unicast.hosts: ["10.0.0.20","10.0.0.21","10.0.0.22"] # 叢集ip # 建立資料目錄 mkdir /service/es/{data,logs} -p chown -R elasticsearch.elasticsearch /service/es/ 5.配置啟動檔案中記憶體鎖 vim /usr/lib/systemd/system/elasticsearch.service [Service] LimitMEMLOCK=infinity 5.啟動ES systemctl daemon-reload systemctl start elasticsearch.service
2.叢集的特點
1.叢集中的資料不論在哪一臺機器操作,都可以看到
2.使用外掛連線任意一臺機器,都能看到三個節點
3.資料會自動分配到多個節點
4.如果主分片所在節點掛掉,副本節點的分片會自動升為主分片
5.如果主節點掛了,資料節點會自動提升為主節點
3.注意事項
1.叢集節點的配置,不需要將所有節點的IP都寫入配置檔案,只需要寫本機IP和叢集中任意一臺機器的IP即可 52配置: discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52"] 53配置: discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.53"] 2.叢集選舉節點配置數量,一定是 叢集數量/2+1 discovery.zen.minimum_master_nodes: 2 3.ES預設5個分片1個副本,索引建立以後,分片數量不得修改,副本數可以修改 4.資料分配時分片顏色 1)紫色:資料正在遷移 2)黃色:資料正在複製 5.三個節點時,故障 1)三個節點,沒有副本時,一臺機器都不能壞 2)三個節點,一個副本時,可以壞兩臺,但是隻能一臺一臺壞 3)三個節點,兩個副本時,可以隨便隨時壞兩臺
4.叢集相關命令
1.檢視主節點
GET _cat/master
2.檢視叢集健康狀態
GET _cat/health
3.檢視索引
GET _cat/indices
4.檢視所有節點
GET _cat/nodes
5.檢視分片
GET _cat/shards
二、叢集修改
1.配置ES預設分片數和副本數
設定索引的分片數,預設為5
#index.number_of_shards: 5
設定索引的副本數,預設為1:
#index.number_of_replicas: 1
2.修改指定索引的副本數
PUT /index/_settings { "number_of_replicas": 2 }
3.修改所有索引副本數
PUT _all/_settings
{
"number_of_replicas": 2
}
4.建立索引時指定分片數和副本數
PUT /qiudao
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
#注意:
1.分片數不是越多越好,會佔用資源
2.每個分片都會佔用檔案控制代碼數
3.查詢資料時會根據演算法去指定節點獲取資料,分片數越少,查詢成本越低
5.企業中一般怎麼設定
1.跟開發溝通
2.看一共要幾個節點
2個節點,預設就可以了
3個節點,重要的資料,2副本5分片,不重要的資料,1副本5分片
3.在開始階段, 一個好的方案是根據你的節點數量按照1.5~3倍的原則來建立分片.
例如:如果你有3個節點, 則推薦你建立的分片數最多不超過9(3x3)個.
4.儲存資料量多的可以設定分片多一些,儲存資料量少的,可以少分寫分片
三、叢集的監控
1.監控內容
1.檢視叢集健康狀態
GET _cat/health
2.檢視所有節點
GET _cat/nodes
#兩者有一個產生變化,說明叢集出現故障
2.指令碼監控
[root@db01 ~]# vim es_cluster_status.py
#!/usr/bin/env python
#coding:utf-8
#Author:_DriverZeng_
#Date:2017.02.12
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import subprocess
body = ""
false = "false"
clusterip = "10.0.0.51"
obj = subprocess.Popen(("curl -sXGET http://"+clusterip+":9200/_cluster/health?pretty=true"),shell=True, stdout=subprocess.PIPE)
data = obj.stdout.read()
data1 = eval(data)
status = data1.get("status")
if status == "green":
print "\033[1;32m 叢集執行正常 \033[0m"
elif status == "yellow":
print "\033[1;33m 副本分片丟失 \033[0m"
else:
print "\033[1;31m 主分片丟失 \033[0m"
[root@db01 ~]# python es_cluster_status.py
叢集執行正常
3.監控外掛 x-pack
四、ES優化
1.限制記憶體
1.啟動記憶體最大是32G
2.伺服器一半的記憶體全都給ES
3.設定可以先給小一點,慢慢提高
4.記憶體不足時
1)讓開發刪除資料
2)加節點
3)提高配置
5.關閉swap空間
2.檔案描述符
1.配置檔案描述符
[root@db02 ~]# vim /etc/security/limits.conf
* soft memlock unlimited
* hard memlock unlimited
* soft nofile 131072
* hard nofile 131072
2.普通使用者
[root@db02 ~]# vim /etc/security/limits.d/20-nproc.conf
* soft nproc 65535
root soft nproc unlimited
[root@db02 ~]# vim /etc/security/limits.d/90-nproc.conf
* soft nproc 65535
root soft nproc unlimited
3.語句優化
1.條件查詢時,使用term查詢,減少range的查詢
2.建索引的時候,儘量使用命中率高的詞
五、資料備份與恢復
0.安裝npm環境
#安裝npm(只需要在一個節點安裝即可,如果前端還有nginx做反向代理可以每個節點都裝)
[root@elkstack01 ~]# yum install -y npm
#進入下載head外掛程式碼目錄
[root@elkstack01 src]# cd /usr/local/
#從GitHub上克隆程式碼到本地
[root@elkstack01 local]# git clone git://github.com/mobz/elasticsearch-head.git
#克隆完成後,進入elasticsearch外掛目錄
[root@elkstack01 local]# cd elasticsearch-head/
#清除快取
[root@elkstack01 elasticsearch-head]# npm cache clean -f
#使用npm安裝n模組(不同的專案js指令碼所需的node版本可能不同,所以就需要node版本管理工具)
1.安裝備份工具
[root@db01 ~]# npm install elasticdump -g
2.備份命令
幫助文件:https://github.com/elasticsearch-dump/elasticsearch-dump
1)備份引數
--input: 資料來源
--output: 接收資料的目標
--type: 匯出的資料型別(settings, analyzer, data, mapping, alias, template)
2)備份資料到另一個ES叢集
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://100.10.0.51:9200/my_index \
--type=analyzer
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://100.10.0.51:9200/my_index \
--type=mapping
elasticdump --input=http://10.0.0.51:9200/my_index --output=http://100.10.0.51:9200/my_index --type=data
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://100.10.0.51:9200/my_index \
--type=template
3)備份資料到本地的json檔案
elasticdump \
--input=http://10.0.0.51:9200/student \
--output=/tmp/student_mapping.json \
--type=mapping
elasticdump \
--input=http://10.0.0.51:9200/student \
--output=/tmp/student_data.json \
--type=data
4)匯出檔案打包
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=$ \
| gzip > /data/my_index.json.gz
5)備份指定條件的資料
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=query.json \
--searchBody="{\"query\":{\"term\":{\"username\": \"admin\"}}}"
3.匯入命令
elasticdump \
--input=./student_template.json \
--output=http://10.0.0.51:9200 \
--type=template
elasticdump \
--input=./student_mapping.json \
--output=http://10.0.0.51:9200 \
--type=mapping
elasticdump \
--input=./student_data.json \
--output=http://10.0.0.51:9200 \
--type=data
elasticdump \
--input=./student_analyzer.json \
--output=http://10.0.0.51:9200 \
--type=analyzer
#恢復資料的時候,如果資料已存在,會覆蓋原資料
4.備份指令碼
#!/bin/bash
echo '要備份的機器是:'${1}
index_name='
test
student
linux7
'
for index in `echo $index_name`
do
echo "start input index ${index}"
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_analyzer.json --type=analyzer &> /dev/null
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_data.json --type=data &> /dev/null
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_template.json --type=template &> /dev/null
done
5.匯入指令碼
#!/bin/bash
echo '要匯入的機器是:'${1}
index_name='
test
student
linux7
'
for index in `echo $index_name`
do
echo "start input index ${index}"
elasticdump --input=/data/${index}_alias.json --output=http://${1}:9200/${index} --type=alias &> /dev/null
elasticdump --input=/data/${index}_analyzer.json --output=http://${1}:9200/${index} --type=analyzer &> /dev/null
elasticdump --input=/data/${index}_data.json --output=http://${1}:9200/${index} --type=data &> /dev/null
elasticdump --input=/data/${index}_template.json --output=http://${1}:9200/${index} --type=template &> /dev/null
done
六、中文分詞器 ik
1.插入資料
POST /index/_doc/1
{"content":"美國留給伊拉克的是個爛攤子嗎"}
POST /index/_doc/2
{"content":"公安部:各地校車將享最高路權"}
POST /index/_doc/3
{"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}
POST /index/_doc/4
{"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"}
2.查詢資料
POST /index/_search
{
"query" : { "match" : { "content" : "中國" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
#檢視結果,會獲取到帶中字和國字的資料,我們查詢的詞被分開了,所以我們要使用ik中文分詞器
3.配置中文分詞器
1)安裝外掛
[root@db01 ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.0/elasticsearch-analysis-ik-6.6.0.zip
[root@db02 ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.0/elasticsearch-analysis-ik-6.6.0.zip
[root@db03 ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.0/elasticsearch-analysis-ik-6.6.0.zip
#解壓到es目錄下
[root@db01 ~]# unzip elasticsearch-analysis-ik-6.6.0.zip -d /etc/elasticsearch/
2)建立索引與mapping
PUT /news
curl -XPOST http://localhost:9200/news/text/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
3)編輯我們要定義的詞
[root@redis01 ~]# vim /etc/elasticsearch/analysis-ik/IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴充套件配置</comment>
<!--使用者可以在這裡配置自己的擴充套件字典 -->
<entry key="ext_dict">/etc/elasticsearch/analysis-ik/my.dic</entry>
[root@redis01 ~]# vim /etc/elasticsearch/analysis-ik/my.dic
中國
[root@redis01 ~]# chown -R elasticsearch.elasticsearch /etc/elasticsearch/analysis-ik/my.dic
4)重新插入資料
POST /news/text/1
{"content":"美國留給伊拉克的是個爛攤子嗎"}
POST /news/text/2
{"content":"公安部:各地校車將享最高路權"}
POST /news/text/3
{"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}
POST /news/text/4
{"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"}
5)重新插入資料
POST /news/_search
{
"query" : { "match" : { "content" : "中國" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}