首页 > 数据库 >5、基于redis5的redis cluster部署

5、基于redis5的redis cluster部署

时间:2022-09-02 01:56:01浏览次数:55  
标签:10.0 6379 redis5 redis cluster master root

5、基于redis5的redis cluster部署

 

 

 

5.1 原生命令手动部署过程

在所有节点安装redis,并配置开启cluster功能

各个节点执行meet,实现所有节点的相互通信

为各个master 节点指派槽位范围

指定各个节点的主从关系

 

5.2 在所有节点安装redis并启动cluster功能

#在所有6个节点上都执行下面相同操作

[root@centos8 ~]#dnf -y install redis

 

 

#批量修改配置文件

 '/ /a /'  a追加一行

 

找出含有masterauth的这行,在这行下面增加masterauth 123456的这一行

sed -i.bak -e '/masterauth/a masterauth 123456' /etc/redis.conf

 

 

'/ /c /'  c替换,把前面的替换为后面的

sed -i.bak -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf

 

[root@CentOS8 ~]# sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf

 

[root@centos8 ~]#systemctl enable --now redis

 

 

 

5.3 执行meet 操作实现相互通信

#在任一节点上和其它所有节点进行meet通信

[root@centos8 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet

10.0.0.18 6379

[root@centos8 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet

10.0.0.28 6379

[root@centos8 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet

10.0.0.38 6379

[root@centos8 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet

10.0.0.48 6379

[root@centos8 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet

10.0.0.58 6379

 

 

 

#可以看到所有节点之间可以相互连接通信

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes

5736b332be81750c3e8dcbec3c54c9bfa98b64b7 10.0.0.48:6379@16379 master - 0 1661330791000 4 connected

7fb18fda5dd7c5dc0aa7173c52268073697c7e24 10.0.0.58:6379@16379 master - 0 1661330790321 0 connected

15c4cf2e696b118dbd6f697329ee8b623ad13a80 10.0.0.18:6379@16379 master - 0 1661330791000 3 connected

42301883b7d02f72da0ed39b3040dc5ec6915fa5 10.0.0.28:6379@16379 master - 0 1661330793344 1 connected

5a326ad0841e833bfe436f8d4a613589503e7c7e 10.0.0.8:6379@16379 myself,master - 0 1661330791000 2 connected

5c9a73c9d49b3877d5c2229dc3f11b1e22d1ec7a 10.0.0.38:6379@16379 master - 0 1661330792336 5 connected

 

#由于没有槽位无法创建key

[root@CentOS8 ~]# redis-cli -a 123456 --no-auth-warning set name wang

(error) CLUSTERDOWN Hash slot not served

 

#查看当前状态

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster info

cluster_state:fail

cluster_slots_assigned:0  #无槽位分配置

cluster_slots_ok:0

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:0      #无集群成员

cluster_current_epoch:5

cluster_my_epoch:2

cluster_stats_messages_ping_sent:690

cluster_stats_messages_pong_sent:556

cluster_stats_messages_meet_sent:5

cluster_stats_messages_sent:1251

cluster_stats_messages_ping_received:555

cluster_stats_messages_pong_received:561

cluster_stats_messages_meet_received:1

cluster_stats_messages_received:1117

 

 

 

5.4 为各个master 节点指派槽位范围

主要命令:

redis-cli -h ${host} -p $port -a ${pass} --no-auth-warning cluster addslots

${slot}

 

#创建添加槽位的脚本

[root@centos8 ~]#cat addslots.sh

#!/bin/bash

#  

host=$1

port=$2

start=$3

end=$4

pass=123456

 

for slot in `seq ${start} ${end}`;do

    echo slot:$slot

    redis-cli -h ${host} -p ${port} -a ${pass} --no-auth-warning cluster addslots ${slot}

done         

 

 

[root@CentOS8 ~]# bash -n addslots.sh

 

#分别为三个master分配槽位,共16364/3=5,461.333333333333,平均每个master分配5461个槽位

 

[root@centos8 ~]#bash addslots.sh 10.0.0.8 6379 0 5461

 

[root@centos8 ~]#bash addslots.sh 10.0.0.18 6379 5462 10922

 

[root@centos8 ~]#bash addslots.sh 10.0.0.28 6379 10923 16383

 

 

#当第一个master分配完槽位后,可以看到下面信息

[root@CentOS8 ~]# redis-cli -a 123456 --no-auth-warning cluster info

cluster_state:ok

cluster_slots_assigned:5462   #分配槽位数

cluster_slots_ok:5462

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:1 #加入集群

cluster_current_epoch:5

cluster_my_epoch:5

cluster_stats_messages_ping_sent:406

cluster_stats_messages_pong_sent:393

cluster_stats_messages_meet_sent:5

cluster_stats_messages_sent:804

cluster_stats_messages_ping_received:393

cluster_stats_messages_pong_received:411

cluster_stats_messages_received:804

 

 

[root@CentOS8 ~]# redis-cli -a 123456 --no-auth-warning cluster nodes

004cb696c74d1082d39fd041608c743108273720 10.0.0.48:6379@16379 master - 0 1661335877753 4 connected

458c4acc71a81eb06626dcab2f5f34044c2a4e13 10.0.0.18:6379@16379 master - 0 1661335876742 1 connected

6dd4724580930e593c4b8d6635a5cc748ab1c306 10.0.0.28:6379@16379 master - 0 1661335877000 2 connected

7fb45b7f2120cfc77adbef4ec35bc50e4bbe3a38 10.0.0.8:6379@16379 myself,master - 0 1661335875000 5 connected 0-5461

a0da91c05138009214b3ce667c331f0c0b847d1d 10.0.0.58:6379@16379 master - 0 1661335876000 0 connected

0b7b8c494a9e137a4ce2a4c8ca82a5c3718737c5 10.0.0.38:6379@16379 master - 0 1661335875735 3 connected

 

 

 

#当所有三个节点都分配槽位后可以创建key

[root@CentOS8 ~]# redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning set name wang

OK

[root@CentOS8 ~]# redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning get name

"wang"

 

#当所有的三个master分配完槽位后,可以看到下面信息,所有节点都是master

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes

004cb696c74d1082d39fd041608c743108273720 10.0.0.48:6379@16379 master - 0 1661336549000 4 connected

458c4acc71a81eb06626dcab2f5f34044c2a4e13 10.0.0.18:6379@16379 master - 0 1661336547283 1 connected 5462-10922

6dd4724580930e593c4b8d6635a5cc748ab1c306 10.0.0.28:6379@16379 master - 0 1661336549299 2 connected 10923-16383

7fb45b7f2120cfc77adbef4ec35bc50e4bbe3a38 10.0.0.8:6379@16379 myself,master - 0 1661336548000 5 connected 0-5461

a0da91c05138009214b3ce667c331f0c0b847d1d 10.0.0.58:6379@16379 master - 0 1661336550309 0 connected

0b7b8c494a9e137a4ce2a4c8ca82a5c3718737c5 10.0.0.38:6379@16379 master - 0 1661336549000 3 connected

 

 

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning 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:5

cluster_my_epoch:5

cluster_stats_messages_ping_sent:1155

cluster_stats_messages_pong_sent:1123

cluster_stats_messages_meet_sent:5

cluster_stats_messages_sent:2283

cluster_stats_messages_ping_received:1123

cluster_stats_messages_pong_received:1160

cluster_stats_messages_received:2283

 

 

5.5 指定各个节点的主从关系

#通过上面cluster nodes 查看master的ID信息,执行下面操作,将对应的slave 指定相应的master节

点,实现三对主从节点

#将10.0.0.38指定为10.0.0.8的从节点, “7fb45b7f2120cfc77adbef4ec35bc50e4bbe3a38”主机8的节点id

[root@CentOS8 ~]# redis-cli -h 10.0.0.38 -a 123456 --no-auth-warning cluster replicate 7fb45b7f2120cfc77adbef4ec35bc50e4bbe3a38

 

#将10.0.0.48指定为10.0.0.18的从节点

[root@CentOS8 ~]# redis-cli -h 10.0.0.48 -a 123456 --no-auth-warning cluster replicate 458c4acc71a81eb06626dcab2f5f34044c2a4e13

 

#将10.0.0.58指定为10.0.0.28的从节点

[root@CentOS8 ~]# redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster replicate 6dd4724580930e593c4b8d6635a5cc748ab1c306

 

#所有三组主从节点创建成功后,可以看到最终结果

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes

004cb696c74d1082d39fd041608c743108273720 10.0.0.48:6379@16379 slave 458c4acc71a81eb06626dcab2f5f34044c2a4e13 0 1661337215000 4 connected

458c4acc71a81eb06626dcab2f5f34044c2a4e13 10.0.0.18:6379@16379 master - 0 1661337217000 1 connected 5462-10922

6dd4724580930e593c4b8d6635a5cc748ab1c306 10.0.0.28:6379@16379 master - 0 1661337217339 2 connected 10923-16383

7fb45b7f2120cfc77adbef4ec35bc50e4bbe3a38 10.0.0.8:6379@16379 myself,master - 0 1661337215000 5 connected 0-5461

a0da91c05138009214b3ce667c331f0c0b847d1d 10.0.0.58:6379@16379 slave 6dd4724580930e593c4b8d6635a5cc748ab1c306 0 1661337216000 2 connected

0b7b8c494a9e137a4ce2a4c8ca82a5c3718737c5 10.0.0.38:6379@16379 slave 7fb45b7f2120cfc77adbef4ec35bc50e4bbe3a38 0 1661337215327 5 connected

 

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning 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:5

cluster_my_epoch:5

cluster_stats_messages_ping_sent:1863

cluster_stats_messages_pong_sent:1814

cluster_stats_messages_meet_sent:5

cluster_stats_messages_sent:3682

cluster_stats_messages_ping_received:1814

cluster_stats_messages_pong_received:1868

cluster_stats_messages_received:3682

 

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning info replication

# Replication

role:master

connected_slaves:1

slave0:ip=10.0.0.38,port=6379,state=online,offset=574,lag=0

master_replid:9acbd50849673077fcec443bc749e6d598ea1bf0

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:574

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:574

 

[root@CentOS8 ~]# redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning info replication

# Replication

role:master

connected_slaves:1

slave0:ip=10.0.0.48,port=6379,state=online,offset=434,lag=0

master_replid:53764a8dc5670083ffa7de55e9ec0ec787f51e94

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:434

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:434

 

[root@CentOS8 ~]# redis-cli -h 10.0.0.28 -a 123456 --no-auth-warning info replication

# Replication

role:master

connected_slaves:1

slave0:ip=10.0.0.58,port=6379,state=online,offset=350,lag=1

master_replid:1622ff2da39386a37eba3454f234257cab107210

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:350

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:350

 

 

 

#查看主从节关系及槽位信息

[root@CentOS8 ~]# redis-cli -h 10.0.0.28 -a 123456 --no-auth-warning cluster slots

1) 1) (integer) 10923

   2) (integer) 16383

   3) 1) "10.0.0.28"

      2) (integer) 6379

      3) "6dd4724580930e593c4b8d6635a5cc748ab1c306"

   4) 1) "10.0.0.58"

      2) (integer) 6379

      3) "a0da91c05138009214b3ce667c331f0c0b847d1d"

