redis cluster + redis replication 搭建

redis cluster + redis replication 搭建

环境

部署搭建

192.168.255.250
[[email protected] 3010]# grep -vE "^#|^$" redis.conf
bind 192.168.255.250  ##一定要写本机ip并且建立集群的时候要用这个ip建立
port 3010
daemonize yes  #守护线程模式(后台启动)
pidfile /etc/redis-cluster/3010/redis_3010.pid  #pid文件
logfile "/etc/redis-cluster/3010/redis.log"   #日志文件
dir "/etc/redis-cluster/3010/data"   #文件/数据路径
appendonly yes  #开启持久化功能
cluster-enabled yes    #启动集群
cluster-config-file nodes-3010.conf   #节点信息,自动生成
cluster-node-timeout 5000   #超时时间,单位毫秒
## 在搭建集群之前就设置密码的话可能会导致无法添加节点,所以在后面集群搭建好之后再进行设置就好了
# masterauth "[email protected]#"  #主机密码
# requirepass "[email protected]#"  #访问密码

255.250:3011、255.100:3010-3011、255.101:3010-3011 配置均相同,只是把bind跟port以及路径修改一下即可

#写完配置文件之后,分别启动六个节点
redis-server   /etc/redis-cluster/3010/redis.conf

向集群添加master节点(需要多出一个port,此处直接在255.250上开启一个6379端口以控制cluster的添加删除等)
将255.250、100、101的3010端口作为master添加到cluster里面
--cluster-replicas 表示有一个主有几个slave

[[email protected] redis-cluster]# redis-cli --cluster  create 192.168.255.250:3010  192.168.255.100:3010  192.168.255.101:3010 --cluster-replicas 0
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
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.255.250:3010)
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#检查cluster状态,可以看到只有三个master而没有slave,master都对应有自己的uuid,此集群信息会记录到 nodes-**.conf文件里面
[[email protected] redis-cluster]# redis-cli  --cluster check 192.168.255.250:3010
192.168.255.250:3010 (75b5a3a5...) -> 0 keys | 5461 slots | 0 slaves.
192.168.255.100:3010 (278944c8...) -> 0 keys | 5462 slots | 0 slaves.
192.168.255.101:3010 (0b7abd55...) -> 0 keys | 5461 slots | 0 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.255.250:3010)
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[[email protected] redis-cluster]# redis-cli  --cluster check 192.168.255.100:3010
192.168.255.100:3010 (278944c8...) -> 0 keys | 5462 slots | 0 slaves.
192.168.255.101:3010 (0b7abd55...) -> 0 keys | 5461 slots | 0 slaves.
192.168.255.250:3010 (75b5a3a5...) -> 0 keys | 5461 slots | 0 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.255.100:3010)
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[[email protected] redis-cluster]# redis-cli  --cluster check 192.168.255.101:3010
192.168.255.101:3010 (0b7abd55...) -> 0 keys | 5461 slots | 0 slaves.
192.168.255.100:3010 (278944c8...) -> 0 keys | 5462 slots | 0 slaves.
192.168.255.250:3010 (75b5a3a5...) -> 0 keys | 5461 slots | 0 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.255.101:3010)
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

添加集群slave节点

为cluster添加nodes,并且是针对每一个master添加slave节点(格式: redis-cli --cluster add-node slave-nodes-ip:port 控制节点ip:端口(任意集群中的节点都可) --cluster-slave --cluster-master-id master-uuid )

[[email protected] redis-cluster]# redis-cli  --cluster add-node 192.168.255.100:3011 192.168.255.250:3010 --cluster-slave --cluster-master-id  75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5
>>> Adding node 192.168.255.100:3011 to cluster 192.168.255.250:3010
>>> Performing Cluster Check (using node 192.168.255.250:3010)
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.255.100:3011 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.255.250:3010.
[OK] New node added correctly.

[[email protected] redis-cluster]# redis-cli  --cluster add-node 192.168.255.101:3011 192.168.255.250:3010 --cluster-slave --cluster-master-id  278944c80cd36651809de466e9e9dd3fc243cc8b
>>> Adding node 192.168.255.101:3011 to cluster 192.168.255.250:3010
>>> Performing Cluster Check (using node 192.168.255.250:3010)
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots: (0 slots) slave
   replicates 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.255.101:3011 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.255.100:3010.
