1、扩容
当前节点,如果有密码,可以加参数 -u user:passwd 方式认证
[root@centos-164 ~]# curl http://127.0.0.1:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.20.20.165 36 22 0 0.00 0.01 0.05 dilm * es02
172.20.20.164 54 33 0 0.05 0.10 0.07 dilm - es01
172.20.20.166 47 23 0 0.00 0.01 0.05 dilm - es03
[root@centos-164 ~]#
各个节点上已有的数据:
[root@centos-164 ~]# curl http://127.0.0.1:9200/_cat/allocation?v
shards disk.indices disk.used disk.avail disk.total disk.percent host ip node
4 18.8mb 6.9gb 48.1gb 55gb 12 172.20.20.164 172.20.20.164 es01
4 6.5mb 5gb 50gb 55gb 9 172.20.20.165 172.20.20.165 es02
4 24.1mb 5.7gb 20.2gb 25.9gb 22 172.20.20.166 172.20.20.166 es03
[root@centos-164 ~]#
索引健康情况:
[root@centos-164 ~]# curl http://127.0.0.1:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .monitoring-es-7-2022.09.05 jVP55X-hQ1medkar8fBEfw 1 1 40179 28614 36mb 17.9mb
green open .kibana_task_manager_1 tRoUNbDQTb-Y4hIUxfUQeA 1 1 2 2 113.6kb 56.8kb
green open .monitoring-kibana-7-2022.09.05 rNfgudsaR1-Y6Dvo11LsnQ 1 1 2859 0 1.3mb 703.7kb
green open .apm-agent-configuration lUpIK18XT_6WmzoOS1p_bQ 1 1 0 0 566b 283b
green open nginx_access_log_2022-09-05 2VhHP-voTBO3aNskr19imA 1 1 9465 0 11.9mb 5.7mb
green open .kibana_1 y1tot0pISA6JF8WcJ5LRaw 1 1 10 3 93.2kb 45kb
[root@centos-164 ~]#
索引分片情况:
[root@centos-164 ~]# curl http://127.0.0.1:9200/_cat/shards?v
index shard prirep state docs store ip node
.monitoring-kibana-7-2022.09.05 0 p STARTED 2859 703.7kb 172.20.20.164 es01
.monitoring-kibana-7-2022.09.05 0 r STARTED 2859 719.6kb 172.20.20.165 es02
.kibana_task_manager_1 0 r STARTED 2 31.6kb 172.20.20.164 es01
.kibana_task_manager_1 0 p STARTED 2 31.6kb 172.20.20.165 es02
.monitoring-es-7-2022.09.05 0 r STARTED 40179 18mb 172.20.20.164 es01
.monitoring-es-7-2022.09.05 0 p STARTED 40179 18mb 172.20.20.166 es03
.apm-agent-configuration 0 p STARTED 0 283b 172.20.20.165 es02
.apm-agent-configuration 0 r STARTED 0 283b 172.20.20.166 es03
.kibana_1 0 p STARTED 10 45kb 172.20.20.164 es01
.kibana_1 0 r STARTED 10 48.1kb 172.20.20.166 es03
nginx_access_log_2022-09-05 0 p STARTED 9465 5.7mb 172.20.20.165 es02
nginx_access_log_2022-09-05 0 r STARTED 9486 6.1mb 172.20.20.166 es03
[root@centos-164 ~]#
索引碎片恢复进度,这个功能主要在扩容或者缩容进行中、分片或副本转移时查看
[root@centos-164 ~]# curl http://127.0.0.1:9200/_cat/recovery?v
index shard time type stage source_host source_node target_host target_node repository snapshot files files_recovered files_percent files_total bytes bytes_recovered bytes_percent bytes_total translog_ops translog_ops_recovered translog_ops_percent
.monitoring-kibana-7-2022.09.05 0 27ms empty_store done n/a n/a 172.20.20.164 es01 n/a n/a 0 0 0.0% 0 0 0 0.0% 0 0 0 100.0%
.monitoring-kibana-7-2022.09.05 0 184ms peer done 172.20.20.164 es01 172.20.20.165 es02 n/a n/a 1 1 100.0% 1 230 230 100.0% 230 1 1 100.0%
.kibana_task_manager_1 0 148ms peer done 172.20.20.165 es02 172.20.20.164 es01 n/a n/a 0 0 0.0% 0 0 0 0.0% 0 0 0 100.0%
.kibana_task_manager_1 0 161ms existing_store done n/a n/a 172.20.20.165 es02 n/a n/a 0 0 100.0% 19 0 0 100.0% 32411 0 0 100.0%
.monitoring-es-7-2022.09.05 0 372ms peer done 172.20.20.166 es03 172.20.20.164 es01 n/a n/a 1 1 100.0% 1 230 230 100.0% 230 21 21 100.0%
.monitoring-es-7-2022.09.05 0 39ms empty_store done n/a n/a 172.20.20.166 es03 n/a n/a 0 0 0.0% 0 0 0 0.0% 0 0 0 100.0%
.apm-agent-configuration 0 134ms existing_store done n/a n/a 172.20.20.165 es02 n/a n/a 0 0 100.0% 1 0 0 100.0% 283 0 0 100.0%
.apm-agent-configuration 0 168ms peer done 172.20.20.165 es02 172.20.20.166 es03 n/a n/a 0 0 0.0% 0 0 0 0.0% 0 0 0 100.0%
nginx_access_log_2022-09-05 0 25ms empty_store done n/a n/a 172.20.20.165 es02 n/a n/a 0 0 0.0% 0 0 0 0.0% 0 0 0 100.0%
nginx_access_log_2022-09-05 0 1.4s peer done 172.20.20.165 es02 172.20.20.166 es03 n/a n/a 1 1 100.0% 1 230 230 100.0% 230 523 321 61.4%
.kibana_1 0 200ms existing_store done n/a n/a 172.20.20.164 es01 n/a n/a 0 0 100.0% 13 0 0 100.0% 17262 0 0 100.0%
.kibana_1 0 274ms peer done 172.20.20.164 es01 172.20.20.166 es03 n/a n/a 0 0 0.0% 0 0 0 0.0% 0 0 0 100.0%
[root@centos-164 ~]#
也可以通过http://127.0.0.1:9200/_cluster/health地址返回的数据
“cluster_name”: “my-es”, #集群名
“status”: “yellow”, #集群健康状态,正常的话是green,缺少副本分片为yellow,缺少主分片为red
“timed_out”: false,
“number_of_nodes”: 1,#集群节点数
“number_of_data_nodes”: 1,#数据节点数
“active_primary_shards”: 15,#主分片数
“active_shards”: 15,#可用的分片数
“relocating_shards”: 0,#正在迁移的分片数
“initializing_shards”: 0,#正在初始化的分片数
“unassigned_shards”: 15, #未分配的分片,但在集群中存在
“delayed_unassigned_shards”: 0, #延时待分配到具体节点上的分片数
“number_of_pending_tasks”: 0, #待处理的任务数,指主节点创建索引并分配shards等任务
“number_of_in_flight_fetch”: 0, #未完成的提取次数
“task_max_waiting_in_queue_millis”: 0, #任务最大等待时长(以毫秒为单位)
“active_shards_percent_as_number”: 50 #可用分片数占总分片的比例
开始扩容,停止分片重新分配
curl -H "Content-Type: application/json" -X PUT "http://127.0.0.1:9200/_cluster/settings?pretty" -d '
{
"persistent": {
"cluster.routing.allocation.enable": "none"
}
}'
参数解释
persistent ——永久变更配置
临时(Transient)这些变更在集群重启之前一直会生效。一旦整个集群重启,这些配置就被清除。
永久(Persistent)这些变更会永久存在。即使全集群重启它们也会存活下来并覆盖掉静态配置文件里的选项。
cluster.routing.allocation.enable ——分片分配模式
all -(默认)允许为所有类型的碎片分配碎片,设置为null也可以,他俩一个意思。
primaries -仅允许主碎片分配碎片。
new_primaries -仅允许为新索引的主碎片分配碎片。
none -任何索引都不允许任何类型的碎片分配。
修改新加入的es配置文件,旧的3台服务器不用动,也不用重启
elasticsearch.yml文件改动2个地方:
node.name: es04
discovery.seed_hosts: ["172.20.20.164", "172.20.20.165", "172.20.20.166", "172.20.20.163"]
新加入的ip为172.20.20.163,其他地方可以保持和旧节点一致
启动新节点es,在旧节点上验证
[root@centos-164 ~]# curl http://127.0.0.1:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.20.20.165 43 22 0 0.00 0.01 0.05 dilm * es02
172.20.20.164 77 34 0 0.05 0.03 0.05 dilm - es01
172.20.20.163 29 80 4 0.46 0.21 0.12 dilm - es04
172.20.20.166 24 23 0 0.00 0.01 0.05 dilm - es03
[root@centos-164 ~]#
已经加入到集群中了,开启数据分配
curl -H "Content-Type: application/json" -X PUT "http://127.0.0.1:9200/_cluster/settings?pretty" -d '
{
"persistent": {
"cluster.routing.allocation.enable": "all"
}
}'
可以通过上面的查看方式观察各节点数据同步情况
2、缩容
首先禁止数据分配在某一节点上,将上面新加入的节点剔除出去
curl -H "Content-Type: application/json" -X PUT "http://127.0.0.1:9200/_cluster/settings?pretty" -d '
{
"transient" : {
"cluster.routing.allocation.exclude._ip" : "172.20.20.163"
}
}'
执行之后查看节点在集群内的情况
[root@centos-164 ~]# curl http://127.0.0.1:9200/_cat/allocation?v
shards disk.indices disk.used disk.avail disk.total disk.percent host ip node
4 19.7mb 6.9gb 48.1gb 55gb 12 172.20.20.164 172.20.20.164 es01
0 0b 10gb 45gb 55gb 18 172.20.20.163 172.20.20.163 es04
4 5.9mb 5gb 50gb 55gb 9 172.20.20.165 172.20.20.165 es02
4 25.8mb 5.7gb 20.2gb 25.9gb 22 172.20.20.166 172.20.20.166 es03
[root@centos-164 ~]#
通过第二列数据可以看出该节点已经没有数据了
开始缩容,关闭该节点的es,再查看集群状态
[root@centos-164 ~]# curl http://127.0.0.1:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.20.20.165 45 22 0 0.00 0.01 0.05 dilm * es02
172.20.20.164 72 34 0 0.09 0.16 0.11 dilm - es01
172.20.20.166 46 23 1 0.00 0.01 0.05 dilm - es03
[root@centos-164 ~]#
节点已经不在了,最后需要将剔除节点列表置空
curl -H "Content-Type: application/json" -X PUT "http://127.0.0.1:9200/_cluster/settings?pretty" -d '
{
"transient" : {
"cluster.routing.allocation.exclude._ip" : "null"
}
}'
缩容完成。