使用阿里云搭建Redis集群

本文记录了如何在阿里云服务器中使用多主机方式搭建Redis集群,以及如何管理redis集群包括添加删除redis节点。

环境介绍

  • redis:5.0.5
  • system 1:阿里云轻量级应用服务器
  • system 2:阿里云ECS服务器

Redis主从复制

新建节点20007,20008。节点20007作为master节点,20008作为master的从节点。
Redis从节点应该使用只读模式,若从节点更改数据,会导致主从数据不一致,配置只读模式只需要在从节点中加入配置slave-read-only=yes。

配置主从复制可以有两种方式一种是在从节点中指定主节点ip和端口,另一种是使用./redis-server –slaveof ip port(临时生效,redis重启后无效)。线上环境推荐使用第一种配置。

使用配置文件方式配置主从复制

在从节点配置文件中

1
2
3
4
#主节点IP 端口
slaveof 39.105.76.133 20007
#当前子节点只允许读
slave-read-only ye

首先启动主节点20007,再以此启动其他子节点,使用info replication分别查看主从节点,结果如下

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
29
30
31
32
33
34
35
36
37
38
39
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# ./src/redis-cli -a 密码 -p 20007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:20007> info replication
# Replication
role:master
connected_slaves:0
master_replid:c57bfaad5f0e1a0a1267cf43455d7ff742d77768
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:20007>
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# ./src/redis-cli -a 密码 -p 20008
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:20008> info replication
# Replication
role:slave
master_host:39.105.76.133
master_port:20007
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1562393345
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a09b6c8febdd1fef10fd67765fc7a28b5c80e433
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:20008>

断开主从复制

在slave节点执行slaveof no one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:20008> slaveof no one
OK
127.0.0.1:20008> info replication
# Replication
role:master
connected_slaves:0
master_replid:c460d81360a33de807cb7d2654ddd63a75d1a349
master_replid2:a09b6c8febdd1fef10fd67765fc7a28b5c80e433
master_repl_offset:0
second_repl_offset:1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:20008>

可以看到savle节点已经变成了master

使用slaveof配置临时主从复制

在从节点(20007)中执行 slaveof 39.105.76.133 20007

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
127.0.0.1:20008> slaveof 39.105.76.133 20007
OK
127.0.0.1:20008> info replication
# Replication
role:slave
master_host:39.105.76.133
master_port:20007
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1562393637
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c460d81360a33de807cb7d2654ddd63a75d1a349
master_replid2:a09b6c8febdd1fef10fd67765fc7a28b5c80e433
master_repl_offset:0
second_repl_offset:1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:20008>

从中可以看出当前的节点是slave,主节点是20007.

redis cluster集群配置

本次集群采用三主三从配置。

1
2
3
4
5
6
7
8
9
主机A.IP:www.zhangchuany.com
20001
20002
20003

主机B.IP:47.100.201.99
20004
20005
20006

创建redis节点实例

在主机A中配置端口为20001~20003的redis实例

1
2
3
4
5
6
7
8
9
10
11
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src utils
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# mkdir cluster
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# cd cluster
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5/cluster# mkdir 20001
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5/cluster# mkdir 20002
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5/cluster# mkdir 20003
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5/cluster# ls
20001 20002 20003
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5/cluster#

copy redis.conf分别至20001、20002、20003目录中,并对其进行配置修改

1
2
3
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5/cluster# cp ../redis.conf ./20001
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5/cluster# cp ../redis.conf ./20002
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5/cluster# cp ../redis.conf ./20003

主要修改端口(port)以及相关目录,以端口20001为例,其他配置类似只需要更改端口以及相关目录

1
2
3
4
5
6
7
8
#....
port 20001
pidfile "/usr/ben/redis/redis-5.0.5/cluster/20001/redis_20001.pid"
logfile "/usr/ben/redis/redis-5.0.5/cluster/20001/redis_20001.log"
dir "/usr/ben/redis/redis-5.0.5/cluster/20001"
protected-mode no
cluster-enabled yes
#....

分别启动主机A和主机B上的redis节点,下列以主机A为例

1
2
3
4
5
6
7
8
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# ./src/redis-server ./cluster/20001/redis.conf 
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# ./src/redis-server ./cluster/20002/redis.conf
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# ./src/redis-server ./cluster/20003/redis.conf
root@iZ2ze0cuk708a6i2e80d06Z:/usr/ben/redis/redis-5.0.5# ps -ef | grep redis
root 2270 1 0 15:23 ? 00:00:00 ./src/redis-server *:20001 [cluster]
root 2275 1 0 15:23 ? 00:00:00 ./src/redis-server *:20002 [cluster]
root 2281 1 0 15:23 ? 00:00:00 ./src/redis-server *:20003 [cluster]
root 2286 1502 0 15:23 pts/0 00:00:00 grep --color=auto redis

redis节点已经启动,现在使用redis-cli将这些节点添加到集群中,使用redis-cli添加集群需要依赖redis-tools,安装redis-tools
安装Ruby

1
sudo apt-get install redis-tools

使用redis-cli添加集群,注意添加集群的redis节点不能使localhost或者127.0.0.1,必须是当前设备的IP地址(外网or内网),如果主机是阿里云服务器需要开放redis节点端口号即20001-20006。除此之外还需要开放redis事件总线端口号10000+redis节点端口号,如10000+20001,最终需要开放的端口号为30001

1
./redis-cli -a 你的密码(节点密码必须统一) --cluster create 39.105.76.133:20001 39.105.76.133:20002 39.105.76.133:20003 47.100.201.99:20004 47.100.201.99:20005 47.100.201.99:20006 --cluster-replicas 1

redis-cli会分配主从库节点,分配的原则是尽量保证每个主库运行在不同的IP地址上,同时每个从库和主库均不运行在同一IP地址上,以保证系统的容灾能力。分配结果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 47.100.201.99:20006 to 127.0.0.1:20001
Adding replica 127.0.0.1:20003 to 47.100.201.99:20004
Adding replica 47.100.201.99:20005 to 127.0.0.1:20002
M: 000af950839ab30463661710ff9e026701bcbcd6 127.0.0.1:20001
slots:[0-5460] (5461 slots) master
M: 555980741ad65da25e2d5c5a4e3fe02e7d1b581f 127.0.0.1:20002
slots:[10923-16383] (5461 slots) master
S: 11d4baec66ed400f36fefa06625b5dfdcbb79ff4 127.0.0.1:20003
replicates 421754bb415dbdc1fc5cdadb31057249adeb8514
M: 421754bb415dbdc1fc5cdadb31057249adeb8514 47.100.201.99:20004
slots:[5461-10922] (5462 slots) master
S: d28f633801275cc41d223c2a10c43e10ad2a631c 47.100.201.99:20005
replicates 555980741ad65da25e2d5c5a4e3fe02e7d1b581f
S: 4ab537406994b37dc92385f32f74421116a692f8 47.100.201.99:20006
replicates 000af950839ab30463661710ff9e026701bcbcd6
Can I set the above configuration? (type 'yes' to accept):

从日志上可以看出主机B(47.100.201.99:20006)是主机A(127.0.0.1:20001)的从库,主机A(127.0.0.1:20003)是主机B(47.100.201.99:20004)的从库,主机B(47.100.201.99:20005)是主机A(127.0.0.1:20002)的从库

Redis哨兵机制(sentinel)

随意分享,您的支持将鼓励我继续创作!