【问题标题】:how can I connect, from a flask app, to a remote elasticsearch cluster on aws?如何从烧瓶应用程序连接到 aws 上的远程弹性搜索集群?
【发布时间】:2019-05-13 16:42:29
【问题描述】:

我有一个在 EC2 服务器上运行的 elasticsearch 集群。尝试连接时收到各种不同的错误消息。

目前,在 elasticsearch.yml 文件中,所有传输项都被注释掉了,但我已经尝试过:

network.host: 0.0.0.0
and
network.host: ec2-xx-xxx-xxx.aws.instance.com

在我的烧瓶应用中,代码如下:

from datetime import datetime
from flask import Flask, jsonify, request
from elasticsearch import Elasticsearch

#es = Elasticsearch()http://34.245.51.240/
es = Elasticsearch(['ec2-34-xxx-xx-240.eu-west-1.compute.amazonaws.com','9200'])
#es = Elasticsearch(['34.245.51.240','9200'])

application = Flask(__name__)

@application.route('/', methods=['GET'])
def index():
    #results = es.get(index='contents', doc_type='title', id='my-new-slug')
    #return jsonify(results['_source'])
    doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
    }
    res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
    print(res['res'])
    return res


#application.run(port=5000, debug=True)
if __name__ == '__main__':
    application.debug = True
    application.run()

我已经多次搜索并尝试了我能找到的所有可能的配置。

实现这一目标的正确方法是什么?

谢谢。

【问题讨论】:

  • 确保 ElasticSearch 实例上的安全组接受来自烧瓶应用程序的端口 9200 上的传入连接。
  • 谢谢。我在 elasticsearch.yml 文件中找不到任何特定的“安全组”项目,我设置了 host.port:0.0.0.0,除此之外我看不到任何其他我能做的事情。请您说得更具体些吗?
  • 安全组将位于您用于运行 ElasticSearch 的 EC2 实例上。除非您使用的是 AWS 提供的 ElasticSearch 服务,否则情况并非如此。
  • 谢谢好点。我会检查并确保它设置为 0.0.0.0 并报告回来。
  • 这不是问题所在。就是没有开放9200端口。谢谢。

标签: python amazon-web-services elasticsearch flask


【解决方案1】:

我希望这对其他人有所帮助。

elasticsearch 默认使用 9200 端口,因此需要在 EC2 服务器上打开这样的端口:

CustomTCP TCP 9200 0.0.0.0

这是通过在设置服务器时编辑设置安全组的安全组向导来完成的。

那么在你的 Python 应用程序中,连接字符串是:

es = Elasticsearch("http://00.111.222.33")  //the public IP you can see on your EC2 dashboard

就是这样。数小时的痛苦,如此简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2019-07-19
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多