如何从Flask应用程序连接到EC2上的Elasticsearch集群?

时间:2020-02-16 10:30:37

标签: python elasticsearch flask amazon-ec2

我有一台运行Ubuntu和4G RAM的EC2计算机。 Elasticsearch 7.6.0已安装,我可以使用以下方法从本地计算机远程访问它:

curl http://ec2-ip-address:9200

结果是:

{
  "name" : "ip-xxx-xx-x-xx",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "_na_",
  "version" : {
    "number" : "7.6.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "7f634e9f44834fbc12724506cc1da681b0c3b1e3",
    "build_date" : "2020-02-06T00:09:00.449973Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

elasticsearch.yml配置文件如下:

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#

其他所有内容均保持不变,这意味着所有内容均已注释。

jvm.options文件如下:

################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g
-Xmx1g

################################################################

对于此文件的其余部分,保留了默认选项。

在EC2服务器上,入站规则如下:

Ports   Protocol    Source  launch-wizard-3
80  tcp 0.0.0.0/0, ::/0 ✔
22  tcp 0.0.0.0/0   ✔
9200    tcp 0.0.0.0/0, ::/0 ✔

正如我之前所说,我可以从终端进行连接,但是当我尝试与运行在本地计算机上的flask应用程序进行连接时,我会收到一条错误消息,该消息基本上表明连接超时。

elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='xx.xxx..xx', port=9200): Read timed out. (read timeout=10))

Python代码如下:

from flask import Flask
import json
from datetime import datetime
from elasticsearch import Elasticsearch

application = Flask(__name__)

es = Elasticsearch("http://xx.219.xx.x6")
# es = Elasticsearch([{'host': 'ec2-xx-218-xx-4.us-west-2.compute.amazonaws.com', 'port': 443,  'use_ssl': True}])

# es = Elasticsearch("http://1xx.31.xx.xx")

@application.route('/', methods=['GET', 'POST'])
def index():
    print(es.cluster.health())
    return "OK"

如您所见,我已经尝试了多个连接配置,但都出现了相同的错误。我本来以为该错误是由于未在EC2服务器上专门打开端口9200引起的,但这现在无法解决问题。

可能没有足够的RAM。我在代码中尝试了不同的timeout = xx选项。一切都不成功。

有人可以帮忙或给我介绍一下Flask / Ealsticsearch教程吗?谢谢!

0 个答案:

没有答案