redis创建集群——[ERR] Sorry, can't connect to node 192.168.X.X
redis默认只允许本地访问,要使redis可以远程访问可以修改redis.conf
解决办法:注释掉bind 127.0.0.1可以使所有的ip访问redis
若是想指定多个ip访问,但并不是全部的ip访问,可以bind
在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错
修改办法:protected-mode no
绑定IP:
sed -i 's/bind 127.0.0.1/bind 192.168.152.100/' redis.conf
sed -i 's/protected-mode yes/protected-mode no/' redis.conf
sed -i 's/protected-mode yes/protected-mode no/' redis.conf
创建成功:(仅在192.168.152.100上执行)
[root@localhost redis-3.2.1]# /usr/local/redis-3.2.1/src/redis-trib.rb create --replicas 1 \
> 192.168.152.100:7000 \
> 192.168.152.100:7001 \
> 192.168.152.100:7002 \
> \
> 192.168.152.101:7003 \
> 192.168.152.101:7004 \
> 192.168.152.101:7005 \
>
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.152.100:7000
192.168.152.101:7003
192.168.152.100:7001
Adding replica 192.168.152.101:7004 to 192.168.152.100:7000
Adding replica 192.168.152.100:7002 to 192.168.152.101:7003
Adding replica 192.168.152.101:7005 to 192.168.152.100:7001
M: b7f8755b7a44fd6a87b05f762f64150279cd9427 192.168.152.100:7000
slots:0-5460 (5461 slots) master
M: 747b2ffe0cb766e58d7759c69eaca3a76c67545b 192.168.152.100:7001
slots:10923-16383 (5461 slots) master
S: d57bac1e831711945bff5e8d7e4ff05361f3f45e 192.168.152.100:7002
replicates e725fc4470f58c17a04d69f8eafc595e6c264fda
M: e725fc4470f58c17a04d69f8eafc595e6c264fda 192.168.152.101:7003
slots:5461-10922 (5462 slots) master
S: 9613979d1b98725cc4f4b145c458cfc51120bc4c 192.168.152.101:7004
replicates b7f8755b7a44fd6a87b05f762f64150279cd9427
S: 023b3d0f0b74e1811bb11de0eddee8133b47fd8f 192.168.152.101:7005
replicates 747b2ffe0cb766e58d7759c69eaca3a76c67545b
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 192.168.152.100:7000)
M: b7f8755b7a44fd6a87b05f762f64150279cd9427 192.168.152.100:7000
slots:0-5460 (5461 slots) master
M: 747b2ffe0cb766e58d7759c69eaca3a76c67545b 192.168.152.100:7001
slots:10923-16383 (5461 slots) master
M: d57bac1e831711945bff5e8d7e4ff05361f3f45e 192.168.152.100:7002
slots: (0 slots) master
replicates e725fc4470f58c17a04d69f8eafc595e6c264fda
M: e725fc4470f58c17a04d69f8eafc595e6c264fda 192.168.152.101:7003
slots:5461-10922 (5462 slots) master
M: 9613979d1b98725cc4f4b145c458cfc51120bc4c 192.168.152.101:7004
slots: (0 slots) master
replicates b7f8755b7a44fd6a87b05f762f64150279cd9427
M: 023b3d0f0b74e1811bb11de0eddee8133b47fd8f 192.168.152.101:7005
slots: (0 slots) master
replicates 747b2ffe0cb766e58d7759c69eaca3a76c67545b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-3.2.1]#
> 192.168.152.100:7000 \
> 192.168.152.100:7001 \
> 192.168.152.100:7002 \
> \
> 192.168.152.101:7003 \
> 192.168.152.101:7004 \
> 192.168.152.101:7005 \
>
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.152.100:7000
192.168.152.101:7003
192.168.152.100:7001
Adding replica 192.168.152.101:7004 to 192.168.152.100:7000
Adding replica 192.168.152.100:7002 to 192.168.152.101:7003
Adding replica 192.168.152.101:7005 to 192.168.152.100:7001
M: b7f8755b7a44fd6a87b05f762f64150279cd9427 192.168.152.100:7000
slots:0-5460 (5461 slots) master
M: 747b2ffe0cb766e58d7759c69eaca3a76c67545b 192.168.152.100:7001
slots:10923-16383 (5461 slots) master
S: d57bac1e831711945bff5e8d7e4ff05361f3f45e 192.168.152.100:7002
replicates e725fc4470f58c17a04d69f8eafc595e6c264fda
M: e725fc4470f58c17a04d69f8eafc595e6c264fda 192.168.152.101:7003
slots:5461-10922 (5462 slots) master
S: 9613979d1b98725cc4f4b145c458cfc51120bc4c 192.168.152.101:7004
replicates b7f8755b7a44fd6a87b05f762f64150279cd9427
S: 023b3d0f0b74e1811bb11de0eddee8133b47fd8f 192.168.152.101:7005
replicates 747b2ffe0cb766e58d7759c69eaca3a76c67545b
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 192.168.152.100:7000)
M: b7f8755b7a44fd6a87b05f762f64150279cd9427 192.168.152.100:7000
slots:0-5460 (5461 slots) master
M: 747b2ffe0cb766e58d7759c69eaca3a76c67545b 192.168.152.100:7001
slots:10923-16383 (5461 slots) master
M: d57bac1e831711945bff5e8d7e4ff05361f3f45e 192.168.152.100:7002
slots: (0 slots) master
replicates e725fc4470f58c17a04d69f8eafc595e6c264fda
M: e725fc4470f58c17a04d69f8eafc595e6c264fda 192.168.152.101:7003
slots:5461-10922 (5462 slots) master
M: 9613979d1b98725cc4f4b145c458cfc51120bc4c 192.168.152.101:7004
slots: (0 slots) master
replicates b7f8755b7a44fd6a87b05f762f64150279cd9427
M: 023b3d0f0b74e1811bb11de0eddee8133b47fd8f 192.168.152.101:7005
slots: (0 slots) master
replicates 747b2ffe0cb766e58d7759c69eaca3a76c67545b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-3.2.1]#
测试:
redis集群有16383个slot组成,通过分片分布到多个节点上,读写都发生在master节点。
redis.conf 将 bind 改为了ip地址,所以 -h 参数不省略。
[root@localhost redis-3.2.1]# redis-cli -h 192.168.152.100 -c -p 7000
192.168.152.100:7000> set hello isok
OK
192.168.152.100:7000> get hello
"isok"
[root@localhost redis-3.2.1]# redis-cli -h 192.168.152.101 -c -p 7003
192.168.152.101:7003> get hello
-> Redirected to slot [866] located at 192.168.152.100:7000
"isok"
192.168.152.100:7000>
192.168.152.100:7000> set hello isok
OK
192.168.152.100:7000> get hello
"isok"
[root@localhost redis-3.2.1]# redis-cli -h 192.168.152.101 -c -p 7003
192.168.152.101:7003> get hello
-> Redirected to slot [866] located at 192.168.152.100:7000
"isok"
192.168.152.100:7000>