Redis教程

5、基于redis5的redis cluster部署

本文主要是介绍5、基于redis5的redis cluster部署,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

 

这篇关于5、基于redis5的redis cluster部署的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!