2021-09-16

本文详细介绍了如何在三台服务器上搭建Redis集群,包括从单机安装、配置一主一从,到创建集群,再到集群的扩容和缩容操作。整个过程涵盖了Redis的下载、编译、配置、启动、集群创建、节点添加、槽位分配和删除等关键步骤,对于理解Redis集群管理和运维具有指导意义。

准备

三台服务器,每台启动两个节点,搭建一主一从集群:

192.168.3.4

192.168.3.5

192.168.3.6

1

2

3

连接服务器192.168.3.4,下载最新版redis 6.2.1

cd /home/soft/download-package/

wget https://download.redis.io/releases/redis-6.2.1.tar.gz

1

2

复制到其他机器上

scp redis-6.2.1.tar.gz root@192.168.3.5:/home/soft/download-package/

scp redis-6.2.1.tar.gz root@192.168.3.6:/home/soft/download-package/

1

2

ps:scp命令,复制本地文件到其它服务器上

解压

tar -zxvf redis-6.2.1.tar.gz

mv redis-6.2.1 /home/soft

1

2

编译

cd /home/soft/redis-6.2.1

make

1

2

如果编译报错,先安装gcc编译器

yum install gcc-c++ -y

1

安装gcc成功后清除之前编译的信息,重新编译

make distclean

make

1

2

编译成功后,执行安装redis命令(PREFIX,指定可执行文件存放目录)

make install PREFIX=/home/soft/redis-6.2.1

1

测试启动

cd /home/soft/redis-6.2.1

./bin/redis-server redis.conf

1

2

显示

Ready to accept connections

1

表示启动成功,简单单机版安装成功(192.168.3.5、192.168.3.6也按以上步骤安装)

搭建

在192.168.3.4服务器上建立集群文件夹,存放集群节点文件

cd /home/soft/redis-6.2.1

mkdir cluster

1

2

建立节点文件夹 7001、7002(192.168.3.5建立7003、7004,192.168.3.6建立7005、7006)

cd cluster

mkdir 7001

mkdir 7002

1

2

3

复制配置文件到7001

mv redis.conf cluster/7001

1

修改配置文件

# 指定只能以192.168.3.4这个IP访问

bind 192.168.3.4

# 指定端口

port 7001

# 启动后台运行

daemonize yes

# 存放进程Id

pidfile /home/soft/redis-6.2.1/cluster/7001/7001.pid

# 日志文件

logfile "7001.log"

# 3600秒内有一个key变化就执行快照持久化

save 3600 1

# 快照存放文件

dbfilename 7001.rdb

# 工作目录

dir /home/soft/redis-6.2.1/cluster/7001

# 是否启动保存操作日志

appendonly yes

# 操作日志存放文件

appendfilename "7001.aof"

# 是否开启集群

cluster-enabled yes

# 集群配置文件(启动后自动生成)

cluster-config-file nodes-7001.conf

# 集群节点ping、pong超时时间

cluster-node-timeout 5000

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

启动

../../bin/redis-server redis.conf

1

查看进程

ps -ef| grep redis

root       1159      1  0 21:35 ?        00:00:00 ../../bin/redis-server 192.168.3.5:7003 [cluster]

1

2

3

生成7002配置文件

sed 's/7001/7002/g' redis.conf > ../7002/redis.conf

1

ps:sed命令,替换原文件中的内容,另存为新的文件,不改变原文件

启动

../../bin/redis-server redis.conf

1

查看进程

ps -ef| grep redis

root       1230      1  0 21:35 ?        00:02:32 ../../bin/redis-server 192.168.3.4:7001 [cluster]

root       1293      1  0 21:37 ?        00:00:02 ../../bin/redis-server 192.168.3.4:7002 [cluster]

1

2

3

4

复制7002配置文件到192.168.3.5服务器上

scp redis.conf root@192.168.3.5:/home/soft/redis-6.2.1/cluster/7003

1

修改192.168.3.5上7003的配置文件

cd cluster/7003

vim redis.conf

bind 192.168.3.5

1

2

3

替换配置文件中的7002为7003

:%s/7002/7003/g

1

生成7004配置文件

sed 's/7003/7004/g' redis.conf > ../7004/redis.conf

1

启动

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

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

1

2

查看进程

ps -ef| grep redis

root       1159      1  0 22:19 ?        00:00:00 ../../bin/redis-server 192.168.3.5:7003 [cluster]

