基于redis6.2.4的redis cluster部署

实验架构
在这里插入图片描述

1.修改配置文件启用cluster功能

1.修改主机名
hostnamectl set-hostname node1.magedu.org;exit
hostnamectl set-hostname node2.magedu.org;exit
hostnamectl set-hostname node3.magedu.org;exit
hostnamectl set-hostname node4.magedu.org;exit
hostnamectl set-hostname node5.magedu.org;exit
hostnamectl set-hostname node6.magedu.org;exit

2.安装redis6.2.4
脚本实现一键安装:详见https://blog.csdn.net/aa896517050/article/details/125955201

3.修改配置文件(所有节点)
sed -i.bak  -e '/masterauth/a masterauth 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' /apps/redis/etc/redis.conf

4.重启服务(所有节点)
systemctl enable --now redis
[root@node1:~]#
ps aux|grep redis
redis      5578  0.1  0.1 162500  2944 ?        Ssl  Jul24   0:06 /apps/redis/bin/redis-server 0.0.0.0:6379
root       5866  0.0  0.0 112808   968 pts/0    S+   00:24   0:00 grep --color=auto redis

#再次重启
systemctl restart redis
[root@node1:~]#
ps aux|grep redis
redis      6013  0.1  0.1 162500  3520 ?        Ssl  00:57   0:00 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]  #cluster标志
root       6021  0.0  0.0 112808   968 pts/0    S+   00:58   0:00 grep --color=auto redis

[root@node1:~]#
redis-cli -a 123456
127.0.0.1:6379> info cluster
# Cluster
cluster_enabled:1

2.所有节点相互进行meet

1.查看连接情况
[root@node1:~]#
redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
fdec26daee7636c212283fd8b32c8088e54a6655 :6379@16379 myself,master - 0 0 0 connected
#此时没有和任何一个连接,6各节点是各自独立的。

2.在任一节点上和其它所有节点进行meet通信(本次在node1操作,node1是中间人,全部连接)
[root@node1:~]#
redis-cli  -a 123456 --no-auth-warning cluster meet 10.0.0.17 6379
[root@node1:~]#
ss -nt
State       Recv-Q Send-Q       Local Address:Port        Peer Address:Port              
ESTAB       0      0               10.0.0.7:36605             10.0.0.17:16379              
ESTAB       0      36              10.0.0.7:22                10.0.0.1:51254              
ESTAB       0      0               10.0.0.7:16379             10.0.0.17:44342  
#集群端口为16379、

[root@node1:~]#
redis-cli  -a 123456 --no-auth-warning cluster meet 10.0.0.27 6379
OK
[root@node1:~]#
redis-cli  -a 123456 --no-auth-warning cluster meet 10.0.0.37 6379
OK
[root@node1:~]#
redis-cli  -a 123456 --no-auth-warning cluster meet 10.0.0.47 6379
OK
[root@node1:~]#
redis-cli  -a 123456 --no-auth-warning cluster meet 10.0.0.57 6379
OK
 
#查看连接情况
[root@node1:~]#
redis-cli -a 123456 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6  #表示6个节点之间建立了联系
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:0
cluster_stats_messages_ping_sent:468
cluster_stats_messages_pong_sent:475
cluster_stats_messages_meet_sent:6
cluster_stats_messages_sent:949
cluster_stats_messages_ping_received:475
cluster_stats_messages_pong_received:474
cluster_stats_messages_received:949

[root@node1:~]#
redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4e456d304f1971c3fe776c21d8dd2c880dd29fd2 10.0.0.27:6379@16379 master - 0 1658683099799 2 connected
008abaa9cf3b273f905a26838f675c1be17b8570 10.0.0.37:6379@16379 master - 0 1658683097000 3 connected
c30419b368a783f4c4d9de948dcc7f3d44ee4797 10.0.0.17:6379@16379 master - 0 1658683098000 1 connected
f941e86ede0fad0dfe70b5ee6ad0577e21cb0d98 10.0.0.47:6379@16379 master - 0 1658683099000 4 connected
46b3deba6bdaf57e481cce86f4335963d555dcdc 10.0.0.57:6379@16379 master - 0 1658683096000 5 connected
fdec26daee7636c212283fd8b32c8088e54a6655 10.0.0.7:6379@16379 myself,master - 0 1658683098000 0 connected

3.分配槽位(脚本实现)

[root@node1:~]#
vim addslot.sh

#!/bin/bash
#****************************************************
#Date:         2022-07-25
#Author:       wwzroom
#FileName:     addslot.sh
#Description:  The test script
#QQ:           896517050
#sh****************************************************
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

2.添加执行权限
[root@node1:~]#
chmod +x addslot.sh

3.分配槽位
[root@node1:~]#
./addslot.sh 10.0.0.7 6379 0 5461
[root@node1:~]#
./addslot.sh 10.0.0.17 6379 5462 10922
[root@node1:~]#
./addslot.sh 10.0.0.27 6379 10923 16383
#分配的时候如果第一个节点分配错了,第二个节点再分配的时候,就分配不上了,不影响实验,如果第一个分多了,后面就没办法分了,因此可以删除记录的文件,rm -rf /apps/redis/data/nodes-6379.conf,重新meet,重新分配。

