【发布时间】:2022-01-20 13:29:55
【问题描述】:
我有一个在 Kubernetes 上运行的 Elasticsearch DB 作为 Istio 虚拟服务暴露给 my_domain.com/elastic,我通过浏览器访问它没有问题(因为我可以成功登录到端点)。我还可以使用 Python 的请求查询数据库。但是如果我使用my_domain.com/elastic,我将无法使用官方 python 客户端访问数据库。即使在客户端上,LoadBalancer IP 也能很好地工作。我错过了什么?我通过 Cert-Manager 和 CloudFlare 为 my_domain.com 设置了 SSL 证书。
这行得通:
import requests
import os
data = ' { "query": { "match_all": {} } }'
headers = {'Content-Type': 'application/json'}
auth= ('elastic', os.environ['ELASTIC_PASSWORD'])
response = requests.post('https://mydomain.cloud/elastic/_search', auth=auth, data=data, headers=headers)
print(response.text)
这不起作用(我尝试了许多不同的参数):
from datetime import datetime
import os
from elasticsearch import Elasticsearch, RequestsHttpConnection
es = Elasticsearch(,
[{'host': 'mydomain.cloud', 'port': 443, 'url_prefix': 'elastic', 'use_ssl': True}],
http_auth=('elastic', os.environ['ELASTIC_PASSWORD']), # 1Password or kubectl get secret elastic-cluster-es-elastic-user -o go-template='{{.data.elastic | base64decode}}' -n elastic-system
schema='https'#, verify_certs=False,
# use_ssl=True,
# connection_class = RequestsHttpConnection,
# port=443,
)
# if not es.ping():
# raise ValueError("Connection failed")
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
res = es.index(index="test-index", id=1, document=doc)
print(res['result'])
res = es.get(index="test-index", id=1)
print(res['_source'])
es.indices.refresh(index="test-index")
res = es.search(index="test-index", query={"match_all": {}})
print("Got %d Hits:" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
产生的错误:
elasticsearch.exceptions.RequestError: RequestError(400, 'no handler found for uri [//test-index/_doc/1] and method [PUT]', 'no handler found for uri [//test-index/_doc/1] and method [PUT]')
cluster.yaml
apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
name: elastic-cluster
namespace: elastic-system
spec:
version: 7.15.2
http:
# tls:
# selfSignedCertificate:
# disabled: true
service:
spec:
type: LoadBalancer
nodeSets:
- name: master-nodes
count: 2
config:
node.roles: ["master"]
volumeClaimTemplates:
- metadata:
name: elasticsearch-data # Do not change this name unless you set up a volume mount for the data path.
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local-storage
- name: data-nodes
count: 2
config:
node.roles: ["data"]
volumeClaimTemplates:
- metadata:
name: elasticsearch-data # Do not change this name unless you set up a volume mount for the data path.
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: local-storage
podTemplate:
# metadata:
# annotations:
# traffic.sidecar.istio.io/includeInboundPorts: "*"
# traffic.sidecar.istio.io/excludeOutboundPorts: "9300"
# traffic.sidecar.istio.io/excludeInboundPorts: "9300"
spec:
# automountServiceAccountToken: true
containers:
- name: elasticsearch
resources:
requests:
memory: 4Gi
cpu: 3
limits:
memory: 4Gi
cpu: 3
虚拟服务.yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: elastic-vts
namespace: elastic-system
spec:
hosts:
- "mydomain.cloud"
gateways:
- istio-system/gateway
http:
- match:
- port: 443
- uri:
prefix: /elastic
rewrite:
uri: /
route:
- destination:
host: elastic-cluster-es-http.elastic-system.svc.cluster.local
port:
number: 9200
destination-rule.yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: elastic-destination-rule
namespace: elastic-system
spec:
host: elastic-cluster-es-http.elastic-system.svc.cluster.local
trafficPolicy:
tls:
mode: SIMPLE
gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: gateway
namespace: istio-system
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- 'mydomain.cloud'
tls:
httpsRedirect: true
- port:
number: 443
name: https
protocol: HTTPS
hosts:
- 'mydomain.cloud'
tls:
mode: SIMPLE
credentialName: letsencrypt-staging-tls
【问题讨论】:
-
你从 python 得到什么错误? my_domain.com/elastic 是指向实际集群还是指向 kibana?
-
好点。我得到 elasticsearch.exceptions.RequestError: RequestError(400, 'no handler found for uri [//test-index/_doc/1] and method [PUT]', 'no handler found for uri [//test-index/_doc /1] 和方法 [PUT]')
-
浏览器登录给出 { "name" : "elastic-cluster-es-data-nodes-0", "cluster_name" : "elastic-cluster", "cluster_uuid" : "8vKO-Jw-RQyeGBFuXkthVQ ”,“版本”:{“编号”:“7.15.2”,“build_flavor”:“默认”,“build_type”:“docker”,“build_hash”:“93d5a7f6192e8a1a12e154a2b81bf6fa7309da0c”,“build_date”:“2021-11- 04T14:04:42.515624022Z”,“build_snapshot”:假,“lucene_version”:“8.9.0”,“minimum_wire_compatibility_version”:“6.8.0”,“minimum_index_compatibility_version”:“6.0.0-beta1”},“标语” :“你知道,搜索”}
-
你是如何实例化客户端的?确保您使用的是url_prefix
-
感谢@ilvar 我尝试了许多不同的组合。例如。
es = Elasticsearch([{'host': 'my_domain.com', 'port': 443, 'url_prefix': 'elastic', 'use_ssl': True}], http_auth=('elastic', os.environ['ELASTIC_PASSWORD']) , schema='https', connection_class = RequestsHttpConnection, )此外,使用 Requests 进行查询没有任何问题,因此显然与 elasticsearch 客户端有关
标签: http elasticsearch kubernetes istio