ELK 在日志场景下的常用命令

elasticsearch在不断的迭代下,对其默认参数已经做了诸多优化,在日常的学习使用场景下,我们是不需要对elasticsearch进行参数更改的。但是在生产环境下,各种环境变得复杂,网络,磁盘,节点等都会带来各种突发情况,这时,需要熟悉以下命令。

分片分配

分片分配设置:下面的动态设置可以用来控制分片的分配和recovery。

cluster.routing.allocation.enable 可选的参数有:

  • all 允许所有的分片被分配
  • primaries 只允许主分片被分配
  • new_primaries 只允许新创建的索引的主分片被分配
  • none 不允许任何分片进行分配

当一个节点出于任何原因离开集群时,master会主动地或以其他方式作出反应:

  • 将副本分片提升为主分片以替换该节点上的任何主分片。
  • 分配副本分片以替换缺失的副本(假设有足够的节点)。
  • 在剩余节点上重新平衡所有的分片。

这些操作旨在通过每个碎片尽快完成复制来确保集群免遭数据丢失。

尽管我们在节点级别和集群级别上节省并发恢复 ,但这种“shard-shuffle”仍然会给群集带来很多额外的负载,如果缺少的节点很快可能会返回,这可能不是必需的。想象一下这个场景:

  • node 5失去网络连接。
  • master节点将node 5上的每个主节点的副本分片都提升为主节点。
  • 主服务器将新副本分配给群集中的其他节点。
  • 每个新副本是主分片的完整拷贝,并通过网络进行传输。
  • 更多的分片移动到不同的节点以重新平衡集群。
  • node 5在几分钟后返回。
  • master节点通过为节点5分配分片来重新平衡集群。

如果master节点刚刚等了几分钟,那么丢失的分片可能会以最少的网络流量重新分配给节点5。对于已经自动同步刷新(sync-flushed)的空闲分片(分片没有收到索引请求)来说,这个过程会更快。

一般,当某个 node 临时 left 的时候(因为重启,或者因为网络的问题),为了防止 master将其上的shard进行重新分配,需要临时禁止分配

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

如果不能及时的禁止分配,也可以将timeout的时间设置得大一些:

PUT /_all/_settings
{
    "settings": {
       "index.unassigned.node_left.delayed_timeout": "5m"
    }
}

手动分配

有时,我们需要手动的分配 unassigned shard:

主分片

POST _cluster/reroute?retry_failed=true
{
  "commands": [
    {
      "allocate_stale_primary": {
        "index": "app_all-2018.04.22",
        "shard": 1,
        "from_node": "10.60.4.243",
        "to_node": "10.60.4.242",
        "accept_data_loss": true
      }
    }
  ]
}

副本

POST _cluster/reroute?retry_failed=true
{
  "commands": [
    {
      "allocate_replica": {
        "index": "app_all-2018.04.22",
        "shard": 1,
        "node": "10.60.4.243"
      }
    }
  ]
}

移动

POST _cluster/reroute?retry_failed=true
{
  "commands": [
    {
      "move": {
        "index": "app_all-2018.05.08",
        "shard": 1,
        "from_node": "10.50.5.53",
        "to_node": "10.60.4.242"
      }
    }
  ]
}

reindex

将多个已存在的index合并为一个大的index

POST _reindex
{
  "source": {
    "index": "app_901221-2018.02.*"
  },
  "dest": {
    "index": "app_901221-2018.02"
  }
}

测试分词器

默认的英文分词器,是不会对.进行分词。在日志环境下,如果你想搜索一个NullPointerException,而该关键字出现在 com.alibaba.NullPointerException 之中,因为不会对.进行分词,只搜索NullPointerException,得到的hits个数为0,因此需要修改分词器,一般是通过模版的方式

POST _template/app_template
{
  "order": 0,
  "index_patterns":["app_*"],
  "settings": {
    "number_of_replicas": "1",
    "refresh_interval": "15s",
    "number_of_shards": "4",
    "analysis": {
      "analyzer": {
        "message_analyzer": { 
          "tokenizer":      "letter"
        }
      }
    }
  },
  "mappings":{
    "doc":{
      "properties":{
        "message":{
          "type":     "text",
          "analyzer": "message_analyzer"
        }
      }
    }
  }
}

然后可做测试:

POST app_all-2018.05.08/_analyze
{
  "field": "message", 
  "text": "com.alibaba.NullPointerException"
}

返回为三个token,

{
  "tokens": [
    {
      "token": "com",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 0
    },
    {
      "token": "alibaba",
      "start_offset": 4,
      "end_offset": 11,
      "type": "word",
      "position": 1
    },
    {
      "token": "NullPointerException",
      "start_offset": 12,
      "end_offset": 32,
      "type": "word",
      "position": 2
    }
  ]
}

若是默认的分词器,返回只有一个token:

{
  "tokens": [
    {
      "token": "com.alibaba.nullpointerexception",
      "start_offset": 0,
      "end_offset": 32,
      "type": "<ALPHANUM>",
      "position": 0
    }
  ]
}

猜你喜欢

转载自blog.csdn.net/u013613428/article/details/80255149