![](/img/trans.png)
[英]urlopen error [Errno 111] Connection refused in python when connecting from one VM to another
[英]Errno 111: Connection refused when connecting to Elasticsearch with Python Script
我遇到了一個 dockerized elasticsearch 集群的問題,我想用一個簡單的 python 索引腳本連接它。
執行 docker-compose up 后,它會啟動集群、kibana,但是當它嘗試通過 python 腳本連接到 ES 時,出現以下錯誤:
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f1c04e0c0d0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f1c04e0c0d0>: Failed to establish a new connection: [Errno 111] Connection refused)
用瀏覽器可以到ES,執行pycharm里面的腳本也可以定時索引。所以跟docker有關,但是找不到問題的根源。 這是我的 dockerfile:
version: '3.4'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
container_name: es01
environment:
#- discovery.type=single-node needed?
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- xpack.security.enabled=false
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data02:/usr/share/elasticsearch/data
networks:
- elastic
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data03:/usr/share/elasticsearch/data
networks:
- elastic
kib01:
image: docker.elastic.co/kibana/kibana:7.8.1
container_name: kib01
depends_on:
- es01
- es02
- es03
ports:
- 5601:5601
environment:
ELASTICSEARCH_URL: http://es01:9200
ELASTICSEARCH_HOSTS: http://es01:9200
networks:
- elastic
web:
build: .
ports:
- 8000:8000
depends_on:
- es01
- es02
- es03
networks:
- elastic
volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local
networks:
elastic:
driver: bridge
和 python 腳本:
from elasticsearch import Elasticsearch, helpers
import sys, json, os
import requests
es = Elasticsearch([{'host': '127.0.0.1', 'port': 9200}])
def load_json(directory):
" Use a generator, no need to load all in memory"
for filename in os.listdir(directory):
if filename.endswith('.json'):
filename = "JSON/" + filename
with open(filename,'r') as open_file:
yield json.load(open_file)
if __name__ == '__main__':
helpers.bulk(es, load_json("JSON"), index='urteile')
容器外部的 Curl localhost 有效,但容器內部無效,並給出以下錯誤:
docker 文件中的 curl 命令也給我一個 Connection refused by the way。 這是我得到的錯誤:
mapping_1 | * TCP_NODELAY set
mapping_1 | * connect to 127.0.0.1 port 9200 failed: Connection refused
mapping_1 | * Trying ::1...
mapping_1 | * TCP_NODELAY set
mapping_1 | * Immediate connect fail for ::1: Address not available
mapping_1 | * Trying ::1...
mapping_1 | * TCP_NODELAY set
mapping_1 | * Immediate connect fail for ::1: Address not available
mapping_1 | * Failed to connect to localhost port 9200: Connection refused
mapping_1 | * Closing connection 0
mapping_1 | curl: (7) Failed to connect to localhost port 9200: Connection refused
任何人都知道這里可能是什么問題?
編輯:
好的,所以使用 es01 而不是 localhost 的評論有所幫助,因為當插入“客戶端”服務時,它將成功 ping Elasticsearch。仍然 - python 應用程序告訴我連接被拒絕......
您在 python 代碼中使用 localhost 作為 ES 集群地址。 ES 在單獨的容器上運行,因此您需要使用這些主機名。 在您的情況下,您可以使用 docker 中聲明的服務名稱組成 yaml 文件(es01、es02 等)而不是 127.0.0.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.