[OK] New node added correctly.

[[email protected] redis-cluster]# redis-cli  --cluster add-node 192.168.255.250:3011 192.168.255.250:3010 --cluster-slave --cluster-master-id 0b7abd55faf62a007468eccc9a0fa30354df5adf
>>> Adding node 192.168.255.250:3011 to cluster 192.168.255.250:3010
>>> Performing Cluster Check (using node 192.168.255.250:3010)
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots: (0 slots) slave
   replicates 278944c80cd36651809de466e9e9dd3fc243cc8b
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots: (0 slots) slave
   replicates 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.255.250:3011 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.255.101:3010.
[OK] New node added correctly.

[[email protected] redis-cluster]# redis-cli  --cluster check 192.168.255.250:3010
192.168.255.250:3010 (75b5a3a5...) -> 0 keys | 5461 slots | 1 slaves.
192.168.255.101:3010 (0b7abd55...) -> 0 keys | 5461 slots | 1 slaves.
192.168.255.100:3010 (278944c8...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.255.250:3010)
M: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 143ccae6e432af1c530e2a7d6fbfc2320e735316 192.168.255.250:3011
   slots: (0 slots) slave
   replicates 0b7abd55faf62a007468eccc9a0fa30354df5adf
S: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots: (0 slots) slave
   replicates 278944c80cd36651809de466e9e9dd3fc243cc8b
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots: (0 slots) slave
   replicates 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看节点状态

192.168.255.250:3010> cluster nodes
143ccae6e432af1c530e2a7d6fbfc2320e735316 192.168.255.250:[email protected] slave 0b7abd55faf62a007468eccc9a0fa30354df5adf 0 1564373272729 3 connected
75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:[email protected] myself,master - 0 1564373268000 1 connected 0-5460
adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:[email protected] slave 278944c80cd36651809de466e9e9dd3fc243cc8b 0 1564373273234 2 connected
0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:[email protected] master - 0 1564373272228 3 connected 10923-16383
278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:[email protected] master - 0 1564373272000 2 connected 5461-10922
5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:[email protected] slave 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 0 1564373271718 1 connected
192.168.255.250:3010> 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:5
cluster_my_epoch:4
cluster_stats_messages_ping_sent:15951
cluster_stats_messages_pong_sent:14146
cluster_stats_messages_sent:30097
cluster_stats_messages_ping_received:14146
cluster_stats_messages_pong_received:14929
cluster_stats_messages_fail_received:2
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_received:29078

设置集群nodes密码(每个node都需要进行设置)

192.168.255.250:3010> config set masterauth [email protected]#
OK
192.168.255.250:3010> config set requirepass [email protected]#
OK
192.168.255.250:3010> auth [email protected]#
OK
192.168.255.250:3010> config rewrite
OK

扩容集群节点

[[email protected] app]# redis-cli --cluster add-node 192.168.255.253:3010  192.168.255.250:3010  -a [email protected]#   #这里是将节点加入了集群中,但是并没有分配slot,所以这个节点并没有真正的开始分担集群工作。
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
>>> Adding node 192.168.255.253:3010 to cluster 192.168.255.250:3010
>>> Performing Cluster Check (using node 192.168.255.250:3010)
S: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots: (0 slots) slave
   replicates 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982
S: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots: (0 slots) slave
   replicates adf58df6918571490eb51a0ea3a52df6812419fc
M: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 143ccae6e432af1c530e2a7d6fbfc2320e735316 192.168.255.250:3011
   slots: (0 slots) slave
   replicates 0b7abd55faf62a007468eccc9a0fa30354df5adf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.255.253:3010 to make it join the cluster.
[OK] New node added correctly.

[[email protected] app]# redis-cli  --cluster check 192.168.255.250:3010 -a [email protected]#
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
192.168.255.101:3011 (adf58df6...) -> 4 keys | 5462 slots | 1 slaves.
192.168.255.253:3010 (d700cfa9...) -> 0 keys | 0 slots | 0 slaves.
192.168.255.101:3010 (0b7abd55...) -> 4 keys | 5461 slots | 1 slaves.
192.168.255.100:3011 (5c4ac88c...) -> 4 keys | 5461 slots | 1 slaves.
[OK] 12 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.255.250:3010)
S: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots: (0 slots) slave
   replicates 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982
