我有一个在Kubernetes集群中运行的NodeJs应用程序(我正在使用microk8s)。我还遵循official steps在Kubernetes上设置Elasticsearch。
问题
但是我无法连接到Elasticsearch集群。我收到此错误:
ConnectionError: self signed certificate in certificate chain
这是我的连接的code snippet:const client = new elasticsearch.Client({
node: process.env.elasticsearch_node,
// https://elasticsearch-es-http.default.svc.cluster.local:9200
});
极少的繁殖我在这里创建了此问题的最小复制:https://github.com/flolu/elasticsearch-k8s-connection。 (设置说明在自述文件中)
基本上,在Docker compose中运行Elasticsearch时一切正常,但是在Kubernetes中运行时我无法连接。
造成这种情况的原因可能是因为我没有正确设置TLS证书,但没有找到有关它的任何信息。创建ES客户端时或在集群级别时,是否在NodeJs应用程序中配置它?
最佳答案
解决方案是在创建Client
时配置SSL和Elastic用户
const client = new elasticsearch.Client({
node: process.env.elasticsearch_node,
auth: {
username: "elastic",
password: process.env.elasticsearch_password || "changeme",
},
ssl: {
ca: process.env.elasticsearch_certificate,
rejectUnauthorized: false,
},
});
密码和证书由Elastic提供。它们存储在Kubernetes的 secret 中。因此,我刚刚通过如下环境变量将密码和证书传递到了我的NodeJs服务中:
apiVersion: apps/v1
kind: Deployment
metadata:
name: search-deployment
spec:
selector:
matchLabels:
app: search
replicas: 1
template:
metadata:
labels:
app: search
spec:
containers:
- name: search
image: search:placeholder_name
imagePullPolicy: Always
env:
- name: elasticsearch_node
value: https://elasticsearch-es-http.default.svc.cluster.local:9200
- name: elasticsearch_certificate
valueFrom:
secretKeyRef:
name: elasticsearch-es-http-ca-internal
key: tls.crt
- name: elasticsearch_password
valueFrom:
secretKeyRef:
name: elasticsearch-es-elastic-user
key: elastic
关于node.js - 无法通过Kubernetes上的Node.Js连接到Elasticsearch(证书链中的自签名证书),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62792477/