如何从Docker容器连接到本地主机Elasticsearch

时间:2019-07-18 16:57:57

标签: docker elasticsearch

所以我有一个本地运行的elasticsearch实例:

elasticsearch
OpenJDK 64-Bit Server VM warning: Cannot open file logs/gc.log due to No such file or directory
...starting 
...
[2019-07-18T12:45:46,137][INFO ][o.e.t.TransportService   ] [H1YDvcg] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2019-07-18T12:45:49,182][INFO ][o.e.c.s.MasterService    ] [H1YDvcg] zen-disco-elected-as-master ([0] nodes joined), reason: new_master {H1YDvcg}{H1YDvcgbTMSnTmVdTNQo7A}{9j3_6Y8LTg2KaImHpCQvaw}{127.0.0.1}{127.0.0.1:9300}
[2019-07-18T12:45:49,186][INFO ][o.e.c.s.ClusterApplierService] [H1YDvcg] new_master {H1YDvcg}{H1YDvcgbTMSnTmVdTNQo7A}{9j3_6Y8LTg2KaImHpCQvaw}{127.0.0.1}{127.0.0.1:9300}, reason: apply cluster state (from master [master {H1YDvcg}{H1YDvcgbTMSnTmVdTNQo7A}{9j3_6Y8LTg2KaImHpCQvaw}{127.0.0.1}{127.0.0.1:9300} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)]])
[2019-07-18T12:45:49,198][INFO ][o.e.h.n.Netty4HttpServerTransport] [H1YDvcg] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2019-07-18T12:45:49,199][INFO ][o.e.n.Node               ] [H1YDvcg] started
[2019-07-18T12:45:49,343][INFO ][o.e.g.GatewayService     ] [H1YDvcg] recovered [1] indices into cluster_state
[2019-07-18T12:45:49,511][INFO ][o.e.c.r.a.AllocationService] [H1YDvcg] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[person][2]] ...]).

我可以通过浏览器点击localhost:9200

我还试图为使用此ES进程的应用程序运行docker容器。

docker run -e ES_HOST=host.docker.internal -e ES_PORT='9200' -e AWS_ACCESS_KEY_ID='<some_key>' -e AWS_SECRET_ACCESS_KEY='<some_key>'             -e AWS_DEFAULT_REGION='us-west-2' <application_name>

我的es客户端如下所示(python):

import ssl
from elasticsearch import Elasticsearch, RequestsHttpConnection
from elasticsearch.connection import create_ssl_context
from aws_requests_auth.boto_utils import 

# this is temporary to ignore SSL certs while working in the sandbox
ssl_context = create_ssl_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

# create the client
try:
    _es_host = environ["ES_HOST"]
    _es_port = int(environ["ES_PORT"])
except KeyError:
    logger.error("Environment variables ES_HOST and ES_PORT must be set")
    exit(1)

# see https://github.com/DavidMuller/aws-requests-auth
auth = BotoAWSRequestsAuth(aws_host=_es_host,
                           aws_region="us-west-2",
                           aws_service="es")

es_client = Elasticsearch(host=_es_host,
                          port=_es_port,
                          connection_class=RequestsHttpConnection,
                          scheme="http" if "localhost" in _es_host else "https",
                          use_ssl=False,
                          ssl_context=ssl_context,
                          http_auth=auth)

我得到这个错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='host.docker.internal', port=9200): Max retries exceeded with url: /person/_search (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1076)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/elasticsearch/connection/http_requests.py", line 76, in perform_request
    response = self.session.send(prepared_request, **send_kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='host.docker.internal', port=9200): Max retries exceeded with url: /person/_search (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1076)')))
^CTraceback (most recent call last):
  File "dataloader.py", line 267, in <module>
    receive_messages(assertions_queue, dlq, primary_queue_name.endswith(".fifo"))
  File "dataloader.py", line 133, in receive_messages
    handle_assertion_message(message)
  File "dataloader.py", line 46, in handle_assertion_message
    persons = execute_match_rule(match_rule_clauses)
  File "dataloader.py", line 22, in execute_match_rule
    return search_for_matches(PERSON_INDEX, match_rule_clauses)
  File "dataloader.py", line 232, in search_for_matches

这似乎是某种ssl错误。有人对如何解决这个问题有任何想法吗?

我使用host.docker.internal是因为es位于我的计算机的本地主机上,而不是我的Docker容器的本地主机上。这样说对吗?

0 个答案:

没有答案