在Elasticsearch中加载索引模版

🏳️‍🌈连接Elasticsearch是要求加载索引模版的。如果你的输出目标不是Elasticsearch,那么你就必须手动加载模版

在Elasticsearch中,索引模版通常用来定义设置和映射关系,确定域需要如何被数据分析。

Metricbeat建议的索引模版在已经在安装Metricbeat的时候配置好了。如果你使用默认的metricbeat.yml配置文件,那么Metricbeat会自动在成功连接到Elasticsearch之后加载模版。如果模版已经存在,那就不会被覆盖除非你配置了Metricbeat强制覆盖。

配置模版加载

默认情况,如果Metricbeat激活了Elasticsearch作为输出,那么Metricbeat会自动加载推荐的模版文件fields.yml。如果你向使用默认的索引模版,不需要其他额外的配置。否则,你可以修改在metricbeat.yml配置文件的默认配置:

  • 加载不同的模版
setup.template.name: "your_template_name"
setup.template.fields: "path/to/fields.yml"

如果模版已存在,除非你配置Metricbeat强制覆盖,否则文件不会做改动。

  • 覆盖已存在的模版
setup.template.overwrite: true
  • 禁用模版加载
setup.template.enabled: false

如果你禁用自动模版加载,那么你就需要自己手动加载模版

  • 重命名索引

ℹ️如果你正在发送事件到一个支持索引生命周期管理的集群,那么请参考 Index lifecycle management (ILM)重命名索引。

默认情况下,当禁用或者不支持索引生命周期管理时,Metricbeat使用时间序列索引。这个索引叫metricbeat-7.x.x-yyyy.MM.dd,这里的yyyy.MM.dd是这个触发索引的事件发生的时间。要用其他命名的话,参考Elasticsearch输出的索引配置选项。你所指定的名字应该包含这个索引的根名称+版本号+日期信息。同时你还需要配置setup.template.namesetup.template.pattern来匹配这个新名字。如下:

output.elasticsearch.index: "customname-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.template.name: "customname"
setup.template.pattern: "customname-*"

⚠️如果索引生命周期管理被启用(通常是默认的),那么setup.template.namesetup.template.pattern会被忽略。

如果你使用预构建Kibana仪表盘,同时设置了setup.dashboards.index选项,如下:

setup.dashboards.index: "customname-*"

参考Elasticsearch index template获取所有配置列表。

手动加载模版

要手动加载模版,运行setup命令。要求已有Elasticsearch
的连接。如果还启用了另一个输出,你需要临时将其禁用,并使用-E参数吧Elasticsearch启用。下面的例子假设Logstash输出已经启用。你可以省略-E参数,如果Elasticsearch输出已经启用。

如果你连接到启用安全性的Elasticsearch集群,确保你已经按照第二步配置Metricbeat所表述的方式配置了credentials。

如果你所运行的Metricbeat没有直连Elasticsearch,参考 Load the template manually (alternate method).

加载模版,使用对应系统的命令:
deb and rpm:

metricbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

mac:

./metricbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

brew:

metricbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

linux:

./metricbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

docker:

docker run docker.elastic.co/beats/metricbeat:7.8.0 setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

win:
用管理员打开 一个PowerShell prompt (右键单击PowerShell 选择以管理员运行).
在PowerShell prompt中, 重定向到你安装Metricbeat的目录, 然后运行:

PS > .\metricbeat.exe setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
强制让Kibana查看新文档

如果你已经用Metricbeat索引数据到Elasticsearch,这些索引可能包含旧文档。当你加载了索引模版之后,你可以在metricbeat-*删除旧文档,一强制Kibana查看新文档。
使用如下命令:
deb and rpm:

curl -XDELETE 'http://localhost:9200/metricbeat-*'

mac:

curl -XDELETE 'http://localhost:9200/metricbeat-*'

linux:

curl -XDELETE 'http://localhost:9200/metricbeat-*'

win:

PS > Invoke-RestMethod -Method Delete "http://localhost:9200/metricbeat-*"

这个命令会删除所有符合模式匹配metricbeat-*的索引。在运行此命令之前,请确认你确实想要删除这些符合模式匹配的索引。

手动加载模版(替代方法)

如果你运行的Metricbeat没有直接连接Elasticsearch,你可以将索引模版到导出到一个文件中,然后将文件移动到具有连接的机器上,然后手动安装模版。

导出索引模版的命令如下:
deb and rpm:

metricbeat export template > metricbeat.template.json

mac:

./metricbeat export template > metricbeat.template.json

brew:

metricbeat export template > metricbeat.template.json

linux:

./metricbeat export template > metricbeat.template.json

win:

PS > .\metricbeat.exe export template --es.version 7.8.0 | Out-File -Encoding UTF8 metricbeat.template.json

加载模版,运行下面这个命令:
deb and rpm:

curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/metricbeat-7.8.0 -d@metricbeat.template.json

mac:

curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/metricbeat-7.8.0 -d@metricbeat.template.json

linux:

curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/metricbeat-7.8.0 -d@metricbeat.template.json

win:

PS > Invoke-RestMethod -Method Put -ContentType "application/json" -InFile metricbeat.template.json -Uri http://localhost:9200/_template/metricbeat-7.8.0
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355

推荐阅读更多精彩内容