Redis集群实现了对Redis的水平扩容,即启动了N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。
Redis集群通过分区(partition)来提供一定程度的可用性(availablitity):即使集群中有一部分节点失效或者无法进行通讯,集群也可以继续处理命令请求。
制作6个redis实例,6379、6380、6381、6389、6390、6391
daemonize yes
pidfile "/var/run/redis_6379.pid"
port 6379
dbfilename "dump6379.rdb"
daemonize yes
pidfile "/var/run/redis_6379.pid"
port 6379
dbfilename "dump6379.rdb"
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
集群中配置的cluster-config-file文件自动生成。
组合之前,请确保所有的redis实例启动,nodes-xxx.conf文件都生成正常。
进入redis源文件目录。(redis低版本需要安装rb环境)。
重点配置解析
# 此处不要用127.0.0.1,请用真实ip地址
# --cluster-replicas 1 表示采用最简单的方式配置集群,一台主机,一台从机,正好三组。
[root@master src]# redis-cli --cluster create --cluster-replicas 1 192.168.230.128:6379 192.168.230.128:6380 192.168.230.128:6381 192.168.230.128:6389 192.168.230.128:6390 192.168.230.128:6391
具体配置结果如下:
[root@master src]# pwd
/usr/local/bin/redis/src
# 此处不要用127.0.0.1,请用真实ip地址
[root@master src]# redis-cli --cluster create --cluster-replicas 1 192.168.230.128:6379 192.168.230.128:6380 192.168.230.128:6381 192.168.230.128:6389 192.168.230.128:6390 192.168.230.128:6391
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.230.128:6390 to 192.168.230.128:6379
Adding replica 192.168.230.128:6391 to 192.168.230.128:6380
Adding replica 192.168.230.128:6389 to 192.168.230.128:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 7613a39531e47a6ff6fd56983cdae88e5d9df94a 192.168.230.128:6379
slots:[0-5460] (5461 slots) master
M: 70c1e43570d7bc31e90e47e4220d428ac7f4816c 192.168.230.128:6380
slots:[5461-10922] (5462 slots) master
M: b34b9c9224c937d8078395ceb5448fe6ebd23273 192.168.230.128:6381
slots:[10923-16383] (5461 slots) master
S: fb8df6c560b021bf7d1aa883791888d59d8fed01 192.168.230.128:6389
replicates b34b9c9224c937d8078395ceb5448fe6ebd23273
S: a89052128eec0c4b1aab900255ae60047ceafb8f 192.168.230.128:6390
replicates 7613a39531e47a6ff6fd56983cdae88e5d9df94a
S: 11097faa158e438842c12cbcd71d498abec61783 192.168.230.128:6391
replicates 70c1e43570d7bc31e90e47e4220d428ac7f4816c
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.230.128:6379)
M: 7613a39531e47a6ff6fd56983cdae88e5d9df94a 192.168.230.128:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 70c1e43570d7bc31e90e47e4220d428ac7f4816c 192.168.230.128:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a89052128eec0c4b1aab900255ae60047ceafb8f 192.168.230.128:6390
slots: (0 slots) slave
replicates 7613a39531e47a6ff6fd56983cdae88e5d9df94a
M: b34b9c9224c937d8078395ceb5448fe6ebd23273 192.168.230.128:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 11097faa158e438842c12cbcd71d498abec61783 192.168.230.128:6391
slots: (0 slots) slave
replicates 70c1e43570d7bc31e90e47e4220d428ac7f4816c
S: fb8df6c560b021bf7d1aa883791888d59d8fed01 192.168.230.128:6389
slots: (0 slots) slave
replicates b34b9c9224c937d8078395ceb5448fe6ebd23273
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@master src]#
[root@master src]# redis-cli -p 6379
127.0.0.1:6379> get a
(error) MOVED 15495 192.168.230.128:6381
127.0.0.1:6379> get b
"11"
127.0.0.1:6379>
[root@master src]# redis-cli -c -p 6379
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 192.168.230.128:6381
"10"
192.168.230.128:6381> get b
-> Redirected to slot [3300] located at 192.168.230.128:6379
"11"
192.168.230.128:6379> keys *
1) "b"
192.168.230.128:6379>
# 查看集群节点信息
192.168.230.128:6379> cluster nodes
70c1e43570d7bc31e90e47e4220d428ac7f4816c 192.168.230.128:6380@16380 master - 0 1659536510264 2 connected 5461-10922
a89052128eec0c4b1aab900255ae60047ceafb8f 192.168.230.128:6390@16390 slave 7613a39531e47a6ff6fd56983cdae88e5d9df94a 0 1659536508000 5 connected
b34b9c9224c937d8078395ceb5448fe6ebd23273 192.168.230.128:6381@16381 master - 0 1659536508249 3 connected 10923-16383
11097faa158e438842c12cbcd71d498abec61783 192.168.230.128:6391@16391 slave 70c1e43570d7bc31e90e47e4220d428ac7f4816c 0 1659536507241 6 connected
7613a39531e47a6ff6fd56983cdae88e5d9df94a 192.168.230.128:6379@16379 myself,master - 0 1659536506000 1 connected 0-5460
fb8df6c560b021bf7d1aa883791888d59d8fed01 192.168.230.128:6389@16389 slave b34b9c9224c937d8078395ceb5448fe6ebd23273 0 1659536509257 4 connected
一个集群至少要有三个主节点。
选项–cluster-replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
分配原则尽量保证每个主数据库运行在不同的IP地址上,每个从库和主库不在一个IP地址上。
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
一个Redis集群包含16384个插槽(hash slot),数据库中的每个键都属于这16384个插槽的其中的一个。
集群使用公式CRC16(key)%16384来计算键key属于哪个槽,其中CRC16(key)语句用于计算键key的CRC16校验和。
集群中的每个节点负责处理一部分插槽。举个例子,如果一个集群可以有主节点,其中:
节点A负责处理0号至5460号插槽。
节点B负责处理5461号至10922号插槽。
节点C负责处理10923号至16383号插槽。
每个插槽可以存放多个key-value键值对。