redis_6.2.8集群配置(三台)

一.安装

1.上传redis_6.2.8.tar.gz文件到soft里的redis下

环境准备

IP主机名端口号节点
192.168.238.102redis-cluster6701master
192.168.238.102redis-cluster6702slave —>6705的master
192.168.238.103redis-cluster6703master
192.168.238.103redis-cluster6704slave —>6701的master
192.168.238.104redis-cluster6705master
192.168.238.104redis-cluster6706slave —>6703的master

image-20221215103144259

2.解压安装

192.168.238.102安装6701和6702,注意需要修改redis.conf 文件,打开里面的集群配置

#安装
#修改好对应端口6701
./install_redis_6.2.8.sh redis
#克隆6702,修改里面端口6702
./install_redis_6.2.8.sh clone

#修改配置文件
sed -i -e '1387c cluster-enabled yes' -e '1395c cluster-config-file nodes-6701.conf' -e '1401c cluster-node-timeout 15000' /opt/redis_6701/redis.conf
sed -i -e '1387c cluster-enabled yes' -e '1395c cluster-config-file nodes-6702.conf' -e '1401c cluster-node-timeout 15000' /opt/redis_6702/redis.conf
egrep "cluster-enabled|cluster-config-file|cluster-node-timeout"  /opt/redis_6701/redis.conf
egrep "cluster-enabled|cluster-config-file|cluster-node-timeout"  /opt/redis_6702/redis.conf

systemctl restart redis_6701
systemctl restart redis_6702
#检查,后面出现cluster即可
ps -ef|grep redis
root       5628      1  0 10:40 ?        00:00:00 /opt/redis_6701/redis-server 192.168.238.102:6701 [cluster]
root       5643      1  0 10:40 ?        00:00:00 /opt/redis_6702/redis-server 192.168.238.102:6702 [cluster]

192.168.238.103安装6703和6704

sed -i -e '1387c cluster-enabled yes' -e '1395c cluster-config-file nodes-6703.conf' -e '1401c cluster-node-timeout 15000' /opt/redis_6703/redis.conf
sed -i -e '1387c cluster-enabled yes' -e '1395c cluster-config-file nodes-6704.conf' -e '1401c cluster-node-timeout 15000' /opt/redis_6704/redis.conf
egrep "cluster-enabled|cluster-config-file|cluster-node-timeout"  /opt/redis_6703/redis.conf
egrep "cluster-enabled|cluster-config-file|cluster-node-timeout"  /opt/redis_6704/redis.conf

systemctl restart redis_6703
systemctl restart redis_6704
#检查,后面出现cluster即可
ps -ef|grep redis
root       6724      1  1 10:45 ?        00:00:00 /opt/redis_6703/redis-server 192.168.238.103:6703 [cluster]
root       6740      1  9 10:45 ?        00:00:00 /opt/redis_6704/redis-server 192.168.238.103:6704 [cluster]

192.168.238.104安装6705和6706

sed -i -e '1387c cluster-enabled yes' -e '1395c cluster-config-file nodes-6705.conf' -e '1401c cluster-node-timeout 15000' /opt/redis_6705/redis.conf
sed -i -e '1387c cluster-enabled yes' -e '1395c cluster-config-file nodes-6706.conf' -e '1401c cluster-node-timeout 15000' /opt/redis_6706/redis.conf
egrep "cluster-enabled|cluster-config-file|cluster-node-timeout"  /opt/redis_6705/redis.conf
egrep "cluster-enabled|cluster-config-file|cluster-node-timeout"  /opt/redis_6706/redis.conf

systemctl restart redis_6705
systemctl restart redis_6706
#检查,后面出现cluster即可
ps -ef|grep redis
root       7058      1  0 10:48 ?        00:00:00 /opt/redis_6705/redis-server 192.168.238.104:6705 [cluster]
root       7074      1  0 10:48 ?        00:00:00 /opt/redis_6706/redis-server 192.168.238.104:6706 [cluster]

二.手动配置(不建议手动)

1.手动配置集群节点之间相互发现,在任意一个节点中操作即可(建议用自动配置,手动配置可以熟悉下配置过程)
#确保firewalld和iptables防火墙都关闭在的
/opt/redis_6701/redis-cli -h 192.168.238.102 -p 6701

CLUSTER MEET 192.168.238.102 6702
CLUSTER MEET 192.168.238.103 6703
CLUSTER MEET 192.168.238.103 6704
CLUSTER MEET 192.168.238.104 6705
CLUSTER MEET 192.168.238.104 6706

CLUSTER NODES

ll /opt/redis_*
2.为集群中的充当Master的节点分配槽位

1)分配槽位

#192.168.238.102
/opt/redis_6701/redis-cli -h 192.168.238.102 -p 6701 cluster addslots {0..5461}
#192.168.238.103
/opt/redis_6703/redis-cli -h 192.168.238.103 -p 6703 cluster addslots {5462..10922}
#192.168.238.104
/opt/redis_6705/redis-cli -h 192.168.238.104 -p 6705 cluster addslots {10923..16383}

