Redis Cluster 集群搭建

本文参照Redis官方集群搭建教程进行集群部署,所使用的Redis版本为4.0.8。

由于部署环境有限,通过在单机上部署多个Redis实例,进行集群模拟。大致步骤与多节点部署类似。

集群部署

安装Redis

Redis官网下载所需要的Redis版本。

1
2
3
4
$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
$ tar xzf redis-4.0.8.tar.gz
$ cd redis-4.0.8
$ make

由于官方建议部署六个节点,三个节点作为Master节点,三个作为Slave节点。

Note that the minimal cluster that works as expected requires to contain at least three master nodes. For your first tests it is strongly suggested to start a six nodes cluster with three masters and three slaves.

所以,通过将下载好的Redis文件拷贝六份,作为六个Redis实例。命令如下:

1
2
3
cp redis-4.0.5 redis-4.0.8_7001 //7001为Redis启动的端口号
......
cp redis-4.0.5 redis-4.0.8_7006 //7006为Redis启动的端口号

配置Redis

进入redis-4.0.8_7001目录下,编辑redis.conf文件,进行如下修改:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 端口号
port 7001

# 绑定的IP,若在真实集群下部署,需要改为其他节点可以访问到的IP
bind 127.0.0.1

# redis后台运行
daemonize yes

# pidfile
pidfile /var/run/redis_7001.pid

# 开启集群
cluster-enabled yes

# 集群的配置文件,在集群首次启动时生成
cluster-config-file nodes_7001.conf

# 请求超时设置
cluster-node-timeout 5000

# 开启AOF,每次写操作都会记录一条日志
appendonly yes

剩下五个Redis配置,重复以上步骤,只需要把相关的端口号修改即可。

1
sed -i '' 's/7001/7006/g' redis.conf

启动Redis实例

分别进入redis-4.0.8_700X/src目录下,执行如下命令,启动redis:

1
./redis-server ../redis.conf

最好可以写一个启动脚本来执行。每个实例启动完成之后,会出现如下信息:

1
[82462] 26 Nov 11:56:55.329 * No cluster configuration found, I'm 97a3a64667477371c4479320d683e4c8db5858b1

实例会一直使用同一个ID,从而在集群中保持一个唯一的名字。

This ID will be used forever by this specific instance in order for the instance to have a unique name in the context of the cluster. Every node remembers every other node using this IDs, and not by IP or port. IP addresses and ports may change, but the unique node identifier will never change for all the life of the node. We call this identifier simply Node ID.

创建集群

通过上述步骤,已经有六个Redis实例处于运行中,为了创建Redis集群需要为每个实例编写配置文件。

由于部署的Redis版本为4.0.8,redis提供了redis-trib.rb,一个ruby脚本来进行配置文件的编写,该脚本位于redis/src目录下。

为了运行该脚本,需要运行如下命令:

1
gem install redis

安装完成之后,执行ruby脚本:

1
./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

The command used here is create, since we want to create a new cluster. The option –cluster-replicas 1 means that we want a slave for every master created. The other arguments are the list of addresses of the instances I want to use to create the new cluster.

该命令会创建一个包括3个master节点和3个slave节点的集群,执行完命令后,出现如下信息:

1
2
......
Can I set the above configuration? (type 'yes' to accept): yes

输入yes,接受建议的配置。最后,如果一切顺利的话,会看到如下信息:

1
2
......
[OK] All 16384 slots covered

至此,集群已成功创建。集群创建只需一次,

注:若Redis版本为5.0.0及以上的话,可以直接使用redis/src目录下的redis-cli命令:

1
redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1

关闭集群

进入redis/src目录下,执行如下命令:

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

其余实例类似。最好写一个启动脚本来执行。

集群测试

连接集群

进入redis/src目录下,执行如下命令:

1
2
3
4
5
6
7
8
9
$ ./redis-cli -c -h 127.0.0.1 -p 7001

127.0.0.1:7001> get hello
-> Redirected to slot [11359] located at 127.0.0.1:7003
(nil)
127.0.0.1:7003> set hello world
OK
127.0.0.1:7003> GET hello
"world"

检查集群状态

 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
27
28
➜  $redis-4.0.8_1 ./src/redis-trib.rb check 127.0.0.1:7001

>>> Performing Cluster Check (using node 127.0.0.1:7001)
S: 01f9a40568f54aa9861ec22f3fb6c04be4a24380 127.0.0.1:7001
   slots: (0 slots) slave
   replicates 5fa6c5c54852073665999685319352441efffe76
M: be74a93db7c434e5a149d9ca1196026ce49c551d 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: d898e44c998f78543083156fc3489d62306a436e 127.0.0.1:7007
   slots: (0 slots) master
   0 additional replica(s)
M: 5fa6c5c54852073665999685319352441efffe76 127.0.0.1:7006
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: dfdb934060c1ac3a844175cb67a864bcb578fd5c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates be74a93db7c434e5a149d9ca1196026ce49c551d
M: 94b0d815f4c0f6466999a4204f25babb6266fd69 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: e0c04bf08a4933d9f3f8225a240099fc55162309 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 94b0d815f4c0f6466999a4204f25babb6266fd69
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群命令

客户端连接集群的语法:

1
redis-cli -c -h <host> -p <port>

以下命令均在redis-cli下执行:

集群信息

CLUSTER INFO,打印集群信息

CLUSTER NODES,列出集群当前所有已知节点及其信息

节点操作

CLUSTER MEET <IP>:<PORT>,将指定节点加入到集群中

CLUSTER FORGET <NODE_ID>,移除指定节点

CLUSTER REPLICATE <NODE_ID>,将当前节点设置为指定节点的从节点

剩下的操作命令,可以从Redis官网查看,本文就不列举了。

未完待续

本文只是介绍了Redis Cluster部署相关步骤,并没有对cluster相关的基本概念和原理进行阐述,接下来会对此进行整理,形成系列文章。