Elasticsearch-php: "No alive nodes found in your cluster" using aws elasticsearch

Created on 6 Apr 2016  路  15Comments  路  Source: elastic/elasticsearch-php

I'm trying to connect to amazon elasticsearch endpoint and get the error "No alive nodes found in your cluster" using the version 2.0 of elasticsearch-php, this is my code:

$host = ["http://amazon-end-point"];

$clientBuilder = Elasticsearch\ClientBuilder::create();
$clientBuilder->setHosts($host);
$client =   $clientBuilder->build();

$params = [
    'index' => 'test',
    'type' => 'type_test',
    'id'=> '1'
];

$response = $client->get($params);

If I try to connect to the endpoint by curl in command line it works perfectly

I tried everything that i found in google with the same issue, but anything helped me with this problem :(

Most helpful comment

Hmm. The output definitely looks like it is just timing out.

  • Are you using the cluster sniffing mode (off by default)?
  • I see port 9200 in the debug output, but I think the AWS elasticsearch service runs over port 80. Can you explicitly set the port to 80 in your $host's array? The client defaults to 9200 if you don't specify a port, since that is the default for Elasticsearch (and most hosted ES services too).

If the port doesn't work, can you try with both HTTP handlers? Perhaps it is a curl_single vs curl_multi issue:

$singleHandler  = ClientBuilder::singleHandler();
$multiHandler   = ClientBuilder::multiHandler();

$client = ClientBuilder::create()
            ->setHandler($singleHandler)
            ->build();

// try search

$client = ClientBuilder::create()
            ->setHandler($multiHandler)
            ->build();

// try search again

All 15 comments

Apparently, downgrading the version of ES-PHP works. In my case, ver1.3 worked with ES installation ver 2.2.2. Still trying to figure out the actual cause...

My ES version is 1.5.2 (amazon don't have support to the version 2.x yet). I did the downgrade to the version 1.3 of ES-PHP, and now showed me another error:

Failed to connect to [my endpoint] port 9200: Connection timed out

I've turned off the SELinux but have no effect

Is this an EC2 Elasticsearch instance, or the AWS Elasticsearch service? Are you using authentication of some sort (HTTP Basic auth, etc)?

I've seen some instances where special characters (?, etc) in the user/pass can cause PHP's parse_url to abort parsing prematurely, which mangles the url. We discovered that adding a trailing slash to the host name resolved that particular oddity: https://github.com/elastic/elasticsearch-php/issues/376#issuecomment-172603980

To confirm, the exact issue is caused by a combination of special characters in my password that parse_url fails to parse correctly. Oddly, adding a trailing slash to the end of the complete URL resolves this.

Another issue is sometimes string interpolation. E.g. if you use double quotes and your user/pass has a dollar sign ($), PHP will attempt to substitute that part of the string with a variable, which inevitably mangles the user/pass.

Otherwise, can you try to execute something like this and post the debug output?

$hosts = [
    // the host
];

$client = Elasticsearch\ClientBuilder::create()
    ->setHosts($hosts)->build();

$searchParams = [
    "index" => "test",
    "type" => "test_type",
    'client' => [
        'verbose' => true
    ]
];

try {
  $docs = $client->search($searchParams);
  print_r($docs);
catch (Exception $e) {
  $last = $client->transport->getLastConnection()->getLastRequestInfo();
  $last['response']['error'] = [];
  print_r($last);
}

It toggles verbose mode and prints the output if the command succeeds, or in your case, dumps the last connection's curl info if it fails.

Some other thoughts:

  • Are you using SSL? @monothorn I suspect your issue may be SSL certificate validation related. The 1.x branch of ES-PHP didn't validate bad certificates, while 2.x does. So that could explain the behavior why downgrading works
  • @LucasCambraia You said you were on "2.0" of ES-PHP. Was that the most recent version, 2.1.5
  • Any other firewalls or IP tables? (I see you turned SElinux off temporarily, so rules that out)
  • Is this running from the command line, or via a server (apache, nginx, etc)

I'm using the AWS Elasticsearch service and not using authentication, just access policy allowing some ips to connect with my endpoint. I had the Elastica running perfectly, so i think there is no problem in access the endpoint, but now not work using the ES-PHP

Running your code, get this:

Array ( [request] => Array ( [http_method] => GET [scheme] => http [uri] => /test_index/_search [body] => [headers] => Array ( [host] => Array ( [0] => http://my-end-pont:9200 ) ) ) [response] => Array ( [transfer_stats] => Array ( [url] => http://my-end-pont:9200/test_index/_search [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 126.82615 [namelookup_time] => 0.509281 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] => [primary_ip] => [certinfo] => Array ( ) [primary_port] => 0 [local_ip] => [local_port] => 0 [error] => Failed to connect to my-end-pont: port 9200: Connection timed out [errno] => 7 ) [curl] => Array ( [error] => Failed to connect to my-end-pont: port 9200: Connection timed out [errno] => 7 ) [effective_url] => http://my-end-pont::9200/test_index/_search [status] => [reason] => [body] => [headers] => Array ( ) [error] => Array ( ) ) )

I get the same error before: "Connection timed out"

1 - I tried to install the 2.1.5 version, show me the same error
2 - I think that iptables or firewall is not the problem because i put the code into a EC2 to test and get the same error, but everything works with Elastica
3 - My code is running in localhost using the apache, but like i said before, using the EC2 get the same error. When i try to access the endpoint using curl by command line works perfectly

Hmm. The output definitely looks like it is just timing out.

  • Are you using the cluster sniffing mode (off by default)?
  • I see port 9200 in the debug output, but I think the AWS elasticsearch service runs over port 80. Can you explicitly set the port to 80 in your $host's array? The client defaults to 9200 if you don't specify a port, since that is the default for Elasticsearch (and most hosted ES services too).

If the port doesn't work, can you try with both HTTP handlers? Perhaps it is a curl_single vs curl_multi issue:

$singleHandler  = ClientBuilder::singleHandler();
$multiHandler   = ClientBuilder::multiHandler();

$client = ClientBuilder::create()
            ->setHandler($singleHandler)
            ->build();

// try search

$client = ClientBuilder::create()
            ->setHandler($multiHandler)
            ->build();

// try search again

I can't believe that i missed that! Change the port solved the problem

Thank you! You helped me a lot!!

No problem, happy to help! I'll add a blurb to the docs about that particular gotcha... I have a feeling it will start to crop up more often.

I meet the same problem:
Fatal error: Uncaught exception 'Elasticsearch\Common\ExceptionsNoNodesAvailableException' with message 'No alive nodes found in your cluster' in /app/web/testes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php:51 Stack trace: #0 /app/web/testes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php(71): Elasticsearch\ConnectionPool\StaticNoPingConnectionPool->nextConnection() #1 /app/web/testes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php(89): Elasticsearch\Transport->getConnection() #2 /app/web/testes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(230): Elasticsearch\Transport->performRequest('GET', '/index4/fulltex...', Array, '{"query":{"matc...', Array) #3 /app/web/testes/vendor/react/promise/src/FulfilledPromise.php(25): Elasticsearch\Connections\Connection->Elasticsearch\Connections{closure}(Array) #4 /app/web/testes/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php(55): React\Promise\F in /app/web/testes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php on line 51

@sanpangzi how did you fixed it?

Hi, @williamdes it still not resolved my problem

Hello guys,

Unfortunately I am getting this same error. But strange part is, it is wrking perfectly fine on my localhost:9200

But when I connect this with my elasticsearch server i.e host URL is changed, this throws the above error: "No alive nodes found in your cluster"

I am tried every possible solution but couldn't find anyone facing this issue.

Elastic search working fine on localhost, but not working when I connect with other hosting server. Tried with 443 HTTPS and 80,9200 HTTP as well.

Any help will be appreciated. Thanks

In the PHP framework Laravel environment, I also encountered such problems, occasionally appear during the running process, each time you restart elasticsearch to solve the problem. Trying various programs has not been completely solved.

When settings hosts do not include the http[s]:// in your host address as amazon suggests, and set the port to 80 (or 443 if'you enabled Require HTTPS option).

That should do the trick.

Setting both https:// AND :443 did the trick for some reason.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

m9rco picture m9rco  路  3Comments

marksparrish picture marksparrish  路  6Comments

ls
ssanchezromero picture ssanchezromero  路  6Comments

paulhuisman picture paulhuisman  路  6Comments

thinkspill picture thinkspill  路  3Comments