S: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots: (0 slots) slave
   replicates adf58df6918571490eb51a0ea3a52df6812419fc
M: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: d700cfa9b25a44cee8805783cf9548ce525b337a 192.168.255.253:3010
   slots: (0 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 143ccae6e432af1c530e2a7d6fbfc2320e735316 192.168.255.250:3011
   slots: (0 slots) slave
   replicates 0b7abd55faf62a007468eccc9a0fa30354df5adf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

分配slot,让新加入cluster的节点提集群分担工作

--cluster-from:表示slot目前所在的节点的node ID,多个ID用逗号分隔
--cluster-to:表示需要新分配节点的node ID(貌似每次只能分配一个)
--cluster-slots:分配的slot数量

[[email protected] app]# redis-cli --cluster reshard 192.168.255.253:3010 --cluster-from  5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982,adf58df6918571490eb51a0ea3a52df6812419fc,0b7abd55faf62a007468eccc9a0fa30354df5adf --cluster-to d700cfa9b25a44cee8805783cf9548ce525b337a --cluster-slots 5461 -a [email protected]#
[[email protected] app]# redis-cli  --cluster check 192.168.255.250:3010 -a [email protected]#
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
192.168.255.101:3011 (adf58df6...) -> 3 keys | 3641 slots | 1 slaves.
192.168.255.253:3010 (d700cfa9...) -> 3 keys | 5461 slots | 0 slaves.
192.168.255.101:3010 (0b7abd55...) -> 4 keys | 3641 slots | 1 slaves.
192.168.255.100:3011 (5c4ac88c...) -> 2 keys | 3641 slots | 1 slaves.
[OK] 12 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.255.250:3010)
S: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots: (0 slots) slave
   replicates 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982
S: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots: (0 slots) slave
   replicates adf58df6918571490eb51a0ea3a52df6812419fc
M: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots:[7282-10922] (3641 slots) master
   1 additional replica(s)
M: d700cfa9b25a44cee8805783cf9548ce525b337a 192.168.255.253:3010
   slots:[0-1819],[5461-7281],[10923-12742] (5461 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[12743-16383] (3641 slots) master
   1 additional replica(s)
M: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots:[1820-5460] (3641 slots) master
   1 additional replica(s)
S: 143ccae6e432af1c530e2a7d6fbfc2320e735316 192.168.255.250:3011
   slots: (0 slots) slave
   replicates 0b7abd55faf62a007468eccc9a0fa30354df5adf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

为255.253:3010添加slave node

(因为目前有4个master 跟3个slave ,所以架构图需要进行调整,将原来的255.101:3010 -> 255.250:3011调整为255.101:3010 -> 255.253:3011 原来的255.250的3011作为 255.253的slave 255.253:3010 -> 255.250:3011)
add-node: 后面的分别跟着新加入的slave和slave对应的master
cluster-slave:表示加入的是slave节点
--cluster-master-id:表示slave对应的master的node ID

[[email protected] app]# redis-cli  --cluster check 192.168.255.250:3010 -a [email protected]#
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
192.168.255.101:3011 (adf58df6...) -> 3 keys | 3641 slots | 1 slaves.
192.168.255.253:3010 (d700cfa9...) -> 3 keys | 5461 slots | 0 slaves.
192.168.255.101:3010 (0b7abd55...) -> 4 keys | 3641 slots | 1 slaves.
192.168.255.100:3011 (5c4ac88c...) -> 2 keys | 3641 slots | 1 slaves.
[OK] 12 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.255.250:3010)
S: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots: (0 slots) slave
   replicates 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982
S: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots: (0 slots) slave
   replicates adf58df6918571490eb51a0ea3a52df6812419fc
M: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots:[7282-10922] (3641 slots) master
   1 additional replica(s)
