我们可以在获取索引时调整Python Elasticsearch连接参数

时间:2017-11-15 10:56:53

标签: python elasticsearch

希望有人可以使用Python Elasticsearch客户端(http://elasticsearch-py.readthedocs.io/en/master/)与类似的经验和可能的解决方案发表评论

>>> from elasticsearch import Elasticsearch
>>> import requests 
>>> import json
>>> es = Elasticsearch(['http://es-service:9200'],sniff_on_start=True, sniff_timeout=300)
>>> es.indices.get_alias("*")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/user/tester/pyenv/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/user/tester/pyenv/lib/python2.7/site-packages/elasticsearch/client/indices.py", line 416, in get_alias
    '_alias', name), params=params)
  File "/user/tester/pyenv/lib/python2.7/site-packages/elasticsearch/transport.py", line 329, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/user/tester/pyenv/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 105, in perform_request
    raise ConnectionError('N/A', str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError((<urllib3.connection.HTTPConnection object at 0x7fb46baa8750>, u'Connection to 192.168.5.245 timed out. (connect timeout=10)')) caused by: ConnectTimeoutError((<urllib3.connection.HTTPConnection object at 0x7fb46baa8750>, u'Connection to 192.168.5.245 timed out. (connect timeout=10)'))

[root@myhost ~]# nslookup es-service
Server:         172.16.50.150
Address:        172.16.50.150#53

Name:   es-service.somedomain.com
Address: 10.90.5.245

问题是,es-service中的Elasticsearch配置为使用 192.168.5.245作为传输地址。这是与10.90.5.245不同的界面,并不是可以从外部访问的。

>>> r = requests.get('http://es-service:9200/_nodes/_all/clear.HTTP?pretty')
>>> print(r.text)
{
  "cluster_name" : "super-es",
  "nodes" : {
    "oOabsR4dSzC7zxNVnX0l5g" : {
      "name" : "Bounty Hunter",
      "transport_address" : "192.168.5.245:9300",
      "host" : "192.168.5.245",
      "ip" : "192.168.5.245",
      "version" : "2.4.1",
      "build" : "c67dc32",
      "http_address" : "192.168.5.245:9200"
    },
    "unCQYE3jRBSMiMxs9DRG0A" : {
      "name" : "Jack Flag",
      "transport_address" : "192.168.5.220:9300",
      "host" : "192.168.5.220",
      "ip" : "192.168.5.220",
      "version" : "2.4.1",
      "build" : "c67dc32",
      "http_address" : "192.168.5.220:9200"
    }
  }
}

问题:有没有办法告诉Python或Elasticsearch模块使用我用作主机的IP,而不是它从es-service接收的传输IP?

欢迎提出意见和建议。

0 个答案:

没有答案