1.cluster集群介绍
Redis Cluster是Redis提供的一种分布式方案,可以将数据分散到多个节点上进行存储和处理,提高数据的可用性和性能。Redis Cluster采用哈希槽的方式来分片数据,将整个数据集分为16384个哈希槽,每个节点负责一部分哈希槽的数据存储和处理,节点之间通过gossip协议进行信息交换和数据同步,从而实现高可用和负载均衡。
Redis Cluster支持自动节点发现和故障转移,当节点出现故障或者添加新节点时,集群会自动进行重新分片和数据迁移,保证数据的一致性和可用性。同时,Redis Cluster还提供了一些监控和管理工具,方便管理人员进行集群的管理和维护。
优点:
- 高性能,高可用,可扩展的分布式解决方案
2.搭建cluster集群
2.1.架构图
2.2.搭建集群
2.2.1.创建所需配置文件
- 在
/data/redis/redisConf
目录下创建创建节点所需要的配置文件
master1,文件名:clusterMaster1.conf
port 6379 dir "/redisConfig" logfile "clusterMaster1.log" cluster-enabled yes cluster-config-file nodes-6379.conf
以上配置:
第一行:指定端口
第二、三行:指定日志路径和文件名
第四行:开启cluster集群模式并加入集群
第五行:cluster集群相关的配置文件,该文件会自动生成
master2,文件名:clusterMaster2.conf
port 6380 dir "/redisConfig" logfile "clusterMaster2.log" cluster-enabled yes cluster-config-file nodes-6380.conf
master3,文件名:clusterMaster3.conf
port 6381 dir "/redisConfig" logfile "clusterMaster3.log" cluster-enabled yes cluster-config-file nodes-6381.conf
slave1,文件名:clusterSlave1.conf
port 16379 dir "/redisConfig" logfile "clusterSlave1.log" cluster-enabled yes cluster-config-file nodes-16379.conf
slave2,文件名:clusterSlave2.conf
port 16380 dir "/redisConfig" logfile "clusterSlave2.log" cluster-enabled yes cluster-config-file nodes-16380.conf
slave3,文件名:clusterSlave3.conf
port 16381 dir "/redisConfig" logfile "clusterSlave3.log" cluster-enabled yes cluster-config-file nodes-16381.conf
2.2.2.创建集群所需容器
- 容器master1-3,slave1-3创建命令
docker run -itd --name redisMaster1 -v /data/redis/redisConf:/redisConfig:rw -p 6379:6379 redis:6.0 redis-server /redisConfig/clusterMaster1.conf docker run -itd --name redisMaster2 -v /data/redis/redisConf:/redisConfig:rw -p 6380:6380 redis:6.0 redis-server /redisConfig/clusterMaster2.conf docker run -itd --name redisMaster3 -v /data/redis/redisConf:/redisConfig:rw -p 6381:6381 redis:6.0 redis-server /redisConfig/clusterMaster3.conf docker run -itd --name redisSlave1 -v /data/redis/redisConf:/redisConfig:rw -p 16379:16379 redis:6.0 redis-server /redisConfig/clusterSlave1.conf docker run -itd --name redisSlave2 -v /data/redis/redisConf:/redisConfig:rw -p 16380:16380 redis:6.0 redis-server /redisConfig/clusterSlave2.conf docker run -itd --name redisSlave3 -v /data/redis/redisConf:/redisConfig:rw -p 16381:16381 redis:6.0 redis-server /redisConfig/clusterSlave3.conf
- 打开master1节点的nodes文件,可以看到如下信息
[root@localhost redisConf]# cat nodes-6379.conf 57adbc5123224d7510712906e7fb853b179ca88b :0@0 myself,master - 0 0 0 connected vars currentEpoch 0 lastVoteEpoch 0
内容为该容器为主节点,只连接了自身,没有其他节点
- 通过
docker inspect
查看得知,容器IP分别为172.17.0.2-7
2.2.3.创建集群:master1节点连接其他节点
- 进入到master1节点容器,连接其他节点
[root@localhost ~]# docker exec -it redisMaster1 /bin/bash root@a6ee8fb2d3f5:/data# redis-cli -p 6379 cluster meet 172.17.0.3 6380 OK root@a6ee8fb2d3f5:/data# redis-cli -p 6379 cluster meet 172.17.0.4 6381 OK root@a6ee8fb2d3f5:/data# redis-cli -p 6379 cluster meet 172.17.0.5 16379 OK root@a6ee8fb2d3f5:/data# redis-cli -p 6379 cluster meet 172.17.0.6 16380 OK root@a6ee8fb2d3f5:/data# redis-cli -p 6379 cluster meet 172.17.0.7 16381 OK
- 连接master1服务器,查看集群状态
root@a6ee8fb2d3f5:/data# redis-cli 127.0.0.1:6379> cluster info cluster_state:fail cluster_known_nodes:6
- 为三个主节点分配哈希槽
为master1注入哈希槽
root@a6ee8fb2d3f5:/redisConfig# for i in $(seq 0 5460);do /usr/local/bin/redis-cli -h 172.17.0.2 -p 6379 CLUSTER ADDSLOTS $i;done
为master2注入哈希槽(进入容器内部执行)
for i in $(seq 5461 10922);do /usr/local/bin/redis-cli -h 172.17.0.3 -p 6380 CLUSTER ADDSLOTS $i;done
为master3注入哈希槽(进入容器内部执行)
for i in $(seq 10923 16383);do /usr/local/bin/redis-cli -h 172.17.0.4 -p 6381 CLUSTER ADDSLOTS $i;done
- 回到master1容器内部查看集群状态
root@a6ee8fb2d3f5:/data# redis-cli 127.0.0.1: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
可以看到集群运行正常,16384个哈希槽也分配完成
2.2.4.配置从节点,完成三主三从
- 在master1容器内查看各节点id
127.0.0.1:6379> CLUSTER nodes 9357e7ff8892533ea056a52bef836f811d57b27a 172.17.0.7:16381@26381 master - 0 1686132119000 5 connected 28d44b6219bb1b29e032c7ed1d89f5fc4b1331db 172.17.0.6:16380@26380 master - 0 1686132121109 4 connected 930cdb19e676169c4c1bc155408efa6876236fae 172.17.0.5:16379@26379 master - 0 1686132122117 3 connected 57adbc5123224d7510712906e7fb853b179ca88b 172.17.0.2:6379@16379 myself,master - 0 1686132119000 1 connected 0-5460 86b66441b357d59c68660eb09f3d9bb20ea99346 172.17.0.4:6381@16381 master - 0 1686132120000 2 connected 10923-16383 f4e9388ae47aff89a9512e39c88eea4a5ecfa4b0 172.17.0.3:6380@16380 master - 0 1686132121000 0 connected 5461-10922
知道id后就可以进行主从关系配置
- 进入到slave1容器内部,进行配置
[root@localhost ~]# docker exec -it redisSlave1 /bin/bash root@1f8209b74008:/data# redis-cli -p 16379 127.0.0.1:16379> cluster replicate 57adbc5123224d7510712906e7fb853b179ca88b OK
- 进入到slave2容器内部,进行配置
[root@localhost ~]# docker exec -it redisSlave2 /bin/bash root@90692280afb4:/data# redis-cli -p 16380 127.0.0.1:16380> cluster replicate f4e9388ae47aff89a9512e39c88eea4a5ecfa4b0 OK
- 进入到slave3容器内部,进行配置
[root@localhost ~]# docker exec -it redisSlave3 /bin/bash root@2906e497f3d4:/data# redis-cli -p 16381 127.0.0.1:16381> cluster replicate 86b66441b357d59c68660eb09f3d9bb20ea99346 OK
- 查看集群状态
127.0.0.1:16381> cluster nodes 930cdb19e676169c4c1bc155408efa6876236fae 172.17.0.5:16379@26379 slave 57adbc5123224d7510712906e7fb853b179ca88b 0 1686132676594 1 connected 86b66441b357d59c68660eb09f3d9bb20ea99346 172.17.0.4:6381@16381 master - 0 1686132678000 2 connected 10923-16383 f4e9388ae47aff89a9512e39c88eea4a5ecfa4b0 172.17.0.3:6380@16380 master - 0 1686132676000 0 connected 5461-10922 28d44b6219bb1b29e032c7ed1d89f5fc4b1331db 172.17.0.6:16380@26380 slave f4e9388ae47aff89a9512e39c88eea4a5ecfa4b0 0 1686132678611 0 connected 57adbc5123224d7510712906e7fb853b179ca88b 172.17.0.2:6379@16379 master - 0 1686132677601 1 connected 0-5460 9357e7ff8892533ea056a52bef836f811d57b27a 172.17.0.7:16381@26381 myself,slave 86b66441b357d59c68660eb09f3d9bb20ea99346 0 1686132675000 2 connected
至此集群内三主三从的工作已完成
3.在cluster集群内读写数据
- 在当前的slave3容器创建数据测试
127.0.0.1:16381> set name 'zhangsan' (error) MOVED 5798 172.17.0.3:6380
报错,内容是这个键应该放在172.17.0.3:6380的5798哈希槽内
- +c参数进行正常写入
root@2906e497f3d4:/data# redis-cli -p 16381 -c 127.0.0.1:16381> set name 'zhangsan' -> Redirected to slot [5798] located at 172.17.0.3:6380 OK
- 这个时候在172.17.0.3节点上可以查看到数据
[root@localhost ~]# docker exec -it redisMaster2 /bin/bash root@aa2cb23a71c1:/data# redis-cli -p 6380 127.0.0.1:6380> get name "zhangsan"
如果当该节点故障无法使用,该键回设置到其他节点上,实现故障自动恢复的效果。
本节内容到此为止,内容总结自基于docker的redis入门与实战