Redis 集群 & Redis 集群搭建

1.非集群

Redis 如果部署的是单机,单实例的;这种模式缺点是很明显的,当单节点出现故障的时候,对系统的可用性几乎是毁灭性的;

缺点:

  • 单点故障,不可以
  • 容量有限,不能大量数据储存
  • 访问压力大,单节点无法快速处理大量IO操作;

2.主从集群

2.1 主从数据同步方案

如果自己来设计Redis数据的主从同步;从节点的同步方式上来看 ,有两种方式,从节点同步响应和异步响应;

redis跨服务器部署主从和哨兵 跨机房redis集群_redis-sentinel

从客户端是否访问从节点又可以分为主从主备

主从:是从节点也是可以访问的

主备:从节点的作用是在主节点不可用时,代替主节点;

redis跨服务器部署主从和哨兵 跨机房redis集群_redis-cluster_02

不论是主从还是主备,都是一个主节点,如何进行主节点的替换?

对所有节点监控 ;

注:监控方式

对所有节点监控,如果想要监控是可用的 ,监控服务也需要是需要多节点的;

redis跨服务器部署主从和哨兵 跨机房redis集群_redis跨服务器部署主从和哨兵_03

多节点如克判断一个Redis 是否存活

  • 都成功判断成功 ,所有监控强一致性,新能问题,代价较大,不可取、
  • 一部分成功 判断节点存活 (下面分析为什么是一半)

为什么是一半以上

一般的监控是有一个势力范围(监控状态一致)的概念,节点的存活是有最大的势力范围决策;

当有3个监控节点的时候,2 的监控达成一个势力范围,还有1个单独达成一个,决策权力在2 的监控

当有4个监控节点的时候,3的监控达成一个势力范围,还有1个单独达成一个,决策权力在3的监控;当两两达成,发生脑裂,无法决策

当有5个监控节点的时候,3的监控达成一个势力范围,还有2个单独达成一个,决策权力在3的监控; 4的监控达成一个势力范围,还有1个单独达成一个,决策权力在4的监控;

可以推导出节点的个数必须是 n/2 +1 才可以判断成功;

一般集群使用基数;

  • n 和 n+1 可以承受的风险是一致的,比如5,6 都只是允许2个宕机,超过就不行了,风险是一样的,还多一台服务器
  • 服务器多了更容易出现故障

2.2 Redis 主从数据同步

从库只可以读 ,不可以写 ( 可以修改 :有配置文件 默认 replica-read-only yes)

细节:

Redis全量复制一般发生在Slave初始化阶段;

  • slave连接master 服务器,发送SYNC命令;
  • SYNC 收到后 master -> BGSAVE 生成RDB文件并使用缓冲区记录此后执行的所有写命令 : ##有一个配置可以直接走网络 repl-diskless-sync
  • 发送 RDB文件 继续记录写命令
  • slave 收到rdb文件后丢弃所有old data,load rdb (从节点开启AOF 后 ,加载到主节点的RDB 文件后,还需要重写AOF文件;)
  • slave load rdb文件 后 ,master 发送缓冲区记录的写命令
  • slave加载完毕;

主节点日志

1630:M 30 Mar 2022 15:22:54.186 * Replica 127.0.0.1:6666 asks for synchronization
1630:M 30 Mar 2022 15:22:54.186 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '0632811093f3bf21fc134d1a7fd1ec28d4c4f105', my replication IDs are '18d6f46477d5b48953508af87203adff9937536b' and '00000000000000000000000000000
00000000000')1630:M 30 Mar 2022 15:22:54.186 * Replication backlog created, my new replication IDs are '7e83762b88959d336fddc6567e9bef0612936554' and '0000000000000000000000000000000000000000'
## 2
1630:M 30 Mar 2022 15:22:54.186 * Starting BGSAVE for SYNC with target: disk
1630:M 30 Mar 2022 15:22:54.206 * Background saving started by pid 1695
1695:C 30 Mar 2022 15:22:54.218 * DB saved on disk
1695:C 30 Mar 2022 15:22:54.218 * RDB: 4 MB of memory used by copy-on-write
1630:M 30 Mar 2022 15:22:54.221 * Background saving terminated with success
## 3
1630:M 30 Mar 2022 15:22:54.221 * Synchronization with replica 127.0.0.1:6666 succeeded

