使用 Kibana 6.0 或 7+ (v7.0.1) 从控制台创建索引模式

Create index-patterns from console with Kibana 6.0 or 7+ (v7.0.1)

我最近将我的 ElasticStack 实例从 5.5 升级到 6.0,这个版本的一些重大变化似乎损害了我的管道。我有一个脚本,它根据 ElasticSearch 中的索引自动为一些相似索引组创建索引模式。问题是随着 6.0 版本的新映射更改,我无法从控制台添加任何新的索引模式。这是我在 5.5 中使用并运行良好的请求:

curl -XPOST "http://localhost:9200/.kibana/index-pattern" -H 'Content-  Type: application/json' -d'
{
  "title" : "index_name",
  "timeFieldName" : "execution_time"
}'

这是我现在在 6.0 中从 ElasticSearch 得到的响应:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [.kibana] as the final mapping would have more than 1 type: [index-pattern, doc]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Rejecting mapping update to [.kibana] as the final mapping would have more than 1 type: [index-pattern, doc]"
  },
  "status": 400
}

我如何从控制台添加索引模式来避免这个多重映射问题?

在 Elasticsearch 6.0.0 或更高版本中创建的索引只能包含一种映射类型。

在 5.x 中创建的具有多种映射类型的索引将继续像以前一样在 Elasticsearch 6.x 中运行。

Elasticsearch 7.0.0 中将完全删除映射类型。

也许您在 ES 6.0.0 中创建了一个包含多个 doc_types 的索引。 https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html

URL在6.0.0版本中有改动,这里是新的URL:

http://localhost:9200/.kibana/doc/doc:index-pattern:my-index-pattern-name 

这个 CURL 应该适合你:

curl -XPOST "http://localhost:9200/.kibana/doc/index-pattern:my-index-pattern-name" -H 'Content-Type: application/json' -d'
{
  "type" : "index-pattern",
  "index-pattern" : {
    "title": "my-index-pattern-name*",
    "timeFieldName": "execution_time"
  }
}'

如果你是 Kibana 7.0.1 / 7+ 那么你可以参考 saved_objects API 例如: 参考:https://www.elastic.co/guide/en/kibana/master/saved-objects-api.html(查找获取、创建、删除等)。

在这种情况下,我们将使用:https://www.elastic.co/guide/en/kibana/master/saved-objects-api-create.html

$ curl -X POST -u $user:$pass -H "Content-Type: application/json" -H "kbn-xsrf:true"  "${KIBANA_URL}/api/saved_objects/index-pattern/dummy_index_pattern" -d '{ "attributes": { "title":"index_name*", "timeFieldName":"sprint_start_date"}}'  -w "\n" | jq

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   327  100   250  100    77    543    167 --:--:-- --:--:-- --:--:--   543
{
  "type": "index-pattern",
  "id": "dummy_index_pattern",
  "attributes": {
    "title": "index_name*",
    "timeFieldName": "sprint_start_date"
  },
  "references": [],
  "migrationVersion": {
    "index-pattern": "6.5.0"
  },
  "updated_at": "2020-02-25T22:56:44.531Z",
  "version": "Wzg5NCwxNV0="
}

其中 $KIBANA_URL 设置为:http://my-elk-stack.devops.local:5601

如果您没有安装 jq,请从命令中删除 | jq(如上所列)。

PS:当使用 KIBANA 的 GUI 创建一个 index-pattern 时,Kibana 将其即索引 ID 存储为一个 字母数字值(ex:laskl32ukdflsdjflskadf-sdf-sdfsaldkjfhsdf-dsfasdf) 在执行 GET 操作时很难 use/find/type 使用以下 curl 命令查找有关现有索引模式的信息。

如果您传递了索引模式名称(就像我们上面所做的那样),那么在 Kibana/Elasticsearch 中,它将按照您为 REST 调用提供的名称记录索引模式的 ID例如: .../api/saved_objects/index-pattern/dummy_index_pattern")

此处:dummy_index_pattern 将变为 ID(仅当您将鼠标悬停在 Kibana GUI 中的索引模式名称上时才可见)和

它的索引名称为:index_name*(即,当您单击 Kibana Home > Gear icon > Index Patterns 并查看列出的索引模式时,GUI 中列出的内容在右侧。

注意:timeFieldName 非常重要。这是用于查找时间序列事件的字段(即特别是 TSVB 时间序列可视化生成器可视化类型)。默认情况下,它使用 @timestamp 字段,但是如果您每次都重新创建索引(而不是将增量信息从数据源(例如:JIRA)发送到目标 Elasticsearch 索引)并从头开始一次性发送所有数据来自数据源,那么 @timestamp 将无助于可视化的 Time-Spanning/Window 功能(您将时间从过去 1 周更改为过去 1 小时或过去 6 个月 );在这种情况下,您可以设置一个不同的字段,即 sprint_start_date 就像我使用的那样(现在在 Kibana Discover 数据页面中,如果您 select 这个索引模式,它将使用 sprint_start_date (类型:日期)字段,用于事件。

GET关于新建索引模式的索引模式信息,可以参考:https://www.elastic.co/guide/en/kibana/master/saved-objects-api-get.html --OR 运行下面的where (the URL 路径中的最后一个值是我们之前创建的索引模式的 ID 值:

curl -X GET "${KIBANA_URL}/api/saved_objects/index-pattern/dummy_index_pattern" | jq

否则(如果您想对通过 Kibana 的 GUI/webpage 在页面 Index Pattern > Create Index Pattern 下创建的索引模式执行 GET,您必须输入如下内容:

curl -X GET "${KIBANA_URL}/api/saved_objects/index-pattern/jqlaskl32ukdflsdjflskadf-sdf-sdfsaldkjfhsdf-dsfasdf" | jq 

批量创建带时间戳的索引模式:

cat index_svc.txt
my-index1
my-index2
my-index3
my-index4
my-index5
my-index6

cat index_svc.txt | while read index; do
    echo -ne "create index-pattern ${index} \t"
    curl -XPOST "http://10.0.1.44:9200/.kibana/doc/index-pattern:${index}" -H 'Content-Type: application/json' -d "{\"type\":\"index-pattern\",\"index-pattern\":{\"title\":\"${index}2020*\",\"timeFieldName\":\"@timestamp\"}}"
    echo 
done

对于带有 Open Distro 安全插件的 Kibana 7.7.0(准确地说是 amazon/opendistro-for-elasticsearch-kibana:1.8.0 Docker 图片),这对我有用:

curl -X POST \
-u USERNAME:PASSWORD \
KIBANA_HOST/api/saved_objects/index-pattern \
-H "kbn-version: 7.7.0" \
-H "kbn-xsrf: true" \
-H "content-type: application/json; charset=utf-8" \
-d '{"attributes":{"title":"INDEX-PATTERN*","timeFieldName":"@timestamp","fields":"[]"}}'

请注意,kbn-xsrf header 是必需的,但从安全角度来看似乎没有用。

输出如下:

{"type":"index-pattern","id":"UUID","attributes":{"title":"INDEX-PATTERN*","timeFieldName":"@timestamp","fields":"[]"},"references":[],"migrationVersion":{"index-pattern":"7.6.0"},"updated_at":"TIMESTAMP","version":"VERSION"}

我不知道为什么 migrationVersion.index-pattern 是“7.6.0”。

对于其他 Kibana 版本,您应该能够:

  1. 在浏览器中打开 Kibana UI
  2. 打开开发者控制台,导航到“网络”选项卡
  3. 使用UI
  4. 创建索引模式
  5. 在开发者控制台中打开POST请求,查看URL和headers,然后将其重写为cURL