gpt4 book ai didi

python - 无法连接到Docker中的Elasticsearch

转载 作者:行者123 更新时间:2023-12-03 02:22:25 36 4
gpt4 key购买 nike

我无法连接到docker内部kubernetes中的elasticsearch。我的Elasticsearch是通过kubernetes访问的,我有一个名为“radius_ml_posts”的索引。我正在使用elasticsearch的python库连接到elasticsearch。当我在python IDE(Spyder)上运行整个过程时,它工作正常。但是,当我尝试在docker容器中运行它时,出现连接问题。我想念什么?以下是我的配置和代码:
localhost:9200:

{
"name" : "elasticsearch-dev-client-6858c5f9dc-zbz8p",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "lJJbPJpJRaC1j7k5IGhj7g",
"version" : {
"number" : "6.7.0",
"build_flavor" : "oss",
"build_type" : "docker",
"build_hash" : "8453f77",
"build_date" : "2019-03-21T15:32:29.844721Z",
"build_snapshot" : false,
"lucene_version" : "7.7.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

我的python代码连接到elasticsearch主机:
def get_data_es(question):
es = Elasticsearch(hosts=[{"host": "elastic", "port": 9200}], connection_class=RequestsHttpConnection, max_retries=30,
retry_on_timeout=True, request_timeout=30)
#es = Elasticsearch(hosts='http://host.docker.internal:5000', connection_class=RequestsHttpConnection, max_retries=30, timeout=30)
doc = {'author': 'gunner','text': 'event', "timestamp": datetime.now()}
es.indices.refresh(index="radius_ml_posts")
res = es.index(index="radius_ml_posts", id = 1, body = doc)
res = es.search(index="radius_ml_posts", size = 30, body={ "query": {
"query_string": {
"default_field": "search_text",
"query": question
}
}
}
)
return res

我的 docker-compose.yml文件:
version: '2.2'
services:
elastic:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.7.0
container_name: elastic
environment:
- discovery.type=single-node
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9300:9300
- 9200:9200
networks:
- elastic

myimage:
image: myimage:myversion
ports:
- 5000:5000
expose:
- 5000
networks:
- elastic


volumes:
data01:
driver: local

networks:
elastic:
driver: bridge

我的 Dockerfile:
FROM python:3.7.4

COPY . /app
WORKDIR /app

RUN pip install --upgrade pip
RUN pip3 install -U nltk
RUN python3 -m nltk.downloader all
RUN pip --default-timeout=100 install -r requirements.txt

EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["main.py"]

我正在逐步运行的docker命令:
  • docker build -t myimage:myversion .
  • docker-compose up

  • 我得到的错误:
    myimage_1            | Traceback (most recent call last):
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    myimage_1 | response = self.full_dispatch_request()
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    myimage_1 | rv = self.handle_user_exception(e)
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    myimage_1 | reraise(exc_type, exc_value, tb)
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    myimage_1 | raise value
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    myimage_1 | rv = self.dispatch_request()
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    myimage_1 | return self.view_functions[rule.endpoint](**req.view_args)
    myimage_1 | File "main.py", line 41, in launch_app
    myimage_1 | ques = get_data_es(ques1)
    myimage_1 | File "/app/Text_Cleaning.py", line 32, in get_data_es
    myimage_1 | es.indices.refresh(index="radius_ml_posts")
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/utils.py", line 92, in _wrapped
    myimage_1 | return func(*args, params=params, headers=headers, **kwargs)
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/indices.py", line 42, in refresh
    myimage_1 | "POST", _make_path(index, "_refresh"), params=params, headers=headers
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/elasticsearch/transport.py", line 362, in perform_request
    myimage_1 | timeout=timeout,
    myimage_1 | File "/usr/local/lib/python3.7/site-packages/elasticsearch/connection/http_requests.py", line 157, in perform_request
    myimage_1 | raise ConnectionError("N/A", str(e), e)
    myimage_1 | elasticsearch.exceptions.ConnectionError: ConnectionError(HTTPConnectionPool(host='elastic', port=9200): Max retries exceeded with url: /radius_ml_posts/_refresh (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f967a9b1710>: Failed to establish a new connection: [Errno -2] Name or service not known'))) caused by: ConnectionError(HTTPConnectionPool(host='elastic', port=9200): Max retries exceeded with url: /radius_ml_posts/_refresh (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f967a9b1710>: Failed to establish a new connection: [Errno -2] Name or service not known')))

    请帮助解决问题。

    提前致谢。

    最佳答案

    您可以尝试在应用程序环境部分中将ELASTICSEARCH_NODES变量设置为,然后在python代码中将其作为http://ELASTICSEARCH_NODES使用:

    version: '2.2'
    services:
    elastic:
    image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.7.0
    container_name: elastic
    environment:
    - discovery.type=single-node
    - bootstrap.memory_lock=true
    - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
    memlock:
    soft: -1
    hard: -1
    volumes:
    - data01:/usr/share/elasticsearch/data
    ports:
    - 9300:9300
    - 9200:9200
    networks:
    - elastic

    myimage:
    image: myimage:myversion
    depends_on:
    - elastic
    environment:
    - ELASTICSEARCH_NODES=http://elastic:9200
    ports:
    - 5000:5000
    expose:
    - 5000
    networks:
    - elastic


    volumes:
    data01:
    driver: local

    networks:
    elastic:
    driver: bridge

    关于python - 无法连接到Docker中的Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62034833/

    36 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com