从节点日志

1646:S 30 Mar 2022 15:22:54.181 * Connecting to MASTER 127.0.0.1:6379
1646:S 30 Mar 2022 15:22:54.182 * MASTER <-> REPLICA sync started
1646:S 30 Mar 2022 15:22:54.183 * REPLICAOF 127.0.0.1:6379 enabled (user request from 'id=3 addr=127.0.0.1:40992 laddr=127.0.0.1:6666 fd=8 name= age=74 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=44 qbuf-free=40910 argv-mem=22 obl=0 oll=0 omem=0 tot-mem=6148
6 events=r cmd=replicaof user=default redir=-1')1646:S 30 Mar 2022 15:22:54.184 * Non blocking connect for SYNC fired the event.
1646:S 30 Mar 2022 15:22:54.185 * Master replied to PING, replication can continue... 
## 1
1646:S 30 Mar 2022 15:22:54.185 * Trying a partial resynchronization (request 0632811093f3bf21fc134d1a7fd1ec28d4c4f105:1).
1646:S 30 Mar 2022 15:22:54.206 * Full resync from master: 7e83762b88959d336fddc6567e9bef0612936554:0
1646:S 30 Mar 2022 15:22:54.206 * Discarding previously cached master state
## 4
1646:S 30 Mar 2022 15:22:54.221 * MASTER <-> REPLICA sync: receiving 175 bytes from master to disk
1646:S 30 Mar 2022 15:22:54.221 * MASTER <-> REPLICA sync: Flushing old data
1646:S 30 Mar 2022 15:22:54.221 * MASTER <-> REPLICA sync: Loading DB in memory
1646:S 30 Mar 2022 15:22:54.221 * Loading RDB produced by version 6.2.6
1646:S 30 Mar 2022 15:22:54.221 * RDB age 0 seconds
1646:S 30 Mar 2022 15:22:54.221 * RDB memory usage when created 1.85 Mb
1646:S 30 Mar 2022 15:22:54.221 # Done loading RDB, keys loaded: 0, keys expired: 0.
## 5 (没有命令) 6
1646:S 30 Mar 2022 15:22:54.221 * MASTER <-> REPLICA sync: Finished with success

2.3 节点监控

前面说的监控Redis 存活状态,Redis 使用Sentinel 监控

3.主从集群搭建

搭建一主两从节点的集群

3.1 修改节点配置

2个从节点配置文件中的 replicaof <masterip> <masterport>

################################# REPLICATION #################################

# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
#   +------------------+      +---------------+
#   |      Master      | ---> |    Replica    |
#   | (receive writes) |      |  (exact copy) |
#   +------------------+      +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition replicas automatically try to reconnect to masters
#    and resynchronize with them.
#
# replicaof <masterip> <masterport>
replicaof 127.0.0.1 6379

3.2 监控 Sentinel

Redis-server 也是可以是一个监控程序 启动脚本添加 --sentinel 如下 脚本 Redis-sentinel 是一个软链接 也是Redis-server

-rwxr-xr-x. 1 root root 4829560 3月  12 14:52 Redis-benchmark
lrwxrwxrwx. 1 root root      12 3月  12 14:52 Redis-check-aof -> Redis-server
lrwxrwxrwx. 1 root root      12 3月  12 14:52 Redis-check-rdb -> Redis-server
-rwxr-xr-x. 1 root root 5003856 3月  12 14:52 Redis-cli
lrwxrwxrwx. 1 root root      12 3月  12 14:52 Redis-sentinel -> Redis-server
-rwxr-xr-x. 1 root root 9518984 3月  12 14:52 Redis-server

编写三个sentinel 启动的脚本

# 三个文件
-rw-r--r--. 1 root root   705 3月  30 16:35 26379Sen.conf
-rw-r--r--. 1 root root   705 3月  30 16:35 26380.conf
-rw-r--r--. 1 root root   705 3月  30 16:35 26381.conf

##文件内容
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
# sentinel down-after-milliseconds mymaster 60000
# sentinel failover-timeout mymaster 180000
# sentinel parallel-syncs mymaster 1
# Generated by CONFIG REWRITE

port 26380
sentinel monitor mymaster 127.0.0.1 6379 2
# sentinel down-after-milliseconds mymaster 60000
# sentinel failover-timeout mymaster 180000
# sentinel parallel-syncs mymaster 1
# Generated by CONFIG REWRITE


port 26381
sentinel monitor mymaster 127.0.0.1 6379 2
# sentinel down-after-milliseconds mymaster 60000
# sentinel failover-timeout mymaster 180000
# sentinel parallel-syncs mymaster 1
# Generated by CONFIG REWRITE

3.3 启动 Redis

启动三个节点

#### 主节点日志
1934:M 30 Mar 2022 16:21:34.891 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1934:M 30 Mar 2022 16:21:34.891 # Server initialized
1934:M 30 Mar 2022 16:21:34.892 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1'
 for this to take effect.1934:M 30 Mar 2022 16:21:34.892 * Loading RDB produced by version 6.2.6
1934:M 30 Mar 2022 16:21:34.892 * RDB age 404 seconds
1934:M 30 Mar 2022 16:21:34.892 * RDB memory usage when created 1.83 Mb
1934:M 30 Mar 2022 16:21:34.892 # Done loading RDB, keys loaded: 1, keys expired: 0.
1934:M 30 Mar 2022 16:21:34.892 * DB loaded from disk: 0.000 seconds
1934:M 30 Mar 2022 16:21:34.892 * Ready to accept connections
1934:M 30 Mar 2022 16:21:42.597 * Replica 127.0.0.1:6666 asks for synchronization
1934:M 30 Mar 2022 16:21:42.597 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '7e83762b88959d336fddc6567e9bef0612936554', my replication IDs are 'ee141f7b1e0c7efecfd7c763b1f58d2030e0767e' and '00000000000000000000000000000
00000000000')1934:M 30 Mar 2022 16:21:42.597 * Replication backlog created, my new replication IDs are 'a58f67cd2df2777982b648aacb0f2ca2d004039f' and '0000000000000000000000000000000000000000'
1934:M 30 Mar 2022 16:21:42.597 * Starting BGSAVE for SYNC with target: disk
1934:M 30 Mar 2022 16:21:42.597 * Background saving started by pid 1944
1944:C 30 Mar 2022 16:21:42.599 * DB saved on disk
1944:C 30 Mar 2022 16:21:42.600 * RDB: 2 MB of memory used by copy-on-write
1934:M 30 Mar 2022 16:21:42.653 * Background saving terminated with success
1934:M 30 Mar 2022 16:21:42.653 * Synchronization with replica 127.0.0.1:6666 succeeded
1934:M 30 Mar 2022 16:21:50.290 * Replica 127.0.0.1:6555 asks for synchronization
1934:M 30 Mar 2022 16:21:50.290 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '7e83762b88959d336fddc6567e9bef0612936554', my replication IDs are 'a58f67cd2df2777982b648aacb0f2ca2d004039f' and '00000000000000000000000000000
00000000000')1934:M 30 Mar 2022 16:21:50.290 * Starting BGSAVE for SYNC with target: disk
1951:C 30 Mar 2022 16:21:50.292 * DB saved on disk
1951:C 30 Mar 2022 16:21:50.293 * RDB: 2 MB of memory used by copy-on-write
1934:M 30 Mar 2022 16:21:50.302 * Background saving started by pid 1951
1934:M 30 Mar 2022 16:21:50.312 * Background saving terminated with success
1934:M 30 Mar 2022 16:21:50.313 * Synchronization with replica 127.0.0.1:6555 succeeded



#从节点1 
1939:S 30 Mar 2022 16:21:42.594 * Before turning into a replica, using my own master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
1939:S 30 Mar 2022 16:21:42.594 * Ready to accept connections
1939:S 30 Mar 2022 16:21:42.596 * Connecting to MASTER 127.0.0.1:6379
1939:S 30 Mar 2022 16:21:42.596 * MASTER <-> REPLICA sync started
1939:S 30 Mar 2022 16:21:42.596 * Non blocking connect for SYNC fired the event.
1939:S 30 Mar 2022 16:21:42.596 * Master replied to PING, replication can continue...
1939:S 30 Mar 2022 16:21:42.597 * Trying a partial resynchronization (request 7e83762b88959d336fddc6567e9bef0612936554:4395).
1939:S 30 Mar 2022 16:21:42.598 * Full resync from master: a58f67cd2df2777982b648aacb0f2ca2d004039f:0
1939:S 30 Mar 2022 16:21:42.598 * Discarding previously cached master state.
1939:S 30 Mar 2022 16:21:42.653 * MASTER <-> REPLICA sync: receiving 189 bytes from master to disk
1939:S 30 Mar 2022 16:21:42.653 * MASTER <-> REPLICA sync: Flushing old data
1939:S 30 Mar 2022 16:21:42.653 * MASTER <-> REPLICA sync: Loading DB in memory
1939:S 30 Mar 2022 16:21:42.654 * Loading RDB produced by version 6.2.6
1939:S 30 Mar 2022 16:21:42.654 * RDB age 0 seconds
1939:S 30 Mar 2022 16:21:42.654 * RDB memory usage when created 1.83 Mb
1939:S 30 Mar 2022 16:21:42.654 # Done loading RDB, keys loaded: 1, keys expired: 0.
1939:S 30 Mar 2022 16:21:42.654 * MASTER <-> REPLICA sync: Finished with success


##从节点2 
1800:S 30 Mar 2022 15:27:13.908 * Before turning into a replica, using my own master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
1800:S 30 Mar 2022 15:27:13.908 * Connecting to MASTER 127.0.0.1:6379
1800:S 30 Mar 2022 15:27:13.908 * MASTER <-> REPLICA sync started
1800:S 30 Mar 2022 15:27:13.908 * REPLICAOF 127.0.0.1:6379 enabled (user request from 'id=3 addr=127.0.0.1:32968 laddr=127.0.0.1:6555 fd=8 name= age=34 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=44 qbuf-free=40910 argv-mem=22 obl=0 oll=0 omem=0 tot-mem=6148
6 events=r cmd=replicaof user=default redir=-1')1800:S 30 Mar 2022 15:27:13.908 * Non blocking connect for SYNC fired the event.
1800:S 30 Mar 2022 15:27:13.909 * Master replied to PING, replication can continue...
1800:S 30 Mar 2022 15:27:13.909 * Trying a partial resynchronization (request 2bfd7e43c382fe91adfde97e7013df8215bef09a:1).
1800:S 30 Mar 2022 15:27:13.928 * Full resync from master: 7e83762b88959d336fddc6567e9bef0612936554:364
1800:S 30 Mar 2022 15:27:13.928 * Discarding previously cached master state.
1800:S 30 Mar 2022 15:27:14.029 * MASTER <-> REPLICA sync: receiving 176 bytes from master to disk
1800:S 30 Mar 2022 15:27:14.029 * MASTER <-> REPLICA sync: Flushing old data
1800:S 30 Mar 2022 15:27:14.029 * MASTER <-> REPLICA sync: Loading DB in memory
1800:S 30 Mar 2022 15:27:14.030 * Loading RDB produced by version 6.2.6
1800:S 30 Mar 2022 15:27:14.030 * RDB age 1 seconds
1800:S 30 Mar 2022 15:27:14.030 * RDB memory usage when created 1.91 Mb
1800:S 30 Mar 2022 15:27:14.030 # Done loading RDB, keys loaded: 0, keys expired: 0.
1800:S 30 Mar 2022 15:27:14.030 * MASTER <-> REPLICA sync: Finished with success

3.4 启动Sentinel

Redis-server 26381.conf --sentinel
Redis-server 26380.conf --sentinel
Redis-server 26379.conf --sentinel
#1
2335:X 30 Mar 2022 21:04:22.512 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2335:X 30 Mar 2022 21:04:22.514 # Sentinel ID is c987033cc8e5bc291fe53d38ca207ccfef622271
2335:X 30 Mar 2022 21:04:22.514 # +monitor master mymaster 127.0.0.1 6379 quorum 2
// 检测到master 后面两个slave
2335:X 30 Mar 2022 21:04:22.515 * +slave slave 127.0.0.1:6666 127.0.0.1 6666 @ mymaster 127.0.0.1 6379
2335:X 30 Mar 2022 21:04:22.516 * +slave slave 127.0.0.1:6555 127.0.0.1 6555 @ mymaster 127.0.0.1 6379
// 后面 #2 #3 上线后检查到的其他的sentilnel  基于Redis 订阅发布
2335:X 30 Mar 2022 21:05:18.923 * +sentinel sentinel 6c320b0564e0434b87d2a1449c65a1d0f28df75a 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
2335:X 30 Mar 2022 21:05:41.192 * +sentinel sentinel 73a5136033e7cc93a352742de6b67aba5fde42da 127.0.0.1 26381 @ mymaster 127.0.0.1 6379


#2
2344:X 30 Mar 2022 21:05:16.912 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2344:X 30 Mar 2022 21:05:16.914 # Sentinel ID is 6c320b0564e0434b87d2a1449c65a1d0f28df75a
2344:X 30 Mar 2022 21:05:16.914 # +monitor master mymaster 127.0.0.1 6379 quorum 2
// 检测到master 后面两个slave
2344:X 30 Mar 2022 21:05:16.914 * +slave slave 127.0.0.1:6666 127.0.0.1 6666 @ mymaster 127.0.0.1 6379
2344:X 30 Mar 2022 21:05:16.915 * +slave slave 127.0.0.1:6555 127.0.0.1 6555 @ mymaster 127.0.0.1 6379
2344:X 30 Mar 2022 21:05:17.530 * +sentinel sentinel c987033cc8e5bc291fe53d38ca207ccfef622271 127.0.0.1 26379 @ mymaster 127.0.0.1 6379



#3
2349:X 30 Mar 2022 21:05:39.121 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2349:X 30 Mar 2022 21:05:39.123 # Sentinel ID is 73a5136033e7cc93a352742de6b67aba5fde42da
2349:X 30 Mar 2022 21:05:39.124 # +monitor master mymaster 127.0.0.1 6379 quorum 2
2349:X 30 Mar 2022 21:05:39.125 * +slave slave 127.0.0.1:6666 127.0.0.1 6666 @ mymaster 127.0.0.1 6379
2349:X 30 Mar 2022 21:05:39.126 * +slave slave 127.0.0.1:6555 127.0.0.1 6555 @ mymaster 127.0.0.1 6379
2349:X 30 Mar 2022 21:05:39.393 * +sentinel sentinel 6c320b0564e0434b87d2a1449c65a1d0f28df75a 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
2349:X 30 Mar 2022 21:05:39.984 * +sentinel sentinel c987033cc8e5bc291fe53d38ca207ccfef622271 127.0.0.1 26379 @ mymaster 127.0.0.1 6379

3.5 主Redis宕机

Sentinel 一段时间后开始选择其中 正常的节点推选为主节点

Sentinel 都会有日志

#  mymaster 127.0.0.1 6379 检测到宕机
2344:X 30 Mar 2022 21:07:59.114 # +sdown master mymaster 127.0.0.1 6379
2344:X 30 Mar 2022 21:07:59.238 # +new-epoch 1
2344:X 30 Mar 2022 21:07:59.239 # +vote-for-leader c987033cc8e5bc291fe53d38ca207ccfef622271 1
2344:X 30 Mar 2022 21:07:59.560 # +config-update-from sentinel c987033cc8e5bc291fe53d38ca207ccfef622271 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
#  切换master 到  127.0.0.1 6555
2344:X 30 Mar 2022 21:07:59.560 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6555

3.6 原主Redis 恢复

恢复后变成slave 6379日志为:

## 原6379 是master 现在 6555 是master
2375:S 30 Mar 2022 21:19:51.640 * Connecting to MASTER 127.0.0.1:6555
2375:S 30 Mar 2022 21:19:51.640 * MASTER <-> REPLICA sync started
2375:S 30 Mar 2022 21:19:51.640 * REPLICAOF 127.0.0.1:6555 enabled (user request from 'id=3 addr=127.0.0.1:34324 laddr=127.0.0.1:6379 fd=8 name=sentinel-6c320b05-cmd age=10 idle=0 flags=x db=0 sub=0 psub=0 multi=4 qbuf=196 qbuf-free=40758 argv-mem=4 obl=45 oll=
0 omem=0 tot-mem=61468 events=r cmd=exec user=default redir=-1')2375:S 30 Mar 2022 21:19:51.642 # CONFIG REWRITE executed with success.

3.7 注意事项

sentinel 会修改sentinel 自身的配置文件和监控的Redis 的配置文件;

4.分片集群种类

4.1 基于哈希的分片

redis跨服务器部署主从和哨兵 跨机房redis集群_redis 集群_04

数据存储过程:

  • 有一个环形槽位,最大是16384 个槽位,每个槽位上是可以有一个Redis 节点比如说你有两个Redis 节点 ,就来一个映射 就是 0 ~18264/2;第二个是18264/2 ~18264 ;这个一Redis 可以作为多个虚拟节点添加到环形内;
  • 数据存储的时候,先计算key 的 Hash取模得到 0 ~18264 。也就是获取到槽位,在根据槽位映射的Redis 节点去该节点获取数据;
  • 数据读取也是和存储的时候一样,先计算一致性哈希;

优点:可以增加节点,负担其他节点的压力;

缺点:新增节点导致一部分数据不能命中(映射修改,原来的数据还在原来的Redis节点),

综合: 该方案适合用于作为缓存,不适合作为数据库存放数据

4.2 基于代理的分片

客户端不做如何事情,不去hash 判断槽位; 客户端连接一个代理节点,代理节点处理所有Redis 后端节点;

redis跨服务器部署主从和哨兵 跨机房redis集群_redis-sentinel_05

客户端不直接连接物理节点;连接代理节点,代理节点内部可以通过也槽位,取模分配数据,新增获取删除节点会迁移数据;

基于此的实现有 :predixytemproxy

4.3 基于转发的分片

无主模型,也是Redis-cluster 实现

每个Redis服务端记录,所有模和槽位的映射关系; 比如说,当客户端请求数据key1的时候,其中一个节点连接上 节点A, 如果节点A有该key1 的数据,数据返回,如果没有,会告知客户端端,器群中的node B有数据

redis跨服务器部署主从和哨兵 跨机房redis集群_redis_06

5.分片集群搭建 Redis-cluster

基于 4.3 中的方法的模式 ;

5.1 配置文件

注 :老版本的Redis 还需要ruby 脚本来协助安装集群,新版本5.0+ 的版本有自带的现成的脚本 在 /soft/Redis-6.2.6/utils/create-cluster 脚本支持;

Redis 的 Sentinel 解决的问题是单节点 Redis 故障转移和数据备份的问题,但是当我的业务数据过多,单节点的Redis不能有效的支持存储的话,就需要考虑Redis集群的数据分片,就是将要存放在Redis的数据分配到几个Redis中去;

[root@localhost create-cluster]# pwd
/soft/redis-6.2.6/utils/create-cluster
[root@localhost create-cluster]# ll
总用量 8
-rwxrwxr-x. 1 root root 2787 4月   4 13:32 create-cluster
-rw-rw-r--. 1 root root 1437 10月  4 18:59 README
[root@localhost create-cluster]# 

###配置脚本文件
create-cluster
# Settings
BIN_PATH="../../src/"
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
NODES=6  ##所有的节点
REPLICAS=1 ## 副本的个数 可以推倒出集群有三个主从  一起6个
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""

5.2 开启集群方式1

## 开启所有节点
[root@localhost create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
## 分配节点主从槽位
[root@localhost create-cluster]# ./create-cluster create
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 345234142d9eedb1109c961d7072502930feda3d 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: b106e77b1e52b30d81603e3679dd1f76df3062b8 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: a617c06648edc3b8a68dfe7230e89a5c97f18a7a 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: 5bd30ce39426bebf0c5c2c8478687ce1416f0072 127.0.0.1:30004
   replicates 345234142d9eedb1109c961d7072502930feda3d
S: 1c7d7f17f1916ddc50ea9db59d64f19a54f7ba31 127.0.0.1:30005
   replicates b106e77b1e52b30d81603e3679dd1f76df3062b8
S: ea1f4947ba5718992104951bb28778da71a5eb71 127.0.0.1:30006
   replicates a617c06648edc3b8a68dfe7230e89a5c97f18a7a
   ###默认配置是否同一
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 127.0.0.1:30001)
M: 345234142d9eedb1109c961d7072502930feda3d 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1c7d7f17f1916ddc50ea9db59d64f19a54f7ba31 127.0.0.1:30005
   slots: (0 slots) slave
   replicates b106e77b1e52b30d81603e3679dd1f76df3062b8
M: b106e77b1e52b30d81603e3679dd1f76df3062b8 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 5bd30ce39426bebf0c5c2c8478687ce1416f0072 127.0.0.1:30004
   slots: (0 slots) slave
   replicates 345234142d9eedb1109c961d7072502930feda3d
M: a617c06648edc3b8a68dfe7230e89a5c97f18a7a 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ea1f4947ba5718992104951bb28778da71a5eb71 127.0.0.1:30006
   slots: (0 slots) slave
   replicates a617c06648edc3b8a68dfe7230e89a5c97f18a7a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
###槽位分配完成

连接任意一个节点

[root@localhost create-cluster]# redis-cli -p 30001
###会计算key1 的hash 后的槽位 服务端返回给客户端跳转后的位置
127.0.0.1:30001> set key1 111
(error) MOVED 9189 127.0.0.1:30002
127.0.0.1:30001> get key1
(error) MOVED 9189 127.0.0.1:30002
127.0.0.1:30001>

5.3 开启集群方式2

### 1.先开启节点
......

### 2. 指定所有连接 来创建集群
[root@localhost create-cluster]# redis-cli --cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006 --cluster-replicas 1

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 6a654bff357b268a984fa86df6dfb0016a41f95c 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: d39593dc0a60879710e5112288eb85a33c89049f 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: 5dc4ee7df085793ac193cfadcd4ff1d35b9a94e6 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: 7a024ea3b2c203c95c7d0ee83c69f486372186ae 127.0.0.1:30004
   replicates d39593dc0a60879710e5112288eb85a33c89049f
S: 4b7a35cf5b46a1d5bd9fa385beb0f640c0a672a7 127.0.0.1:30005
   replicates 5dc4ee7df085793ac193cfadcd4ff1d35b9a94e6
S: df18e0bae63483c59d61367e85df9b28fc06538e 127.0.0.1:30006
   replicates 6a654bff357b268a984fa86df6dfb0016a41f95c
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 127.0.0.1:30001)
M: 6a654bff357b268a984fa86df6dfb0016a41f95c 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 7a024ea3b2c203c95c7d0ee83c69f486372186ae 127.0.0.1:30004
   slots: (0 slots) slave
   replicates d39593dc0a60879710e5112288eb85a33c89049f
M: 5dc4ee7df085793ac193cfadcd4ff1d35b9a94e6 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 4b7a35cf5b46a1d5bd9fa385beb0f640c0a672a7 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 5dc4ee7df085793ac193cfadcd4ff1d35b9a94e6
M: d39593dc0a60879710e5112288eb85a33c89049f 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: df18e0bae63483c59d61367e85df9b28fc06538e 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 6a654bff357b268a984fa86df6dfb0016a41f95c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

5.4 其他命令

帮助命令

[root@localhost create-cluster]# redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
                 --cluster-fix-with-unreachable-masters
  reshard        host:port   ### 数据迁移   数据倾斜 数据均衡
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
                 --cluster-only-masters
                 --cluster-only-replicas
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-from-user <arg>
                 --cluster-from-pass <arg>
                 --cluster-from-askpass
                 --cluster-copy
                 --cluster-replace
  backup         host:port backup_directory
  help