Redis Cluster架构
# 节点 配置cluster-enabled:yes,表示以集群的方式启动 # meet(节点直接相互通信) 所有节点,都有联系,例如,三个节点A B C A meet B, A meet C, B meet C # 指派槽,共16383个slot,数据存储在哪个节点 节点 槽 A 0~5460 B 5461~10922 c 10923~16383 客户端: 1.keyhash=hash(key) 2.slot=keyhash%16383 # 复制 # Redis Cluster特性 复制,高可用,分片
原生命令安装-理解架构
# 三主三从的搭建 1.配置开启节点 port ${port} daemonize yes dir "/opt/redis/data" dbfilename "dump-${port}.rdb" logfile "${port}.log" cluster-enabled yes cluster-config-file nodes-${port}.conf # 开启节点 redis-server redis-7000.conf # 主 2.meet(节点之间通信) cluster meet ip port redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7001 ... 这几个就互相有通信了 # Cluster节点主要配置 cluster-enabled yes cluster-node-timeout 15000 # 15s cluster-config-file "nodes.conf" cluster-require-full-coverage yes # 一般配为no,表示一个节点出问题,也可以继续使用 3.指派槽(数据访问)共16383个slot 分配槽 cluster addslots slot [slot...] redis-cli -h 127.0.0.1 -p 7000 cluster addslots {0...5461} redis-cli -h 127.0.0.1 -p 7001 cluster addslots {5462...10922} redis-cli -h 127.0.0.1 -p 7002 cluster addslots {10923...16383} 4.主从(故障自动转移) cluster replicate node-id redis-cli -h 127.0.0.1 -p 7003 cluster replicate ${node-id-7000} redis-cli -h 127.0.0.1 -p 7004 cluster replicate ${node-id-7001} redis-cli -h 127.0.0.1 -p 7005 cluster replicate ${node-id-7002}
配置三主三从[原生命令]
# 配置redis-7000.conf,主7000端口的redis port 7000 daemonize yes dir "/home/zby/redis-4.0.6/data" dbfilename "dump-7000.rdb" logfile "7000.log" cluster-enabled yes cluster-config-file nodes-7000.conf cluster-require-full-coverage no # 在配置主7001,和主7002,从7003,7004,7005 # 将redis-7000.conf文件中的7000改为7001,并写到redis-7001.conf文件中 sed 's/7000/7001/g' redis-7000.conf > redis-7001.conf # meet操作,搭建集群,互通 redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7001 redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7002 redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7003 redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7004 redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7005 # 给主分配槽共16383,分配三个主,即7000,7001,7002 7000 0-5461 7001 5462-10922 7002 10923-16383 编写脚本来进行执行addslots.sh start=$1 end=$2 host=$3 port=$4 for slot in `seq ${start} ${end}` do echo "slot:${slot}" redis-cli -h ${host} -p ${port} cluster addslots ${slot} done 执行脚本 bash addslots.sh 0 5461 127.0.0.1 7000 bash addslots.sh 5462 10922 127.0.0.1 7001 bash addslots.sh 10923 16383 127.0.0.1 7002 # 主从 redis-cli -h 127.0.0.1 -p 7003 cluster replicate 8f2130460c33790666236e9fd338cc83ec427c5b redis-cli -h 127.0.0.1 -p 7004 cluster replicate 34fd90ab7c42fc1d9db9ecfe27b0ffc2105e9c1c redis-cli -h 127.0.0.1 -p 7005 cluster replicate d319c8083f8149f95ad8984cd87cbdfd6c543d2e # 连接,以中这种方式进行连接带上-c redis-cli -c -p 7000 redis-cli -c -p 7001
Ruby搭建redis集群
安装ruby
# 下载ruby的包 tar -azvf ruby-2.6.3.tar.gz ./configure --prefix=/usr/local/ruby make make install cd /usr/local/ruby cp bin/ruby /usr/local/bin cp bin/gem /usr/local/bin
安装rubygem redis
# 下载 http://rubygems.org/downloads/redis-4.0.3.gem # 安装 gem install -l redis-4.0.0.gem gem list -- check redis gem # 检查是否安装好 cd /home/zby/redis-4.0.6/src ./redis-trib.rb # 如果不报错,就表示安装成功 # 在把连接cp到/usr/local/bin/ cp /home/zby/redis-4.0.6/src/redis-trib.rb /usr/local/bin/
redis-trib.rb搭建集群
# 批量杀死进程 ps -ef | grep redis | awk '{print $2}' | xargs kill # 三主三从 # 配置redis-8000.conf文件 port 8000 daemonize yes dir "/home/zby/redis-4.0.6/data" dbfilename "dump-8000.rdb" logfile "8000.log" cluster-enabled yes cluster-config-file nodes-8000.conf cluster-require-full-coverage no # 配置8001,8002,8003,8004,8005 sed 's/8000/8001/g' redis-8000.conf > redis-8001.conf # 把这6个启动起来 redis-server redis-8000.conf redis-server redis-8001.conf ... # 配置主从关系,前面1表示,一主一从,改为2就表示1主2从,会自动去分配 redis-trib.rb create --replicas 1 127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005 # 连接,要带-c redis-cli -c -p 8000