elasticsearch 日常运维日常命令

elasticsearch 日常运维日常命令
ps:版本 elasticsearch 5.X
第一:_cat系列
1._cat系列提供了一系列查询elasticsearch集群状态的接口。你可以通过执行

curl -XGET localhost:9200/_cat
curl -XGET http://elastic:changeme@127.0.0.1:9200/?pretty

2.获取所有_cat系列的操作

/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}

你也可以后面加一个v,让输出内容表格显示表头,举例

name       component        version type url
Prometheus analysis-mmseg   NA      j
Prometheus analysis-pinyin  NA      j
Prometheus analysis-ik      NA      j
Prometheus analysis-ik      NA      j
Prometheus analysis-smartcn 2.1.0   j
Prometheus segmentspy       NA      s    /_plugin/segmentspy/
Prometheus head             NA      s    /_plugin/head/
Prometheus bigdesk          NA      s    /_plugin/bigdesk/
Xandu      analysis-ik      NA      j
Xandu      analysis-pinyin  NA      j
Xandu      analysis-mmseg   NA      j
Xandu      analysis-smartcn 2.1.0   j
Xandu      head             NA      s    /_plugin/head/
Xandu      bigdesk          NA      s    /_plugin/bigdesk/
Onyxx      analysis-ik      NA      j
Onyxx      analysis-mmseg   NA      j
Onyxx      analysis-smartcn 2.1.0   j
Onyxx      analysis-pinyin  NA      j
Onyxx      head             NA      s    /_plugin/head/
Onyxx      bigdesk          NA      s    /_plugin/bigdesk/

第二:_cluster系列
1、查询设置集群状态

curl -XGET localhost:9200/_cluster/health?pretty=true

pretty=true表示格式化输出
level=indices 表示显示索引状态
level=shards 表示显示分片信息
特例:集群的状态(检查 节点数量)

GET _ cluster/health?level=indices 

所有索引的健康状态(查看有问题的索引)

GET _ cluster/health/kibana_ sample_ _data_ ecommerce 

查看某- -一个指定索引的健康状态(具体索引)

GET _cluster/health?level=shards          

分片级索引

GET _ cluster/allocation/explain?pretty 

返回第一个未返回shard的原因
unassigned 分片问题可能的原因?
1)INDEX_CREATED:由于创建索引的API导致未分配。
2)CLUSTER_RECOVERED :由于完全集群恢复导致未分配。
3)INDEX_REOPENED :由于打开open或关闭close一个索引导致未分配。
4)DANGLING_INDEX_IMPORTED :由于导入dangling索引的结果导致未分配。
5)NEW_INDEX_RESTORED :由于恢复到新索引导致未分配。
6)EXISTING_INDEX_RESTORED :由于恢复到已关闭的索引导致未分配。
7)REPLICA_ADDED:由于显式添加副本分片导致未分配。
8)ALLOCATION_FAILED :由于分片分配失败导致未分配。
9)NODE_LEFT :由于承载该分片的节点离开集群导致未分配。
10)REINITIALIZED :由于当分片从开始移动到初始化时导致未分配(例如,使用影子shadow副本分片)。
11)REROUTE_CANCELLED :作为显式取消重新路由命令的结果取消分配。
12)REALLOCATED_REPLICA :确定更好的副本位置被标定使用,导致现有的副本分配被取消,出现未分配。

2、curl -XGET localhost:9200/_cluster/stats?pretty=true

显示集群系统信息,包括CPU JVM等等

3、curl -XGET localhost:9200/_cluster/state?pretty=true

集群的详细信息。包括节点、分片等。

3、curl -XGET localhost:9200/_cluster/pending_tasks?pretty=true

获取集群堆积的任务
3、修改集群配置
举例:

curl -XPUT localhost:9200/_cluster/settings -d '{
    "persistent" : {
        "discovery.zen.minimum_master_nodes" : 2
    }
}'

transient 表示临时的,persistent表示永久的

