1:Redis集群没有使用一致性hash,而是引入了哈希槽概念
2:Redis 集群有16384个哈希槽
3:每个key通过CRC16校验后对16384取模来决定放置槽
4:集群的每个节点负责一部分哈希槽
5:以3个节点组成的集群为例
6:添加删除节点无需停止服务
1:每台服务器导入redis软件包,其中一台主redis要有群集文件
2:解压缩redis软件包,6台服务器同步操作
[root@redis1 ~]# tar zxvf redis-5.0.4.tar.gz
3:配置安装,6台服务器同步操作
[root@redis1 ~]# cd redis-5.0.4/
[root@redis1 redis-5.0.4]# make
[root@redis1 redis-5.0.4]# make PREFIX=/usr/local/redis install
4:链接快捷命令,6台服务器同步操作
[root@redis1 ~]# ln -s /usr/local/redis/bin/* /usr/local/bin/
5:设置Redis相关配置文件,6台服务器同步操作
[root@redis1 ~]# cd redis-5.0.4/utils/
[root@redis1 utils]# ./install_server.sh
[root@redis1 utils]# netstat -anptu | grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 59531/redis-server
6:修改主配置文件,6台服务器同步操作
[root@slave3 ~]# vi /etc/redis/6379.conf
cluster-enabled yes
appendonly yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-require-full-coverage yes
修改IP地址需要每台服务器单独修改
[root@redis1 ~]# vi /etc/redis/6379.conf
bind 20.0.0.10
[root@redis2 ~]# vi /etc/redis/6379.conf
bind 20.0.0.11
[root@redis3 ~]# vi /etc/redis/6379.conf
bind 20.0.0.12
[root@slave1 ~]# vi /etc/redis/6379.conf
bind 20.0.0.13
[root@slave2 ~]# vi /etc/redis/6379.conf
bind 20.0.0.14
[root@slave3 ~]# vi /etc/redis/6379.conf
bind 20.0.0.15
7:重启服务,6台服务器同步操作
[root@redis1 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
[root@redis1 ~]# netstat -anptu | grep redis
8:在20.0.0.10服务器上,使用脚本创建群集
[root@redis1 ~]# yum -y install ruby rubygems
[root@redis1 ~]# gem install redis-3.2.0.gem
9:建立群集
[root@redis1 ~]# redis-cli --cluster create --cluster-replicas 1 20.0.0.10:6379 20.0.0.11:6379 20.0.0.12:6379 20.0.0.13:6379 20.0.0.14:6379 20.0.0.15:6379
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 20.0.0.14:6379 to 20.0.0.10:6379
Adding replica 20.0.0.15:6379 to 20.0.0.11:6379
Adding replica 20.0.0.13:6379 to 20.0.0.12:6379
M: 2ce100a99c214bfac23ae1c31b8068e93506615b 20.0.0.10:6379
slots:[0-5460] (5461 slots) master
M: 5f5ff79b9301f10e175aea6604ff80372d6c97a8 20.0.0.11:6379
slots:[5461-10922] (5462 slots) master
M: a6234f074de2ee05b4f7c1ba96c8a3e084104dd4 20.0.0.12:6379
slots:[10923-16383] (5461 slots) master
S: b378987085da43974359cb02bcbc9443e808bb13 20.0.0.13:6379
replicates a6234f074de2ee05b4f7c1ba96c8a3e084104dd4
S: 5d27b053b2607dfed82f79d29aa2e5c3b8846bc2 20.0.0.14:6379
replicates 2ce100a99c214bfac23ae1c31b8068e93506615b
S: 1d2b8d8fd38f5e7c2dce00ee43e13f162a18508d 20.0.0.15:6379
replicates 5f5ff79b9301f10e175aea6604ff80372d6c97a8
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 20.0.0.10:6379)
M: 2ce100a99c214bfac23ae1c31b8068e93506615b 20.0.0.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: a6234f074de2ee05b4f7c1ba96c8a3e084104dd4 20.0.0.12:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 1d2b8d8fd38f5e7c2dce00ee43e13f162a18508d 20.0.0.15:6379
slots: (0 slots) slave
replicates 5f5ff79b9301f10e175aea6604ff80372d6c97a8
S: b378987085da43974359cb02bcbc9443e808bb13 20.0.0.13:6379
slots: (0 slots) slave
replicates a6234f074de2ee05b4f7c1ba96c8a3e084104dd4
M: 5f5ff79b9301f10e175aea6604ff80372d6c97a8 20.0.0.11:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 5d27b053b2607dfed82f79d29aa2e5c3b8846bc2 20.0.0.14:6379
slots: (0 slots) slave
replicates 2ce100a99c214bfac23ae1c31b8068e93506615b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
10:测试群集
登录写入一个数据
[root@redis1 ~]# redis-cli -h 20.0.0.10 -p 6379 -c
20.0.0.10:6379> set aaa 111
-> Redirected to slot [10439] located at 20.0.0.11:6379
OK
另一台主服务器获取数据
[root@redis3 ~]# redis-cli -h 20.0.0.13 -p 6379 -c
20.0.0.13:6379> get aaa
-> Redirected to slot [10439] located at 20.0.0.11:6379
"111"
登录不同服务器获取数据
[root@redis2 ~]# redis-cli -h 20.0.0.13 -p 6379 -c
20.0.0.13:6379> get aaa
-> Redirected to slot [10439] located at 20.0.0.11:6379
"111"
查看集群状态
20.0.0.11:6379> 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:2
cluster_stats_messages_ping_sent:5018
cluster_stats_messages_pong_sent:4927
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:9947
cluster_stats_messages_ping_received:4923
cluster_stats_messages_pong_received:5020
cluster_stats_messages_meet_received:4
cluster_stats_messages_received:9947
查看节点状态
20.0.0.11:6379> cluster nodes
5d27b053b2607dfed82f79d29aa2e5c3b8846bc2 20.0.0.14:6379@16379 slave 2ce100a99c214bfac23ae1c31b8068e93506615b 0 1608200801776 1 connected
b378987085da43974359cb02bcbc9443e808bb13 20.0.0.13:6379@16379 slave a6234f074de2ee05b4f7c1ba96c8a3e084104dd4 0 1608200799000 3 connected
a6234f074de2ee05b4f7c1ba96c8a3e084104dd4 20.0.0.12:6379@16379 master - 0 1608200801000 3 connected 10923-16383
1d2b8d8fd38f5e7c2dce00ee43e13f162a18508d 20.0.0.15:6379@16379 slave 5f5ff79b9301f10e175aea6604ff80372d6c97a8 0 1608200802783 6 connected
5f5ff79b9301f10e175aea6604ff80372d6c97a8 20.0.0.11:6379@16379 myself,master - 0 1608200801000 2 connected 5461-10922
2ce100a99c214bfac23ae1c31b8068e93506615b 20.0.0.10:6379@16379 master - 0 1608200801000 1 connected 0-5460
11:宕机测试
主服务器停止服务
[root@redis1 ~]# pkill redis
查看状态
20.0.0.11:6379> cluster nodes
5d27b053b2607dfed82f79d29aa2e5c3b8846bc2 20.0.0.14:6379@16379 slave 2ce100a99c214bfac23ae1c31b8068e93506615b 0 1608201296530 1 connected
b378987085da43974359cb02bcbc9443e808bb13 20.0.0.13:6379@16379 slave a6234f074de2ee05b4f7c1ba96c8a3e084104dd4 0 1608201297542 3 connected
a6234f074de2ee05b4f7c1ba96c8a3e084104dd4 20.0.0.12:6379@16379 master - 0 1608201298547 3 connected 10923-16383
1d2b8d8fd38f5e7c2dce00ee43e13f162a18508d 20.0.0.15:6379@16379 slave 5f5ff79b9301f10e175aea6604ff80372d6c97a8 0 1608201299558 6 connected
5f5ff79b9301f10e175aea6604ff80372d6c97a8 20.0.0.11:6379@16379 myself,master - 0 1608201294000 2 connected 5461-10922
2ce100a99c214bfac23ae1c31b8068e93506615b 20.0.0.10:6379@16379 master,fail - 1608201280782 1608201277358 1 disconnected 0-5460
备份20.0.0.15以上线