2)查看集群状态

/opt/redis_6701/redis-cli -h 192.168.238.102 -p 6701
192.168.238.102:6701> 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:2413
cluster_stats_messages_pong_sent:2476
cluster_stats_messages_meet_sent:8
cluster_stats_messages_sent:4897
cluster_stats_messages_ping_received:2476
cluster_stats_messages_pong_received:2421
cluster_stats_messages_received:4897
3.配置三主三从交叉复制模式

1)获取集群主节点的信息

/opt/redis_6701/redis-cli -h 192.168.238.102 -p 6701 cluster nodes | egrep '6701|6703|6705'  | awk '{print $1,$2}'
ae2dccc6e607c3d0449cc91682017eaa67e3a2da 192.168.238.102:6701@16701
e07b397da862a1507406d8f81aa821cc538e77b7 192.168.238.104:6705@16705
c62b586edff56572750a196a26b1d41c66534d99 192.168.238.103:6703@16703

2)配置交叉复制,登录从库

# 以下配置根据上面的节点信息修改
# 在192.168.238.103上,配置6704节点复制6701主节点,充当6701 master节点的slave
/opt/redis_6704/redis-cli -h 192.168.238.103 -p 6704
127.0.0.1:6704> CLUSTER REPLICATE ae2dccc6e607c3d0449cc91682017eaa67e3a2da
OK

# 在192.168.238.104上,配置6706节点复制6703主节点,充当6703 master节点的slave
/opt/redis_6706/redis-cli -h 192.168.238.104 -p 6706
127.0.0.1:6706> CLUSTER REPLICATE c62b586edff56572750a196a26b1d41c66534d99
OK

# 在192.168.238.102上,配置6702节点复制6705主节点,充当6705 master节点的slave
/opt/redis_6702/redis-cli -h 192.168.238.102 -p 6702
127.0.0.1:6702> CLUSTER REPLICATE e07b397da862a1507406d8f81aa821cc538e77b7
OK

3)查看集群节点信息

已经是三主三从交叉复制集群模式了。

[root@redis-cluster ~]# /opt/redis_6701/redis-cli -h 192.168.238.102 -p 6701
192.168.238.102:6701> CLUSTER NODES
61a1340fdb0f20396def763861394db5a9dd5a4c 192.168.238.103:6704@16704 slave ae2dccc6e607c3d0449cc91682017eaa67e3a2da 0 1671083131000 1 connected
ae2dccc6e607c3d0449cc91682017eaa67e3a2da 192.168.238.102:6701@16701 myself,master - 0 1671083131000 1 connected 0-5461
e07b397da862a1507406d8f81aa821cc538e77b7 192.168.238.104:6705@16705 master - 0 1671083132778 0 connected 10923-16383
c62b586edff56572750a196a26b1d41c66534d99 192.168.238.103:6703@16703 master - 0 1671083131000 2 connected 5462-10922
c63098cb00c40e9767116375f525bc4e97299f50 192.168.238.102:6702@16702 slave e07b397da862a1507406d8f81aa821cc538e77b7 0 1671083131755 0 connected
9c9ff0c21e853606ec9d3dbc731ba916e0b3b01e 192.168.238.104:6706@16706 slave c62b586edff56572750a196a26b1d41c66534d99 0 1671083131000 2 connected

4)测试

#参数后面需要加-c
[root@redis-cluster ~]# /opt/redis_6701/redis-cli -h 192.168.238.102 -p 6701 -c
192.168.238.102:6701> set k1 v1
-> Redirected to slot [12706] located at 192.168.238.104:6705
OK
192.168.238.104:6705> set k2 v2
-> Redirected to slot [449] located at 192.168.238.102:6701
OK
192.168.238.102:6701> set k3 v3
OK
192.168.238.102:6701> set k4 v4
-> Redirected to slot [8455] located at 192.168.238.103:6703
OK
192.168.238.103:6703> get k1
-> Redirected to slot [12706] located at 192.168.238.104:6705
"v1"
192.168.238.104:6705> get k2
-> Redirected to slot [449] located at 192.168.238.102:6701
"v2"
192.168.238.102:6701> get k3
"v3"
192.168.238.102:6701> get k4
-> Redirected to slot [8455] located at 192.168.238.103:6703
"v4"
192.168.238.103:6703> 

三.快速搭建Redis Cluster集群(推荐)

前提条件需要先将6个节点搭建起来,因为我们前面搭建好,执行会报如下错误。

