redis-cli --cluste 集群管理

centos默认的epel仓库中没有redis-trib.rb,而redis5.0以后就开始使用redis-cli --cluster管理集群。

1)解压redis6.0

[root@ydong ~]# tar xf redis-6.0.6.tar.gz 

2)安装编译环境,此处直接全部安装development tools和server platform development。其实不用安装这么多,安装gcc和gcc++就可以

yum groupinstall -y "Develo+pment Tools" "Server Platform Development"

3)由于安装完之后,默认的版本是版本 :4.8.5,而高版本的redis需要高版本的gcc,所以还得继续升级gcc版本

yum install centos-release-scl

yum install devtoolset-7-gcc*

scl enable devtoolset-7 bash

4)编译redis

cd /usr/local/src/redis-6.0.6/
make

5)把命令加入到环境变量中

[root@ydong src]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/src/redis/src

6)启动服务,使用redis-server /PATH/CONFIG_FILE默认是redis.conf

redis-server

7)这里使用redis-cli --cluster来创建集群,首先修改redis.conf文件。3主3从

 bind 0.0.0.0
 cluster-enabled yes
 cluster-config-file nodes-6379.conf
 cluster-node-timeout 15000
 cluster-replica-validity-factor 10
 cluster-migration-barrier 1
 cluster-require-full-coverage yes

8)创建7001、7002、7003、7004、7005文件夹,每个文件夹内放入redis.conf。将里面的port修改成跟文件同名。

[root@ydong ~]# ss -tnl | grep "700"
LISTEN     0      128          *:17001                    *:*                  
LISTEN     0      128          *:17002                    *:*                  
LISTEN     0      128          *:17003                    *:*                  
LISTEN     0      128          *:17004                    *:*                  
LISTEN     0      128          *:17005                    *:*                  
LISTEN     0      128          *:7001                     *:*                  
LISTEN     0      128          *:7002                     *:*                  
LISTEN     0      128          *:7003                     *:*                  
LISTEN     0      128          *:7004                     *:*                  
LISTEN     0      128          *:7005                     *:*         

9)使用redis-cli --cluster来创建集群

[root@ydong ~]# redis-cli --cluster create --cluster-replicas 1 192.168.199.157:6379  192.168.199.157:7004 192.168.199.157:7001 192.168.199.157:7002 192.168.199.157:7003 192.168.199.157:7005
>>> 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.199.157:7003 to 192.168.199.157:6379
Adding replica 192.168.199.157:7005 to 192.168.199.157:7004
Adding replica 192.168.199.157:7002 to 192.168.199.157:7001
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: e2c9b8c3c985fcbdb1eb6815fbbe8cbf4df819a3 192.168.199.157:6379
   slots:[0-5460] (5461 slots) master
M: 3e645db2a11f848d0f727549053f49f77004ed9d 192.168.199.157:7004
   slots:[5461-10922] (5462 slots) master
M: 0f2f36eba63aaf62ab07abbb7e38c1d54405b08f 192.168.199.157:7001
   slots:[10923-16383] (5461 slots) master
S: adf47225a89a1dcb669be2b0b6904ab1a49ee8ab 192.168.199.157:7002
   replicates e2c9b8c3c985fcbdb1eb6815fbbe8cbf4df819a3
S: 990ba160bf0f2c22b41bffe65f2d14eb3361049e 192.168.199.157:7003
   replicates 3e645db2a11f848d0f727549053f49f77004ed9d
S: 778506f84ceedc9873b83536d2094b40a2380c9a 192.168.199.157:7005
   replicates 0f2f36eba63aaf62ab07abbb7e38c1d54405b08f
Can I set the above configuration? (type 'yes' to accept): yes

#自动创建集群,并平均分配slots  replicas 1表示为希望给每一个主节点配置一个从节点。

10)查看集群信息

[root@ydong ~]# redis-cli
127.0.0.1:6379> 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:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:97
cluster_stats_messages_pong_sent:111
cluster_stats_messages_sent:208
cluster_stats_messages_ping_received:106
cluster_stats_messages_pong_received:97
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:208

127.0.0.1:6379> CLUSTER NODES
adf47225a89a1dcb669be2b0b6904ab1a49ee8ab 192.168.199.157:7002@17002 slave e2c9b8c3c985fcbdb1eb6815fbbe8cbf4df819a3 0 1599576610540 1 connected
0f2f36eba63aaf62ab07abbb7e38c1d54405b08f 192.168.199.157:7001@17001 master - 0 1599576613557 3 connected 10923-16383
e2c9b8c3c985fcbdb1eb6815fbbe8cbf4df819a3 192.168.199.157:6379@16379 myself,master - 0 1599576611000 1 connected 0-5460
990ba160bf0f2c22b41bffe65f2d14eb3361049e 192.168.199.157:7003@17003 slave 3e645db2a11f848d0f727549053f49f77004ed9d 0 1599576610000 2 connected
3e645db2a11f848d0f727549053f49f77004ed9d 192.168.199.157:7004@17004 master - 0 1599576612000 2 connected 5461-10922
778506f84ceedc9873b83536d2094b40a2380c9a 192.168.199.157:7005@17005 slave 0f2f36eba63aaf62ab07abbb7e38c1d54405b08f 0 1599576612549 3 connected

11)增加一个节点
需要指明新加入的ip:port和已经存在的任意一个ip:port

[root@ydong ~]# redis-cli --cluster add-node  192.168.199.157:7006  192.168.199.157:7005

12)新加入一个节点就需要重新分片。

[root@ydong ~]# redis-cli --cluster reshard 192.168.199.157:7006  #这个可以随意连接
How many slots do you want to move (from 1 to 16384)? 100  #希望移动多少hash槽
What is the receiving node ID? 296f04ddaad1a56d805ce8d7d898b1bc6c7151fc   #接受的redis ID
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.   #从所有的redis实例中挑出来给你
  Type 'done' once you entered all the source nodes IDs.  #  选择down的话,需要先指明源redis id
Source node #1: all  


13)删除一个节点,在删除的时候,需要保证节点的hash槽是空的,如果不是空的,需要把hash槽移走在删除

[root@ydong ~]# redis-cli --cluster del-node 192.168.199.157:7006 296f04ddaad1a56d805ce8d7d898b1bc6c7151fc
>>> Removing node 296f04ddaad1a56d805ce8d7d898b1bc6c7151fc from cluster 192.168.199.157:7006
[ERR] Node 192.168.199.157:7006 is not empty! Reshard data away and try again.

[root@ydong ~]# redis-cli --cluster reshard 192.168.199.157:7006
How many slots do you want to move (from 1 to 16384)? adf47225a89a1dcb669be2b0b6904ab1a49ee8ab
How many slots do you want to move (from 1 to 16384)? 100
What is the receiving node ID? adf47225a89a1dcb669be2b0b6904ab1a49ee8ab
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: 296f04ddaad1a56d805ce8d7d898b1bc6c7151fc
Source node #2: done

[root@ydong ~]# redis-cli --cluster del-node 192.168.199.157:7006 296f04ddaad1a56d805ce8d7d898b1bc6c7151fc
>>> Removing node 296f04ddaad1a56d805ce8d7d898b1bc6c7151fc from cluster 192.168.199.157:7006
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

你可能感兴趣的:(redis)