2) 1) (integer) 5462

   2) (integer) 10922

   3) 1) "10.0.0.18"

      2) (integer) 6379

      3) "458c4acc71a81eb06626dcab2f5f34044c2a4e13"

   4) 1) "10.0.0.48"

      2) (integer) 6379

      3) "004cb696c74d1082d39fd041608c743108273720"

3) 1) (integer) 0

   2) (integer) 5461

   3) 1) "10.0.0.8"

      2) (integer) 6379

      3) "7fb45b7f2120cfc77adbef4ec35bc50e4bbe3a38"

   4) 1) "10.0.0.38"

      2) (integer) 6379

      3) "0b7b8c494a9e137a4ce2a4c8ca82a5c3718737c5"

 

 

 

 

 

 

 

5.6 验证 redis cluster 访问

# 创建key,指定选项 -c 表示以集群方式连接

[root@CentOS8 ~]# redis-cli -c -h 10.0.0.8 -a 123456 --no-auth-warning set name wang

OK

[root@CentOS8 ~]# redis-cli -c -h 10.0.0.8 -a 123456 --no-auth-warning get name

"wang"

 

# 创建的key落到了10.0.0.18节点

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning get name

(error) MOVED 5798 10.0.0.18:6379

 

[root@CentOS8 ~]# redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning get name