#查看分配情况
[root@node1:~]#
redis-cli -a 123456 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> cluster nodes
4e456d304f1971c3fe776c21d8dd2c880dd29fd2 10.0.0.27:6379@16379 master - 0 1658684953613 2 connected 10923-16383
008abaa9cf3b273f905a26838f675c1be17b8570 10.0.0.37:6379@16379 master - 0 1658684952000 3 connected
c30419b368a783f4c4d9de948dcc7f3d44ee4797 10.0.0.17:6379@16379 master - 0 1658684953000 1 connected 5462-10922
f941e86ede0fad0dfe70b5ee6ad0577e21cb0d98 10.0.0.47:6379@16379 master - 0 1658684953000 4 connected
46b3deba6bdaf57e481cce86f4335963d555dcdc 10.0.0.57:6379@16379 master - 0 1658684952609 5 connected
fdec26daee7636c212283fd8b32c8088e54a6655 10.0.0.7:6379@16379 myself,master - 0 1658684951000 0 connected 0-5461


4.指定各节点的主从关系

[root@node1:~]#
redis-cli -h 10.0.0.37 -a 123456 --no-auth-warning cluster replicate fdec26daee7636c212283fd8b32c8088e54a6655 #主节点的id,这里设置37为07的从节点,id号为07的,ID号有上文中的nodes信息得到。

[root@node1:~]#
redis-cli -h 10.0.0.47 -a 123456 --no-auth-warning cluster replicate c30419b368a783f4c4d9de948dcc7f3d44ee4797
[root@node1:~]#
redis-cli -h 10.0.0.57 -a 123456 --no-auth-warning cluster replicate 4e456d304f1971c3fe776c21d8dd2c880dd29fd2

#查看具体情况
[root@node1:~]#
redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4e456d304f1971c3fe776c21d8dd2c880dd29fd2 10.0.0.27:6379@16379 master - 0 1658686186000 2 connected 10923-16383
008abaa9cf3b273f905a26838f675c1be17b8570 10.0.0.37:6379@16379 slave fdec26daee7636c212283fd8b32c8088e54a6655 0 1658686187701 0 connected
c30419b368a783f4c4d9de948dcc7f3d44ee4797 10.0.0.17:6379@16379 master - 0 1658686186693 1 connected 5462-10922
f941e86ede0fad0dfe70b5ee6ad0577e21cb0d98 10.0.0.47:6379@16379 slave c30419b368a783f4c4d9de948dcc7f3d44ee4797 0 1658686185684 1 connected
46b3deba6bdaf57e481cce86f4335963d555dcdc 10.0.0.57:6379@16379 slave 4e456d304f1971c3fe776c21d8dd2c880dd29fd2 0 1658686185000 2 connected
fdec26daee7636c212283fd8b32c8088e54a6655 10.0.0.7:6379@16379 myself,master - 0 1658686185000 0 connected 0-5461

[root@node1:~]#
redis-cli -a 123456 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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             #3组集群
cluster_current_epoch:5
cluster_my_epoch:0
cluster_stats_messages_ping_sent:3721
cluster_stats_messages_pong_sent:3753
cluster_stats_messages_meet_sent:6
cluster_stats_messages_sent:7480
cluster_stats_messages_ping_received:3753
cluster_stats_messages_pong_received:3727
cluster_stats_messages_received:7480

[root@node1:~]#
redis-cli  -a 123456 --no-auth-warning cluster slots
1) 1) (integer) 0
   2) (integer) 5461
   3) 1) "10.0.0.7"
      2) (integer) 6379
      3) "fdec26daee7636c212283fd8b32c8088e54a6655"
   4) 1) "10.0.0.37"
      2) (integer) 6379
      3) "008abaa9cf3b273f905a26838f675c1be17b8570"
2) 1) (integer) 5462
   2) (integer) 10922
   3) 1) "10.0.0.17"
      2) (integer) 6379
      3) "c30419b368a783f4c4d9de948dcc7f3d44ee4797"
   4) 1) "10.0.0.47"
      2) (integer) 6379
      3) "f941e86ede0fad0dfe70b5ee6ad0577e21cb0d98"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.27"
      2) (integer) 6379
      3) "4e456d304f1971c3fe776c21d8dd2c880dd29fd2"
   4) 1) "10.0.0.57"
      2) (integer) 6379
      3) "46b3deba6bdaf57e481cce86f4335963d555dcdc"

5.写入数据测试

1.随便连接一个主节点进行写数据,对于key值进行hash运算(与value无关),再根据卡槽关系进行复分配,如果未分配到本节点,就会提示报错(error) MOVED 7755 10.0.0.17:6379,提示放到具体的槽位。
[root@node1:~]#
redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set class m44
(error) MOVED 7755 10.0.0.17:6379
127.0.0.1:6379> 

2登录指定槽位进行写数据,能够成功,不能在从节点查数据,不被启用
[root@node2:~]#
redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set class m44
OK

#从节点不能查数据,也就是说读和写只能由主节点提供,从节点不能提供服务。
127.0.0.1:6379> keys class
1) "class"
127.0.0.1:6379> get class
(error) MOVED 7755 10.0.0.17:6379

3.指定选项-c表示以集群方式访问,不受限制,主或者从登录都可以使用。
[root@node1:~]#
redis-cli -c -a 123456 --no-auth-warning set name wang
OK
[root@node1:~]#
redis-cli -c -a 123456 --no-auth-warning get name 
"wang"

#提示进入重定向到具体的节点。
[root@node5:~]#
redis-cli -a 123456 -c  #以集群方式登录
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set age 20
-> Redirected to slot [741] located at 10.0.0.7:6379
OK
10.0.0.7:6379> get age
"20"
10.0.0.7:6379> set class M47
-> Redirected to slot [7755] located at 10.0.0.17:6379
OK
10.0.0.17:6379> get class
"M47"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值