在docker中运行elasticsearch时go程序无法连接到节点

时间:2023-03-09 09:06:15
在docker中运行elasticsearch时go程序无法连接到节点

错误信息:

panic: no active connection found: no Elasticsearch node available

在docker中运行es时,默认启动sniffing 模式,es自动查找节点, 要确保Elasticsearch返回可从容器外部访问的IP地址。

解决方式为关闭sniffing或者配置es返回的主机地址,参考 https://github.com/olivere/elastic/wiki/Docker

1. 配置elasticsearch

- 将network.publish_host和network.bind_host配置为可从容器外部访问的地址

- 配置network.host (不用再单独配置network.bind_host 和 network.publish_host

2. 禁用 sniffing

elastic.SetSniff(false)

# 查看节点信息,详细返回参数:Nodes Info
curl -XGET '0.0.0.0:9200/_nodes/http?pretty'
{
"_nodes" : {
"total" : ,
"successful" : ,
"failed" :
},
"cluster_name" : "escluster",
"nodes" : {
"nodename" : {
"name" : "name",
"transport_address" : "xxxx:9300",
"host" : "xxxx",
"ip" : "xxxx",
"version" : "6.4.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "e36acdb",
"roles" : [
"master",
"data",
"ingest"
],
"attributes" : {
"ml.machine_memory" : "",
"xpack.installed" : "true",
"ml.max_open_jobs" : "",
"ml.enabled" : "true"
},
"http" : {
"bound_address" : [
"0.0.0.0:9200" //http服务绑定的主机地址,配置为可从容器外部访问的地址
],
"publish_address" : "xxxx:9200", //http客户端发布的主机地址,配置为可从容器外部访问的地址
"max_content_length_in_bytes" :
}
}
}
}