redis集群2台服务器 (3主3从) 部署

最后实现效果

集群部署

我们可以用redis自带的 redis-trib.rb 命令来实现。

执行命令:

./redis-trib.rb  create  --replicas  1  172.16.135.186:7001 172.16.135.186:7002 172.16.135.186:7003 172.16.135.190:7004 172.16.135.190:7005 172.16.135.190:7006

这个时候,如果报错:Sorry, can't connect to node

[root@park redis_cluster]# ./redis-trib.rb  create  --replicas  1  172.16.135.186:7001 172.16.135.186:7002 172.16.135.186:7003 172.16.135.190:7004 172.16.135.190:7005 172.16.135.190:7006
>>> Creating cluster
[ERR] Sorry, can't connect to node 172.16.135.186:7002

这个时候,有两种解决方式:

一是bind 的时候,同时bind 127.0.0.1 和 ip

bind 192.168.1.13 127.0.0.1

另一种是在复制集群的时候,原来安装好的Redis是配置了账号密码,必须要为每个redis去掉密码后再配置集群,再在集群中配置账号密码。所以要删除

# requirepass 123456

这个时候,如果报错:Waiting for the cluster to join

  1. 关闭集群中的所有实例
  2. 于此同时删除掉每个节点文件下的 demp.rdb和nodes.conf文件
  3. 然后开放你redis实例端口号+10000的端口

当显示如下时,表示redis集群部署成功:

[root@park redis_cluster]# ./redis-trib.rb  create  --replicas  1  172.16.135.186:7001 172.16.135.186:7002 172.16.135.186:7003 172.16.135.190:7004 172.16.135.190:7005 172.16.135.190:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.135.186:7001
172.16.135.190:7004
172.16.135.186:7002
Adding replica 172.16.135.190:7005 to 172.16.135.186:7001
Adding replica 172.16.135.186:7003 to 172.16.135.190:7004
Adding replica 172.16.135.190:7006 to 172.16.135.186:7002
M: cd928e7310b817fe5b6f4b62351967e32a1eff38 172.16.135.186:7001
   slots:0-5460 (5461 slots) master
M: bb798821db4b68c535b0f004670f860f515b4e6b 172.16.135.186:7002
   slots:10923-16383 (5461 slots) master
S: 47c787edcc565ba0d61a461d60294fc4ba0d5e82 172.16.135.186:7003
   replicates 4c60c89037077ae4cde196b1f92cf70a7fc00072
M: 4c60c89037077ae4cde196b1f92cf70a7fc00072 172.16.135.190:7004
   slots:5461-10922 (5462 slots) master
S: 54552c93bed301946533bf88c21396dd88a0c8fc 172.16.135.190:7005
   replicates cd928e7310b817fe5b6f4b62351967e32a1eff38
S: 21d9c26c6b082d5daee8c029ac8a1a77e383fa15 172.16.135.190:7006
   replicates bb798821db4b68c535b0f004670f860f515b4e6b
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 172.16.135.186:7001)
M: cd928e7310b817fe5b6f4b62351967e32a1eff38 172.16.135.186:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: bb798821db4b68c535b0f004670f860f515b4e6b 172.16.135.186:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 4c60c89037077ae4cde196b1f92cf70a7fc00072 172.16.135.190:7004
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 21d9c26c6b082d5daee8c029ac8a1a77e383fa15 172.16.135.190:7006
   slots: (0 slots) slave
   replicates bb798821db4b68c535b0f004670f860f515b4e6b
S: 54552c93bed301946533bf88c21396dd88a0c8fc 172.16.135.190:7005
   slots: (0 slots) slave
   replicates cd928e7310b817fe5b6f4b62351967e32a1eff38
S: 47c787edcc565ba0d61a461d60294fc4ba0d5e82 172.16.135.186:7003
   slots: (0 slots) slave
   replicates 4c60c89037077ae4cde196b1f92cf70a7fc00072
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群的验证

[root@yq2 redis_cluster]# /opt/tools/redis/src/redis-cli -c -h 172.16.135.190 -p 7004
172.16.135.190:7004> keys *
(empty list or set)
172.16.135.190:7004> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:4
cluster_stats_messages_ping_sent:616
cluster_stats_messages_pong_sent:639
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:1258
cluster_stats_messages_ping_received:636
cluster_stats_messages_pong_received:619
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:1258
172.16.135.190:7004> 
172.16.135.190:7004> 
172.16.135.190:7004> set 7004a a
-> Redirected to slot [14240] located at 172.16.135.186:7002
OK
172.16.135.186:7002> set 7004b b
-> Redirected to slot [1987] located at 172.16.135.186:7001
OK
172.16.135.186:7001> set 7004c c
-> Redirected to slot [6114] located at 172.16.135.190:7004
OK
172.16.135.190:7004> set 7004d d
OK
172.16.135.190:7004> set 7004e e
-> Redirected to slot [14116] located at 172.16.135.186:7002
OK
172.16.135.186:7002> get 7004a
"a"
172.16.135.186:7002> get 7004b
-> Redirected to slot [1987] located at 172.16.135.186:7001
"b"
172.16.135.186:7001> get 7004c
-> Redirected to slot [6114] located at 172.16.135.190:7004
"c"
172.16.135.190:7004> get 7004d
"d"
172.16.135.190:7004> get 7004e
-> Redirected to slot [14116] located at 172.16.135.186:7002
"e"

查看节点:

172.16.135.186:7002> CLUSTER NODES
54552c93bed301946533bf88c21396dd88a0c8fc 172.16.135.190:7005@17005 slave cd928e7310b817fe5b6f4b62351967e32a1eff38 0 1589010553317 5 connected
47c787edcc565ba0d61a461d60294fc4ba0d5e82 172.16.135.186:7003@17003 slave 4c60c89037077ae4cde196b1f92cf70a7fc00072 0 1589010553000 4 connected
bb798821db4b68c535b0f004670f860f515b4e6b 172.16.135.186:7002@17002 myself,master - 0 1589010554000 2 connected 10923-16383
cd928e7310b817fe5b6f4b62351967e32a1eff38 172.16.135.186:7001@17001 master - 0 1589010555322 1 connected 0-5460
21d9c26c6b082d5daee8c029ac8a1a77e383fa15 172.16.135.190:7006@17006 slave bb798821db4b68c535b0f004670f860f515b4e6b 0 1589010555000 6 connected
4c60c89037077ae4cde196b1f92cf70a7fc00072 172.16.135.190:7004@17004 master - 0 1589010556323 4 connected 5461-10922

猜你喜欢

转载自blog.csdn.net/u014209205/article/details/106019752