ELK 的ES常用的运维命令

本文详细介绍了Elasticsearch集群的管理与Shell操作技巧,包括许可证修改、索引管理、数据写入与查询,以及解决集群健康状态问题的方法。通过具体的curl命令示例,读者可以学习如何进行索引的创建、删除、重命名,以及如何处理红色状态的索引。此外,还提供了自动化脚本用于批量操作,如删除特定日期前的日志和设置副本分片数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题外话

如需转载文章请标明出处,因为我的很多文章一般是会进行更新的。也避免百度搜出来一大推相似的文章,却找不到原创博主。



 ES的license由trial更改为为basic

## 查看
curl -u elastic:zhoulong -X DELETE http://127.0.0.1:9200/_license?pretty

### 修改成basic认证
curl -H "Content-type:application/json" -X POST http://localhost:9200/_xpack/license/start_basic?acknowledge=true -u elastic:xxx

shell 操作索引

## 查看索引
curl 10.125.20.48:9200/_cat/indices

## 查看集群健康状态
curl -s http://10.125.20.48:9200/_cat/health?v

# 创建索引
curl -XPUT 10.125.20.48:9200/knight

## 删除索引
curl -XDELETE http://localhost:9200/twitter

## 重命名索引,在这之前先创建dest索引
curl -H "Content-Type:application/json"  -XPOST 'http://10.125.20.48:9200/_reindex' -d '
{
  "source": {
    "index": "knight"
  },
  "dest": {
    "index": "test_knight"
  }
}'

##  查看进度
curl 10.125.20.48:9200/_tasks?detailed=true&actions=*reindex

## 单机版索引是red的情况的思路
# 如果是red的索引, 可以先复制到新的索引上去(前提是先创建索引),那样新的索引就有数据了
# 然后删除掉是red的索引,然后再创建名为之前为red的索引。
#  然后再复制数据过来即可

写入数据

## knight是索引,_doc是类型,不指定id 
POST knight/_doc
{
  "name":"tom",
  "city":"cs"
  
}

##  使用shell进行操作
curl -s -H "Content-Type:application/json"  -XPOST 'http://10.125.20.48:9200/knight/_doc' -d '{"name":"tom","age":32}'

查询 UNASSIGNED的索引

curl -s -u elastic:xxoo "http://127.0.0.1:9200/_cat/shards"|grep UNASSIGNED|egrep -v "^\."

或者你也可以这样查询UNASSIGNED 的个数:

GET _cat/allocation?v

默认情况下,ES会自动将未分配的shards,分配到各node上。使用以下命令确定自动分配分片的功能是打开的。

GET _cluster/settings?pretty

{
  "persistent" : {
    "xpack" : {
      "monitoring" : {
        "collection" : {
          "enabled" : "true"
        }
      }
    }
  },
  "transient" : {
    "cluster" : {
      "max_shards_per_node" : "900000"
    }
  }
}

从上图中我们没有看到自动分片已经打开,所以我们可以使用如下命令来开启自动分配。

PUT _cluster/settings
{
  "transient":{
    "cluster.routing.allocation.enable" : "all"
  }
}

再次查看:

GET _cluster/settings

{
  "persistent" : {
    "xpack" : {
      "monitoring" : {
        "collection" : {
          "enabled" : "true"
        }
      }
    }
  },
  "transient" : {
    "cluster" : {
      "routing" : {
        "allocation" : {
          "enable" : "all"
        }
      },
      "max_shards_per_node" : "900000"
    }
  }
}

如果想要永久生效,可以这样改成 persistent 形式


PUT _cluster/settings
{
  "persistent":{
    "cluster.routing.allocation.enable" : "all"
  }
}

删除 UNASSIGNED 索引脚本

#!/bin/bash
for line in `cat xx.txt`
do
    #echo "aaa/$line"
    curl -u elastic:xxoo -XDELETE "192.168.0.3:9200/$line"
    echo -e "\n"

done

一次性删除多天之前的日志脚本

#!/bin/bash
# 本文博客地址: https://knight.blog.csdn.net/article/details/107957840

for line in {30..35}
do
    riqi=$(date -d "$line days ago" +"%Y.%m.%d")
    #echo $riqi
    curl -s -u elastic:xxoo -XDELETE "http://192.168.0.6:9200/*${riqi}*"
    echo "\n"
    sleep 1
    
done

查看状态是red的索引

curl -s -u oo:xx "http://$host:9200/_cat/indices"|grep red

对副本分片不可用的索引进行设置,从而消除 Red 或者 Yellow 状态。

先查找:

GET _cat/shards?h=index,shard,prirep,state,unassigned.reason

对某一个索引的状态进行查询

GET xxoo/_settings

对某一个索引设置副本数为0.

PUT xxoo/_settings
{
  "number_of_replicas": 0
}

那么如何在shell 中进行执行了?

indexname="hhh"
curl -H "Content-Type: application/json" -s -u xx:00 -XPUT "http://$host:9200/$indexname/_settings" -d '{"number_of_replicas":0}'

在shell中如何循环执行了?

#!/bin/bash
host="10.19.19.248"

for line in `cat xx.txt`
do
    #echo "aaa/$line"
    curl -H "Content-Type: application/json" -s -u xx:00  -XPUT "http://$host:9200/$line/_settings" -d '{"number_of_replicas":0}'
    echo -e "\n"
 
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叱咤少帅(少帅)

如果文章对你有帮助就打赏下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值