redis-cluster 三主三从环境搭建

环境说明

linux系统:centos 7.5

redis版本:redis-5.0.2

1、三主三从集群搭建

第一步: 各节点说明

RedisCluster最少需要三台主服务器,三台从服务器,效果图如下;

主节点对应点的端口号:7001、7002、7003;

从节点对应点的端口号:7004、7005、7006;

第二步: 集群搭建

1:创建7001实例:

  1. 创建安装目录:
 /home/wyh/data/redis/redis-cluster/7001
  1. 安装redis
    make install PREFIX=/home/wyh/data/redis/redis-cluster/7007
  2. 拷贝redis.conf配置文件到7001/bin目录下
  3. 修改redis.conf配置文件
    #注释掉
    #bind 127.0.0.1
    #是否开启保护模式,由yes该为no
    protected-mode no
    #端口改为7001
    port 7001
    #将`daemonize`由`no`改为`yes`
    daemonize yes
    #集群模式取消注释,改为yes
    cluster-enabled yes

2:复制7001到7002~7006实例:

  1. 拷贝实例:
    [root@vm redis-cluster]# cp -r 7001 7002
  2. 分别修改每一个实例中redis.conf端口
    [root@vm redis-cluster]# vi 7002/bin/redis.conf 

3:创建start.sh,启动所有的实例:

[root@vm redis-cluster]# cat start.sh 
cd 7001/bin
./redis-server redis.conf
cd ..
cd ..

cd 7002/bin
./redis-server redis.conf
cd ..
cd ..

cd 7003/bin
./redis-server redis.conf
cd ..
cd ..

cd 7004/bin
./redis-server redis.conf
cd ..
cd ..

cd 7005/bin
./redis-server redis.conf
cd ..
cd ..

cd 7006/bin
./redis-server redis.conf
cd ..
cd ..
[root@vm redis-cluster]#

1.赋写和执行的权限

[root@vm redis-cluster]# chmod 777 start.sh

2.启动:

[root@vm redis-cluster]# ./start.sh

4:创建Redis集群(redis里不要有数据):

1.创建集群:前三个ip加端口为master机器(此处将命令中127.0.0.1改为真是ip:192.168.44.3)

 ./redis-cli --cluster create 192.168.44.3:7001 192.168.44.3:7002 192.168.44.3:7003 192.168.44.3:7004 192.168.44.3:7005 192.168.44.3:7006 --cluster-replicas 1

image-20210327221009095

2.命令客户端连接集群:-c 表示是以redis集群方式进行连接

./redis-cli -h 127.0.0.1 -p 7001 -c

3.设置key值查看集群情况:

127.0.0.1:7001> set name:1 zhangfei
-> Redirected to slot [12501] located at 192.168.44.3:7003
OK
192.168.44.3:7003>

4.查看redis集群状态:

192.168.44.3:7003> 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:3
cluster_stats_messages_ping_sent:229
cluster_stats_messages_pong_sent:209
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:440
cluster_stats_messages_ping_received:205
cluster_stats_messages_pong_received:231
cluster_stats_messages_meet_received:4
cluster_stats_messages_received:440
192.168.44.3:7003>

5.查看redis节点状态:

192.168.44.3:7003> cluster nodes
97c787d5af725d9df037d5650a913b4146a04cdc 192.168.44.3:7001@17001 master - 0 1616854424000 1 connected 0-5460
6c321e199dfa3d3a1762b540a39a11e216fbab23 192.168.44.3:7004@17004 slave b5404e978246a487f07a3a72d0be3a40e06915ca 0 1616854424427 4 connected
167891b8e4c36ac74260b1f9407f6005ac43cc89 192.168.44.3:7005@17005 slave 97c787d5af725d9df037d5650a913b4146a04cdc 0 1616854424000 5 connected
86ea5943bc4a76eba149fa8ad7b8ad7fe222baa4 192.168.44.3:7006@17006 slave c294d34c5c17ca878053a60ea72d9e95c30f5bfb 0 1616854425441 6 connected
b5404e978246a487f07a3a72d0be3a40e06915ca 192.168.44.3:7003@17003 myself,master - 0 1616854421000 3 connected 10923-16383
c294d34c5c17ca878053a60ea72d9e95c30f5bfb 192.168.44.3:7002@17002 master - 0 1616854423000 2 connected 5461-10922
192.168.44.3:7003>

