📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

更新集群的设置配置,支持持久化设置和临时设置两种类型。

API #

PUT /_cluster/settings
GET /_cluster/settings

API 的作用 #

该 API 用于更新或获取集群的设置配置。Easysearch 支持两种类型的设置:

设置类型描述持久性
persistent(持久化设置)在集群重启后仍然保持设置的值持久
transient(临时设置)只在当前集群会话中有效,集群重启后恢复默认值临时

常见集群设置 #

设置路径描述
cluster.routing.allocation.enable控制分片分配
cluster.routing.allocation.include._attr包含具有特定属性的节点
cluster.routing.allocation.exclude._attr排除具有特定属性的节点
cluster.routing.allocation.awareness.attributes分片感知属性
cluster.max_shards_per_node每个节点的最大分片数
indices.memory.index_buffer_size索引缓冲区大小
cluster.routing.rebalance.enable控制分片重平衡
cluster.blocks.read_only将集群设为只读

API 的参数 #

Query String 参数 #

参数类型是否必填默认值描述
timeout时间值默认超时操作超时时间
master_timeout时间值默认超时连接到主节点的超时时间
flat_settings布尔值false是否以扁平格式返回设置(仅对 GET 有效)

请求体参数(PUT/POST 请求时使用) #

参数类型是否必填描述
persistent对象包含要更新的持久化设置,集群重启后仍然保持
transient对象包含要更新的临时设置,集群重启后恢复默认值

注意:请求体必须至少包含 persistenttransient 中的一个,否则会返回错误。

示例 #

更新持久化设置 #

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

更新临时设置 #

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

同时更新两种设置 #

PUT /_cluster/settings
{
  "persistent": {
    "indices.memory.index_buffer_size": "30%"
  },
  "transient": {
    "cluster.routing.allocation.enable": "all"
  }
}

响应示例:

{
  "acknowledged": true,
  "persistent": {
    "indices.memory.index_buffer_size": "30%"
  },
  "transient": {
    "cluster.routing.allocation.enable": "all"
  }
}

禁用分片分配(维护模式) #

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

重新启用分片分配 #

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

排除特定节点不分配新分片 #

PUT /_cluster/settings
{
  "persistent": {
    "cluster.routing.allocation.exclude._host": "maintenance-server"
  }
}

设置集群为只读模式 #

PUT /_cluster/settings
{
  "persistent": {
    "cluster.blocks.read_only": true
  }
}

解除只读模式 #

PUT /_cluster/settings
{
  "persistent": {
    "cluster.blocks.read_only": false
  }
}

设置超时时间 #

PUT /_cluster/settings?master_timeout=10s
{
  "transient": {
    "cluster.routing.rebalance.enable": "all"
  }
}

删除设置(设置为 null) #

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

获取集群设置(GET) #

GET /_cluster/settings

响应示例:

{
  "persistent": {
    "cluster.routing.allocation.enable": "all",
    "cluster.max_shards_per_node": "1000"
  },
  "transient": {}
}

以扁平格式获取设置 #

GET /_cluster/settings?flat_settings=true

响应示例:

{
  "persistent": {
    "cluster.routing.allocation.enable": "all",
    "cluster.max_shards_per_node": "1000"
  }
}

分片分配控制选项 #

描述
all允许所有分片分配
primaries只允许主分片分配
new_primaries只允许新创建的主分片分配
none禁止所有分片分配
replicas只允许副本分片分配