root       1205      1  0 22:27 ?        00:00:00 ../../bin/redis-server 192.168.3.5:7004 [cluster]

1

2

3

4

复制7002配置文件到192.168.3.6服务器上

scp redis.conf root@192.168.3.6:/home/soft/redis-6.2.1/cluster/7005

1

修改配置文件

cd cluster/7005

vim redis.conf

bind 192.168.3.6

1

2

3

替换配置文件中的7002为7005

:%s/7002/7005/g

1

生成7006配置文件

sed 's/7005/7006/g' redis.conf > ../7006/redis.conf

1

启动

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

../../bin/redis-server ../7006/redis.conf

1

2

查看进程

ps -ef| grep redis

root       1172      1  0 22:43 ?        00:00:00 ../../bin/redis-server 192.168.3.6:7005 [cluster]

root       1178      1  0 22:43 ?        00:00:00 ../../bin/redis-server 192.168.3.6:7006 [cluster]

1

2

3

4

使用redis-cli --cluster create 命令创建集群(将节点加入集群,指定节点主从关系,同时执行均匀分槽,将16384个槽位分配给3个主节点)

./bin/redis-cli --cluster create 192.168.3.4:7001 192.168.3.4:7002 192.168.3.5:7003 192.168.3.5:7004 192.168.3.6:7005 192.168.3.6:7006 --cluster-replicas 1

1

ps:最后这个 --cluster-replicas 1,表示每个主节点一个备份节点,即一主一从

查看集群节点信息

./bin/redis-cli -h 192.168.3.4 -p 7001 cluster nodes

1

可以看到集群创建成功

294dedb58f99c192c33c9c395df1a39cbbc355e7 192.168.3.5:7003@17003 master - 0 1614953197000 3 connected 5461-10922

4872d8388cf6c68cf3246fd6017c9eefe9290cc4 192.168.3.6:7006@17006 slave 294dedb58f99c192c33c9c395df1a39cbbc355e7 0 1614953196257 3 connected

b33d2fd2845c6863eb7261395a6dc73af1372bae 192.168.3.5:7004@17004 slave d0710306539aaf338255be387610902ac8a01959 0 1614953196000 1 connected

d0710306539aaf338255be387610902ac8a01959 192.168.3.4:7001@17001 myself,master - 0 1614953196000 1 connected 0-5460

a502ed92ed0b4919460cd8bc9bead0869060a710 192.168.3.4:7002@17002 slave 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 0 1614953196000 5 connected

63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 192.168.3.6:7005@17005 master - 0 1614953197273 5 connected 10923-16383

1

2

3

4

5

6

集群扩容

新建7007、7008节点,加入集群,指定7007为主,7008为从,并为7007分配384个槽位

在192.168.3.6服务器器上新节点文件夹7007、7008

cd cluster

mkdir 7007

mkdir 7008

1

2

3

生成7007、7008配置文件

sed 's/7005/7007/g' 7005/redis.conf > 7007/redis.conf

sed 's/7005/7008/g' 7005/redis.conf > 7008/redis.conf

1

2

启动7007、7008节点

../bin/redis-server 7007/redis.conf

../bin/redis-server 7008/redis.conf

1

2

查看进程

ps -ef| grep redis

root       1070      1  0 21:53 ?        00:00:10 ./bin/redis-server 192.168.3.6:7005 [cluster]

root       1076      1  0 21:53 ?        00:00:10 ./bin/redis-server 192.168.3.6:7006 [cluster]

root       1138      1  0 22:56 ?        00:00:00 ../bin/redis-server 192.168.3.6:7007 [cluster]

root       1144      1  0 22:56 ?        00:00:00 ../bin/redis-server 192.168.3.6:7008 [cluster]

1

2

3

4

5

6

将新节点(192.168.3.6:7007)加入已有集群(192.168.3.6:7005)

../bin/redis-cli -h 192.168.3.6 -p 7005 --cluster add-node 192.168.3.6:7007 192.168.3.6:7005

1

ps:…/bin/redis-cli -h 192.168.3.6 -p 7005 --cluster help,help可以帮助查看命令相关信息

查看新节点(192.168.3.6:7007)集群Id(默认为master节点)

../bin/redis-cli -h 192.168.3.6 -p 7005 cluster nodes

