下载redis 源码包,编译安装redis
#下载redis
wget http://download.redis.io/releases/redis-5.0.8.tar.gz
#解压并 进入redis解压包中
tar -zxvf redis-5.0.8.tar.gz
cd redis-5.0.8
# 创建redis 目录
mkdir /var/local/redis-cluster/7001 -p
# make 安装redis 二进制服务包
make install PREFIX=/var/local/redis-cluster/7001
修改redis 参数
# 注释到绑定ip参数
#bind 127.0.0.1
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes
protected-mode no
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 7001
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
pidfile /var/run/redis_7001.pid
# output for logging but daemonize, logs will be sent to /dev/null
logfile "/var/local/redis-cluster/7001/logs/redis.log"
复制几个不同的redis 服务节点
cp 7001/ -R 7002
cp 7001/ -R 7003
cp 7001/ -R 7004
cp 7001/ -R 7005
cp 7001/ -R 7006
cp 7001/ -R 7007
cp 7001/ -R 7008
修改复制的服务相应的配置信息
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes
protected-mode no
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port <7002 - 7008>
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
pidfile /var/run/redis_<7002 - 7008>.pid
# output for logging but daemonize, logs will be sent to /dev/null
logfile "/var/local/redis-cluster/<7002-7008>/logs/redis.log"
创建redis 集群
./redis-cli --cluster create 192.168.1.102:7001 192.168.1.102:7002 192.168.1.102:7003 192.168.1.102:7004 192.168.1.102:7005 192.168.1.102:7006 --cluster-replicas 1
客户端命令行模式连接redis 集群
# -c 表示以集群的方式连接
./redis-cli -h 192.168.1.102 -p 7001 -c
查看集群状态
[root@master1 bin]# ./redis-cli -h 192.168.1.102 -p 7001 -c
192.168.1.102:7001> 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:855
cluster_stats_messages_pong_sent:833
cluster_stats_messages_sent:1688
cluster_stats_messages_ping_received:828
cluster_stats_messages_pong_received:855
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1688
查看集群中节点信息
192.168.1.102:7001> cluster nodes
227b6da7e4ba4414f90d3b1eb0085df4a5a84681 192.168.1.102:7006@17006 slave 0d01f85fab5e086afde3eebcd0de39c544dae02c 0 1595870090000 6 connected
0d01f85fab5e086afde3eebcd0de39c544dae02c 192.168.1.102:7003@17003 master - 0 1595870091016 3 connected 10923-16383
a10b150d343122ba0a9676842b4494bedb277276 192.168.1.102:7004@17004 slave 1acc43a0295b08139b7bd29241500c66ba40499a 0 1595870088980 4 connected
24d804068d10070904f46320117e44df7fb98e1e 192.168.1.102:7005@17005 slave 0407753bce270c9c4bea6bc8bc52f033157d234e 0 1595870089000 5 connected
1acc43a0295b08139b7bd29241500c66ba40499a 192.168.1.102:7001@17001 myself,master - 0 1595870089000 1 connected 0-5460
0407753bce270c9c4bea6bc8bc52f033157d234e 192.168.1.102:7002@17002 master - 0 1595870090003 2 connected 5461-10922
增加节点扩容
# 增加节点
./redis-cli --cluster add-node 192.168.1.102:7007 192.168.1.102:7001
# 执行结果
[root@master1 bin]# ./redis-cli --cluster add-node 192.168.1.102:7007 192.168.1.102:7001
>>> Adding node 192.168.1.102:7007 to cluster 192.168.1.102:7001
>>> Performing Cluster Check (using node 192.168.1.102:7001)
M: 1acc43a0295b08139b7bd29241500c66ba40499a 192.168.1.102:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 227b6da7e4ba4414f90d3b1eb0085df4a5a84681 192.168.1.102:7006
slots: (0 slots) slave
replicates 0d01f85fab5e086afde3eebcd0de39c544dae02c
M: 0d01f85fab5e086afde3eebcd0de39c544dae02c 192.168.1.102:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: a10b150d343122ba0a9676842b4494bedb277276 192.168.1.102:7004
slots: (0 slots) slave
replicates 1acc43a0295b08139b7bd29241500c66ba40499a
S: 24d804068d10070904f46320117e44df7fb98e1e 192.168.1.102:7005
slots: (0 slots) slave
replicates 0407753bce270c9c4bea6bc8bc52f033157d234e
M: 0407753bce270c9c4bea6bc8bc52f033157d234e 192.168.1.102:7002
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.
>>> Send CLUSTER MEET to node 192.16
为新增节点分配 槽点(slot) rehash
# 为新节点分配槽点
./redis-cli --cluster reshard 192.168.1.102:7007
为新增master 节点添加从节点
# 执行命令添加从节点
./redis-cli --cluster add-node 192.168.1.102:7008 192.168.1.102:7007 --cluster-slave --cluster-master-id 7641497e971067b5226d06cb33a33a5e94398078
[root@master1 bin]# ./redis-cli --cluster add-node 192.168.1.102:7008 192.168.1.102:7007 --cluster-slave --cluster-master-id 7641497e971067b5226d06cb33a33a5e94398078
>>> Adding node 192.168.1.102:7008 to cluster 192.168.1.102:7007
>>> Performing Cluster Check (using node 192.168.1.102:7007)
M: 7641497e971067b5226d06cb33a33a5e94398078 192.168.1.102:7007
slots:[0-1332],[5461-6794],[10923-12255] (4000 slots) master
S: 24d804068d10070904f46320117e44df7fb98e1e 192.168.1.102:7005
slots: (0 slots) slave
replicates 0407753bce270c9c4bea6bc8bc52f033157d234e
M: 0407753bce270c9c4bea6bc8bc52f033157d234e 192.168.1.102:7002
slots:[6795-10922] (4128 slots) master
1 additional replica(s)
M: 0d01f85fab5e086afde3eebcd0de39c544dae02c 192.168.1.102:7003
slots:[12256-16383] (4128 slots) master
1 additional replica(s)
S: 227b6da7e4ba4414f90d3b1eb0085df4a5a84681 192.168.1.102:7006
slots: (0 slots) slave
replicates 0d01f85fab5e086afde3eebcd0de39c544dae02c
M: 1acc43a0295b08139b7bd29241500c66ba40499a 192.168.1.102:7001
slots:[1333-5460] (4128 slots) master
1 additional replica(s)
S: a10b150d343122ba0a9676842b4494bedb277276 192.168.1.102:7004
slots: (0 slots) slave
replicates 1acc43a0295b08139b7bd29241500c66ba40499a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.1.102:7008 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.1.102:7007.
[OK] New node added correctly.
缩容测试
# 执行缩容命令
./redis-cli --cluster del-node 192.168.1.102:7008 3104e8001e93f9117b3a5421e020ee9753b4677b
[root@master1 bin]# ./redis-cli --cluster del-node 192.168.1.102:7008 3104e8001e93f9117b3a5421e020ee9753b4677b
>>> Removing node 3104e8001e93f9117b3a5421e020ee9753b4677b from cluster 192.168.1.102:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.