question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't connect to Elasticsearch with Node.Js on Kubernetes (socket hang up)

See original GitHub issue

I have a NodeJs application running inside a Kubernetes cluster. I’ve also followed the official steps to setup Elasticsearch on Kubernetes.

I am not sure if this has actually something to do with elasticsearch-js or if I am doing something wrong here.

Issue

I am unable to connect to the Elasticsearch cluster. I get this error:

ConnectionError: socket hang up

This is a code snippet of my connection:

const client = new elasticsearch.Client({
  node: process.env.elasticsearch_node, // http://elasticsearch-es-http.default.svc.cluster.local:9200
});

Minimal reproduction

I’ve created a minimal reproduction of this issue here: https://github.com/flolu/elasticsearch-k8s-connection. (Setup instructions are in the README)

Basically, everything works fine when running Elasticsearch inside Docker compose, but I can’t connect when running inside Kubernetes.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
delvedorcommented, Jul 14, 2020

Given that you are using a self-signed certificate, you should also set rejectUnauthorized to false:

const { Client } = require('@elastic/elasticsearch')
const client = new Client({
  node: 'https://localhost:9200',
  auth: {
    username: 'elastic',
    password: 'changeme'
  },
  ssl: {
    ca: fs.readFileSync('./cacert.pem'),
    rejectUnauthorized: false
  }
})

You can find the SSL documentation here.

1reaction
delvedorcommented, Jul 14, 2020

Glad you were able to sort it out! Yeah, it’s fine to use it as long as you trust that certificate. By default, Node.js rejects certificates that didn’t use an official CA authority, but given that you have created that cert via Elasticsearch, it’s safe to use.

Closing this, feel free to reopen if you have other questions! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't connect to Elasticsearch with Node.Js on Kubernetes ...
The solution is to configure SSL and the Elastic user when creating the Client const client = new elasticsearch.Client({ node: ...
Read more >
"message":"socket hang up" in kibana logs - Elastic Discuss
i am getting below error in the kibana logs continuously for every second. The error is logged even when there is no use....
Read more >
How to handle a socket hang up error in Node.js usually - Quora
Close the connection if you have opened any for connecting to database after your work is done. Most of the times this is...
Read more >
Hang in There! (A solution to socket hang up) - Medium
(A solution to socket hang up). Recently, while watching a YouTube video about how to create a Node.js backend (Building a RESTful API...
Read more >
Unable to retrieve version information from Elasticsearch nodes
Typically, you'll see the “Unable to version information Elasticsearch nodes” message when Kibana can't connect to one of the hosts included in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found