Redis支持三种集群方案:主从复制模式、Sentinel(哨兵)模式、Cluster集群模式
本文是基于Centos7系统、Redis5.0.14版本,来搭建Cluster集群模式。
安装部署比较简单,根据官方推荐,部署Redis集群至少需要6个节点(3个主节点,3个从节点),我这里为了演示就不开6台服务器了,直接在一台服务器上进行部署,这和生产环境唯一的区别就是,生产环境需要6台机器进行创建集群。
1、初始化服务器
我这里使用的是CentOS7.9系统,关闭防火墙、关闭selinux(或自行开启放行)。
2、下载redis-5.0.14
cd /usr/local/src wget https://download.redis.io/releases/redis-5.0.14.tar.gz
|-----|-------------------------------------------------------------------------------| | 1 2 | cd /usr/local/src wget https://download.redis.io/releases/redis-5.0.14.tar.gz |
3、编译redis
如果是6台机器,6台机器都需要执行
yum install zlib zlib-devel openssl openssl-devel gcc gcc-c++ readline readline-devel tar xf redis-5.0.14.tar.gz cd /usr/local/src/redis-5.0.14/deps make lua hiredis linenoise cd /usr/local/src/redis-5.0.14/deps/jemalloc ./configure make && make install_bin install_include install_lib cd /usr/local/src/redis-5.0.14 make -j 8 make PREFIX=/usr/local/redis install
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 | yum install zlib zlib-devel openssl openssl-devel gcc gcc-c++ readline readline-devel tar xf redis-5.0.14.tar.gz cd /usr/local/src/redis-5.0.14/deps make lua hiredis linenoise cd /usr/local/src/redis-5.0.14/deps/jemalloc ./configure make && make install_bin install_include install_lib cd /usr/local/src/redis-5.0.14 make -j 8 make PREFIX=/usr/local/redis install |
4、创建cluster目录
mkdir -p /usr/local/src/redis_cluster/{7000,7001,7002,7003,7004,7005}
|---|-----------------------------------------------------------------------| | 1 | mkdir -p /usr/local/src/redis_cluster/{7000,7001,7002,7003,7004,7005} |
5、修改配置
cd /usr/local/src/redis-5.0.14 cp redis.conf redis.conf.bak
|-----|-------------------------------------------------------------| | 1 2 | cd /usr/local/src/redis-5.0.14 cp redis.conf redis.conf.bak |
vi redis.conf
|---|---------------| | 1 | vi redis.conf |
修改的内容如下(注意替换本机IP,你也可以配置相关文件的绝对路径,否则在重启时,要注意启动目录与原来一致)
port 7000 bind 本机ip daemonize yes pidfile /var/run/redis_7000.pid cluster-enabled yes cluster-config-file nodes_7000.conf cluster-node-timeout 15000 appendonly yes requirepass whsir masterauth whsir
|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 | port 7000 bind 本机ip daemonize yes pidfile /var/run/redis_7000.pid cluster-enabled yes cluster-config-file nodes_7000.conf cluster-node-timeout 15000 appendonly yes requirepass whsir masterauth whsir |
相关参数注释:
port 7000 #修改端口
bind 本机ip #默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes #redis后台运行
pidfile /var/run/redis_7000.pid #pidfile文件对应7000,7001,7002,7003,7004,7005
cluster-enabled yes #开启集群 把注释#去掉
cluster-config-file nodes_7000.conf #集群的配置,配置文件首次启动自动生成7000,7001,7002,7003,7004,7005文件,如果是生产环境,这里建议配置绝对路径
cluster-node-timeout 15000 #请求超时默认15秒,可自行设置
appendonly yes #aof日志开启有需要就开启,它会每次写操作都记录一条日志
requirepass和masterauth:redis密码,我这里为了方便都设置成了一样的
将redis.conf拷贝到/redis_cluster目录,并修改相应端口
for ((i=0;i<=5;i++)); do cp redis.conf /usr/local/src/redis_cluster/700$i/; done for ((i=0;i<=5;i++)); do sed -i s#7000#700$i#g /usr/local/src/redis_cluster/700$i/redis.conf; done
|-----|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 | for ((i=0;i<=5;i++)); do cp redis.conf /usr/local/src/redis_cluster/700$i/; done for ((i=0;i<=5;i++)); do sed -i s#7000#700$i#g /usr/local/src/redis_cluster/700$i/redis.conf; done |
6、创建软连接
ln -sv /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli ln -sv /usr/local/redis/bin/redis-server /usr/local/bin/redis-server
|-----|-------------------------------------------------------------------------------------------------------------------------------------| | 1 2 | ln -sv /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli ln -sv /usr/local/redis/bin/redis-server /usr/local/bin/redis-server |
7、将6个redis服务启动
注意:pkill redis-server停止后,启动时也还要在这个目录下执行
cd /usr/local/src/redis_cluster for((i=0;i<=5;i++)); do redis-server /usr/local/src/redis_cluster/700$i/redis.conf; done
|-----|---------------------------------------------------------------------------------------------------------------------------| | 1 2 | cd /usr/local/src/redis_cluster for((i=0;i<=5;i++)); do redis-server /usr/local/src/redis_cluster/700$i/redis.conf; done |
附停止命令:
for((i=0;i<=5;i++)); do redis-cli -h IP -p 700$i -a 密码 shutdown; done
|---|------------------------------------------------------------------------| | 1 | for((i=0;i<=5;i++)); do redis-cli -h IP -p 700$i -a 密码 shutdown; done |
或
pkill redis-server
|---|--------------------| | 1 | pkill redis-server |
8、查看服务启动状态
netstat -lntp | grep redis
|---|-----------------------------| | 1 | netstat -lntp | grep redis |
9、下载ruby
注意:默认yum安装的ruby版本过低,无法提供redis支持,所以这里需要通过编译安装
cd /usr/local/src/ wget https://cache.ruby-lang.org/pub/ruby/ruby-2.6.8.tar.gz
|-----|--------------------------------------------------------------------------------| | 1 2 | cd /usr/local/src/ wget https://cache.ruby-lang.org/pub/ruby/ruby-2.6.8.tar.gz |
注:如果官方包由于某种原因无法下载,可尝试我这里的本地包(与官方包一样):
wget https://down.whsir.com/downloads/ruby-2.6.8.tar.gz
|---|---------------------------------------------------------| | 1 | wget https://down.whsir.com/downloads/ruby-2.6.8.tar.gz |
10、编译安装ruby
tar xf ruby-2.6.8.tar.gz cd ruby-2.6.8 ./configure --prefix=/usr/local/ruby make -j 8 make install
|-----------|----------------------------------------------------------------------------------------------------| | 1 2 3 4 5 | tar xf ruby-2.6.8.tar.gz cd ruby-2.6.8 ./configure --prefix=/usr/local/ruby make -j 8 make install |
查看安装后的版本
/usr/local/ruby/bin/ruby -v
|---|-----------------------------| | 1 | /usr/local/ruby/bin/ruby -v |
ruby 2.6.8p205 (2021-07-07 revision 67951) [x86_64-linux]
11、设置ruby环境变量
vi /etc/profile
|---|-----------------| | 1 | vi /etc/profile |
最下面添加
export PATH=$PATH:/usr/local/ruby/bin:
|---|----------------------------------------| | 1 | export PATH=$PATH:/usr/local/ruby/bin: |
保存退出使其生效
source /etc/profile
|---|---------------------| | 1 | source /etc/profile |
12、ruby镜像加速(非必须)
为了快速的安装包,我这里替换国内的镜像
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/ gem sources -l
|-----|----------------------------------------------------------------------------------------------| | 1 2 | gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/ gem sources -l |
13、安装redis
gem install redis
|---|-------------------| | 1 | gem install redis |
Fetching redis-4.5.1.gem
Successfully installed redis-4.5.1
Parsing documentation for redis-4.5.1
Installing ri documentation for redis-4.5.1
Done installing documentation for redis after 0 seconds
1 gem installed
14、初始化redis集群
--cluster-replicas 1表示为集群中的每个主节点创建一个从节点,即1主1从,6个节点就是3主3从。注意替换IP地址
/usr/local/redis/bin/redis-cli --cluster create IP:7000 IP:7001 IP:7002 IP:7003 IP:7004 IP:7005 --cluster-replicas 1 -a whsir
|---|-------------------------------------------------------------------------------------------------------------------------------| | 1 | /usr/local/redis/bin/redis-cli --cluster create IP:7000 IP:7001 IP:7002 IP:7003 IP:7004 IP:7005 --cluster-replicas 1 -a whsir |
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica IP:7004 to IP:7000
Adding replica IP:7005 to IP:7001
Adding replica IP:7003 to IP:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 999e387f1455993e036e50333b79627b87dcc5fd IP:7000
slots:[0-5460] (5461 slots) master
M: 77089374a717e0fe798e1f312b0a525af1addaaa IP:7001
slots:[5461-10922] (5462 slots) master
M: 194b9b60f0bc7d75cecc89588e3ed1ea64b4014f IP:7002
slots:[10923-16383] (5461 slots) master
S: c5da649d80f975858b65154b83759ec2a95db1f4 IP:7003
replicates 77089374a717e0fe798e1f312b0a525af1addaaa
S: 960d540beacb2367e4c4f35d3a8eec3cd2886278 IP:7004
replicates 194b9b60f0bc7d75cecc89588e3ed1ea64b4014f
S: 068c6505728d0524e38f5185f59a99aa2ff01304 IP:7005
replicates 999e387f1455993e036e50333b79627b87dcc5fd
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 IP:7000)
M: 999e387f1455993e036e50333b79627b87dcc5fd IP:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 194b9b60f0bc7d75cecc89588e3ed1ea64b4014f IP:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: c5da649d80f975858b65154b83759ec2a95db1f4 IP:7003
slots: (0 slots) slave
replicates 77089374a717e0fe798e1f312b0a525af1addaaa
S: 068c6505728d0524e38f5185f59a99aa2ff01304 IP:7005
slots: (0 slots) slave
replicates 999e387f1455993e036e50333b79627b87dcc5fd
M: 77089374a717e0fe798e1f312b0a525af1addaaa IP:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 960d540beacb2367e4c4f35d3a8eec3cd2886278 IP:7004
slots: (0 slots) slave
replicates 194b9b60f0bc7d75cecc89588e3ed1ea64b4014f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
15、验证集群状态
redis-cli -c -h 本机IP -p 7000
|---|------------------------------| | 1 | redis-cli -c -h 本机IP -p 7000 |
此时可看到cluster_state集群状态ok
IP:7000> 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:1
cluster_stats_messages_ping_sent:96
cluster_stats_messages_pong_sent:107
cluster_stats_messages_sent:203
cluster_stats_messages_ping_received:102
cluster_stats_messages_pong_received:96
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:203
至此redis cluster集群搭建完成
redis cluster在设计的时候,就考虑到了去中心化、去中间件,集群中的每个节点都是平等关系,每个节点都保存各自的数据和整个集群的状态。
redis集群没有并使用传统的一致性哈希来分配数据,而是采用另外一种叫做哈希槽(hash slot)的方式来分配的。redis cluster默认分配了16384个槽(slot),当我们set一个key时,会用CRC16算法来取模得到所属的槽(slot),然后将这个key分到哈希槽区间的节点上,具体算法就是:CRC16(key)%16384。
redis集群会把数据存在一个master节点,然后在这个master和其对应的salve之间进行数据同步。当一个master挂掉之后,会启动一个对应的salve节点,充当master。