"wang"

 

[root@CentOS8 ~]# redis-cli -h 10.0.0.28 -a 123456 --no-auth-warning get name

(error) MOVED 5798 10.0.0.18:6379

 

标签:10.0,6379,redis5,redis,cluster,master,root
From: https://www.cnblogs.com/biaoming534/p/16648399.html

相关文章

  • 1、简述redis特点及其应用场景
    1、简述redis特点及其应用场景 1.1redis特点速度快:10WQPS,基于内存,C语言实现单线程持久化支持多种数据结构支持多种编程语言功能丰富:支持Lua脚本,发布订阅......
  • 2. 对比redis的RDB、AOF模式的优缺点
    2. 对比redis的RDB、AOF模式的优缺点 2.1 redis的RDB模式2.1.1RDB模式工作原理  RDB(RedisDataBase):基于时间的快照,其默认只保留当前最新的一次快照,特点......
  • 3.实现redis哨兵,模拟master故障场景
    3.实现redis哨兵,模拟master故障场景实验拓扑图  3.1哨兵的准备实现主从复制架构哨兵的前提是已经实现了一个redis的主从复制的运行环境,从而实现一个一主两从基于......
  • redis异地多活
    what:异地多活:简单来说,就是在不同地域建立数据中心,每个数据中心在日常使用中都需要正常接入业务流量,做业务支撑。异地多活,也属于分布式架构的系统。......
  • 使用golang实现Redis中间件
    (一).RESP协议解释  RESP协议在Redis1.2被引入,直到Redis2.0才成为和Redis服务器通信的标准。这个协议需要在你的Redis客户端实现。RESP是一个支持多种数据类型的序列化协......
  • GaussDB(for Redis)即将亮相华为云快成长直播间,让成本直降75%!
    “828选华为云,实惠更实用”。当前,华为云828B2B企业节正如火如荼进行中。旨在帮助企业解决数字化转型难题,加速千行百业云上创新的华为云快成长企业科技直播间同步精彩开播,......
  • Spring整合Redis(十八)
    一、Redis简介Redis是一款基于键值对的NoSQL数据库,它的值支持多种数据结构:字符串(strings)、哈希(hashes)、列表(lists)、集合(sets)、有序集合(sortedsets)等。Redi......
  • Redis使用 Redis封装
    封装Redis:一、application.properties:#过期日期:10800秒(3分钟)PitND.expire.pro=10800二、MainEventHERDMRedis.java:importorg.springframework.beans.facto......
  • 【Azure Redis 缓存】Azure Redis 功能性讨论三: 调优参数配置
    问题描述在使用AzureRedis的服务中,遇见了以下系列问题需要澄清:在开源Redis6.0中,多线程默认禁用,只使用主线程。如需开启需要修改redis.config配置文件。Redis的多线......
  • Redis 管理工具 - Redis Desktop Manager
     RedisDesktopManager可以轻松管理Redis桌面。为您提供了一个易于使用的GUI,可以访问您的Redis数据库并执行一些基本操作:将键视为树,CRUD键,通过shell执行命令。RESP.app......