node.js - 无法通过Kubernetes上的Node.Js连接到Elasticsearch(证书链中的自签名证书)

标签 node.js typescript elasticsearch kubernetes microk8s

我有一个在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/

相关文章:

amazon-web-services - 如果它在 ec2 实例中运行,如何检查我的 Elasticsearch 是否启动

scala - 将Elastic4s与 Elasticsearch 和喷雾路由一起使用时异常(exception)

elasticsearch - Elasticsearch.Net.ElasticSearch.Path已弃用,我应该改用什么?

javascript - 如果后端和前端代码位于同一存储库中,则处理依赖关系的正确方法是什么?

javascript - ES6的reduce方法没有返回期望的输出

angular - 如何在 Angular 6 中绑定(bind)来自 HttpClient 的数据?

node.js - 在单独的函数中进行类型检查在 typescript 中不起作用

javascript - Azure:通过主体 ID 获取用户电子邮件

node.js - 如何将配置的 Passport 对象传递到 Express4 中的路由模块?

node.js - Nodejs,使用 Bluehost SMTP 使用 Nodemailer 发送电子邮件未送达,没有错误