作者: | 来源:互联网 | 2023-07-06 20:54
RedisCluster原理图一、RedisCluster单节点多实例实现方式:(1)创建工作目录mkdiroldboyedusoftwaresredisconfredis_clu
Redis Cluster原理图
一、Redis Cluster 单节点多实例实现方式:
(1)创建工作目录
mkdir /oldboyedu/softwares/redis/conf/redis_cluster
mkdir -pv /oldboyedu/data/redis_cluster/1000{1..6}
(2)创建配置文件
cd /oldboyedu/softwares/redis/conf/redis_cluster
cat > redis10001.conf <<'EOF'
port 10001
bind 0.0.0.0
databases 5
dbfilename "oldboyedu-linux79-redis-cluster.rbd"
dir "/oldboyedu/data/redis_cluster/10001"
logfile "/oldboyedu/data/redis_cluster/10001/oldboyedu-linux79-redis-cluster.log"
daemonize yes
save 900 1
save 300 10
save 60 10000
# 关闭保护模式,这样就可以不用设置密码啦!
protected-mode no
# 配置Redis Cluster 启用redis集群功能
cluster-enabled yes
# 指定集群的配置文件,通常情况下无需修改,这是由redis集群自行维护。
cluster-config-file oldboyedu_linux79.conf
# 指定集群节点的超时时间,以毫秒为单位,此处我指定是5s。
cluster-node-timeout 5000
EOF
cat > redis10002.conf <<'EOF'
port 10002
bind 0.0.0.0
databases 5
dbfilename "oldboyedu-linux79-redis-cluster.rbd"
dir "/oldboyedu/data/redis_cluster/10002"
logfile "/oldboyedu/data/redis_cluster/10002/oldboyedu-linux79-redis-cluster.log"
daemonize yes
save 900 1
save 300 10
save 60 10000
protected-mode no
cluster-enabled yes
cluster-config-file oldboyedu_linux79.conf
cluster-node-timeout 5000
EOF
cat > redis10003.conf <<'EOF'
port 10003
bind 0.0.0.0
databases 5
dbfilename "oldboyedu-linux79-redis-cluster.rbd"
dir "/oldboyedu/data/redis_cluster/10003"
logfile "/oldboyedu/data/redis_cluster/10003/oldboyedu-linux79-redis-cluster.log"
daemonize yes
save 900 1
save 300 10
save 60 10000
protected-mode no
cluster-enabled yes
cluster-config-file oldboyedu_linux79.conf
cluster-node-timeout 5000
EOF
cat > redis10004.conf <<'EOF'
port 10004
bind 0.0.0.0
databases 5
dbfilename "oldboyedu-linux79-redis-cluster.rbd"
dir "/oldboyedu/data/redis_cluster/10004"
logfile "/oldboyedu/data/redis_cluster/10004/oldboyedu-linux79-redis-cluster.log"
daemonize yes
save 900 1
save 300 10
save 60 10000
protected-mode no
cluster-enabled yes
cluster-config-file oldboyedu_linux79.conf
cluster-node-timeout 5000
EOF
cat > redis10005.conf <<'EOF'
port 10005
bind 0.0.0.0
databases 5
dbfilename "oldboyedu-linux79-redis-cluster.rbd"
dir "/oldboyedu/data/redis_cluster/10005"
logfile "/oldboyedu/data/redis_cluster/10005/oldboyedu-linux79-redis-cluster.log"
daemonize yes
save 900 1
save 300 10
save 60 10000
protected-mode no
cluster-enabled yes
cluster-config-file oldboyedu_linux79.conf
cluster-node-timeout 5000
EOF
cat > redis10006.conf <<'EOF'
port 10006
bind 0.0.0.0
databases 5
dbfilename "oldboyedu-linux79-redis-cluster.rbd"
dir "/oldboyedu/data/redis_cluster/10006"
logfile "/oldboyedu/data/redis_cluster/10006/oldboyedu-linux79-redis-cluster.log"
daemonize yes
save 900 1
save 300 10
save 60 10000
protected-mode no
cluster-enabled yes
cluster-config-file oldboyedu_linux79.conf
cluster-node-timeout 5000
EOF
(3)启动Redis实例
cd /oldboyedu/softwares/redis/conf/redis_cluster
redis-server redis10001.conf
redis-server redis10002.conf
redis-server redis10003.conf
redis-server redis10004.conf
redis-server redis10005.conf
redis-server redis10006.conf
(4)安装集群管理软件
yum -y install ruby rubygems
(5)配置ruby的gem工具软件源
gem sources -r https://rubygems.org/
gem sources -a https://mirrors.aliyun.com/rubygems/
(6)安装redis驱动
gem install redis -v 3.3.3
(7)初始化集群
redis-trib.rb create --replicas 1 10.0.0.51:10001 10.0.0.51:10002 10.0.0.51:10003 10.0.0.51:10004 10.0.0.51:10005 10.0.0.51:10006
提示是否接受上述配置 : yes
(8)查看集群的状态
redis-cli -p 10001 CLUSTER NODES | grep master
redis-cli -p 10001 CLUSTER NODES | grep slave
...
参数说明: http://www.redis.cn/commands/cluster-nodes.html
(9)测试集群使用
# redis-cli -p 10001
> set school oldboyedu #提示取余超出了该节点槽位,报错
温馨提示:遇到上面的报错,去10002实例操作即可!读写都在该节点!slave只是同步数据而已。
(10)测试集群的可用性
此步骤只需要kill任意一个master角色即可,然后观察之前的slave角色是否转为master角色!
二、Redis Cluster集群扩容:
(1)创建工作目录
mkdir -pv /oldboyedu/data/redis_cluster/1000{7,8}
(2)创建配置文件并启动Redis实例
cd /oldboyedu/softwares/redis/conf/redis_cluster
cat > redis10007.conf <<'EOF'
port 10007
bind 0.0.0.0
databases 5
dbfilename "oldboyedu-linux79-redis-cluster.rbd"
dir "/oldboyedu/data/redis_cluster/10007"
logfile "/oldboyedu/data/redis_cluster/10007/oldboyedu-linux79-redis-cluster.log"
daemonize yes
save 900 1
save 300 10
save 60 10000
protected-mode no
cluster-enabled yes
cluster-config-file oldboyedu_linux79.conf
cluster-node-timeout 5000
EOF
cat > redis10008.conf <<'EOF'
port 10008
bind 0.0.0.0
databases 5
dbfilename "oldboyedu-linux79-redis-cluster.rbd"
dir "/oldboyedu/data/redis_cluster/10008"
logfile "/oldboyedu/data/redis_cluster/10008/oldboyedu-linux79-redis-cluster.log"
daemonize yes
save 900 1
save 300 10
save 60 10000
protected-mode no
cluster-enabled yes
cluster-config-file oldboyedu_linux79.conf
cluster-node-timeout 5000
EOF
# redis-server redis10007.conf
# redis-server redis10008.conf
(3)将节点加入到现有Redis Cluster集群
redis-trib.rb add-node 10.0.0.51:10007 10.0.0.51:10001
温馨提示: 新加入的master节点并没有solt槽位哟~
# redis-cli -p 10001 CLUSTER NODES | grep master
545a972fa68331 10.0.0.51:10007 master - 0 1648003108514 0 connected
(3)重新分配槽位
redis-trib.rb reshard 10.0.0.51:10005
提示给新节点分配多少槽位 (from 1 to 16384)? 4096 # 此数量根据集群分组数计算得出。echo "16384 / 4" | bc
提示接受solt槽位的节点ID: 545a92cb47791
提示选择所有的源节点进行分片: all
提示是否同意分配计划:yes
(4)加入slave节点
redis-trib.rb add-node --slave --master-id 545a92cb47 10.0.0.51:10008 10.0.0.51:10001
温馨提示: 节点添加成功的关系如下所示。
# redis-cli -p 10001 CLUSTER NODES | grep slave
cef1108 10.0.0.51:10008 slave 545a92cb47 0 1648003980515 8 connected
(5)验证从库
redis-cli -p 10008 info Replication
三、Redis Cluster 缩容:
(1)将现有的数据分片迁移到其他节点
# redis-trib.rb reshard 10.0.0.51:10005
提示要移除多少槽位 (from 1 to 16384)? 1365
提示接受solt槽位的节点ID: 1ecbdfa407
提示选择缩容节点ID: 545a92cb477
提示输入..完成:done
提示是否同意分配计划:yes
提示是否继续执行reshard计划:yes
缩容后会自动退出,然后继续重复此步骤,缩容其他几个节点
# redis-trib.rb reshard 10.0.0.51:10005
(2)删除master节点
redis-trib.rb del-node 10.0.0.51:10007 545a92cb477
(3)删除slave节点
redis-trib.rb del-node 10.0.0.51:10008 cef1108a7b7
温馨提示:
(1)Redis Codis调研;
(2)哨兵模式的VIP调研;
(3)Redis Cluster ---> 建议拆分成多个哨兵模式。