ES常用命令

1.策略和模板

1.1 概述

通过ilm来配置策略,策略绑定索引模板,创建索引时,会根据正则,自动命中索引模板。

1.2 策略

下面创建了一个14天就删除的策略,策略名称取名为zipkin_indices_policy

1
2
3
4
#创建策略
curl -X PUT -H "Content-Type: application/json" \
-d '{"policy": {"phases": {"delete": {"min_age": "14d","actions": {"delete": {}}}}}}' \
http://账号:密码@127.0.0.1:9200/_ilm/policy/zipkin_indices_policy

查看所有策略

1
2
3
curl -X GET http://账号:密码@127.0.0.1:9200/_ilm/status
curl -X GET http://账号:密码@127.0.0.1:9200/_ilm/policy
curl -X POST http://账号:密码@127.0.0.1:9200/_ilm/start

ilm基本每10分钟左右会扫所有的索引,并执行策略。

下面创建了一个名为test-template的索引模板,正则匹配所有zipkin开头的索引,索引的生命周期绑定到上面创建的策略index.lifecycle.name

1.3 模板

1
2
3
4
#创建索引模板,应用策略
curl -X PUT -H "Content-Type: application/json" \
-d '{"index_patterns":["zipkin-span-*"],"template":{"settings":{"number_of_shards":10,"index.lifecycle.name":"zipkin_indices_policy","index.lifecycle.rollover_alias":"zipkin_indices_policy","number_of_replicas":"1","requests":{"cache":{"enable":"true"}}},"mappings":{"_source":{"enabled":true},"properties":{"_class":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"account_status":{"type":"long"},"created_by":{"type":"long"},"data_status":{"type":"long"},"gmt_create":{"type":"date","format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"},"gmt_modified":{"type":"date","format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"},"login_account":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"modified_by":{"type":"long"},"uid":{"type":"long"},"user_name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"user_note":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}}}' \
http://账号:密码@127.0.0.1:9200/_index_template/zipkin-index-template

那么,当es新增了zipkin开头的索引后,就会自动应用上述策略

查看所有模板:

1
2
3
4
#查看所有模板
curl http://账号:密码@127.0.0.1:9200/_cat/templates?v
#查看指定模板详情
curl http://账号:密码@127.0.0.1:9200/_cat/template/zipkin-span_template

2.索引管理

1
2
3
4
5
6
7
#查看索引状态
curl -X GET http://账号:密码@127.0.0.1:9200/_cat/indices
#创建zipkin-span-0001索引
curl -X PUT http://账号:密码@127.0.0.1:9200/zipkin-span-0001
#批量删除索引
curl -X DELETE http://账号:密码@127.0.0.1:9200/zipkin-span-2022-05-01,zipkin-span-2022-05-02,zipkin-span-2022-05-03,zipkin-span-2022-05-04,zipkin-span-2022-05-05

3.查看集群状态

查看节点状况

1
2
3
4
5
curl http://账号:密码@127.0.0.1:9200/_cat/nodes
# *表示主节点
192.168.1.1 48 98 56 6.00 5.77 6.23 cdfhilmrstw - node-3
192.168.1.2 16 82 24 2.83 3.19 2.72 cdfhilmrstw - node-1
192.168.1.3 20 68 1 0.26 0.15 0.12 cdfhilmrstw * node-2

查看各类设置:

1
2
3
4
5
# 查看集群的设置
curl http://账号:密码@127.0.0.1:9200/_cluster/settings?include_defaults=true
# 查看指定索引的设置
curl http://账号:密码@127.0.0.1:9200/你的索引/_settings?include_defaults=true

查看索引状况

1
2
3
4
5
6
curl http://账号:密码@127.0.0.1:9200/_cat/indices?v
# 查看索引状况
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .geoip_databases RY8YvmHPREWTz_oJiwgLgQ 1 1 41 0 77.7mb 38.8mb
green open .security-7 W9AMEHPIQMmunTBQPe5T9Q 1 1 7 0 51.5kb 25.7kb
green open zipkin-span-2022-05-15 pwEeWr-_Tkesav9B095sZw 1 1 38050160 0 28.6gb 12.4gb

查看分片情况

1
2
3
4
5
6
7
8
9
10
11
12
curl http://账号:密码@127.0.0.1:9200/_cat/shards?v
index shard prirep state docs store ip node
.security-7 0 r STARTED 7 25.7kb 192.168.7.72 node-2
.security-7 0 p STARTED 7 25.7kb 192.168.6.191 node-1
.geoip_databases 0 p STARTED 41 38.8mb 192.168.7.72 node-2
.geoip_databases 0 r STARTED 41 38.8mb 192.168.7.73 node-3
.ds-ilm-history-5-2022.05.15-000001 0 r STARTED 192.168.7.72 node-2
.ds-ilm-history-5-2022.05.15-000001 0 p STARTED 192.168.6.191 node-1
zipkin-span-2022-05-15 0 p STARTED 38050160 12.4gb 192.168.7.73 node-3
zipkin-span-2022-05-15 0 r STARTED 38340050 16.2gb 192.168.6.191 node-1
.ds-.logs-deprecation.elasticsearch-default-2022.05.15-000001 0 p STARTED 192.168.7.72 node-2
.ds-.logs-deprecation.elasticsearch-default-2022.05.15-000001 0 r STARTED 192.168.7.73 node-3

查看集群状况

1
curl http://账号:密码@127.0.0.1:9200/_cluster/stats?pretty

查看健康状况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
curl http://账号:密码@127.0.0.1:9200/_cluster/health?pretty
{
"cluster_name" : "es-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 5,
"active_shards" : 10,
"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
}