[root@test102 ~]# /opt/redis_6701/redis-cli --cluster create 192.168.238.102:6701 192.168.238.102:6702 192.168.238.103:6703 192.168.238.103:6704 192.168.238.104:6705 192.168.238.104:6706 --cluster-replicas 1
[ERR] Node 192.168.238.102:6701 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
# 确保是测试环境,没有重要的数据,因为前面手动配置,生成了一些配置信息,需要删除
# 如果是新启动的,不需要此操作
# 192.168.238.102上
systemctl stop redis_6701
systemctl stop redis_6702
ps -ef|grep redis
find /opt/redis_670* -type f -name "*.rdb" -o -name "nodes-*"|xargs rm
find /opt/redis_670* -type f -name "*.rdb" -o -name "nodes-*"
systemctl start redis_6701
systemctl start redis_6702
ps -ef|grep redis

# 192.168.238.103上
systemctl stop redis_6703
systemctl stop redis_6704
ps -ef|grep redis
find /opt/redis_670* -type f -name "*.rdb" -o -name "nodes-*"|xargs rm
find /opt/redis_670* -type f -name "*.rdb" -o -name "nodes-*"
systemctl start redis_6703
systemctl start redis_6704
ps -ef|grep redis

# 192.168.238.104上
systemctl stop redis_6705
systemctl stop redis_6706
ps -ef|grep redis
find /opt/redis_670* -type f -name "*.rdb" -o -name "nodes-*"|xargs rm
find /opt/redis_670* -type f -name "*.rdb" -o -name "nodes-*"
systemctl start redis_6705
systemctl start redis_6706
ps -ef|grep redis
#再执行,输入yes执行完成
#--cluster起到的作用就是省掉了节点发现、分配槽位、交叉复制的过程,自动替我们分配。
# 经过测试,不需要手动配置,直接自动配置自动交叉复制
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.238.103:6704 to 192.168.238.102:6701
Adding replica 192.168.238.104:6706 to 192.168.238.103:6703
Adding replica 192.168.238.102:6702 to 192.168.238.104:6705
M: 8b07a03f6a28fb3f8276c8131d87bddf7fdceb92 192.168.238.102:6701
   slots:[0-5460] (5461 slots) master
S: 1f9957009188a54765a81d7a668f8c5a811a3b8d 192.168.238.102:6702
   replicates 6caed29bba52a949c5987704c2cf9a7852e950c2
M: 1e6fd88dcdca08a26f4de397bb91093261b63373 192.168.238.103:6703
   slots:[5461-10922] (5462 slots) master
S: 755fb548c53d06eee95d25888f7a84cf15c5e643 192.168.238.103:6704
   replicates 8b07a03f6a28fb3f8276c8131d87bddf7fdceb92
M: 6caed29bba52a949c5987704c2cf9a7852e950c2 192.168.238.104:6705
   slots:[10923-16383] (5461 slots) master
S: a1b0c9c5d751702ea25d17fa21d463a7ec191143 192.168.238.104:6706
   replicates 1e6fd88dcdca08a26f4de397bb91093261b63373
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.238.102:6701)
M: 8b07a03f6a28fb3f8276c8131d87bddf7fdceb92 192.168.238.102:6701
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1f9957009188a54765a81d7a668f8c5a811a3b8d 192.168.238.102:6702
   slots: (0 slots) slave
   replicates 6caed29bba52a949c5987704c2cf9a7852e950c2
S: a1b0c9c5d751702ea25d17fa21d463a7ec191143 192.168.238.104:6706
   slots: (0 slots) slave
   replicates 1e6fd88dcdca08a26f4de397bb91093261b63373
M: 6caed29bba52a949c5987704c2cf9a7852e950c2 192.168.238.104:6705
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 755fb548c53d06eee95d25888f7a84cf15c5e643 192.168.238.103:6704
   slots: (0 slots) slave
   replicates 8b07a03f6a28fb3f8276c8131d87bddf7fdceb92
M: 1e6fd88dcdca08a26f4de397bb91093261b63373 192.168.238.103:6703
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看集群状态

/opt/redis_6701/redis-cli -h 192.168.238.102 -p 6701
192.168.238.102:6701> cluster info

测试集群读写

#参数后面需要加-c
[root@redis-cluster ~]# /opt/redis_6701/redis-cli -h 192.168.238.102 -p 6701 -c
192.168.238.102:6701> set k1 v1
-> Redirected to slot [12706] located at 192.168.238.104:6705
OK
192.168.238.104:6705> set k2 v2
-> Redirected to slot [449] located at 192.168.238.102:6701
OK
192.168.238.102:6701> set k3 v3
OK
192.168.238.102:6701> set k4 v4
-> Redirected to slot [8455] located at 192.168.238.103:6703
OK
192.168.238.103:6703> get k1
-> Redirected to slot [12706] located at 192.168.238.104:6705
"v1"
192.168.238.104:6705> get k2
-> Redirected to slot [449] located at 192.168.238.102:6701
"v2"
192.168.238.102:6701> get k3
"v3"
192.168.238.102:6701> get k4
-> Redirected to slot [8455] located at 192.168.238.103:6703
"v4"

至此,配置完成。