Elasticsearch 支持各种语言使用 RESTful API 通过端口 9200 与之进行通信,可以用你习惯的 web 客户端访问 Elasticsearch
可以用三种方式和 Elasticsearch进行交互
- curl 命令和其它浏览器:基于命令行,操作不方便
- 插件:在node节点上安装head,Cerebro 等插件,实现图形操作,查看数据方便
- Kibana:需要java环境并配置,图形操作,显示格式丰富
监控下面两个条件都满足才是正常的状态
- 集群状态为 green
- 所有节点都启动
1、Shell 命令
curl -sXGET http://elk服务器:9200/_cluster/health?pretty=true
获取到的是一个json格式的返回值,那就可以通过python对其中的信息进行分析,例如对status进行分析,如果等于green(绿色)就是运行在正常,等于yellow(黄色)表示副本分片丢失,red(红色)表示主分片丢失。
ES集群状态:
- 绿色状态:表示集群各节点运行正常,而且没有丢失任何数据,各主分片和副本分片都运行正常。
- 黄色状态:表示由于某个节点宕机或者其他情况引起的,node节点无法连接、所有主分片都正常分配,有副本分片丢失,但是还没有丢失任何数据。
- 红色状态:表示由于某个节点宕机或者其他情况引起的主分片丢失及数据丢失,但仍可读取数据和存储。
# 查看支持的指令
root@client:~# curl http://192.168.1.101:9200/_cat
# 查看es集群状态
root@client:~# curl http://192.168.1.101:9200/_cat/health
1672448195 00:56:35 es-cluster green 3 3 6 3 0 0 0 0 - 100.0%
root@client:~# curl 'http://192.168.1.101:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1672448208 00:56:48 es-cluster green 3 3 6 3 0 0 0 0 - 100.0%
# 查看所有的节点信息
root@client:~# curl 'http://192.168.1.101:9200/_cat/nodes?v'
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.1.102 10 88 0 0.01 0.00 0.00 cdfhilmrstw - node-2
192.168.1.101 24 90 0 0.00 0.00 0.01 cdfhilmrstw - node-1
192.168.1.103 9 89 0 0.02 0.04 0.06 cdfhilmrstw * node-3
root@client:~# curl 'http://192.168.1.101:9200/_cat/nodes?human'
192.168.1.102 11 88 0 0.00 0.00 0.00 cdfhilmrstw - node-2
192.168.1.101 25 90 0 0.01 0.02 0.01 cdfhilmrstw - node-1
192.168.1.103 11 89 0 0.01 0.03 0.04 cdfhilmrstw * node-3
# 列出所有的索引 以及每个索引的相关信息
root@client:~# curl 'http://192.168.1.101:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .geoip_databases aJ5J9UamTD-REcymUshcxw 1 1 40 0 75.7mb 37.8mb
# 查看集群分健康性
root@client:~# curl http://192.168.1.101:9200/_cluster/health?pretty=true
{
"cluster_name" : "es-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 3,
"active_shards" : 6,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
范例:创建索引
# elasticsearch 7.X 默认创建1个分片1个副本,之前版本默认5分版1副本
# 创建索引index1,简单输出
root@client:~# curl -XPUT '192.168.1.101:9200/index1'
{"acknowledged":true,"shards_acknowledged":true,"index":"index1"}
# 创建索引index2,格式化输出
root@client:~# curl -XPUT '192.168.1.101:9200/index2?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "index2"
}
root@client:~# curl '192.168.1.101:9200/index1?pretty'
{
"index1" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "index1",
"creation_date" : "1672448776469",
"number_of_replicas" : "1",
"uuid" : "keIJlxLlTJehiun1xR6UMA",
"version" : {
"created" : "7170899"
}
}
}
}
}
# 创建3个分片和2个副本的索引
root@client:~# curl -XPUT '192.168.1.101:9200/index3' -H 'Content-Type:application/json' -d '{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
}'
{"acknowledged":true,"shards_acknowledged":true,"index":"index3"}
# 调整副本数为1,但不能调整分片数
root@client:~# curl -XPUT '192.168.1.101:9200/index3/_settings' -H 'Content-Type:application/json' -d '
> {
> "settings": {
> "number_of_replicas": 1
> }
> }'
{"acknowledged":true}
# 早期版本,如es1.X,2.X可以直在下面数据目录下直接看到index的名称,5.X版本后只会显示下面信息
root@es-node1:~# ll /data/es-data/nodes/0/indices/
total 24
drwxr-xr-x 6 elasticsearch elasticsearch 4096 Dec 31 01:09 ./
drwxr-xr-x 5 elasticsearch elasticsearch 4096 Dec 31 01:10 ../
drwxr-xr-x 5 elasticsearch elasticsearch 4096 Dec 31 01:10 1OFmd7DLTfWArgSr--U8xg/
drwxr-xr-x 4 elasticsearch elasticsearch 4096 Dec 31 00:35 7NK_zEKbTsGsNc5pqpJSuQ/
drwxr-xr-x 4 elasticsearch elasticsearch 4096 Dec 31 00:35 aJ5J9UamTD-REcymUshcxw/
drwxr-xr-x 4 elasticsearch elasticsearch 4096 Dec 31 01:06 keIJlxLlTJehiun1xR6UMA/
范例:插入数据
# _id 自动生成
# index1是索引数据库,book是type
root@client:~# curl -XPOST http://192.168.1.101:9200/index1/book/ -H 'Content-Type:application/json' -d '{"name":"linux", "author": "lijiach", "version":"1.0"}'
{"_index":"index1","_type":"book","_id":"cnbCZYUBAEHSP9U9DNgd","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":0,"_primary_term":1}
root@client:~# curl -XPOST 'http://192.168.1.101:9200/index1/book?pretty' -H 'Content-Type:application/json' -d '{"name":"linux111", "author": "lijiacheng", "version":"1.0"}'
{
"_index" : "index1",
"_type" : "book",
"_id" : "dHbDZYUBAEHSP9U9PthP",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
# _id 指定为3
# index1是索引数据库,book是type,3是document记录
root@client:~# curl -XPOST 'http://192.168.1.101:9200/index1/book/3?pretty' -H 'Content-Type:application/json' -d '{"name":"linux111", "author": "cheng", "version":"1.0"}'
{
"_index" : "index1",
"_type" : "book",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
# 数据存放的磁盘位置
root@es-node1:~# tree /data/es-data/ -d
/data/es-data/
└── nodes
└── 0
├── indices
│ ├── 1OFmd7DLTfWArgSr--U8xg
│ │ ├── 0
│ │ │ ├── index
│ │ │ ├── _state
│ │ │ └── translog
│ │ ├── 1
│ │ │ ├── index
│ │ │ ├── _state
│ │ │ └── translog
│ │ └── _state
│ ├── 7NK_zEKbTsGsNc5pqpJSuQ
│ │ ├── 0
│ │ │ ├── index
│ │ │ ├── _state
│ │ │ └── translog
│ │ └── _state
│ ├── aJ5J9UamTD-REcymUshcxw
│ │ ├── 0
│ │ │ ├── index
│ │ │ ├── _state
│ │ │ └── translog
│ │ └── _state
│ └── keIJlxLlTJehiun1xR6UMA
│ ├── 0
│ │ ├── index
│ │ ├── _state
│ │ └── translog
│ └── _state
├── snapshot_cache
└── _state
33 directories
范例:查询指定ID的索引
# 指定ID查询
# curl -XGET ‘http://localhost:9200/{index}/{type}/{id}’
root@client:~# curl 'http://192.168.1.101:9200/index1/book/3?pretty'
{
"_index" : "index1",
"_type" : "book",
"_id" : "3",
"_version" : 1,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "linux111",
"author" : "cheng",
"version" : "1.0"
}
}
# 查询所有
root@client:~# curl 'http://192.168.1.101:9200/index1/_search?pretty'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index1",
"_type" : "book",
"_id" : "cnbCZYUBAEHSP9U9DNgd",
"_score" : 1.0,
"_source" : {
"name" : "linux",
"author" : "lijiach",
"version" : "1.0"
}
},
{
"_index" : "index1",
"_type" : "book",
"_id" : "dHbDZYUBAEHSP9U9PthP",
"_score" : 1.0,
"_source" : {
"name" : "linux111",
"author" : "lijiacheng",
"version" : "1.0"
}
},
{
"_index" : "index1",
"_type" : "book",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "linux111",
"author" : "cheng",
"version" : "1.0"
}
}
]
}
}
# 按条件进行查询
root@client:~# curl 'http://192.168.1.101:9200/index1/book/_search?q=name:linux&pretty'
{
"took" : 22,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808291,
"hits" : [
{
"_index" : "index1",
"_type" : "book",
"_id" : "cnbCZYUBAEHSP9U9DNgd",
"_score" : 0.9808291,
"_source" : {
"name" : "linux",
"author" : "lijiach",
"version" : "1.0"
}
}
]
}
}
# 范例
root@client:~# curl -XPOST 'http://192.168.1.101:9200/index1/book/3' -H 'Content-Type:application/json' -d '{"version": "2.0","name":"go","author": "zhang"}'
{"_index":"index1","_type":"book","_id":"3","_version":2,"result":"updated","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":3,"_primary_term":2}
root@client:~# curl 'http://192.168.1.101:9200/index1/book/3?pretty'
{
"_index" : "index1",
"_type" : "book",
"_id" : "3",
"_version" : 2,
"_seq_no" : 3,
"_primary_term" : 2,
"found" : true,
"_source" : {
"version" : "2.0",
"name" : "go",
"author" : "zhang"
}
}
删除格式
curl -XDELETE http://kibana服务器:9200/<索引名称>
范例:删除
root@client:~# curl -XDELETE 'http://192.168.1.101:9200/index1/book/cnbCZYUBAEHSP9U9DNgd'
{"_index":"index1","_type":"book","_id":"cnbCZYUBAEHSP9U9DNgd","_version":2,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":4,"_primary_term":2}
范例:删除所有索引
root@client:~# for i in `curl 'http://192.168.1.101:9200/_cat/indices?v'|awk '{print $3}'`;do curl -XDELETE http://192.168.1.101:9200/$i;done
2、Python脚本
root@client:~# apt -y install python3
root@client:~# vim els-cluster-monitor.py
#!/usr/bin/python3
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import subprocess
import json
body = ""
false="false"
#用另外一个进程运行curl返回结果从stdout中读取
obj = subprocess.Popen(("curl -sXGET http://192.168.1.101:9200/_cluster/health?pretty=true"),shell=True, stdout=subprocess.PIPE)
data = obj.stdout.read()
#print(type(data)) # 应该是字符串类型或bytes类型
#print(data) # 看看是不是返回的json形式的
es_dict = json.loads(data) if data else {}
# 把json字符串解析为字典
status = es_dict.get("status")
# 通过字典查找status
if status == "green":
print("OK")
else:
print("Not OK")
root@client:~# chmod +x els-cluster-monitor.py
root@client:~# ./els-cluster-monitor.py
OK
root@es-node2:~# systemctl stop elasticsearch.service
root@client:~# ./els-cluster-monitor.py
Not OK
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END