2、添加一主一从

第一步:创建7007实例:

1:安装redis

[root@vm src]# make install PREFIX=/home/wyh/data/redis/redis-cluster/7007
  CC Makefile.dep

Hint: It's a good idea to run 'make test' ;)

  INSTALL install
  INSTALL install
  INSTALL install
  INSTALL install
  INSTALL install
[root@vm src]#

2:拷贝7001下redis.conf配置文件到7007/bin目录下

[root@vm redis-cluster]# cp /home/wyh/data/redis/redis-cluster/7001/bin/redis.conf /home/wyh/data/redis/redis-cluster/7007/bin/

3:修改redis.conf配置文件端口号

[root@vm redis-cluster]# vi 7007/bin/redis.conf 

第二步:创建7008实例:

1:复制7007到7008

[root@vm redis-cluster]# cp -r 7007 7008

2:修改redis.conf配置文件端口号

[root@vm redis-cluster]# vi 7008/bin/redis.conf 

第三步:添加7007结点作为新节点,并启动:

1:启动7007

[root@vm bin]# ./redis-server redis.conf 
8267:C 27 Mar 2021 22:22:15.036 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8267:C 27 Mar 2021 22:22:15.036 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=8267, just started
8267:C 27 Mar 2021 22:22:15.036 # Configuration loaded
[root@vm bin]# ps -ef|grep redis
root       7717      1  0 22:05 ?        00:00:03 ./redis-server *:7001 [cluster]
root       7722      1  0 22:05 ?        00:00:02 ./redis-server *:7002 [cluster]
root       7727      1  0 22:05 ?        00:00:02 ./redis-server *:7003 [cluster]
root       7732      1  0 22:05 ?        00:00:02 ./redis-server *:7004 [cluster]
root       7737      1  0 22:05 ?        00:00:02 ./redis-server *:7005 [cluster]
root       7742      1  0 22:05 ?        00:00:02 ./redis-server *:7006 [cluster]
root       8268      1  0 22:22 ?        00:00:00 ./redis-server *:7007 [cluster]
root       8273   3137  0 22:22 pts/0    00:00:00 grep --color=auto redis
[root@vm bin]#

2:7001目录下添加新节点

[root@vm bin]# pwd
/home/wyh/data/redis/redis-cluster/7001/bin
[root@vm bin]# ./redis-cli --cluster add-node 192.168.44.3:7007 192.168.44.3:7001
>>> Adding node 192.168.44.3:7007 to cluster 192.168.44.3:7001
>>> Performing Cluster Check (using node 192.168.44.3:7001)
M: 97c787d5af725d9df037d5650a913b4146a04cdc 192.168.44.3:7001
  slots:[0-5460] (5461 slots) master
  1 additional replica(s)
S: 167891b8e4c36ac74260b1f9407f6005ac43cc89 192.168.44.3:7005
  slots: (0 slots) slave
  replicates 97c787d5af725d9df037d5650a913b4146a04cdc
M: c294d34c5c17ca878053a60ea72d9e95c30f5bfb 192.168.44.3:7002
  slots:[5461-10922] (5462 slots) master
  1 additional replica(s)
S: 6c321e199dfa3d3a1762b540a39a11e216fbab23 192.168.44.3:7004
  slots: (0 slots) slave
  replicates b5404e978246a487f07a3a72d0be3a40e06915ca
M: b5404e978246a487f07a3a72d0be3a40e06915ca 192.168.44.3:7003
  slots:[10923-16383] (5461 slots) master
  1 additional replica(s)
S: 86ea5943bc4a76eba149fa8ad7b8ad7fe222baa4 192.168.44.3:7006
  slots: (0 slots) slave
  replicates c294d34c5c17ca878053a60ea72d9e95c30f5bfb
