docker下迁移elasticsearch问题及解决
直接上图,大致就是这样的操作

数据挂载
对于服务器a下的es如果你没有在启动容器的时候将数据挂载出来,就需要先进行docker cp,将容器中的数据拷贝到服务器中,/usr/share/elasticsearch/data
将取出的数据,或者说挂载的数据,传输到另外一个服务器中,执行启动命令
警告:这里最好版本不要变动,以及启动时候的命令也和原服务器一致
至此迁移差不多就完成了。
报错解决
权限问题
java.lang.illegalstateexception: failed to obtain node locks, tried [[/usr/share/elasticsearch/data]] with lock id [0]; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was [1])?
likely root cause: java.nio.file.nosuchfileexception: /usr/share/elasticsearch/data/nodes/0/node.lock
at java.base/sun.nio.fs.unixexception.translatetoioexception(unixexception.java:92)
at java.base/sun.nio.fs.unixexception.rethrowasioexception(unixexception.java:106)
at java.base/sun.nio.fs.unixexception.rethrowasioexception(unixexception.java:111)
at java.base/sun.nio.fs.unixpath.torealpath(unixpath.java:825)
at org.apache.lucene.store.nativefslockfactory.obtainfslock(nativefslockfactory.java:108)
at org.apache.lucene.store.fslockfactory.obtainlock(fslockfactory.java:41)
at org.apache.lucene.store.basedirectory.obtainlock(basedirectory.java:45)
at org.elasticsearch.env.nodeenvironment$nodelock.<init>(nodeenvironment.java:229)
at org.elasticsearch.env.nodeenvironment.<init>(nodeenvironment.java:298)
at org.elasticsearch.node.node.<init>(node.java:427)
at org.elasticsearch.node.node.<init>(node.java:309)
at org.elasticsearch.bootstrap.bootstrap$5.<init>(bootstrap.java:234)
at org.elasticsearch.bootstrap.bootstrap.setup(bootstrap.java:234)
at org.elasticsearch.bootstrap.bootstrap.init(bootstrap.java:434)
at org.elasticsearch.bootstrap.elasticsearch.init(elasticsearch.java:166)
at org.elasticsearch.bootstrap.elasticsearch.execute(elasticsearch.java:157)
at org.elasticsearch.cli.environmentawarecommand.execute(environmentawarecommand.java:77)
at org.elasticsearch.cli.command.mainwithouterrorhandling(command.java:112)
at org.elasticsearch.cli.command.main(command.java:77)
at org.elasticsearch.bootstrap.elasticsearch.main(elasticsearch.java:122)
at org.elasticsearch.bootstrap.elasticsearch.main(elasticsearch.java:80)
for complete error details, refer to the log at /usr/share/elasticsearch/logs/docker-cluster.log
1. 节点锁文件丢失:/usr/share/elasticsearch/data/nodes/0/node.lock 文件不存在。可能是因为文件未被正确创建,或者数据目录的权限不足,导致 elasticsearch 无法写入。
2. **目录或文件权限问题**:elasticsearch 容器可能没有足够的权限访问或修改 /usr/share/elasticsearch/data 目录下的文件。
解决如下:
执行以下命令,重新启动即可
sudo chown -r 1000:1000 /acowbo/es # 假设你将 /acowbo/es 挂载到 /usr/share/elasticsearch/data sudo chmod -r 775 /acowbo/es
节点故障
2024-10-29 02:15:31.912 error 1 --- [io-11919-exec-4] o.a.c.c.c.[.[.[/].[dispatcherservlet] : servlet.service() for servlet [dispatcherservlet] in context with path [] threw exception [request processing failed; nested exception is elasticsearchstatusexception[elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]] with root cause
根据错误日志中的信息,elasticsearch 报告了 all shards failed 和 no_shard_available_action_exception 错误。
这通常表示 elasticsearch 集群中有一个或多个分片不可用
1.执行
curl -x get "http://127.0.0.1:9200/_cluster/health?pretty"
这里你换为自己的ip和端口
{
"cluster_name" : "docker-cluster",
"status" : "red",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 17,
"active_shards" : 17,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 2,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 89.47368421052632
}elasticsearch 集群的 status 为 red,这表示集群中有一些分片处于未分配状态,导致集群不能正常工作。
特别是,你的集群中有 2 个未分配的分片(unassigned shards),这可能导致你遇到的 all shards failed 错误。
2.执行
curl -x get "http://156.224.28.178:9200/_cat/shards?v&pretty"
获取详细信息

3.查看分片未分配的原因
curl -x get "http://156.224.28.178:9200/_cluster/allocation/explain?pretty" -h 'content-type: application/json' -d '{
"index": "acowbo_new",
"shard": 0,
"primary": true
}'4.结果如下
{
"index" : "acowbo_new",
"shard" : 0,
"primary" : true,
"current_state" : "unassigned",
"unassigned_info" : {
"reason" : "allocation_failed",
"at" : "2024-10-29t02:26:16.215z",
"failed_allocation_attempts" : 5,
"details" : "failed shard on node [godpmtuqsbavpuahkq6yhq]: failed to create index, failure illegalargumentexception[custom analyzer [ik_analyzer] failed to find tokenizer under name [ik_smart]]",
"last_allocation_status" : "no"
},
"can_allocate" : "yes",
"allocate_explanation" : "can allocate the shard",
"target_node" : {
"id" : "godpmtuqsbavpuahkq6yhq",
"name" : "47d6ff15662f",
"transport_address" : "172.20.0.8:9300",
"attributes" : {
"ml.machine_memory" : "3973206016",
"xpack.installed" : "true",
"transform.node" : "true",
"ml.max_open_jobs" : "512",
"ml.max_jvm_size" : "268435456"
}
},
"allocation_id" : "kgcnr2e2sjuotrow7otuea",
"node_allocation_decisions" : [
{
"node_id" : "godpmtuqsbavpuahkq6yhq",
"node_name" : "47d6ff15662f",
"transport_address" : "172.20.0.8:9300",
"node_attributes" : {
"ml.machine_memory" : "3973206016",
"xpack.installed" : "true",
"transform.node" : "true",
"ml.max_open_jobs" : "512",
"ml.max_jvm_size" : "268435456"
},
"node_decision" : "yes",
"store" : {
"in_sync" : true,
"allocation_id" : "kgcnr2e2sjuotrow7otuea"
}
}
]
}从错误信息来看,分片未能分配的原因是由于自定义分析器 ik_analyzer 未能找到名为 ik_smart 的分词器。
这通常意味着在 elasticsearch 的设置中配置的 ik_analyzer 依赖于一个未安装或未正确配置的分词器。
解决如下:
- 进入容器
docker exec -it 容器名/容器id /bin/bash
- 执行
bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.16.2/elasticsearch-analysis-ik-7.16.2.zip
这里需要看你的es是什么版本的,就安装什么版本的插件
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论