RDB和AOF的优缺点
master和slave同步过程
哨兵的使用和实现机制
4、redis cluster集群创建和使用
第一个题目、RDB和AOF的优缺点
一、 RDB的优点
1、优点a:RDB 快照保存了某个时间点的数据,可以通过脚本执行redis指令bgsave(非阻塞,后台执行)或者save(会阻塞写操作,不推荐)命令自定义时间点备份,可以保多个备份,当出现问题可以恢复到不同时间点的版本,很适合备份并且此文件格式也有支持有不少第三方工具可以进行后续的数据分析。
比如:可以在最近的24小时内每小时备份一次RDB文件,并且在每一天,也备份一个RDB文件,这样的话即使遇上问题,也可以随时将数据集还原到不同的版本。
优点b:RDB可以最大化Redis 的性能,父进程在保存RDB文件时唯一要做的就是fork出一个子进程,然后这个子进程就会处理接下来的所有保存工作,父进程无需执行任何磁盘I/O操作。
优点c:RDB在大量数据,比如几个G的数据,恢复的速度比AOF快
2、RDB的缺点
缺点a:不能实时保存数据,可能会丢失自上一次执行RDB备份到当前的内存数据
如果你需要尽量避免在服务器故障时丢失数据,那么RDB不适合你。虽然Redis允许你设置不同的保存点(save point ) 来控制保存RDB文件的频率,但是,因为RD文件需要保存整个数据集的状态,所以它并不是一个轻松的操作。因此你可能会至少5分钟才保存一次RDB文件。在这种情况下,一旦发生故障停机,你就可能会丢失好几分钟的数据。
缺点b:当数据量非常大的时候,从父进程fork子进程进行保存至RDB文件时需要一点时间,可能是毫秒或秒,取决于磁盘的IO性能。
在数据集比较庞大时,fork()可能会非常耗时,造成服务器在一定时间内停止处理客户端:如果数据集非常巨大,并且CPU时间非常紧张的话,那么这种停止时间甚至可能会长达整整一秒或更久。虽然AOF重写也需要进行fork(),但无论AOF重写的执行间隔有多长,数据的持久性都不会有任何损失。
二、AOF的优缺点
1、优点a 数据安全性相对较高,根据所使用的fsnc策略(fsync是同步内存中redis所有已经修改的文件到存储设备),默认是appendfsync everysec,即每秒执行一次fsync,在这种配置下,Redis仍然可以保持良好的性能,并且就算发生故障停机,也最多只会丢失一秒钟的数据(fsync会在后台线程执行,所以主线程可以继续努力地处理命令请求)
优点b:由于该机制对日志文件的写入操作采用的是append模式,因此在写入过程中不需要seek,即使出现宕机现象,也不会破坏旧日志文件中已经存在的内容。然而如果本次操作只是写入了一半数据就出现了崩溃问题,不用担心,在Redis下一次启动前,可以通过redis-check-aof工具来解决数据一致性的问题。
优点c: Redis 可以在AOF文件体积变得过大时,自动地在后台对AOF进行重写:重写后的新AOF文件包含了恢复当前数据集所需要的最小命令集合。整个重写操作时绝对安全的因为Redis在创建新AOF文件的过程中,append模式不断的将修改数据追加到现有的AOF文件里面,即使重写过程中发生停机,现有的AOF文件也不会丢失。而一旦新的AOF文件创建完毕,Redis就会从旧AOF文件切换到 新的AOF文件,并开始对新AOF文件进行追加操作。
优点d:AOF包含一个格式清晰、易于理解的日志文件用于记录所有的修改操作。事实上,也可以通过该文件完成数据的重建。AOF文件有序地保存了对数据库执行的所有写入操作,这些写入操作Redis协议的格式保存,因此AOF文件的内容非常容易被人读懂,对文件进行分析(parse)也很轻松。导出(export)AOF文件也非常简单:举个例子,如果你不小心执行了FLUSHALL.命令,但只要AOF文件未被重写,那么只要停止服务器,移除AOF文件末尾的FULUSHALL命令,并重启Redis,就可以将数据集恢复到FLUSHALL执行之前的状态
2、缺点a: 即使有些操作时重复的也会全部记录,AOF的文件大小要大于RDB格式的文件
缺点b:AOF在恢复大数据集时的速度比RDBB的恢复速度要慢
缺点c:根据fsync策略不同,AOF速度可能会慢于RDB
缺点d;bug出现的可能性更多
RDB和AOF的选择
如果主要充当缓冲功能,或者可以承受数分钟数据的i丢失,通常生产环境一般只需启用RDB即可,此也是默认值。
如果数据需要持久保存,一点不能丢失,可以选择同时开启RDB和AOF,一般不建议只开启AOF
第二个题目master和slave同步过程
实验如下
准备:主节点MASTER-IP 10.0.0.128
从节点SLAVE-IP 10.0.0.137
一、在主节点上修改配置文件
[root@master ~]#vim /etc/redis.config
bind 0.0.0.0
requirepass 123456
configset masterauth 123456
重启服务
systemclt restart redis
二、在从节点上修改配置文件
[root@slave ~]#vim /etc/redis.config
bind 0.0.0.0
requirepass 123456
configset masterauth 123456
replicaof 10.0.0.110 6379
重启服务
systemclt restart redis
三、验证主从是否同步
[root@slave ~]#redis-cli -a 123456 info replication
# Replication
role:slave 本机角色是slave
master_host:10.0.0.128 主节点为10.0.0.128同步成功
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:593
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen
第三个题目、哨兵的使用和实现机制
防止脑裂,准备奇数台主机实验
master: 10.0.0.110
slave1: 10.0.0.120
slave2: 10.0.0.123
一、master配置
1、用sed实现master配置修改
bind 0.0.0.0
masterauth 123456
requirepass 12345
[root@10.0.0.128]#sed -i -e ‘s/bind 127.0.0.1/bind 0.0.0.0/‘ -e ‘s/^# masterauth .*/masterauth 123456/‘ -e ‘s/^# requirepass .*/requirepass 123456/‘ /etc/redis.conf
检查配置是否有效
grep -Ev ‘^#|^$‘ /etc/redis.conf
修改哨兵sentinel配置文件
[root@10.0.0.128]#vim /etc/redis-sentinel.conf
sentinel monitor mymaster 10.0.0.8 6379 2 (mymaster是集群的命名,集群总节点数/2取整数,小数则四舍五入3/2+1=2)
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 3000
grep -Ev ‘^#|^$‘ /etc/redis.conf
grep -Ev ‘^#|^$‘ /etc/redis-sentinel.conf
cat /etc/redis-sentinel.conf | grep sentinel
重启服务
systemctl restart redis redis-sentinel
二、修改slave1配置文件
1、slave1修改配置 用sed实现以下配置 并添加master10.0.0.110为主节点
[root@10.0.0.120]#sed -i -e ‘s/bind 127.0.0.1/bind 0.0.0.0/‘ -e ‘s/^# masterauth .*/masterauth 123456/‘ -e ‘s/^# requirepass .*/requirepass 123456/‘ /etc/redis.conf
[root@10.0.0.120]#echo replicaof 10.0.0.110 6379
检查配置是否有效
grep -Ev ‘^#|^$‘ /etc/redis.conf
修改哨兵sentinel配置文件
[root@10.0.0.120]#vim /etc/redis-sentinel.conf
sentinel monitor mymaster 10.0.0.110 6379 2 (mymaster是集群的命名,集群总节点数/2取整数,小数则四舍五入3/2+1=2)
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 3000
检查配置是否有效
grep -Ev ‘^#|^$‘ /etc/redis-sentinel.conf
三、修改slave2配置文件
1、slave2修改配置 用sed实现以下配置 并添加master10.0.0.110为主节点
[root@10.0.0.123]#sed -i -e ‘s/bind 127.0.0.1/bind 0.0.0.0/‘ -e ‘s/^# masterauth .*/masterauth 123456/‘ -e ‘s/^# requirepass .*/requirepass 123456/‘ /etc/redis.conf
[root@10.0.0.123]#echo replicaof 10.0.0.110 6379
检查配置是否有效
grep -Ev ‘^#|^$‘ /etc/redis.conf
修改哨兵sentinel配置文件
[root@10.0.0.123]#vim /etc/redis-sentinel.conf
sentinel monitor mymaster 10.0.0.110 6379 2 (mymaster是集群的命名,集群总节点数/2取整数,小数则四舍五入3/2+1=2)
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 3000
检查配置是否有效
grep -Ev ‘^#|^$‘ /etc/redis-sentinel.conf
四、检查各节点角色是否1主两从?各节点的myid是否不同,哨兵sentinel总数是否为3
1、检查主从角色
[root@master ~]#redis-cli -a 123456 info replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.120,port=6379,state=online,offset=4087,lag=1
slave1:ip=10.0.0.123,port=6379,state=online,offset=4087,lag=1
master_replid:2be1cf7dfeed95e22c139873b62a3cf6e153b41d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4101
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:4101
检查哨兵总数
[root@centos ~]#redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.0.110:6379,slaves=2,sentinels=3
比较三个节点的myid,不同则配置正确。
[root@master ~]#grep -Ev ‘^#|^$‘ /etc/redis-sentinel.conf |grep myid
sentinel myid e9112c856e07f57b42156fff06aa9e0524bb5395
[root@slave1 ~]#grep -Ev ‘^#|^$‘ /etc/redis-sentinel.conf |grep myid
sentinel myid a0d3ffed61130e96a384649d0538b082ae8137cd
[root@slav2 ~]#grep -Ev ‘^#|^$‘ /etc/redis-sentinel.conf |grep myid
sentinel myid 3c81969730106b45a8988a859360535f682d197e
模拟主节点master出现故障
[root@master ~]#systemctl stop redis
日志查看故障转移过程(任意节点皆可查询)
tail -f /var/log/redis/sentinel.log
3092:X 06 Dec 2020 10:22:11.468 * +slave slave 10.0.0.120:6379 10.0.0.120 6379 @ mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:22:11.470 * +slave slave 10.0.0.123:6379 10.0.0.123 6379 @ mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:22:11.534 # -sdown master mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:22:11.535 # -odown master mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:55:30.121 # +sdown master mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:55:30.177 # +new-epoch 4
3092:X 06 Dec 2020 10:55:30.177 # +vote-for-leader a0d3ffed61130e96a384649d0538b082ae8137cd 4
3092:X 06 Dec 2020 10:55:30.191 # +odown master mymaster 10.0.0.110 6379 #quorum 3/2
3092:X 06 Dec 2020 10:55:30.191 # Next failover delay: I will not start a failover before Sun Dec 6 11:01:30 2020
3092:X 06 Dec 2020 10:55:31.260 # +config-update-from sentinel a0d3ffed61130e96a384649d0538b082ae8137cd 10.0.0.120 26379 @ mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:55:31.260 # +switch-master mymaster 10.0.0.110 6379 10.0.0.120 6379
3092:X 06 Dec 2020 10:55:31.261 * +slave slave 10.0.0.123:6379 10.0.0.123 6379 @ mymaster 10.0.0.120 6379 可看到slave1 10.0.0.120被选举为主节点master
3092:X 06 Dec 2020 10:55:31.261 * +slave slave 10.0.0.110:6379 10.0.0.110 6379 @ mymaster 10.0.0.120 6379
3092:X 06 Dec 2020 10:55:34.291 # +sdown slave 10.0.0.110:6379 10.0.0.110 6379 @ mymaster 10.0.0.120 6379
[root@slave1 ~]#redis-cli -a 123456 info replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
# Replication
role:master 已经为master
connected_slaves:1 由于原来主节点宕机,所以现在是新master和slave2
slave0:ip=10.0.0.123,port=6379,state=online,offset=98579,lag=0
master_replid:e7114ed4c2fee39cf0463a573d4b90fc15d245f5
master_replid2:85631e9b1fb9d1790e8966d05ccd50356d6d9cb0
master_repl_offset:98579
second_repl_offset:969
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98579
检查slave2的配置文件/etc/redis.conf已经自动更新为10.0.0.120
[root@slave2 ~]#grep -Ev ‘^#|^$‘ /etc/redis.conf | grep replicaof
replicaof 10.0.0.120 63
检查slave2的配置文件 /etc/redis-sentinel.conf 是否自动更新成功
[root@slave2 ~]#grep -Ev ‘^#|^$‘ /etc/redis-sentinel.conf | grep mymaster
sentinel monitor mymaster 10.0.0.120 6379 2 已更新
sentinel down-after-milliseconds mymaster 3000
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 4
sentinel leader-epoch mymaster 4
sentinel known-replica mymaster 10.0.0.110 6379
sentinel known-replica mymaster 10.0.0.123 6379
sentinel known-sentinel mymaster 10.0.0.120 26379 a0d3ffed61130e96a384649d0538b082ae8137cd
sentinel known-sentinel mymaster 10.0.0.110 26379 e9112c856e07f57b42156fff06aa9e0524bb5395
第四个题目redis cluster集群创建和使用
准备三台主机
maser1:10.0.0.110
master2:10.0.0.120
master3:10.0.0.123
新加入的master4:10.0.0.130
新加入的slave1:10.0.0.138
一、redis cluster集群的创建
1在三台主机修改同样的操作:安装redis服务
[root@master1/2/3~]#yum -y install redis
在三台主机修改同样的操作:修改配置文件/etc/redis.conf
方法a vim
[root@master1~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456
requirepass 123456
cluster-enabled yes 默认关闭
cluster-config-file nodes-6379.conf 去掉注释
cluster-require-full-coverage no
方法b 用sed命令修改主配置文件
[root@master1 ~]#sed -i.bak -e ‘s/bind 127.0.0.1/bind 0.0.0.0/‘ -e ‘/masterauth/a masterauth 123456‘ -e ‘/# requirepass/a requirepass 123456‘ -e ‘/# cluster-enabled yes/a cluster-enabled yes‘ -e ‘/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf‘ -e ‘/cluster-require-full-coverage yes/c cluster-require-full-coverage no‘ /etc/redis.conf
重启redis服务
[root@master1 ~]#systemctl enable --now redis
备注:mastter2 和master3配置同上面的master1
各个节点之间建立友谊关系meet
[root@mster1 ~]#redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.120 6379
OK
[root@master1 ~]#redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.123 6379
OK
[root@master2~]#redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.123 6379
OK
任意节点查看该集群当前几个主节点
[root@master1 ~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379@16379 master - 0 1607226490256 2 connected
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 myself,master - 0 1607226488000 0 connected
01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379@16379 master - 0 1607226491279 1 connected
*******该集群有三个主节点已经创建好。********
二、redis cluster集群的使用 (该实验以redis5版本为例,若3/4版本,需要用ruby工具安装依赖包来搭建ruby的环境)
下面从扩容,缩容来分别实验
A扩容实验
1、在其中一台主节点分配槽位即可:由于有16834个slots(槽位),所以需要 编辑脚本分别在各节点分配槽位
[root@master1 ~]#vim addslot.sh
host=$1
port=$2
start=$3
end=$4
pass=123456
for slot in `seq ${start} ${end}` ;do
echo slot:$slot
redis-cli -h ${host} -p $port -a ${pass} --no-auth-warning cluster addslots ${slot}
done
2、执行脚本生成预先规划好的槽位
[root@master1 ~]#bash addslot.sh 10.0.0.110 6379 0 5461
[root@master1 ~]#bash addslot.sh 10.0.0.120 6379 5462 10922
[root@master1 ~]#bash addslot.sh 10.0.0.123 6379 10923 16383
查看槽位是否分配好
[root@centos ~]#redis-cli -a 123456 --no-auth-warning cluster nodes
c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379@16379 master - 0 1607239685000 2 connected 10923-16383
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 myself,master - 0 1607239683000 0 connected 0-5461
01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379@16379 master - 0 1607239685330 1 connected 5462-10922
添加从节点并指定从节点和主节点
[root@master1 ~]#redis-cli -a 123456 --cluster add-node 10.0.0.130:6379 10.0.0.110:6379
由于新master加入,需要重新分配槽位,思路:将原来的节点的所有槽位平均分配给它即可。
在任意一台主机即可分配槽位
[root@master1 ~]#redis-cli -a 123456 --cluster reshard 10.0.0.130:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 10.0.0.130:6379)
M: 8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379
slots: (0 slots) master
M: d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379
slots:[0-5461] (5462 slots) master
M: c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379
slots:[10923-16383] (5461 slots) master
M: 01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379
slots:[5462-10922] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096 4个master平均分配槽位= (16384/4=4096)
What is the receiving node ID? 8683d12e877291842b2dadb428fd8b72ab294425 新master的ID号
Please enter all the source node IDs.
Type ‘all‘ to use all the nodes as source nodes for the hash slots.
Type ‘done‘ once you entered all the source nodes IDs.
Source node #1: all 说明将所有节点的槽位都会平均分配到新master
...........................................................................
Moving slot 6824 from 01f1dd9c14ea7f886456a7758f4198d6280ae390
Moving slot 6825 from 01f1dd9c14ea7f886456a7758f4198d6280ae390
Moving slot 6826 from 01f1dd9c14ea7f886456a7758f4198d6280ae390
Do you want to proceed with the proposed reshard plan (yes/no)? yes
任意一台主机查看槽位
[root@master1~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 master - 0 1607241112000 0 connected 1366-5461
8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379@16379 myself,master - 0 1607241110000 4 connected 0-1365 5462-6826 10923-12287
c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379@16379 master - 0 1607241111000 2 connected 12288-16383
01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379@16379 master - 0 1607241111779 1 connected 6827-10922
现在加入一台从节点加入成为10.0.0.110的从节点
修改配置文件 /etc/redis.conf
[root@10.0.0.138 ~]#sed -i.bak -e ‘s/bind 127.0.0.1/bind 0.0.0.0/‘ -e ‘/masterauth/a masterauth 123456‘ -e ‘/# requirepass/a requirepass 123456‘ -e ‘/# cluster-enabled yes/a cluster-enabled yes‘ -e ‘/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf‘ -e ‘/cluster-require-full-coverage yes/c cluster-require-full-coverage no‘ /etc/redis.conf
重启服务
[root@10.0.0.138 ~]#systemctl enable --now redis
将新节点138加入集群,默认是加入后为主节点master
[root@10.0.0.138~]#redis-cli -a 123456 --cluster add-node 10.0.0.138:6379 10.0.0.120:6379 --cluster-slave --cluster-master-id d7fdc965f3d5c6f6c22d049233f90fa97594e0a8
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
>>> Adding node 10.0.0.138:6379 to cluster 10.0.0.120:6379
>>> Performing Cluster Check (using node 10.0.0.120:6379)
M: 01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379
slots:[6827-10922] (4096 slots) master
M: 8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379
slots:[0-1365],[5462-6826],[10923-12287] (4096 slots) master
M: c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379
slots:[12288-16383] (4096 slots) master
M: d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379
slots:[1366-5461] (4096 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 10.0.0.138:6379 to make it join the cluster.
Waiting for the cluster to join
...
>>> Configure node as replica of 10.0.0.110:6379.
[OK] New node added correctly. 添加成功
查看10.0.0.110的从节点是否是138的主节点
[root@localhost ~]#redis-cli -a 123456 --no-auth-warning cluster nodes
901c99dbad1fbbd86bc469015ac99469341bead0 10.0.0.138:6379@16379 slave d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 0 1607242075152 5 connected 已更新为110的从
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 master - 0 1607242076159 0 connected 1366-5461
B缩容实验 (删除冗余的主节点10.0.0.130)实验如下
1、先转移该节点的槽位到其他各节点
[root@localhost ~]#redis-cli -a 123456 --cluster reshard 10.0.0.130:6379
[root@localhost ~]#redis-cli -a 123456 --cluster reshard 10.0.0.130:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 10.0.0.130:6379)
M: 8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379
slots:[0-1365],[5462-6826],[10923-12287] (4096 slots) master 有3个槽位 ,如果转移数据,需要转3次。
S: 901c99dbad1fbbd86bc469015ac99469341bead0 10.0.0.138:6379
slots: (0 slots) slave
replicates d7fdc965f3d5c6f6c22d049233f90fa97594e0a8
M: 01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379
slots:[6827-10922] (4096 slots) master
M: d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379
slots:[1366-5461] (4096 slots) master
1 additional replica(s)
M: c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379
slots:[12288-16383] (4096 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1366 第1个槽位
What is the receiving node ID? 01f1dd9c14ea7f886456a7758f4198d6280ae390
Please enter all the source node IDs.
Type ‘all‘ to use all the nodes as source nodes for the hash slots.
Type ‘done‘ once you entered all the source nodes IDs.
Source node #1: 8683d12e877291842b2dadb428fd8b72ab294425 分配给10.0.0.120
Source node #2: done 结束
同理分别转移[5462-6826],[10923-12287]
3、查看10.0.0.130是否还有槽位
[root@centos ~]#redis-cli -a 123456 cluster nodes
8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379@16379 master - 0 1607243239000 4 connected 已无槽位
删除主节点10.0.0.130
[root@centos ~]#redis-cli -a 123456 --cluster del-node 10.0.0.20:6379 8683d12e877291842b2dadb428fd8b72ab294425
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
>>> Removing node 8683d12e877291842b2dadb428fd8b72ab294425 from cluster 10.0.0.20:6379
Could not connect to Redis at 10.0.0.20:6379: No route to host
5查看主节点10.0.0.130是否存在
[root@centos ~]#redis-cli -a 123456 cluster nodes 可看到10.0.0.130删除成功
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 myself,master - 0 1607243608000 8 connected 1366-5461 10923-12287
c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379@16379 master - 0 1607243608842 7 connected 5462-6826 12288-16383
01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379@16379 master - 0 1607243607000 6 connected 0-1365 6827-10922
901c99dbad1fbbd86bc469015ac99469341bead0 10.0.0.138:6379@16379 slave d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 0 1607243607833 8 connecte
删除10.0.0.130的参与数据并重启
[root@10.0.0.130 ~]#rm -f /var/lib/redis/nodes-6379.conf
[root@10.0.0.130 ~]#systemctl restart redis
7
查看是否有集群信息
[root@localhost ~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
210560ccd0b2767b73d40dacf8ef7727fdf2c21e :6379@16379 myself,master - 0 0 0 connected
删8除从节点10.0.0.138,一条命令即可
[root@localhost ~]#redis-cli -a 123456 --cluster del-node 10.0.0.120:6379 901c99dbad1fbbd86bc469015ac99469341bead0
C将外部数据导入集群中
假设,redis服务器已运行一段时间,由于也许量庞大,需要建立cluster来符合公司要求。
如何将redis服务器10.0.0.130的数据导入新建的集群中?
1、将130的主机恢复单机redis服务器的配置,取消集群相关配置。
2、删除所有节点的密码
[root@localhost ~]#redis-cli -a 123456 --no-auth-warning config set requirepass "" ( ""表示为禁止所有)5版本
[root@localhost ~]#redis-cli --cluster import 10.0.0.128:6379 --cluster-from 10.0.0.136:6379 --cluster-replace --cluster-copy 若有相同Key则覆盖原来的key值
[root@localhost ~]#redis-cli --cluster import 10.0.0.128:6379 --cluster-from 10.0.0.136:6379 5 若有相同key 不覆盖原来的值,外部数据也默认禁止写入。
redis-cli --cluster import 10.0.0.128:6379 --cluster-from 10.0.0.130:6379
redis:RDB AOF -master &slave -sentinel-cluster
原文地址:https://www.cnblogs.com/lijingbo000/p/14093463.html