九、redis cluster自带集群搭建
参考
redis详解——redis集群搭建和使用(二)
redis cluster管理工具redis-trib.rb详解
一、简介
1、redis-cluster(集群)架构图
2、介绍
(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
(2)节点的fail(失败)是通过集群中超过半数的节点检测失效时才生效.(那么要求集群最少三台服务器,每台在带一个备份,则最少6台服务器)
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot(槽)上,cluster负责维护node<->slot<->value。
Redis集群中内置了 16384 个哈希槽,当需要在 Redis集群中放置一个 key-value时,redis先对 key使用 crc16算法算出一个结果,然后把结果对 16384求余数,这样每个 key都会对应一个编号在 0-16383之间的哈希槽,redis会根据节点数量大致均等的将哈希槽映射到不同的节点。
3、redis-cluster投票:容错
(1)领着投票过程是集群中所有master参与,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉.
(2):什么时候整个集群不可用(cluster_state:fail)?
a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态. ps : redis-3.0.0.rc1加入cluster-require-full-coverage参数,默认关闭,打开集群兼容部分失败.
b:如果集群超过半数以上master挂掉,无论是否有slave集群进入fail状态.
ps:当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误
二、搭建
1、创建六个redis实例
redis前面已经搭建过了,可以看前面的博客。
redis自带集群搭建,一般使用三主三从来构建,演示主使用7000 7001 7002,从使用,8000 8001 8002,在同一台机器上开六个示例进行测试。
(1)创建实例目录
mkdir /data/redis-cluster
cd /data/redis-cluster
mkdir -pv 7000 7001 7002 8000 8001 8002
(2)redis配置,依次根据端口进行配置,主要是前面几行,后面都是默认配置,这里只展示一个
每个redis实例redis.conf的端口号port,并将cluster-enabled yes前的注释去掉表示开启集群功能,和之前的相比主要是多了cluster-enabled yes这个配置
vim /data/redis-cluster/7000/redis.conf
cluster-enabled yes
port 7000
logfile "/data/redis-cluster/7000/redis.log"
pidfile "/data/redis-cluster/7000/redis.pid"
dir "/data/redis-cluster/7000"
daemonize yes
bind 0.0.0.0
tcp-backlog 1024
timeout 0
tcp-keepalive 0
loglevel notice
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync ye
(3)启动各个实例
/usr/local/redis/bin/redis-server /data/redis-cluster/7000/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/7001/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/7002/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/8000/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/8001/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/8002/redis.conf
(4)编写一个启动和一个关闭全部实例的脚本
cd /usr/local/redis/bin/
[aaa@qq.com bin]# vim startall.sh
/usr/local/redis/bin/redis-server /data/redis-cluster/7000/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/7001/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/7002/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/8000/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/8001/redis.conf
/usr/local/redis/bin/redis-server /data/redis-cluster/8002/redis.conf
[aaa@qq.com bin]# vim stopall.sh
/usr/local/redis/bin/redis-cli -p 7000 shutdown
/usr/local/redis/bin/redis-cli -p 7001 shutdown
/usr/local/redis/bin/redis-cli -p 7002 shutdown
/usr/local/redis/bin/redis-cli -p 8000 shutdown
/usr/local/redis/bin/redis-cli -p 8001 shutdown
/usr/local/redis/bin/redis-cli -p 8002 shutdown
2、编译安装高ruby版本
Ruby版本需要大于等于2.2.2,centos使用yum安装的不符合
wget --no-check-certificate 'https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.7.tar.gz'
tar -zxvf ruby-2.2.7.tar.gz
cd ruby-2.2.7
./configure #环境探测
make #编译
make install #安装
ruby --version
ruby的redis扩展安装,如果有自带ruby的话,调用gem要用绝对路径,gem文件先下载了再安装,不然连不上源
wget http://rubygems.org/downloads/redis-4.0.3.gem
/usr/local/bin/gem install --local redis-4.0.3.gem
3、redis-trib.rb功能介绍
先从redis-trib.rb的help信息,看下redis-trib.rb提供了哪些功能。
$ruby redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>
create host1:port1 ... hostN:portN
--replicas <arg>
check host:port
info host:port
fix host:port
--timeout <arg>
reshard host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--threshold <arg>
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
del-node host:port node_id
set-timeout host:port milliseconds
call host:port command arg arg .. arg
import host:port
--from <arg>
--copy
--replace
help (show this help)
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
可以看到redis-trib.rb具有以下功能:
1、create:创建集群
2、check:检查集群
3、info:查看集群信息
4、fix:修复集群
5、reshard:在线迁移slot
6、rebalance:平衡集群节点slot数量
7、add-node:将新节点加入集群
8、del-node:从集群中删除节点
9、set-timeout:设置集群节点间心跳连接的超时时间
10、call:在集群全部节点上执行命令
11、import:将外部redis数据导入集群
4、创建集群
##--replicas参数指定集群中每个主节点配备几个从节点,这里设置为1。
redis-trib.rb create --replicas 1 10.0.0.107:7000 10.0.0.107:7001 10.0.0.107:7002 10.0.0.107:8000 10.0.0.107:8001 10.0.0.107:8002
执行结果如下
[aaa@qq.com local]# /usr/local/redis/bin/redis-trib.rb create --replicas 1 10.0.0.107:7000 10.0.0.107:7001 10.0.0.107:7002 10.0.0.107:8000 10.0.0.107:8001 10.0.0.107:8002
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.0.107:7000
10.0.0.107:7001
10.0.0.107:7002
Adding replica 10.0.0.107:8001 to 10.0.0.107:7000
Adding replica 10.0.0.107:8002 to 10.0.0.107:7001
Adding replica 10.0.0.107:8000 to 10.0.0.107:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
##主从信息
M: aa252236bd90317b7b2625199387c0a2f5d31d3b 10.0.0.107:7000
slots:0-5460 (5461 slots) master
M: 88bd89f4adac54f202cf819adc046f102ff892df 10.0.0.107:7001
slots:5461-10922 (5462 slots) master
M: 309c102d0c9f63d7999806260f1708d8a874f364 10.0.0.107:7002
slots:10923-16383 (5461 slots) master
S: 276b2e1099955f56dd34e1ddad16260638d8b5af 10.0.0.107:8000
replicates aa252236bd90317b7b2625199387c0a2f5d31d3b
S: 859d7d26b2e4abb6870c2bf337a4919bdc2a6468 10.0.0.107:8001
replicates 88bd89f4adac54f202cf819adc046f102ff892df
S: 82f695a16572fd9dde45a999306766cf7e0048d6 10.0.0.107:8002
replicates 309c102d0c9f63d7999806260f1708d8a874f364
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 10.0.0.107:7000)
M: aa252236bd90317b7b2625199387c0a2f5d31d3b 10.0.0.107:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 88bd89f4adac54f202cf819adc046f102ff892df 10.0.0.107:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 309c102d0c9f63d7999806260f1708d8a874f364 10.0.0.107:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 859d7d26b2e4abb6870c2bf337a4919bdc2a6468 10.0.0.107:8001
slots: (0 slots) slave
replicates 88bd89f4adac54f202cf819adc046f102ff892df
S: 82f695a16572fd9dde45a999306766cf7e0048d6 10.0.0.107:8002
slots: (0 slots) slave
replicates 309c102d0c9f63d7999806260f1708d8a874f364
S: 276b2e1099955f56dd34e1ddad16260638d8b5af 10.0.0.107:8000
slots: (0 slots) slave
replicates aa252236bd90317b7b2625199387c0a2f5d31d3b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
可以看到创建了三主三从,前三个实例为主,后三个为依次为从,例如7000为主,8000为从,按照顺序先确定主,然后依次进行匹配从。
创建流程如下:
1、首先为每个节点创建ClusterNode对象,包括连接每个节点。检查每个节点是否为独立且db为空的节点。执行load_info方法导入节点信息。
2、检查传入的master节点数量是否大于等于3个。只有大于3个节点才能组成集群。
3、计算每个master需要分配的slot数量,以及给master分配slave。分配的算法大致如下:
先把节点按照host分类,这样保证master节点能分配到更多的主机中。
不停遍历遍历host列表,从每个host列表中弹出一个节点,放入interleaved数组。直到所有的节点都弹出为止。
master节点列表就是interleaved前面的master数量的节点列表。保存在masters数组。
计算每个master节点负责的slot数量,保存在slots_per_node对象,用slot总数除以master数量取整即可。
遍历masters数组,每个master分配slots_per_node个slot,最后一个master,分配到16384个slot为止。
接下来为master分配slave,分配算法会尽量保证master和slave节点不在同一台主机上。对于分配完指定slave数量的节点,还有多余的节点,也会为这些节点寻找master。分配算法会遍历两次masters数组。
第一次遍历masters数组,在余下的节点列表找到replicas数量个slave。每个slave为第一个和master节点host不一样的节点,如果没有不一样的节点,则直接取出余下列表的第一个节点。
第二次遍历是在对于节点数除以replicas不为整数,则会多余一部分节点。遍历的方式跟第一次一样,只是第一次会一次性给master分配replicas数量个slave,而第二次遍历只分配一个,直到余下的节点被全部分配出去。
4、打印出分配信息,并提示用户输入“yes”确认是否按照打印出来的分配方式创建集群。
5、输入“yes”后,会执行flush_nodes_config操作,该操作执行前面的分配结果,给master分配slot,让slave复制master,对于还没有握手(cluster meet)的节点,slave复制操作无法完成,不过没关系,flush_nodes_config操作出现异常会很快返回,后续握手后会再次执行flush_nodes_config。
6、给每个节点分配epoch,遍历节点,每个节点分配的epoch比之前节点大1。
7、节点间开始相互握手,握手的方式为节点列表的其他节点跟第一个节点握手。
8、然后每隔1秒检查一次各个节点是否已经消息同步完成,使用ClusterNode的get_config_signature方法,检查的算法为获取每个节点cluster nodes信息,排序每个节点,组装成node_id1:slots|node_id2:slot2|...的字符串。如果每个节点获得字符串都相同,即认为握手成功。
9、此后会再执行一次flush_nodes_config,这次主要是为了完成slave复制操作。
10、最后再执行check_cluster,全面检查一次集群状态。包括和前面握手时检查一样的方式再检查一遍。确认没有迁移的节点。确认所有的slot都被分配出去了。
11、至此完成了整个创建流程,返回[OK] All 16384 slots covered.。
5、查看信息
#登陆7000端口的实例,需要使用-c,代表连接的是redis集群,不然节点之间是无法自动跳转的
redis-cli -p 7000 -c
#查看集群状态
cluster info
#查看集群节点
cluster nodes
#登陆7000端口的实例,需要加-c代表链接redis集群
[aaa@qq.com local]# redis-cli -p 7000 -c
#查看集群信息
127.0.0.1:7000> 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:1
cluster_stats_messages_ping_sent:833
cluster_stats_messages_pong_sent:824
cluster_stats_messages_sent:1657
cluster_stats_messages_ping_received:819
cluster_stats_messages_pong_received:833
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1657
#查看节点信息,当前是7000端口的实例,可以看到主从和哈希槽分布信息
127.0.0.1:7000> cluster nodes
88bd89f4adac54f202cf819adc046f102ff892df 10.0.0.107:aaa@qq.com master - 0 1602665794127 2 connected 5461-10922
309c102d0c9f63d7999806260f1708d8a874f364 10.0.0.107:aaa@qq.com master - 0 1602665792000 3 connected 10923-16383
859d7d26b2e4abb6870c2bf337a4919bdc2a6468 10.0.0.107:aaa@qq.com slave 88bd89f4adac54f202cf819adc046f102ff892df 0 1602665793121 5 connected
82f695a16572fd9dde45a999306766cf7e0048d6 10.0.0.107:aaa@qq.com slave 309c102d0c9f63d7999806260f1708d8a874f364 0 1602665792113 6 connected
aa252236bd90317b7b2625199387c0a2f5d31d3b 10.0.0.107:aaa@qq.com myself,master - 0 1602665794000 1 connected 0-5460
276b2e1099955f56dd34e1ddad16260638d8b5af 10.0.0.107:aaa@qq.com slave aa252236bd90317b7b2625199387c0a2f5d31d3b 0 1602665795135 4 connected
6、故障自动切换
(1)先将7000端口的主实例关闭
/usr/local/redis/bin/redis-cli -p 7000 shutdown
查看7000的日志已关闭
(2)然后查看8000实例的日志,可以看到切成主节点了
[aaa@qq.com ~]# tailf /data/redis-cluster/8000/redis.log
4398:S 14 Oct 17:14:16.589 # Start of election delayed for 953 milliseconds (rank #0, offset 2618).
4398:S 14 Oct 17:14:17.295 * Connecting to MASTER 10.0.0.107:7000
4398:S 14 Oct 17:14:17.295 * MASTER <-> SLAVE sync started
4398:S 14 Oct 17:14:17.295 # Error condition on socket for SYNC: Connection refused
4398:S 14 Oct 17:14:17.598 # Starting a failover election for epoch 7.
4398:S 14 Oct 17:14:17.602 # Failover election won: I'm the new master.
4398:S 14 Oct 17:14:17.602 # configEpoch set to 7 after successful failover
4398:M 14 Oct 17:14:17.603 # Setting secondary replication ID to 97c62e6da389e7c16593d534019f0c40d144bcc4, valid up to offset: 2619. New replication ID is a3cb1760dfb699138025e5f2a3deefb58579a4a1
4398:M 14 Oct 17:14:17.603 * Discarding previously cached master state.
4398:M 14 Oct 17:14:17.603 # Cluster state changed: ok
(3)登陆8000实例查看节点信息,可以看到当前8000端口也已经是master
127.0.0.1:8000> cluster nodes
859d7d26b2e4abb6870c2bf337a4919bdc2a6468 10.0.0.107:aaa@qq.com slave 88bd89f4adac54f202cf819adc046f102ff892df 0 1602668328495 5 connected
aa252236bd90317b7b2625199387c0a2f5d31d3b 10.0.0.107:aaa@qq.com master,fail - 1602666840853 1602666838000 1 disconnected
88bd89f4adac54f202cf819adc046f102ff892df 10.0.0.107:aaa@qq.com master - 0 1602668329503 2 connected 5461-10922
276b2e1099955f56dd34e1ddad16260638d8b5af 10.0.0.107:aaa@qq.com myself,master - 0 1602668326000 7 connected 0-5460
309c102d0c9f63d7999806260f1708d8a874f364 10.0.0.107:aaa@qq.com master - 0 1602668328000 3 connected 10923-16383
82f695a16572fd9dde45a999306766cf7e0048d6 10.0.0.107:aaa@qq.com slave 309c102d0c9f63d7999806260f1708d8a874f364 0 1602668327000 6 connected
(4)然后再打开7000实例
/usr/local/redis/bin/redis-server /data/redis-cluster/7000/redis.conf
(5)查看7000的日志,可以看到变成从节点了
[aaa@qq.com local]# tailf /data/redis-cluster/7000/redis.log
22003:S 14 Oct 17:42:27.552 * MASTER <-> SLAVE sync started
22003:S 14 Oct 17:42:27.552 * Non blocking connect for SYNC fired the event.
22003:S 14 Oct 17:42:27.552 * Master replied to PING, replication can continue...
22003:S 14 Oct 17:42:27.553 * Trying a partial resynchronization (request abb3d5a9a75abe49e2942aa81940c2d5b0a8f51e:1).
22003:S 14 Oct 17:42:27.557 * Full resync from master: a3cb1760dfb699138025e5f2a3deefb58579a4a1:2618
22003:S 14 Oct 17:42:27.557 * Discarding previously cached master state.
22003:S 14 Oct 17:42:27.720 * MASTER <-> SLAVE sync: receiving 176 bytes from master
22003:S 14 Oct 17:42:27.721 * MASTER <-> SLAVE sync: Flushing old data
22003:S 14 Oct 17:42:27.721 * MASTER <-> SLAVE sync: Loading DB in memory
22003:S 14 Oct 17:42:27.721 * MASTER <-> SLAVE sync: Finished with success
(6)查看8000的日志,可以看到7000实例变为8000的从节点了
[aaa@qq.com ~]# tailf /data/redis-cluster/8000/redis.log
4398:M 14 Oct 17:14:17.603 # Cluster state changed: ok
4398:M 14 Oct 17:42:26.548 * Clear FAIL state for node aa252236bd90317b7b2625199387c0a2f5d31d3b: master without slots is reachable again.
4398:M 14 Oct 17:42:27.553 * Slave 10.0.0.107:7000 asks for synchronization
4398:M 14 Oct 17:42:27.553 * Partial resynchronization not accepted: Replication ID mismatch (Slave asked for 'abb3d5a9a75abe49e2942aa81940c2d5b0a8f51e', my replication IDs are 'a3cb1760dfb699138025e5f2a3deefb58579a4a1' and '97c62e6da389e7c16593d534019f0c40d144bcc4')
4398:M 14 Oct 17:42:27.553 * Starting BGSAVE for SYNC with target: disk
4398:M 14 Oct 17:42:27.556 * Background saving started by pid 22007
22007:C 14 Oct 17:42:27.637 * DB saved on disk
22007:C 14 Oct 17:42:27.638 * RDB: 8 MB of memory used by copy-on-write
4398:M 14 Oct 17:42:27.720 * Background saving terminated with success
4398:M 14 Oct 17:42:27.720 * Synchronization with slave 10.0.0.107:7000 succeeded
(7)登陆7000节点看下当前节点信息,8000变为主,7000为从
127.0.0.1:7000> cluster nodes
88bd89f4adac54f202cf819adc046f102ff892df 10.0.0.107:aaa@qq.com master - 0 1602668851000 2 connected 5461-10922
309c102d0c9f63d7999806260f1708d8a874f364 10.0.0.107:aaa@qq.com master - 0 1602668854615 3 connected 10923-16383
276b2e1099955f56dd34e1ddad16260638d8b5af 10.0.0.107:aaa@qq.com master - 0 1602668852603 7 connected 0-5460
859d7d26b2e4abb6870c2bf337a4919bdc2a6468 10.0.0.107:aaa@qq.com slave 88bd89f4adac54f202cf819adc046f102ff892df 0 1602668853609 5 connected
82f695a16572fd9dde45a999306766cf7e0048d6 10.0.0.107:aaa@qq.com slave 309c102d0c9f63d7999806260f1708d8a874f364 0 1602668853000 6 connected
aa252236bd90317b7b2625199387c0a2f5d31d3b 10.0.0.107:aaa@qq.com myself,slave 276b2e1099955f56dd34e1ddad16260638d8b5af 0 1602668850000 1 connected
(8)登陆8000节点查看一下当前节点信息,8000变为主,7000为从
127.0.0.1:8000> cluster nodes
859d7d26b2e4abb6870c2bf337a4919bdc2a6468 10.0.0.107:aaa@qq.com slave 88bd89f4adac54f202cf819adc046f102ff892df 0 1602668794118 5 connected
aa252236bd90317b7b2625199387c0a2f5d31d3b 10.0.0.107:aaa@qq.com slave 276b2e1099955f56dd34e1ddad16260638d8b5af 0 1602668793113 7 connected
88bd89f4adac54f202cf819adc046f102ff892df 10.0.0.107:aaa@qq.com master - 0 1602668792105 2 connected 5461-10922
276b2e1099955f56dd34e1ddad16260638d8b5af 10.0.0.107:aaa@qq.com myself,master - 0 1602668790000 7 connected 0-5460
309c102d0c9f63d7999806260f1708d8a874f364 10.0.0.107:aaa@qq.com master - 0 1602668791097 3 connected 10923-16383
82f695a16572fd9dde45a999306766cf7e0048d6 10.0.0.107:aaa@qq.com slave 309c102d0c9f63d7999806260f1708d8a874f364 0 1602668792000 6 connected
7、写入数据
在8000上写入数据,可以看到name1这个key的哈希槽12933会被放到7002实例上,所以写入数据后会转到7002实例上,然后name2这个key的哈希槽742在8000实例上,所以又会转到8000,获取的时候也一样。
[aaa@qq.com ~]# redis-cli -c -p 8000
[aaa@qq.com ~]# redis-cli -c -p 8000
127.0.0.1:8000> set name1 wangxiaoyu
-> Redirected to slot [12933] located at 10.0.0.107:7002
OK
10.0.0.107:7002> get name1
"wangxiaoyu"
10.0.0.107:7002> set name2 wangxiaoyu2
-> Redirected to slot [742] located at 10.0.0.107:8000
OK
10.0.0.107:8000> get name2
"wangxiaoyu2"
10.0.0.107:8000> get name1
-> Redirected to slot [12933] located at 10.0.0.107:7002
"wangxiaoyu"
从库也能set和get数据,同样的会进行跳转
7、add-node将新节点加入集群
redis-trib add-node new_host:new_port existing_host:existing_port --slave --master-id <arg>
其中,
new_host:new_port:待添加的节点,必须确保其为空或不在其它集群中。否则,会提示以下错误。
[ERR] Node 127.0.0.1:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
所以,线上建议使用redis-trib.rb添加新节点,因为其会对新节点的状态进行检查。如果手动使用cluster meet命令加入已经存在于其它集群的节点,会造成被加入节点的集群合并到现有集群的情况,从而造成数据丢失和错乱,后果非常严重,线上谨慎操作。
existing_host:existing_port:集群中任意一个节点的地址。
如果添加的是主节点,只需指定源节点和目标节点的地址即可。
redis-trib.rb add-node 127.0.0.1:6379 127.0.0.1:6384
如果添加的是从节点,其语法如下,
redis-trib.rb add-node --slave --master-id f413fb7e6460308b17cdb71442798e1341b56cbc 127.0.0.1:6379 127.0.0.1:6384
注意:–slave和–master-id必须写在前面,同样的参数,如果是下面这样写法,会提示错误,
# redis-trib.rb add-node 127.0.0.1:6379 127.0.0.1:6384 --slave --master-id f413fb7e6460308b17cdb71442798e1341b56cbc
[ERR] Wrong number of arguments for specified sub command
添加从节点,可不设置–master-id,此时会随机选择主节点。
例如7000为主,添加8000实例为从
/usr/local/redis/bin/redis-trib.rb add-node --slave 10.5.96.3:8000 10.5.96.3:7000 #添加从
/usr/local/redis/bin/redis-trib.rb add-node --slave 10.5.96.3:8001 10.5.96.3:7001
/usr/local/redis/bin/redis-trib.rb add-node --slave 10.5.96.3:8002 10.5.96.3:7002
上一篇: redis发布订阅模式
下一篇: Redis的发布订阅模式