以下示例均为完整的 JSON 策略。策略组成部分见 Policies 文档;通过 OpenSearch Dashboards 创建策略的方法见 Creating a policy;使用 API 的方法见 ISM API。
使用 ISM 模板自动滚动索引
下面的模板策略用于 rollover 场景。
如果希望跳过某个索引的滚动,请在该索引设置中将 index.plugins.index_state_management.rollover_skip 设为 true。
1. 创建带 ism_template 字段的策略
PUT _plugins/_ism/policies/rollover_policy
{
"policy": {
"description": "Example rollover policy.",
"default_state": "rollover",
"states": [
{
"name": "rollover",
"actions": [
{
"rollover": {
"min_doc_count": 1
}
}
],
"transitions": []
}
],
"ism_template": {
"index_patterns": ["log*"],
"priority": 100
}
}
}
必须指定 index_patterns 字段。没有指定 priority 时,默认优先级为 0。
2. 创建 rollover_alias 为 log 的模板
PUT _index_template/ism_rollover
{
"index_patterns": ["log*"],
"template": {
"settings": {
"plugins.index_state_management.rollover_alias": "log"
}
}
}
3. 创建具有 log 别名的索引
PUT log-000001
{
"aliases": {
"log": {
"is_write_index": true
}
}
}
4. 写入文档,触发滚动条件
POST log/_doc
{
"message": "dummy"
}
5. 确认策略已附加到 log-000001
GET _plugins/_ism/explain/log-000001?pretty
使用 ISM 模板执行别名操作
下面的策略示例用于 alias 操作。
在这个例子中,第一次作业触发 rollover,创建新索引。接着向两个索引加入另一篇文档。新的作业使第二个索引指向 log 别名,而旧索引会因 alias 操作被移出别名关系。
先创建 ISM 策略:
PUT /_plugins/_ism/policies/rollover_alias_policy
{
"policy": {
"description": "Example alias action policy.",
"default_state": "rollover",
"states": [
{
"name": "rollover",
"actions": [
{
"rollover": {
"min_doc_count": 1
}
}
],
"transitions": [{
"state_name": "alias",
"conditions": {
"min_doc_count": "2"
}
}]
},
{
"name": "alias",
"actions": [
{
"alias": {
"actions": [
{
"remove": {
"alias": "alias-log"
}
}
]
}
}
]
}
],
"ism_template": {
"index_patterns": ["alias-log*"],
"priority": 100
}
}
}
如果某个 ism_template 的索引模式,与同优先级现有策略的模式重叠,该模板会被拒绝。因此,这个策略使用独立的 alias-log* 模式,而不是上一示例的 log*。
接着,创建用于启用策略的索引模板:
PUT /_index_template/ism_rollover_alias
{
"index_patterns": ["alias-log*"],
"template": {
"settings": {
"plugins.index_state_management.rollover_alias": "alias-log"
}
}
}
修改集群设置,让作业每分钟触发一次:
PUT /_cluster/settings?pretty=true
{
"persistent" : {
"plugins.index_state_management.job_interval" : 1
}
}
创建新索引:
PUT /alias-log-000001
{
"aliases": {
"alias-log": {
"is_write_index": true
}
}
}
最后,向索引添加文档以触发作业:
POST /alias-log-000001/_doc
{
"message": "dummy"
}
可以使用别名和索引 API 确认以上操作:
GET /_cat/indices?pretty
GET /_cat/aliases?pretty
alias 操作策略不允许使用 index 和 remove_index 参数,只允许 add 与 remove 别名操作参数。
完成后,将作业间隔恢复为默认值,避免缩短后的间隔影响集群中全部受管理索引:
PUT /_cluster/settings
{
"persistent" : {
"plugins.index_state_management.job_interval" : null
}
}
热、温与删除工作流示例
下面的策略实现 hot、warm、delete 工作流。可以将它作为模板,根据索引活跃程度分配资源优先级。
本例中,索引最初处于 hot 状态。7 天后转换为 warm,副本数量降为 1,并将索引移到带 warm 属性的节点。
30 天后,策略把索引转为 delete 状态。服务会向 Chime 房间发送索引即将删除的通知,然后永久删除索引。
PUT _plugins/_ism/policies/hot_warm_delete_policy
{
"policy": {
"description": "hot warm delete workflow",
"default_state": "hot",
"states": [
{
"name": "hot",
"actions": [
{
"rollover": {
"min_index_age": "7d",
"min_primary_shard_size": "30gb"
}
}
],
"transitions": [
{
"state_name": "warm"
}
]
},
{
"name": "warm",
"actions": [
{
"replica_count": {
"number_of_replicas": 1
}
},
{
"allocation": {
"require": {
"temp": "warm"
}
}
}
],
"transitions": [
{
"state_name": "delete",
"conditions": {
"min_index_age": "30d"
}
}
]
},
{
"name": "delete",
"actions": [
{
"notification": {
"destination": {
"chime": {
"url": "<URL>"
}
},
"message_template": {
"source": "The index {{ctx.index}} is being deleted"
}
}
},
{
"delete": {}
}
]
}
],
"ism_template": {
"index_patterns": ["index-*"],
"priority": 100
}
}
}
下图以有限状态机形式展示上述策略的状态、转换和动作。有限状态机的更多信息可参阅维基百科。

原文:Policy examples。作者/维护方:OpenSearch 文档维护者。本文为中文翻译,代码及命令保留原文。











暂无评论内容