[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.44.3:7007 to make it join the cluster.
[OK] New node added correctly.
[root@vm bin]#

3:查看集群结点发现7007已添加到集群中

[root@vm bin]# ./redis-cli -p 7001 -c
127.0.0.1:7001> cluster nodes
167891b8e4c36ac74260b1f9407f6005ac43cc89 192.168.44.3:7005@17005 slave 97c787d5af725d9df037d5650a913b4146a04cdc 0 1616855138000 5 connected
97c787d5af725d9df037d5650a913b4146a04cdc 192.168.44.3:7001@17001 myself,master - 0 1616855137000 1 connected 0-5460
c294d34c5c17ca878053a60ea72d9e95c30f5bfb 192.168.44.3:7002@17002 master - 0 1616855138000 2 connected 5461-10922
6c321e199dfa3d3a1762b540a39a11e216fbab23 192.168.44.3:7004@17004 slave b5404e978246a487f07a3a72d0be3a40e06915ca 0 1616855140780 4 connected
b5404e978246a487f07a3a72d0be3a40e06915ca 192.168.44.3:7003@17003 master - 0 1616855139770 3 connected 10923-16383
86ea5943bc4a76eba149fa8ad7b8ad7fe222baa4 192.168.44.3:7006@17006 slave c294d34c5c17ca878053a60ea72d9e95c30f5bfb 0 1616855139000 6 connected
86a7a7222e9956f009c0e7303a133d40039230ed 192.168.44.3:7007@17007 master - 0 1616855139000 0 connected
127.0.0.1:7001>

第三步:hash槽重新分配(数据迁移)

1:给刚添加的7007结点分配槽

./redis-cli --cluster reshard 192.168.44.3:7007

2:输入要分配的槽数量3000个

How many slots do you want to move (from 1 to 16384)? 3000

3:输入接收槽的结点id,输入cluster nodes查看7007结点id

What is the receiving node ID?

4:输入源结点id类型输入:all

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.

5:输入yes开始移动槽到目标结点id

Moving slot 991 from 192.168.44.3:7001 to 192.168.44.3:7007: 
Moving slot 992 from 192.168.44.3:7001 to 192.168.44.3:7007:
Moving slot 993 from 192.168.44.3:7001 to 192.168.44.3:7007:
Moving slot 994 from 192.168.44.3:7001 to 192.168.44.3:7007:
Moving slot 995 from 192.168.44.3:7001 to 192.168.44.3:7007:
Moving slot 996 from 192.168.44.3:7001 to 192.168.44.3:7007:
Moving slot 997 from 192.168.44.3:7001 to 192.168.44.3:7007:
Moving slot 998 from 192.168.44.3:7001 to 192.168.44.3:7007:
[root@vm bin]#

6:查看结果

127.0.0.1:7001> cluster nodes
167891b8e4c36ac74260b1f9407f6005ac43cc89 192.168.44.3:7005@17005 slave 97c787d5af725d9df037d5650a913b4146a04cdc 0 1616855466000 5 connected
97c787d5af725d9df037d5650a913b4146a04cdc 192.168.44.3:7001@17001 myself,master - 0 1616855465000 1 connected 999-5460
c294d34c5c17ca878053a60ea72d9e95c30f5bfb 192.168.44.3:7002@17002 master - 0 1616855466000 2 connected 6462-10922
6c321e199dfa3d3a1762b540a39a11e216fbab23 192.168.44.3:7004@17004 slave b5404e978246a487f07a3a72d0be3a40e06915ca 0 1616855467275 4 connected
b5404e978246a487f07a3a72d0be3a40e06915ca 192.168.44.3:7003@17003 master - 0 1616855469000 3 connected 11922-16383
86ea5943bc4a76eba149fa8ad7b8ad7fe222baa4 192.168.44.3:7006@17006 slave c294d34c5c17ca878053a60ea72d9e95c30f5bfb 0 1616855468284 6 connected
86a7a7222e9956f009c0e7303a133d40039230ed 192.168.44.3:7007@17007 master - 0 1616855469295 7 connected 0-998 5461-6461 10923-11921
127.0.0.1:7001>

第四步:添加从节点7008

1:启动7008

[root@vm bin]# pwd
/home/wyh/data/redis/redis-cluster/7008/bin
[root@vm bin]# ./redis-server redis.conf
8395:C 27 Mar 2021 22:32:22.365 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8395:C 27 Mar 2021 22:32:22.365 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=8395, just started
8395:C 27 Mar 2021 22:32:22.365 # Configuration loaded
[root@vm bin]# ps -ef|grep redis
root       7717      1  0 22:05 ?        00:00:06 ./redis-server *:7001 [cluster]
root       7722      1  0 22:05 ?        00:00:05 ./redis-server *:7002 [cluster]
root       7727      1  0 22:05 ?        00:00:05 ./redis-server *:7003 [cluster]
root       7732      1  0 22:05 ?        00:00:04 ./redis-server *:7004 [cluster]
root       7737      1  0 22:05 ?        00:00:04 ./redis-server *:7005 [cluster]
root       7742      1  0 22:05 ?        00:00:04 ./redis-server *:7006 [cluster]
root       8268      1  0 22:22 ?        00:00:03 ./redis-server *:7007 [cluster]
root       8396      1  0 22:32 ?        00:00:00 ./redis-server *:7008 [cluster]
root       8401   3137  0 22:32 pts/0    00:00:00 grep --color=auto redis
[root@vm bin]#

2:将7008作为7007从节点

[root@vm bin]# pwd
/home/wyh/data/redis/redis-cluster/7001/bin
[root@vm bin]# ./redis-cli --cluster add-node 192.168.44.3:7008 192.168.44.3:7007 --cluster-slave --cluster-master-id 86a7a7222e9956f009c0e7303a133d40039230ed
>>> Adding node 192.168.44.3:7008 to cluster 192.168.44.3:7007
>>> Performing Cluster Check (using node 192.168.44.3:7007)
M: 86a7a7222e9956f009c0e7303a133d40039230ed 192.168.44.3:7007
  slots:[0-998],[5461-6461],[10923-11921] (2999 slots) master
M: b5404e978246a487f07a3a72d0be3a40e06915ca 192.168.44.3:7003
  slots:[11922-16383] (4462 slots) master
  1 additional replica(s)
M: 97c787d5af725d9df037d5650a913b4146a04cdc 192.168.44.3:7001
  slots:[999-5460] (4462 slots) master
  1 additional replica(s)
M: c294d34c5c17ca878053a60ea72d9e95c30f5bfb 192.168.44.3:7002
  slots:[6462-10922] (4461 slots) master
  1 additional replica(s)
S: 86ea5943bc4a76eba149fa8ad7b8ad7fe222baa4 192.168.44.3:7006
  slots: (0 slots) slave
  replicates c294d34c5c17ca878053a60ea72d9e95c30f5bfb
S: 167891b8e4c36ac74260b1f9407f6005ac43cc89 192.168.44.3:7005
  slots: (0 slots) slave
  replicates 97c787d5af725d9df037d5650a913b4146a04cdc
S: 6c321e199dfa3d3a1762b540a39a11e216fbab23 192.168.44.3:7004
  slots: (0 slots) slave
  replicates b5404e978246a487f07a3a72d0be3a40e06915ca
[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.44.3:7008 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.44.3:7007.
[OK] New node added correctly.
[root@vm bin]#

3:节点展示:

[root@vm bin]# cat nodes.conf
167891b8e4c36ac74260b1f9407f6005ac43cc89 192.168.44.3:7005@17005 slave 97c787d5af725d9df037d5650a913b4146a04cdc 0 1616856382000 5 connected
97c787d5af725d9df037d5650a913b4146a04cdc 192.168.44.3:7001@17001 myself,master - 0 1616856380000 1 connected 999-5460
75f8e6385ed05f52ce91228e791ff5018e6316b1 192.168.44.3:7008@17008 slave 86a7a7222e9956f009c0e7303a133d40039230ed 0 1616856380000 7 connected
c294d34c5c17ca878053a60ea72d9e95c30f5bfb 192.168.44.3:7002@17002 master - 0 1616856381000 2 connected 6462-10922
6c321e199dfa3d3a1762b540a39a11e216fbab23 192.168.44.3:7004@17004 slave b5404e978246a487f07a3a72d0be3a40e06915ca 0 1616856383000 4 connected
b5404e978246a487f07a3a72d0be3a40e06915ca 192.168.44.3:7003@17003 master - 0 1616856384428 3 connected 11922-16383
86ea5943bc4a76eba149fa8ad7b8ad7fe222baa4 192.168.44.3:7006@17006 slave c294d34c5c17ca878053a60ea72d9e95c30f5bfb 0 1616856382000 6 connected
86a7a7222e9956f009c0e7303a133d40039230ed 192.168.44.3:7007@17007 master - 0 1616856383421 7 connected 0-998 5461-6461 10923-11921
vars currentEpoch 7 lastVoteEpoch 0
[root@vm bin]#

 

posted @ 2021-06-02 23:07  alwaysFly  阅读(404)  评论(0编辑  收藏  举报