OpenSearch Docker Image无法建立一个新连接:[ERRNO 111]连接拒绝)

发布于 2025-02-08 04:11:41 字数 4012 浏览 3 评论 0 原文

我正在使用Docker Desktop(Windows)中的OpenSearch。

使用docker-compose.yml file- file-

https://opensearch.org/down.org/downloads.html

code-

!pip install opensearch-py

from opensearchpy import OpenSearch

host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
# ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.

# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'

# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
    hosts = [{'host': host, 'port': port}],
    http_compress = True, # enables gzip compression for request bodies
    http_auth = auth,
    # client_cert = client_cert_path,
    # client_key = client_key_path,
    use_ssl = True,
    verify_certs = True,
    ssl_assert_hostname = False,
    ssl_show_warn = False,
    # ca_certs = ca_certs_path
)
# Create an index with non-default settings.
index_name = 'python-test-index'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}
response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)

error-

---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/urllib3/connection.py in _new_conn(self)
    158             conn = connection.create_connection(
--> 159                 (self._dns_host, self.port), self.timeout, **extra_kw)
    160 
16 frames
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

NewConnectionError                        Traceback (most recent call last)
NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/opensearchpy/connection/http_urllib3.py in perform_request(self, method, url, params, body, timeout, ignore, headers)
    262             if isinstance(e, ReadTimeoutError):
    263                 raise ConnectionTimeout("TIMEOUT", str(e), e)
--> 264             raise ConnectionError("N/A", str(e), e)
    265 
    266         # raise warnings if any from the 'Warnings' header.

ConnectionError: ConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused)

在另一个站点上找到类似的查询 -

https://solveforum.com/forums/threads/solved-how-to-connect-amazon-opensearch- docker-image-to-streamlit-python-application-docker-image.509702/#登录

在Docker桌面上运行的OpenSearch Instances。

我在互联网上搜索,但没有找到答案,请告诉我。

snap-

谢谢

I am using OpenSearch in docker desktop (Windows).I was trying to connect my python code running on google colab with the OpenSearch instance running in docker desktop.

Steps of installing Opensearch with docker-compose.yml file-

https://opensearch.org/downloads.html

https://opensearch.org/docs/latest/security-plugin/configuration/disable/

Code-

!pip install opensearch-py

from opensearchpy import OpenSearch

host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
# ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.

# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'

# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
    hosts = [{'host': host, 'port': port}],
    http_compress = True, # enables gzip compression for request bodies
    http_auth = auth,
    # client_cert = client_cert_path,
    # client_key = client_key_path,
    use_ssl = True,
    verify_certs = True,
    ssl_assert_hostname = False,
    ssl_show_warn = False,
    # ca_certs = ca_certs_path
)
# Create an index with non-default settings.
index_name = 'python-test-index'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}
response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)

Error-

---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/urllib3/connection.py in _new_conn(self)
    158             conn = connection.create_connection(
--> 159                 (self._dns_host, self.port), self.timeout, **extra_kw)
    160 
16 frames
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

NewConnectionError                        Traceback (most recent call last)
NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/opensearchpy/connection/http_urllib3.py in perform_request(self, method, url, params, body, timeout, ignore, headers)
    262             if isinstance(e, ReadTimeoutError):
    263                 raise ConnectionTimeout("TIMEOUT", str(e), e)
--> 264             raise ConnectionError("N/A", str(e), e)
    265 
    266         # raise warnings if any from the 'Warnings' header.

ConnectionError: ConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused)

Found similar query on another site-

https://solveforum.com/forums/threads/solved-how-to-connect-amazon-opensearch-docker-image-to-streamlit-python-application-docker-image.509702/#login

Opensearch instances running on Docker Desktop.

I searched on internet but didn't find answer, pls let me know.

Snap-

enter image description here

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如荒岛 2025-02-15 04:11:41

问题是我试图使用Google Colab笔记本电脑连接连接OpenSearch Docker实例。

在Python Virtual Env中运行脚本后,我能够连接Docker实例。

ref link- docker:opensearch拒绝与与示例的连接Docker中的OpenSearch文档

工作代码 -

from opensearchpy import OpenSearch

def connect_to_os():
    host = 'localhost'
    port = 9200
    auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
# ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.

# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'

# Create the client with SSL/TLS enabled, but hostname verification disabled.
    try:
        client = OpenSearch(
            hosts = [{'host': host, 'port': port}],
            http_compress = True, # enables gzip compression for request bodies
            http_auth = auth,
        # client_cert = client_cert_path,
        # client_key = client_key_path,
            use_ssl = True,
            verify_certs = False,
            ssl_assert_hostname = False,
            ssl_show_warn = False,
            # ca_certs = ca_certs_path
             )
    except BaseException: 
        #connection error; send error message
        print("ERROR unable to connect opensearch instance")
        return False
    
    return client

# Create an index with non-default settings.
index_name = 'python-test-index'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}
client = connect_to_os()
response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)

Issue was I was trying to connect connect opensearch docker instance using google colab notebooks.

After running the script in python virtual env, I am able to connect docker instance.

Ref link- Docker : Opensearch refuses connection with the example in opensearch documentation in docker

Working code-

from opensearchpy import OpenSearch

def connect_to_os():
    host = 'localhost'
    port = 9200
    auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
# ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.

# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'

# Create the client with SSL/TLS enabled, but hostname verification disabled.
    try:
        client = OpenSearch(
            hosts = [{'host': host, 'port': port}],
            http_compress = True, # enables gzip compression for request bodies
            http_auth = auth,
        # client_cert = client_cert_path,
        # client_key = client_key_path,
            use_ssl = True,
            verify_certs = False,
            ssl_assert_hostname = False,
            ssl_show_warn = False,
            # ca_certs = ca_certs_path
             )
    except BaseException: 
        #connection error; send error message
        print("ERROR unable to connect opensearch instance")
        return False
    
    return client

# Create an index with non-default settings.
index_name = 'python-test-index'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}
client = connect_to_os()
response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文