cluster分布式集群实例

在CentOS8环境下,配置了一个包含6台主机的分布式集群。详细步骤包括在所有节点上安装并配置集群,通过meet命令确保节点间通信,均匀分配槽位至每个master节点,设置主从关系,并验证集群的正确性和功能。最终实现了一种均衡的三对主从节点配置。
摘要由CSDN通过智能技术生成

环境:
centos8
准备6台主机
10.0.0.8
10.0.0.18
10.0.0.28
10.0.0.38
10.0.0.48
10.0.0.58

在六个节点安装cluster并且配置文件

yum -y install redis

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

重启
systemctl enable --now redis

在任意一个节点meet与各个节点进行通信

redis-cli -h 10.0.0.8 -a --no-auth-warning cluster meet 10.0.0.18 6379
redis-cli -h 10.0.0.8 -a --no-auth-warning cluster meet 10.0.0.28 6379
redis-cli -h 10.0.0.8 -a --no-auth-warning cluster meet 10.0.0.38 6379
redis-cli -h 10.0.0.8 -a --no-auth-warning cluster meet 10.0.0.48 6379
redis-cli -h 10.0.0.8 -a --no-auth-warning cluster meet 10.0.0.58 6379

查看所有节点之间可以相互连接通信(只是完成后的样子)

[root@CentOS8 ~]# redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning cluster nodes
bb30f71d89d32b17f8ce6eaeecd8a7c42cab6217 10.0.0.58:6379@16379 slave a7aac8b92a426fd7349fb27b5592f217eeb76207 0 1603622704000 5 connected
a7aac8b92a426fd7349fb27b5592f217eeb76207 10.0.0.28:6379@16379 master - 0 1603622702000 5 connected 10923-16383
3ea3c7f47b9aa1320b1267b4fc441f9ee7dc08ea 10.0.0.18:6379@16379 myself,master - 0 1603622703000 2 connected 5462-10922
0ddd2cf81b2f4a20e9333d7a03f8627e65b9bdfc 10.0.0.8:6379@16379 master - 0 1603622704476 1 connected 0-5461
63771e87fff03ac56a5431c117d0684102e25da5 10.0.0.48:6379@16379 slave 3ea3c7f47b9aa1320b1267b4fc441f9ee7dc08ea 0 1603622702000 4 connected
3e4feff12354c90dd29539ad133059a1afc1f324 10.0.0.38:6379@16379 slave 0ddd2cf81b2f4a20e9333d7a03f8627e65b9bdfc 0 1603622705484 3 connected

为各个节点创建分配指派槽位范围
创建脚本分配

vi redis.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

分配槽位(共16364/3=5,461.333333333333,平均每个master分配5461个槽位)

bash redis.sh 10.0.0.8 6379 0 5461
bash redis.sh 10.0.0.18 6379 5462 10922
bash redis.sh 10.0.0.28 6379 10923 16383

查看master分配完槽位后的信息

[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:1
cluster_stats_messages_ping_sent:10286
cluster_stats_messages_pong_sent:10736
cluster_stats_messages_meet_sent:12
cluster_stats_messages_sent:21034
cluster_stats_messages_ping_received:10734
cluster_stats_messages_pong_received:10298
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:21034

查看master分配完槽位后的信息

[root@CentOS8 ~]# redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning cluster nodes
bb30f71d89d32b17f8ce6eaeecd8a7c42cab6217 10.0.0.58:6379@16379 slave a7aac8b92a426fd7349fb27b5592f217eeb76207 0 1603622704000 5 connected
a7aac8b92a426fd7349fb27b5592f217eeb76207 10.0.0.28:6379@16379 master - 0 1603622702000 5 connected 10923-16383
3ea3c7f47b9aa1320b1267b4fc441f9ee7dc08ea 10.0.0.18:6379@16379 myself,master - 0 1603622703000 2 connected 5462-10922
0ddd2cf81b2f4a20e9333d7a03f8627e65b9bdfc 10.0.0.8:6379@16379 master - 0 1603622704476 1 connected 0-5461
63771e87fff03ac56a5431c117d0684102e25da5 10.0.0.48:6379@16379 slave 3ea3c7f47b9aa1320b1267b4fc441f9ee7dc08ea 0 1603622702000 4 connected
3e4feff12354c90dd29539ad133059a1afc1f324 10.0.0.38:6379@16379 slave 0ddd2cf81b2f4a20e9333d7a03f8627e65b9bdfc 0 1603622705484 3 connected

指定主从关系
通过上面cluster nodes 查看master的ID信息,执行下面操作,将对应的slave 指定相应的master节点,实现三对主从节点

redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning cluster nodes 可以查看id
#3e4feff12354c90dd29539ad133059a1afc1f324 为10.0.0.38的id
redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster replicate 3e4feff12354c90dd29539ad133059a1afc1f324
redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning cluster replicate 63771e87fff03ac56a5431c117d0684102e25da5
redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster replicate bb30f71d89d32b17f8ce6eaeecd8a7c42cab6217

查看关系

[root@CentOS8 ~]# redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster slots
1) 1) (integer) 0
   2) (integer) 5461
   3) 1) "10.0.0.8"
      2) (integer) 6379
      3) "0ddd2cf81b2f4a20e9333d7a03f8627e65b9bdfc"
   4) 1) "10.0.0.38"
      2) (integer) 6379
      3) "3e4feff12354c90dd29539ad133059a1afc1f324"
2) 1) (integer) 5462
   2) (integer) 10922
   3) 1) "10.0.0.18"
      2) (integer) 6379
      3) "3ea3c7f47b9aa1320b1267b4fc441f9ee7dc08ea"
   4) 1) "10.0.0.48"
      2) (integer) 6379
      3) "63771e87fff03ac56a5431c117d0684102e25da5"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.28"
      2) (integer) 6379
      3) "a7aac8b92a426fd7349fb27b5592f217eeb76207"
   4) 1) "10.0.0.58"
      2) (integer) 6379
      3) "bb30f71d89d32b17f8ce6eaeecd8a7c42cab6217"

验证(在18创建的key)#-c 表示以集群方式连接

[root@centos8 ~]# redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning  get name
"zhao"
[root@centos8 ~]# redis-cli -h 10.0.0.48 -c -a 123456 --no-auth-warning get name 
"zhao"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值