环境:
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"