说明
在同一个Ubuntu主机下模拟搭建redis集群的过程,以不同的端口号进行区分
创建集群配置文件
7000.conf
port 7000
bind 192.168.1.106
daemonize yes
pidfile 7000.pid
cluster-enabled yes
cluster-config-file 7000_node.conf
cluster-node-timeout 15000
appendonly yes
7001.conf
port 7001
bind 192.168.1.106
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
7002.conf
port 7002
bind 192.168.1.106
daemonize yes
pidfile 7002.pid
cluster-enabled yes
cluster-config-file 7002_node.conf
cluster-node-timeout 15000
appendonly yes
7003.conf
port 7003
bind 192.168.1.106
daemonize yes
pidfile 7003.pid
cluster-enabled yes
cluster-config-file 7003_node.conf
cluster-node-timeout 15000
appendonly yes
7004.conf
port 7004
bind 192.168.1.106
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes
7005.conf
port 7005
bind 192.168.1.106
daemonize yes
pidfile 7005.pid
cluster-enabled yes
cluster-config-file 7005_node.conf
cluster-node-timeout 15000
appendonly yes
拷贝redis-trib.rb到指定目录
sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/
安装ruby
sudo apt-get install ruby
安装redis库
wang@wjz:~/redis集群$ sudo gem install redis
创建redis-server集群
1.开启redis-server
wang@wjz:~/redis集群$ sudo redis-server 7001.conf
3473:C 02 May 15:48:03.867 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3473:C 02 May 15:48:03.867 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=3473, just started
3473:C 02 May 15:48:03.867 # Configuration loaded
wang@wjz:~/redis集群$ sudo redis-server 7002.conf
3479:C 02 May 15:48:07.019 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3479:C 02 May 15:48:07.019 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=3479, just started
3479:C 02 May 15:48:07.019 # Configuration loaded
wang@wjz:~/redis集群$ sudo redis-server 7003.conf
3485:C 02 May 15:48:11.147 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3485:C 02 May 15:48:11.147 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=3485, just started
3485:C 02 May 15:48:11.147 # Configuration loaded
wang@wjz:~/redis集群$ sudo redis-server 7004.conf
3491:C 02 May 15:48:15.623 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3491:C 02 May 15:48:15.623 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=3491, just started
3491:C 02 May 15:48:15.623 # Configuration loaded
wang@wjz:~/redis集群$ sudo redis-server 7005.conf
3497:C 02 May 15:48:20.155 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3497:C 02 May 15:48:20.155 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=3497, just started
3497:C 02 May 15:48:20.156 # Configuration loaded
wang@wjz:~/redis集群$ ps aux | grep redis
redis 1519 0.2 0.1 58556 3628 ? Ssl 15:20 0:03 /usr/bin/redis-server 192.168.1.106:6379
root 3466 0.1 0.2 58556 4244 ? Ssl 15:47 0:00 redis-server 192.168.1.106:7000 [cluster]
root 3474 0.2 0.2 58556 4308 ? Ssl 15:48 0:00 redis-server 192.168.1.106:7001 [cluster]
root 3480 0.1 0.2 58556 4312 ? Ssl 15:48 0:00 redis-server 192.168.1.106:7002 [cluster]
root 3486 0.2 0.2 58556 4352 ? Ssl 15:48 0:00 redis-server 192.168.1.106:7003 [cluster]
root 3492 1.1 0.1 58556 3496 ? Ssl 15:48 0:00 redis-server 192.168.1.106:7004 [cluster]
root 3498 0.2 0.2 58556 4248 ? Ssl 15:48 0:00 redis-server 192.168.1.106:7005 [cluster]
wang 3503 0.0 0.0 21540 1032 pts/0 S+ 15:48 0:00 grep --color=auto redis
2.运⾏如下命令创建集群
wang@wjz:~/redis集群$ redis-trib.rb create --replicas 1 192.168.1.106:7000 192.168.1.106:7001 192.168.1.106:7002 192.168.1.106:7003 192.168.1.106:7004 192.168.1.106:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.106:7000
192.168.1.106:7001
192.168.1.106:7002
Adding replica 192.168.1.106:7004 to 192.168.1.106:7000
Adding replica 192.168.1.106:7005 to 192.168.1.106:7001
Adding replica 192.168.1.106:7003 to 192.168.1.106:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 5ee9435260da554c63b6f2e8e8ae7af7cc499ba6 192.168.1.106:7000
slots:0-5460 (5461 slots) master
M: 92408098106f9dc4ab6531503a70f853ef34715b 192.168.1.106:7001
slots:5461-10922 (5462 slots) master
M: 268a37b1064166e092796934e67fe9b138ec0f4c 192.168.1.106:7002
slots:10923-16383 (5461 slots) master
S: 93d540f33cec141253aaff991888f2253566d307 192.168.1.106:7003
replicates 92408098106f9dc4ab6531503a70f853ef34715b
S: 45965df077a1ff5afc4611708b99114eff53c07b 192.168.1.106:7004
replicates 268a37b1064166e092796934e67fe9b138ec0f4c
S: 9a439a9e53aeb9c078b60952212b50830924b3fa 192.168.1.106:7005
replicates 5ee9435260da554c63b6f2e8e8ae7af7cc499ba6
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.1.106:7000)
M: 5ee9435260da554c63b6f2e8e8ae7af7cc499ba6 192.168.1.106:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 268a37b1064166e092796934e67fe9b138ec0f4c 192.168.1.106:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 9a439a9e53aeb9c078b60952212b50830924b3fa 192.168.1.106:7005
slots: (0 slots) slave
replicates 5ee9435260da554c63b6f2e8e8ae7af7cc499ba6
M: 92408098106f9dc4ab6531503a70f853ef34715b 192.168.1.106:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 45965df077a1ff5afc4611708b99114eff53c07b 192.168.1.106:7004
slots: (0 slots) slave
replicates 268a37b1064166e092796934e67fe9b138ec0f4c
S: 93d540f33cec141253aaff991888f2253566d307 192.168.1.106:7003
slots: (0 slots) slave
replicates 92408098106f9dc4ab6531503a70f853ef34715b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
提示完成,集群搭建成功。
测试并验证
从上面的创建信息可以得到集群的主redis有7000,7001,7002;从redis有7003,7004,7005
wang@wjz:~/redis集群$ redis-cli -h 192.168.1.106 -p 7000 -c
192.168.1.106:7000> set name zhangsan
-> Redirected to slot [5798] located at 192.168.1.106:7001
OK
192.168.1.106:7001> keys *
1) "name"
192.168.1.106:7001> get name
"zhangsan"
说明
- redis cluster在设计的时候,就考虑到了去中⼼化,去中间件,也就是说,集群中 的每个节点都是平等的关系,都是对等的,每个节点都保存各⾃的数据和整个集 群的状态。每个节点都和其他所有节点连接,⽽且这些连接保持活跃,这样就保 证了我们只需要连接集群中的任意⼀个节点,就可以获取到其他节点的数据
- Redis集群没有并使⽤传统的⼀致性哈希来分配数据,⽽是采⽤另外⼀种叫做哈希 槽 (hash slot)的⽅式来分配的。redis cluster 默认分配了 16384 个slot,当我们 set⼀个key 时,会⽤CRC16算法来取模得到所属的slot,然后将这个key 分到哈 希槽区间的节点上,具体算法就是:CRC16(key) % 16384。所以我们在测试的 时候看到set 和 get 的时候,直接跳转到了7000端⼝的节点
- Redis 集群会把数据存在⼀个 master 节点,然后在这个 master 和其对应的salve 之间进⾏数据同步。当读取数据时,也根据⼀致性哈希算法到对应的 master 节 点获取数据。只有当⼀个master 挂掉之后,才会启动⼀个对应的 salve 节点,充 当 master
需要注意的是:必须要3个或以上的主节点,否则在创建集群时会失败,并且当存 活的主节点数⼩于总节点数的⼀半时,整个集群就⽆法提供服务了