294dedb58f99c192c33c9c395df1a39cbbc355e7 192.168.3.5:7003@17003 master - 0 1614956582337 3 connected 5461-10922

b33d2fd2845c6863eb7261395a6dc73af1372bae 192.168.3.5:7004@17004 slave d0710306539aaf338255be387610902ac8a01959 0 1614956583355 1 connected

a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b 192.168.3.6:7007@17007 master - 0 1614956582134 0 connected

63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 192.168.3.6:7005@17005 myself,master - 0 1614956579000 5 connected 10923-16383

4872d8388cf6c68cf3246fd6017c9eefe9290cc4 192.168.3.6:7006@17006 slave 294dedb58f99c192c33c9c395df1a39cbbc355e7 0 1614956582000 3 connected

a502ed92ed0b4919460cd8bc9bead0869060a710 192.168.3.4:7002@17002 slave 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 0 1614956581319 5 connected

d0710306539aaf338255be387610902ac8a01959 192.168.3.4:7001@17001 master - 0 1614956581000 1 connected 0-5460

1

2

3

4

5

6

7

8

9

将新节点(192.168.3.6:7008)作为从节点加入集群(–cluster-slave),指定master节点集群Id(–cluster-master-id)

../bin/redis-cli -h 192.168.3.6 -p 7005 --cluster add-node 192.168.3.6:7008 192.168.3.6:7005 --cluster-slave --cluster-master-id a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b

1

查看集群信息

../bin/redis-cli -h 192.168.3.6 -p 7005 cluster nodes

294dedb58f99c192c33c9c395df1a39cbbc355e7 192.168.3.5:7003@17003 master - 0 1614956702513 3 connected 5461-10922

b33d2fd2845c6863eb7261395a6dc73af1372bae 192.168.3.5:7004@17004 slave d0710306539aaf338255be387610902ac8a01959 0 1614956703019 1 connected

a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b 192.168.3.6:7007@17007 master - 0 1614956702000 0 connected

63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 192.168.3.6:7005@17005 myself,master - 0 1614956700000 5 connected 10923-16383

1de56b252c703cd8fec6356d30088f188a3537b7 192.168.3.6:7008@17008 slave a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b 0 1614956702513 0 connected

4872d8388cf6c68cf3246fd6017c9eefe9290cc4 192.168.3.6:7006@17006 slave 294dedb58f99c192c33c9c395df1a39cbbc355e7 0 1614956701594 3 connected

a502ed92ed0b4919460cd8bc9bead0869060a710 192.168.3.4:7002@17002 slave 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 0 1614956703525 5 connected

d0710306539aaf338255be387610902ac8a01959 192.168.3.4:7001@17001 master - 0 1614956702000 1 connected 0-5460

1

2

3

4

5

6

7

8

9

10

给新master节点(192.168.3.6:7007)分配槽位

../bin/redis-cli -h 192.168.3.6 -p 7005 --cluster reshard 192.168.3.6:7007

1

分配多少槽位

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

1

分配给哪个节点(7007节点集群Id)

What is the receiving node ID? a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b

1

从哪个节点分割槽位给新节点,可以多个,输入done表示结束(7005节点集群Id:63249b39e5fade6fa6f17b6158ccbc1f9f0996ea)

Source node #1: 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea

Source node #2: done

1

2

接着输入yes确认分配

Do you want to proceed with the proposed reshard plan (yes/no)? yes

1

查看集群信息,分配完成

../bin/redis-cli -h 192.168.3.6 -p 7005 cluster nodes

294dedb58f99c192c33c9c395df1a39cbbc355e7 192.168.3.5:7003@17003 master - 0 1614957610000 3 connected 5461-10922

b33d2fd2845c6863eb7261395a6dc73af1372bae 192.168.3.5:7004@17004 slave d0710306539aaf338255be387610902ac8a01959 0 1614957609150 1 connected

a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b 192.168.3.6:7007@17007 master - 0 1614957610164 8 connected 10923-11306

63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 192.168.3.6:7005@17005 myself,master - 0 1614957610000 5 connected 11307-16383

1de56b252c703cd8fec6356d30088f188a3537b7 192.168.3.6:7008@17008 slave a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b 0 1614957610062 8 connected

4872d8388cf6c68cf3246fd6017c9eefe9290cc4 192.168.3.6:7006@17006 slave 294dedb58f99c192c33c9c395df1a39cbbc355e7 0 1614957611084 3 connected

