Search code examples
phpredisphpredis

RedisClusterException with phpredis when connecting to redis cluster which is not local


I'm using phpredis (builded on 25.02.2016 from https://github.com/phpredis/phpredis) with php 5.5.9. The extension is tested succesfully with a single redis instance (version 3.0.7) (remote and local redis instance).

This is the code to connect to a configured redis cluster (no sentinel, only over configs).

$cluster = new \RedisCluster(NULL, 
         array("192.168.127.203:7000", "192.168.127.203:7001", "192.168.127.203:7002"));
$cluster->setOption(RedisCluster::OPT_SLAVE_FAILOVER, RedisCluster::FAILOVER_ERROR);
var_dump($cluster->_masters());
var_dump($cluster->get('foo1'));

When we execute this code on the same server as the redis instance we successfully get all the masters and the value of foo1. But when we execute the code on another webserver, we get the following masters from the cluster:

array (size=3)
  0 => 
    array (size=2)
      0 => string '127.0.0.1' (length=9)
      1 => int 7005
  1 => 
    array (size=2)
      0 => string '127.0.0.1' (length=9)
      1 => int 7000
  2 => 
    array (size=2)
      0 => string '127.0.0.1' (length=9)
      1 => int 7001

and getting the value will fail with a RedisClusterException with the Message "Can't communicate with any node in the cluster".

I'm not sure if it's a bug in the libary or if i use the lib in a wrong way. the code it the same as in the documentation. i think one problem is that we get the masters with the localhost ip and not the remote ip.

appreciate all your help.


Solution

  • finally found out what's the problem was. it's nothing in the client libary, just a error in the configuration of the cluster.

    for creation the following snippet from the documentation was used:

    ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
    127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
    

    of course the right command would be:

    ./redis-trib.rb create --replicas 1 192.168.127.203:7000 192.168.127.203:7001 \
    192.168.127.203:7002 192.168.127.203:7003 192.168.127.203:7004 192.168.127.203:7005
    

    just for info if someone have a similar problem.