4、curl -XPOST ‘localhost:9200/_cluster/reroute’ -d ‘xxxxxx’

shard的手动控制
5、关闭节点
关闭指定192.168.1.1节点

curl -XPOST ‘http://192.168.1.1:9200/_cluster/nodes/_local/_shutdown’
curl -XPOST ‘http://localhost:9200/_cluster/nodes/192.168.1.1/_shutdown’

关闭主节点

curl -XPOST ‘http://localhost:9200/_cluster/nodes/_master/_shutdown’

关闭整个集群

curl -XPOST ‘http://localhost:9200/_shutdown?delay=10s’
curl -XPOST ‘http://localhost:9200/_cluster/nodes/_shutdown’
curl -XPOST ‘http://localhost:9200/_cluster/nodes/_all/_shutdown’

delay=10s表示延迟10秒关闭
第三:_nodes系列
1、查询节点的状态

curl -XGET ‘http://localhost:9200/_nodes/stats?pretty=true’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2/stats?pretty=true’
curl -XGET ‘http://localhost:9200/_nodes/process’
curl -XGET ‘http://localhost:9200/_nodes/_all/process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/jvm,process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/info/jvm,process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/_all
curl -XGET ‘http://localhost:9200/_nodes/hot_threads’

2.查看线程堵塞

GET _cat/thread_pool?v&h=node_name,name,type,active,size,queue,queue_size,rejected,keep_alive,completed

3.批量队列

GET _cat/thread_pool/bulk?v&h=node_name,name,type,active,size,queue,queue_size,rejected,keep_alive,completed

4.当前等待任务

GET _tasks?detailed=true

5.所有人任务

GET _tasks

6.正在执行任务

GET _nodes/hot_threads

7.所有待处理任务

GET _cat/pending_tasks

第四:索引操作
1、获取索引

curl -XGET ‘http://localhost:9200/{index}/{type}/{id}

2、索引数据

curl -XPOST ‘http://localhost:9200/{index}/{type}/{id}-d'{“a”:”avalue”,”b”:”bvalue”}

3、删除索引

curl -XDELETE ‘http://localhost:9200/{index}/{type}/{id}

4、设置mapping

curl -XPUT http://localhost:9200/{index}/{type}/_mapping -d '{
  "{type}" : {
	"properties" : {
	  "date" : {
		"type" : "long"
	  },
	  "name" : {
		"type" : "string",
		"index" : "not_analyzed"
	  },
	  "status" : {
		"type" : "integer"
	  },
	  "type" : {
		"type" : "integer"
	  }
	}
  }
}'

5、获取mapping

curl -XGET http://localhost:9200/{index}/{type}/_mapping

6、搜索

curl -XGET 'http://localhost:9200/{index}/{type}/_search' -d '{
    "query" : {
        "term" : { "user" : "kimchy" } //查所有 "match_all": {}
    },
	"sort" : [{ "age" : {"order" : "asc"}},{ "name" : "desc" } ],
	"from":0,
	"size":100
}
curl -XGET 'http://localhost:9200/{index}/{type}/_search' -d '{
    "filter": {"and":{"filters":[{"term":{"age":"123"}},{"term":{"name":"张三"}}]},
	"sort" : [{ "age" : {"order" : "asc"}},{ "name" : "desc" } ],
	"from":0,
	"size":100
}

7.指定删除 查询 (非必要不要操作)

curl -X POST "localhost:9200/contact_299/_delete_by_query" -H 'Content-Type: application/json' -d'
                   {
                     "query": {
                       "bool": {
                         "must": [
                         ],
                         "disable_coord": false,
                         "adjust_pure_negative": true,
                         "boost": 1
                       }
                     }
                   }
                   '

在索引合并阶段就会删除.del文件,所以可以执行以下命令清楚.del文件:

POST /index_name/_forcemerge?max_num_segments=1&only_expunge_deletes=true
or
POST /index_name/_forcemerge?max_num_segments=1

8.清空elasticsearch索引缓存
清空所有索引缓存:

POST /{_index}/_flush
POST /{_index}/ _cache/_clear

清空指定索引的缓存:

POST  /{_index}/_cache/_clear

9: 关闭指定索引和打开指定索引
关闭索引

POST  /{_index}/_close 

打开索引

POST /{_index}/_open

10.建议开启慢日志,以方便出现问题时进一步分析,慢日志开启配置命令参考:
慢日志

PUT _settings
{
  "index.search.slowlog.threshold.query.warn": "10s",
  "index.search.slowlog.threshold.query.info": "5s",
  "index.search.slowlog.threshold.query.debug": "2s",
  "index.search.slowlog.threshold.query.trace": "500ms",
  "index.search.slowlog.threshold.fetch.warn": "1s",
  "index.search.slowlog.threshold.fetch.info": "800ms",
  "index.search.slowlog.threshold.fetch.debug": "500ms",
  "index.search.slowlog.threshold.fetch.trace": "200ms",
  "index.indexing.slowlog.threshold.index.warn": "10s",
  "index.indexing.slowlog.threshold.index.info": "5s",
  "index.indexing.slowlog.threshold.index.debug": "2s",
  "index.indexing.slowlog.threshold.index.trace": "500ms",
  "index.indexing.slowlog.level": "info",
  "index.indexing.slowlog.source": "1000"
} 

11.故障加速

PUT _cluster/settings
{
  "transient": {
    "cluster.routing.allocation.node_concurrent_incoming_recoveries": 7,
    "cluster.routing.allocation.node_initial_primaries_recoveries": 7,
    "cluster.routing.allocation.node_concurrent_recoveries": 7
  }
}
12.的 reindex api
第一步,和创建普通索引一样创建新索引。当数据量很大的时候,需要设置刷新时间间隔,把 refresh_intervals 设置为-1,即不刷新,number_of_replicas 副本数设置为 0(因为副本数可以动态调整,这样有助于提升速度)。
```powershell
{
    "settings": {
 
        "number_of_shards": "50",
        "number_of_replicas": "0",
        "index": {
            "refresh_interval": "-1"
        }
    }
    "mappings": {
    }
}

第二步,调用 reindex 接口,建议加上 wait_for_completion=false 的参数条件,这样 reindex 将直接返回 taskId。

POST _reindex?wait_for_completion=false
 
{
  "source": {
    "index": "old_index",   //原有索引
    "size": 5000            //一个批次处理的数据量
  },
  "dest": {
    "index": "new_index",   //目标索引
  }
}

第三步,等待。可以通过 来查询重建的进度

GET _tasks?detailed=true&actions=*reindex 

取消 task 则调用

GET _tasks/node_id:task_id/_cancel。

第四步,删除旧索引,释放磁盘空间。

POST /_reindex
{
    "conflicts": "proceed",          //意思是冲突以旧索引为准,直接跳过冲突,否则会抛出异常,停止task
    "source": {
        "index": "old_index"         //旧索引
        "query": {
            "constant_score" : {
                "filter" : {
                    "range" : {
                        "data_update_time" : {
                            "gte" : 123456789   //reindex开始时刻前的毫秒时间戳
                            }
                        }
                    }
                }
            }
        },
    "dest": {
        "index": "new_index",       //新索引
        "version_type": "external"  //以旧索引的数据为准
        }
}

13:增加别名
为索引{index}添加一个别名in1:
1.:添加别名

POST _aliases
{
  "actions" : [{"add" : {"index" : "{index}" , "alias" : "in1"}}]
}


2.查询别名
查询某个index下的别名:
GET {index}/_alias/*
3.删除别名

POST /_aliases
{
    "actions": [
        {"remove": {"index": "{index}", "alias": "in1"}}
    ]
}


4.修改别名
es没有修改别名的操作,只能先删除后添加

POST _aliases
{
  "actions" : [{"remove" : {"index" : "{index}" , "alias" : "in1"}}],
  "actions" : [{"add" : {"index" : "{index}" , "alias" : "in2"}}]
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值