简介
- Redis 集群采用P2P模式,是完全去中心化的,不存在中心节点或者代理节点;
- Redis 集群是没有统一的入口的,客户端(client)连接集群的时候连接集群中的任意节点(node)即可,集群内部的节点是相互通信的(PING-PONG机制),每个节点都是一个redis实例
- Redis 把所有的 Key 分成了 16384 个 slot,每个 Redis 实例负责其中一部分 slot 。集群中的所有信息(节点、端口、slot等),都通过节点之间定期的数据交换而更新。
- redis-cluster 投票容错机制:实现集群的高可用,即判断节点是否健康(能否正常使用)
- 如果集群中超过半数的节点投票认为某个节点挂了,那么这个节点就挂了(fail)
- 如果集群中任意一个节点挂了,而且该节点没有从节点(备份节点),那么这个集群就挂了
- 集群内置了16384个slot(哈希槽),并且把所有的物理节点映射到了这16384[0-16383]个slot上,或者说把这些slot均等的分配给了各个节点。当需要在Redis集群存放一个数据(key-value)时,redis会先对这个key进行crc16算法,然后得到一个结果。再把这个结果对16384进行求余,这个余数会对应[0-16383]其中一个槽,进而决定key-value存储到哪个节点中。所以一旦某个节点挂了,该节点对应的slot就无法使用,那么就会导致集群无法正常工作。
集群搭建 - 单机
????Redis集群至少需要3个节点,因为投票容错机制要求超过半数节点认为某个节点挂了该节点才是挂了,所以2个节点无法构成集群。 ????要保证集群的高可用,需要每个节点都有从节点,也就是备份节点,所以Redis集群至少需要6台服务器。 ????因为我没有那么多服务器,也启动不了那么多虚拟机,所在这里搭建的是伪分布式集群,即一台服务器虚拟运行6个redis实例,修改端口号为(7001-7006),当然实际生产环境的Redis集群搭建和这里是一样的。
1、配置
[user@localhost app]$ mkdir redis-cluster
[user@localhost app]$ cd redis-cluster
[user@localhost redis-cluster]$ mkdir bin data log conf scripts
[user@localhost redis-cluster]$ cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server /app/redis-cluster/bin
[user@localhost redis-cluster]$ cd scripts
[user@localhost scripts]$ vim cp-conf.sh
# 点击i进入编辑
cp /app/redis/redis.conf ../conf/7001.conf
cp /app/redis/redis.conf ../conf/7002.conf
cp /app/redis/redis.conf ../conf/7003.conf
cp /app/redis/redis.conf ../conf/7004.conf
cp /app/redis/redis.conf ../conf/7005.conf
cp /app/redis/redis.conf ../conf/7006.conf
# 编辑结束:点击Esc、shift+:、wq
[user@localhost scripts]$ ./cp-conf.sh
[user@localhost scripts]$ vim /app/redis-cluser/conf/7001.conf
????修改以下几项:
# 节点端口号
port 7001
# 绑定当前机器 IP
bind 10.20.30.40
# Redis后台运行
daemonize yes
# 数据文件存放位置
dir /app/redis-cluster/data
# pid 7001和port要对应
# pidfile:包含进程标识号(pid)的文件,该文件存储在文件系统定义明确的位置,因此允许其他程序找出正在运行的脚本的pid。
pidfile /var/run/redis_7001.pid
# 日志文件
logfile "/app/redis-cluster/logs/7001.log"
# 启动集群模式
cluster-enabled yes
# 集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-config-file nodes_7001.conf
# 请求超时 默认15秒,可自行设置
cluster-node-timeout 15000
# 是否需要每个节点都可用,集群才算可用,关闭
cluster-require-full-coverage no
# aof日志开启 有需要就开启,它会每次写操作都记录一条日志
appendonly yes
2、进入app/redis-cluster/bin,启动服务节点
[user@localhost scripts]$ vim start-all.sh
cd ../bin
./redis-server ../conf/7001.conf
./redis-server ../conf/7002.conf
./redis-server ../conf/7003.conf
./redis-server ../conf/7004.conf
./redis-server ../conf/7005.conf
./redis-server ../conf/7006.conf
[user@localhost scripts]$ ./start-all.sh
[user@localhost scripts]$ ps -ef|grep redis
appuser 4882 1 0 18:54 ? 00:00:00 ./redis-server 10.20.30.40:7001 [cluster]
appuser 4884 1 0 18:54 ? 00:00:00 ./redis-server 10.20.30.40:7002 [cluster]
appuser 4886 1 0 18:54 ? 00:00:00 ./redis-server 10.20.30.40:7003 [cluster]
appuser 4891 1 0 18:54 ? 00:00:00 ./redis-server 10.20.30.40:7004 [cluster]
appuser 4893 1 0 18:54 ? 00:00:00 ./redis-server 10.20.30.40:7005 [cluster]
appuser 4903 1 0 18:54 ? 00:00:00 ./redis-server 10.20.30.40:7006 [cluster]
appuser 4932 141318 0 18:54 pts/1 00:00:00 grep --color=auto redis
[user@localhost scripts]$ cd ../bin/redis-cli -h 10.20.30.40 -p 7001
localhost:7001> keys *
(empty array)
localhost:7001> set name m
(error) CLUSTERDOWN Hash slot not served
[user@localhost scripts]$ ../bin/redis-cli -h 10.20.30.40 -p 7001 cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
3、创建集群:
[user@localhost scripts]$ ../bin/redis-cli --cluster create --cluster-replicas 1 10.20.30.40:7000 10.20.30.40:7001 10.20.30.40:7002 10.20.30.40:7003 10.20.30.40:7004 10.20.30.40:7005
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.200.202.41:7005 to 10.20.30.40:7001
Adding replica 10.200.202.41:7006 to 10.20.30.40:7002
Adding replica 10.200.202.41:7004 to 10.20.30.40:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: dcff644b74a67e14406490d8c548824b8c35e0d6 10.20.30.40:7001
slots:[0-5460] (5461 slots) master
M: 45a476c96bb93d2f25f1d744e04eaceb90e53622 10.20.30.40:7002
slots:[5461-10922] (5462 slots) master
M: cadad69fcb819e82e02404f11aa0bffa099d40f1 10.20.30.40:7003
slots:[10923-16383] (5461 slots) master
S: 3bc9eb31e19cd099da51116cf28f90ecb280a62f 10.20.30.40:7004
replicates cadad69fcb819e82e02404f11aa0bffa099d40f1
S: d23547994f737edb664d85f990cd41add6e96978 10.20.30.40:7005
replicates dcff644b74a67e14406490d8c548824b8c35e0d6
S: 02830b14f8ca547a30512e2f6846dd1997ac15dd 10.20.30.40:7006
replicates 45a476c96bb93d2f25f1d744e04eaceb90e53622
Can I set the above configuration? (type 'yes' to accept):
[user@localhost bin]$ ./redis-cli -h 10.20.30.40 -p 7001 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:179
cluster_stats_messages_pong_sent:183
cluster_stats_messages_sent:362
cluster_stats_messages_ping_received:178
cluster_stats_messages_pong_received:179
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:362
[user@localhost scripts]$ mv /app/redis/src/redis-trib.rb /app/redis-cluster/bin
[user@localhost scripts]$ ./redis-trib.rb create --replicas 1 10.20.30.40:7001 10.20.30.40:7002 10.20.30.40:7003 10.200.202.41:7004 10.20.30.40:7005 10.20.30.40:7006
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved to redis-cli.
In order to use them you should call redis-cli with the --cluster option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 10.20.30.40:7001 10.20.30.40:7002 10.20.30.40:7003 10.200.202.41:7004 10.20.30.40:7005 10.20.30.40:7006 --cluster-replicas 1
To get help about all subcommands, type:
redis-cli --cluster help
//警告:现在redis cluster安装方式不推荐使用redis-trib.rb,而是使用redis-cli,这样就不用安装ruby等
//打印一下redis-trib.rb文件,内容就是以上警告
//下载个正确的脚本:https://blog.csdn.net/weixin_42364929/article/details/120578068?spm=1001.2014.3001.5502
参考资料
|