前言
我这里的测试机器,单节点ES集群,索引状态一直为yellow,原因可能是副本分片没有正常分配。所以需要手动调整下ES的配置,将索引的副本数设置为0,后面的索引不会设置副本,另外索引的状态也会恢复为green。
具体实践
1.查看索引的副本数和分片数,使用json_pp
命令将输出结果转换为json格式
curl -XGET "http://172.16.108.119:9200/_settings"|json_pp
{
"test" : {
"settings" : {
"index" : {
"number_of_replicas" : "1", # 副本数
"creation_date" : "1667885308646",
"version" : {
"created" : "7010099"
},
"provided_name" : "test",
"number_of_shards" : "1", # 分片数
"uuid" : "0RbW0paxTjqEw_Rv0psdaA"
}
}
},
"testindex" : {
"settings" : {
"index" : {
"number_of_replicas" : "1",
"creation_date" : "1667886384321",
"version" : {
"created" : "7010099"
},
"provided_name" : "testindex",
"number_of_shards" : "1",
"uuid" : "15A8DayoTpqoT78HatP5-Q"
}
}
},
"testtest" : {
"settings" : {
"index" : {
"number_of_replicas" : "0",
"creation_date" : "1667886182068",
"version" : {
"created" : "7010099"
},
"provided_name" : "testtest",
"number_of_shards" : "1",
"uuid" : "rZqy77XmRvi-YTRnQQOiwQ"
}
}
}
}
2.获取索引的模板templates
[root@devops03 ~]# curl -XGET "http://172.16.108.119:9200/_cat/templates"
.watch-history-9 [.watcher-history-9*] 2147483647
.ml-meta [.ml-meta] 0 7010099
.monitoring-kibana [.monitoring-kibana-7-*] 0 7000199
.monitoring-beats [.monitoring-beats-7-*] 0 7000199
.ml-state [.ml-state*] 0 7010099
.watches [.watches*] 2147483647
.ml-anomalies- [.ml-anomalies-*] 0 7010099
.monitoring-alerts-7 [.monitoring-alerts-7] 0 7000199
.ml-notifications [.ml-notifications] 0 7010099
.ml-config [.ml-config] 0 7010099
index_defaults [*] 0
.logstash-management [.logstash] 0
.monitoring-logstash [.monitoring-logstash-7-*] 0 7000199
.triggered_watches [.triggered_watches*] 2147483647
.monitoring-es [.monitoring-es-7-*] 0 7000199
3.设置副本数为0,"number_of_replicas": 0
[root@devops03 ~]# curl -XPUT "http://172.16.108.119:9200/_template/index_defaults" -H 'Content-Type: application/json' -d'
{
"template": "*",
"settings": {
"number_of_replicas": 0,
"number_of_shards": 1
}
}'
4.此时,创建的新索引默认副本数就为0了。
# 创建新索引
[root@devops03 ~]# curl -XPUT "http://172.16.108.119:9200/testnewindex"
# 查看索引副本数
[root@devops03 ~]# curl -XGET "http://172.16.108.119:9200/_settings"|json_pp
"testnewindex" : {
"settings" : {
"index" : {
"number_of_replicas" : "0",
"creation_date" : "1667887446243",
"version" : {
"created" : "7010099"
},
"provided_name" : "testnewindex",
"number_of_shards" : "1",
"uuid" : "sWatJQnRTtigDrJAeyjKog"
}
}
}
}
补充说明:
单个索引临时修改副本数
创建一个只有主分片,没有副本的索引:
[root@devops03 ~]# curl -XPUT "http://172.16.108.119:9200/testindex?pretty" -H 'Content-Type: application/json' -d'
> {
> "settings": {
> "number_of_shards" : 1,
> "number_of_replicas" : 0
> }
> }
> '
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "testindex"
}
可以用update-index-settings
API动态修改副本数:
[root@devops03 ~]# curl -XPUT "http://172.16.108.119:9200/testindex/_settings?pretty" -H 'Content-Type: application/json' -d'
> {
> "number_of_replicas": 1
> }
> '
{
"acknowledged" : true
}
查看修改后的副本数:
[root@devops03 ~]# curl -XGET "http://172.16.108.119:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open testindex xQD99bFNRuKC1wQBwnNojA 1 1 0 0 230b 230b
[root@devops03 ~]# curl -XGET "http://172.16.108.119:9200/_settings"|json_pp
{
"testindex" : {
"settings" : {
"index" : {
"number_of_replicas" : "1",
"creation_date" : "1667889490896",
"version" : {
"created" : "7010099"
},
"provided_name" : "testindex",
"number_of_shards" : "1",
"uuid" : "xQD99bFNRuKC1wQBwnNojA"
}
}
}
}