M: d700cfa9b25a44cee8805783cf9548ce525b337a 192.168.255.253:3010
   slots:[0-1819],[5461-7281],[10923-12742] (5461 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[12743-16383] (3641 slots) master
   1 additional replica(s)
M: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots:[1820-5460] (3641 slots) master
   1 additional replica(s)
S: 143ccae6e432af1c530e2a7d6fbfc2320e735316 192.168.255.250:3011
   slots: (0 slots) slave
   replicates 0b7abd55faf62a007468eccc9a0fa30354df5adf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[[email protected] app]# redis-cli --cluster del-node 192.168.255.250:3011 143ccae6e432af1c530e2a7d6fbfc2320e735316 -a [email protected]#
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
>>> Removing node 143ccae6e432af1c530e2a7d6fbfc2320e735316 from cluster 192.168.255.250:3011
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

[[email protected] app]# redis-cli --cluster add-node 192.168.255.253:3011  192.168.255.250:3010 --cluster-slave --cluster-master-id  0b7abd55faf62a007468eccc9a0fa30354df5adf  -a [email protected]#
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
>>> Adding node 192.168.255.253:3011 to cluster 192.168.255.250:3010
>>> Performing Cluster Check (using node 192.168.255.250:3010)
S: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots: (0 slots) slave
   replicates 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982
S: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots: (0 slots) slave
   replicates adf58df6918571490eb51a0ea3a52df6812419fc
M: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots:[7282-10922] (3641 slots) master
   1 additional replica(s)
M: d700cfa9b25a44cee8805783cf9548ce525b337a 192.168.255.253:3010
   slots:[0-1819],[5461-7281],[10923-12742] (5461 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[12743-16383] (3641 slots) master
M: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots:[1820-5460] (3641 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.255.253:3011 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.255.101:3010.
[OK] New node added correctly.

因为255.250:3011 原来是做从用的,里面会有集群节点信息跟数据,所以需要删除掉原来的数据才可正常;
=======================================================================================
[[email protected] app]# rm -rf  /etc/redis-cluster/3011/data/*
[[email protected] app]# redis-server   /etc/redis-cluster/3011/redis.conf
[[email protected] app]# redis-cli --cluster add-node 192.168.255.250:3011  192.168.255.250:3010 --cluster-slave --cluster-master-id  d700cfa9b25a44cee8805783cf9548ce525b337a  -a [email protected]#
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
>>> Adding node 192.168.255.250:3011 to cluster 192.168.255.250:3010
>>> Performing Cluster Check (using node 192.168.255.250:3010)
S: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots: (0 slots) slave
   replicates 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982
S: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots: (0 slots) slave
   replicates adf58df6918571490eb51a0ea3a52df6812419fc
M: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots:[7282-10922] (3641 slots) master
   1 additional replica(s)
M: d700cfa9b25a44cee8805783cf9548ce525b337a 192.168.255.253:3010
   slots:[0-1819],[5461-7281],[10923-12742] (5461 slots) master
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[12743-16383] (3641 slots) master
   1 additional replica(s)
M: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots:[1820-5460] (3641 slots) master
   1 additional replica(s)
S: 8f33ed15a736bb86c11f16294b68241d848073ca 192.168.255.253:3011
   slots: (0 slots) slave
   replicates 0b7abd55faf62a007468eccc9a0fa30354df5adf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.255.250:3011 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.255.253:3010.
[OK] New node added correctly.
========================================================================

[[email protected] app]# redis-cli  --cluster check 192.168.255.250:3010 -a [email protected]#
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
192.168.255.101:3011 (adf58df6...) -> 3 keys | 3641 slots | 1 slaves.
192.168.255.253:3010 (d700cfa9...) -> 3 keys | 5461 slots | 1 slaves.
192.168.255.101:3010 (0b7abd55...) -> 4 keys | 3641 slots | 1 slaves.
192.168.255.100:3011 (5c4ac88c...) -> 2 keys | 3641 slots | 1 slaves.
[OK] 12 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.255.250:3010)
S: 75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:3010
   slots: (0 slots) slave
   replicates 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982
S: 278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:3010
   slots: (0 slots) slave
   replicates adf58df6918571490eb51a0ea3a52df6812419fc
M: adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:3011
   slots:[7282-10922] (3641 slots) master
   1 additional replica(s)
S: d3c105351fffd4419bc5101819699af984f3a83b 192.168.255.250:3011
   slots: (0 slots) slave
   replicates d700cfa9b25a44cee8805783cf9548ce525b337a
M: d700cfa9b25a44cee8805783cf9548ce525b337a 192.168.255.253:3010
   slots:[0-1819],[5461-7281],[10923-12742] (5461 slots) master
   1 additional replica(s)
M: 0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:3010
   slots:[12743-16383] (3641 slots) master
   1 additional replica(s)
M: 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:3011
   slots:[1820-5460] (3641 slots) master
   1 additional replica(s)
S: 8f33ed15a736bb86c11f16294b68241d848073ca 192.168.255.253:3011
   slots: (0 slots) slave
   replicates 0b7abd55faf62a007468eccc9a0fa30354df5adf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[[email protected] app]# redis-cli  -c -h 192.168.255.250 -p 3010   -a [email protected]#   cluster nodes
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
278944c80cd36651809de466e9e9dd3fc243cc8b 192.168.255.100:[email protected] slave adf58df6918571490eb51a0ea3a52df6812419fc 0 1564386981109 5 connected
adf58df6918571490eb51a0ea3a52df6812419fc 192.168.255.101:[email protected] master - 0 1564386982116 5 connected 7282-10922
d3c105351fffd4419bc5101819699af984f3a83b 192.168.255.250:[email protected] slave d700cfa9b25a44cee8805783cf9548ce525b337a 0 1564386980100 6 connected
d700cfa9b25a44cee8805783cf9548ce525b337a 192.168.255.253:[email protected] master - 0 1564386980101 6 connected 0-1819 5461-7281 10923-12742
0b7abd55faf62a007468eccc9a0fa30354df5adf 192.168.255.101:[email protected] master - 0 1564386981611 3 connected 12743-16383
75b5a3a58ad4c8c668ad2905cec76612f1eeb4e5 192.168.255.250:[email protected] myself,slave 5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 0 1564386977000 1 connected
5c4ac88c46b805ae6e51ac23ec5b2eb89c43d982 192.168.255.100:[email protected] master - 0 1564386981611 4 connected 1820-5460
8f33ed15a736bb86c11f16294b68241d848073ca 192.168.255.253:[email protected] slave 0b7abd55faf62a007468eccc9a0fa30354df5adf 0 1564386980101 3 connected

Redis常见配置

收缩集群

下线节点127.0.0.1:6385(master)/127.0.0.1:6386(slave)
(1)首先删除master对应的slave
redis-cli --cluster del-node 127.0.0.1:6386 530cf27337c1141ed12268f55ba06c15ca8494fc
del-node后面跟着slave节点的 ip:port 和node ID
(2)清空master的slot
redis-cli --cluster reshard 127.0.0.1:6385 --cluster-from 46f0b68b3f605b3369d3843a89a2b4a164ed21e8 --cluster-to 2846540d8284538096f111a8ce7cf01c50199237 --cluster-slots 1024 --cluster-yes
reshard子命令前面已经介绍过了,这里需要注意的一点是,由于我们的集群一共有四个主节点,而每次reshard只能写一个目的节点,因此以上命令需要执行三次(--cluster-to对应不同的目的节点)。
--cluster-yes:不会显示需要迁移的slot,直接迁移。
(3)下线(删除)节点
redis-cli --cluster del-node 127.0.0.1:6385 46f0b68b3f605b3369d3843a89a2b4a164ed21e8

redis加入systemctl开机启动。

(1)、首先需要把redis.conf中的

daemonize yes
修改为
daemonize no

(2)、创建启动脚本

vim /lib/systemd/system/redis.service
在redis.service中输入以下内容
[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target
[Service]
#Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf  #指定运行程序以及redis配置文件
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
#重新载入 systemd
systemctl daemon-reload
#启动redis
systemctl start redis
#加入开机启动
systemctl enable redis
#查看redis状态
systemctl status redis
#停止redis
systemctl stop redis

如果节点添加错误之后的删除方法

redis-cli  --cluster del-node  slave-node:port  slave-uuid
redis-cli --cluster del-node 192.168.255.100:3011  011367d777d2ef363f8a15689efa9df4b0bed8b8
除了在此处删除,还需要删除nodes-xx.conf里面的值,最直接的方法是将这个文件删除,以为重启节点后会生成一个新的文件
如果不删除会导致 100:3011无法重新添加进来(因为键值冲突)

原文地址:https://blog.51cto.com/xiaoqiangjs/2444476

时间: 2024-11-07 16:40:55

redis cluster + redis replication 搭建的相关文章

Redis Cluster集群搭建测试

# Redis Clutser # ## 一.Redis Cluster集群 ## 参考资料: http://www.cnblogs.com/lykxqhh/p/5690923.html Redis集群搭建的方式有多种,例如使用zookper等,但从redis3.0之后版本支持redis cluster集群,Redis Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接.其redis cluster架构图如下: 其结构特点: 1.所有的redis节点彼此互

Redis Cluster 3.0搭建与使用

Redis Cluster终于出了Stable,这让人很是激动,等Stable很久了,所以还是先玩玩. 一. 集群简单概念. Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation). Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令需要在多个 Redis 节点之间移动数据, 并且在高负载的情况下, 这些命令将降低 Redis 集群的性能, 并导致不可预测的行为. Redis 集群通过分区(partition)来提供

Redis Cluster集群搭建

1.服务器 192.168.1.201 192.168.1.204 192.168.1.205 192.168.1.206 192.168.1.207 192.168.1.208 因为Redis Cluster如果数据冗余是1的话,至少要3个Master和3个Slave. 2.安装步骤 (1)准备工作 mkdir /usr/local/redis_cluster mkdir /usr/local/redis_cluster (安装所需的文件) cd /usr/local/redis_cluste

Redis Cluster集群搭建与应用

1.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper,但从redis 3.0之后版本支持redis-cluster集群,redis-cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接.其redis-cluster架构图如下: 其结构特点 所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. 节点的fail是通过集群中超过半数的节点检测失效时才生效. 客户端与redis节点直

Redis Cluster 集群搭建部署

1.准备 1>redis-3.0 tar包 2>yum ruby rubygem 提示:①安装rubygem时,有可能需先安装ruby-rdoc,然后执行ruby setup.rb与gem install -l redis              ②安装redis,先解压,进入rdis安装目录,make&make install&make test,make过程中需保证tcl1.85版本以上 2.当前选定6台机器,端口默认.三个master与三个slave(如果在一台机器,修

redis cluster 集群搭建步骤和注意事项

1.安装Ubuntu ,修改root的密码. sudo passwd  (apt-get update 更新系统) 2.安装 Gcc 和G++  sudo apt-get install build-essential 3.下载 Redis  wget http://download.redis.io/releases/redis-3.2.3.tar.gz 4.解压 tar -xvf redis-3.0.2.tar.gz 5.进入 redis-3.0.2 ,执行make编译源码,进入src目录 

redis cluster 集群搭建(增、删、改、查) :5.0.2

环境简介: 1:centos 7 : 2:redis version 5.0.2 : 3: 安装方式:编译安装 : 4:... 创建集群 节点IP地址 10.42.166.105:6379 10.42.142.202:6379 10.42.11.183:6379 10.42.35.201:6379 10.42.181.22:6379 10.42.225.72:6379 使用公司redis docker 镜像启动6个实例后,开始创建集群:create命令可选replicas参数,replicas表

Redis Cluster集群部署搭建

在Oracle的路上走了许多年,换换感觉,尝试一下新的知识,也是一个不错的感觉.Redis,一个超轻量化的内存数据库,只做一小块数据库功能实现,却非常优秀的一个产品.今天,就分享一下安装Redis集群的过程. 搭建redis集群,建议至少需要准备3台服务器,共搭建6个节点,3个master,3个slave,并且要求3个master节点不能全部跑到同一台服务器上,保证节点安全,3台服务器的配置相同,使用redistest账号搭建,对应的端口是7000/7001/7002端口 我的集群分配如下,每个

Redis cluster集群模式的原理

redis cluster redis cluster是Redis的分布式解决方案,在3.0版本推出后有效地解决了redis分布式方面的需求 自动将数据进行分片,每个master上放一部分数据 提供内置的高可用支持,部分master不可用时,还是可以继续工作的 支撑N个redis master node,每个master node都可以挂载多个slave node 高可用,因为每个master都有salve节点,那么如果mater挂掉,redis cluster这套机制,就会自动将某个slave