Elasticsearch跨集群重索引报unknown_host_exception问题求助
嘿,这个问题我之前排查过类似的!你遇到的unknown_host_exception根本原因是Elasticsearch解析远程主机地址的逻辑和curl不一样:
你把完整的路径/api/elasticsearch和主机地址拼在一起塞给了remote.host参数,但这个字段只负责识别主机名+端口(http默认80、https默认443可以省略),后面的路径部分它会直接当成主机名的一部分去DNS解析,自然找不到叫api.example.com/api/elasticsearch的主机,所以抛出了“名称或服务未知”的错误。
而你用curl http://api.example.com:80/api/elasticsearch/_cat/indices?v能成功,是因为curl会把整个URL拆成主机、端口、路径三部分去处理,Nginx也能正确匹配到对应的代理路径,但Elasticsearch的_reindex模块不支持这种写法。
修正后的重索引命令
把路径部分单独抽出来,用remote下的path_prefix参数指定,正确的命令如下:
curl -X POST http://elasticsearch:9200/_reindex -d '{ "source": { "remote": { "host": "http://api.example.com", "path_prefix": "/api/elasticsearch" }, "index": "alias_name" }, "dest": { "index": "index_name" } }'
如果你的远程服务必须显式指定80端口,也可以把host写成"http://api.example.com:80",不过http协议默认就是80端口,不写也完全没问题。
额外要检查的配置
别忘记确认本地Elasticsearch已经允许这个远程主机的重索引请求:
打开elasticsearch.yml配置文件,检查是否存在以下配置(如果没有就加上):
reindex.remote.whitelist: "api.example.com:80"
如果省略了端口,也可以写成reindex.remote.whitelist: "api.example.com"。修改后需要重启Elasticsearch才能生效。
内容的提问来源于stack exchange,提问作者Khmtrav