a502ed92ed0b4919460cd8bc9bead0869060a710 192.168.3.4:7002@17002 slave 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 0 1614957610000 5 connected

d0710306539aaf338255be387610902ac8a01959 192.168.3.4:7001@17001 master - 0 1614957611187 1 connected 0-5460

1

2

3

4

5

6

7

8

9

10

集群缩容

归还7007节点槽位,移除7007、7008节点

将7007节点(–cluster-from a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b)的384个槽位(–cluster-slots 384 192.168.3.6:7007)重新分配给7005节点(–cluster-to 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea)

../bin/redis-cli -h 192.168.3.6 -p 7005 --cluster reshard --cluster-from a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b --cluster-to 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea --cluster-slots 384 192.168.3.6:7007

1

输入yes确认

Do you want to proceed with the proposed reshard plan (yes/no)? yes

1

查看集群信息

../bin/redis-cli -h 192.168.3.6 -p 7005 cluster nodes

294dedb58f99c192c33c9c395df1a39cbbc355e7 192.168.3.5:7003@17003 master - 0 1614958471327 3 connected 5461-10922

b33d2fd2845c6863eb7261395a6dc73af1372bae 192.168.3.5:7004@17004 slave d0710306539aaf338255be387610902ac8a01959 0 1614958471000 1 connected

a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b 192.168.3.6:7007@17007 master - 0 1614958471966 8 connected

63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 192.168.3.6:7005@17005 myself,master - 0 1614958469000 9 connected 10923-16383

1de56b252c703cd8fec6356d30088f188a3537b7 192.168.3.6:7008@17008 slave 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 0 1614958471024 9 connected

4872d8388cf6c68cf3246fd6017c9eefe9290cc4 192.168.3.6:7006@17006 slave 294dedb58f99c192c33c9c395df1a39cbbc355e7 0 1614958470521 3 connected

a502ed92ed0b4919460cd8bc9bead0869060a710 192.168.3.4:7002@17002 slave 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 0 1614958470522 9 connected

d0710306539aaf338255be387610902ac8a01959 192.168.3.4:7001@17001 master - 0 1614958470824 1 connected 0-5460

1

2

3

4

5

6

7

8

9

10

将7007、7008节点移出集群

../bin/redis-cli --cluster del-node 192.168.3.6:7005 1de56b252c703cd8fec6356d30088f188a3537b7

../bin/redis-cli --cluster del-node 192.168.3.6:7005 a4e969f80ca9634e83cc0cc6e8ea1a4689dcba5b

再次查看集群信息

../bin/redis-cli -h 192.168.3.6 -p 7005 cluster nodes

294dedb58f99c192c33c9c395df1a39cbbc355e7 192.168.3.5:7003@17003 master - 0 1614958697961 3 connected 5461-10922

b33d2fd2845c6863eb7261395a6dc73af1372bae 192.168.3.5:7004@17004 slave d0710306539aaf338255be387610902ac8a01959 0 1614958697459 1 connected

63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 192.168.3.6:7005@17005 myself,master - 0 1614958697000 9 connected 10923-16383

4872d8388cf6c68cf3246fd6017c9eefe9290cc4 192.168.3.6:7006@17006 slave 294dedb58f99c192c33c9c395df1a39cbbc355e7 0 1614958697054 3 connected

a502ed92ed0b4919460cd8bc9bead0869060a710 192.168.3.4:7002@17002 slave 63249b39e5fade6fa6f17b6158ccbc1f9f0996ea 0 1614958697560 9 connected

d0710306539aaf338255be387610902ac8a01959 192.168.3.4:7001@17001 master - 0 1614958696955 1 connected 0-5460

关闭7007、7008节点

../bin/redis-cli -h 192.168.3.6 -p 7007 shutdown

../bin/redis-cli -h 192.168.3.6 -p 7008 shutdown

再次查看进程

ps -ef| grep redis

root       1070      1  0 21:53 ?        00:00:19 ./bin/redis-server 192.168.3.6:7005 [cluster]

root       1076      1  0 21:53 ?        00:00:19 ./bin/redis-server 192.168.3.6:7006 [cluster]

————————————————

版权声明:本文为优快云博主「则好」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.youkuaiyun.com/u013515384/article/details/114434521

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值