Redis集群主备模式部署

时间:2021-02-08 20:10:55

网上有非常多用Ruby安装Redis-cluster的文章。可是在实际环境下不想安装Ruby,所以本文主要介绍了用Redis命令部署Redis集群。而且为集群中每个master实例添加一个slave实例。


0 编译安装

$ wget http://download.redis.io/releases/redis-3.0.3.tar.gz
$ tar xzvf redis-3.0.3.tar.gz
$ cd redis-3.0.3
$ make all
$ make install # use sudo user.

也能够去github下载最新代码:https://github.com/antirez/redis.git


1 创建文件夹

$ mkdir 7000 7001 7002 7003

2 改动例如以下配置redis.conf

port 7000                               #7000改动为相应的7001、7002、7003
daemonize yes
cluster-enabled yes
cluster-config-file nodes-7000.conf #7000改动为相应的7001、7002、7003
luster-node-timeout 15000

将改动好的配置文件放到对用的文件夹下:./7000, ./7001, ./7002, ./7003 。


3 启动redis服务

$ cd 7000
$ redis-server ./redis.conf
$ cd ../7001
$ redis-server ./redis.conf
$ cd ../7002
$ redis-server ./redis.conf
$ cd ../7003
$ redis-server ./redis.conf
$ # 检查redis启动情况
$ ps -ef | grep redis | grep -v grep
cjf 5834 1 0 16:33 ? 00:00:00 redis-server 127.0.0.1:7000 [cluster]
cjf 5839 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster]
cjf 5843 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster]
cjf 5847 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster]

4 将redis实例添加到集群中

主要是通过用cluster meet <host> <port>给集群添加节点,例如以下:

$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
14d4071b97121e703e44bbc834f42d1b31c8ea95 :7000 myself,master - 0 0 0 connected
127.0.0.1:7000> cluster meet 127.0.0.1 7001
OK
127.0.0.1:7000> cluster meet 127.0.0.1 7002
OK
127.0.0.1:7000> cluster meet 127.0.0.1 7003
OK
127.0.0.1:7000> cluster nodes
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850262990 0 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850265007 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850263998 2 connected
127.0.0.1:7000>

假设cluster nodes出现上面的四行记录就表示meet成功,标识myself的是client正在连接的redis实例。

能够通过cluster forget清除集群中的某个实例。


5 分配slots

redis-cluster总共同拥有16384个槽位(0-16383),按例如以下方式改动槽位:

$ cat 7000/nodes-7000.conf #分配slots:0-4095(注意127.0.0.1:7000相应的行尾)
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850266016 3 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850265007 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850263998 2 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7001/nodes-7001.conf #分配slots:4096-8191(注意127.0.0.1:7001相应的行尾)
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850265714 3 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 myself,master - 0 0 2 connected 4096-8191
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850264705 0 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263695 1 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7002/nodes-7002.conf #分配slots:8192-12287(注意127.0.0.1:7002相应的行尾)
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850262787 2 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263796 1 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 myself,master - 0 0 3 connected 8192-12287
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850264806 0 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7003/nodes-7003.conf #分配slots:12288-16383(注意127.0.0.1:7003相应的行尾)
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850264808 2 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850265819 3 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263798 1 connected
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 myself,master - 0 0 0 connected 12288-16383
vars currentEpoch 3 lastVoteEpoch 0
$

配置改动成功之后。又一次启动redis:

$ # 停止redis
$ ps -ef|grep redis
cjf 5834 1 0 16:33 ? 00:00:00 redis-server 127.0.0.1:7000 [cluster]
cjf 5839 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster]
cjf 5843 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster]
cjf 5847 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster]
cjf 5921 5252 0 16:46 pts/2 00:00:00 grep redis
$ kill 5834 5839 5843 5847
$
$ # 又一次启动redis
$ cd 7000
$ redis-server ./redis.conf
$ cd ../7001
$ redis-server ./redis.conf
$ cd ../7002
$ redis-server ./redis.conf
$ cd ../7003
$ redis-server ./redis.conf
$
$ # 检查分配的slots是否生效:
$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850861230 0 connected 12288-16383
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850860221 2 connected 4096-8191
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850859209 3 connected 8192-12287
127.0.0.1:7000>
127.0.0.1:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:4
cluster_size:4
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:416
cluster_stats_messages_received:73
127.0.0.1:7000>

当前情况下,redis-cluster部署完成。


6 给master节点配一个slave节点

在前面。我们配置好了redis的集群并分配了slots,集群已经可用了。集群中如今有4个master节点。全部写入这个集群中的数据都会分片到这4个redis实例中。可是如今存在这样一个问题:假设某个master节点挂掉的话,那么这个集群就不可用了,所以我们如今为集群中的4个master实例各启一个slave实例做主备。

$ mkdir 8000 8001 8002 8003
$ cp 7000/redis.conf 8000
$ cp 7000/redis.conf 8001
$ cp 7000/redis.conf 8002
$ cp 7000/redis.conf 8003

redis.conf中以下两个配置改动为相应的端口:

port 800X
cluster-config-file nodes-800X.conf

然后启动这4个实例:

$ cd 8000/
$ redis-server ./redis.conf
$ cd ../8001
$ redis-server ./redis.conf
$ cd ../8002
$ redis-server ./redis.conf
$ cd ../8003
$ redis-server ./redis.conf
$
$ ps -ef|grep redis
cjf 5928 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7000 [cluster]
cjf 5933 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7001 [cluster]
cjf 5937 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7002 [cluster]
cjf 5941 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7003 [cluster]
cjf 6136 1 0 17:20 ? 00:00:00 redis-server 127.0.0.1:8000 [cluster]
cjf 6141 1 0 17:20 ? 00:00:00 redis-server 127.0.0.1:8001 [cluster]
cjf 6145 1 0 17:21 ? 00:00:00 redis-server 127.0.0.1:8002 [cluster]
cjf 6150 1 0 17:21 ? 00:00:00 redis-server 127.0.0.1:8003 [cluster]
cjf 6157 5252 0 17:21 pts/2 00:00:00 grep redis
[cjf@host1 8003]$

再将这4个redis实例cluster meet到集群中:

$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438852991236 0 connected 12288-16383
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438852992245 2 connected 4096-8191
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438852993255 3 connected 8192-12287
127.0.0.1:7000> cluster meet 127.0.0.1 8000
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8001
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8002
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8003
OK
127.0.0.1:7000> cluster nodes
79c41ed9454f686fe7c35e0a6c505732b951b226 127.0.0.1:8000 master - 0 1438853011586 4 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438853014613 7 connected 12288-16383
833bb1d37b22b2055d2281a5f2dad3d869195f0d 127.0.0.1:8002 master - 0 1438853012596 6 connected
0d03ff5e90fa369c5c39c9415db39f711b943494 127.0.0.1:8001 master - 0 1438853013605 5 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438853010578 3 connected 8192-12287
a34eb2aa56f309793d78ba2f69141be268a23659 127.0.0.1:8003 master - 0 1438853012092 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438853009564 2 connected 4096-8191
127.0.0.1:7000>

最后用cluster replicate <nodeid>将8000、8001、8002、8003分别挂到7000、7001、7002、7003上:

$ redis-cli -p 8000
127.0.0.1:8000> cluster replicate 14d4071b97121e703e44bbc834f42d1b31c8ea95
OK
127.0.0.1:8000>
$ redis-cli -p 8001
127.0.0.1:8001> cluster replicate 2875f2155e3214d7950dd5916f5ecf5edd3d9fee
OK
127.0.0.1:8001>
$ redis-cli -p 8002
127.0.0.1:8002> cluster replicate 75334aae3ced44cf0e8416691aeef8249e7a0621
OK
127.0.0.1:8002>
$ redis-cli -p 8003
127.0.0.1:8003> cluster replicate 8b721ff1ac2e383ffcbcf133d502c6957f8df713
OK
127.0.0.1:8003> cluster nodes
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438853245955 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438853242927 7 connected 12288-16383
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438853242423 2 connected 4096-8191
833bb1d37b22b2055d2281a5f2dad3d869195f0d 127.0.0.1:8002 slave 75334aae3ced44cf0e8416691aeef8249e7a0621 0 1438853245955 6 connected
a34eb2aa56f309793d78ba2f69141be268a23659 127.0.0.1:8003 myself,slave 8b721ff1ac2e383ffcbcf133d502c6957f8df713 0 0 0 connected
0d03ff5e90fa369c5c39c9415db39f711b943494 127.0.0.1:8001 slave 2875f2155e3214d7950dd5916f5ecf5edd3d9fee 0 1438853241925 5 connected
79c41ed9454f686fe7c35e0a6c505732b951b226 127.0.0.1:8000 slave 14d4071b97121e703e44bbc834f42d1b31c8ea95 0 1438853244945 4 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438853243935 3 connected 8192-12287
127.0.0.1:8003>

如上。配置成功。


附录:经常使用的Redis-cluster命令

#### 集群(cluster)
CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的全部节点(node)。以及这些节点的相关信息。
#### 节点(node)
CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点加入到集群其中。让它成为集群的一份子。
CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。
CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。
CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。
#### 槽(slot)
CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。
CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。
CLUSTER FLUSHSLOTS 移除指派给当前节点的全部槽,让当前节点变成一个没有指派不论什么槽的节点。 CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,假设槽已经指派给还有一个节点。那么先让还有一个节点删除该槽>,然后再进行指派。 CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。 CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。 CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。 #### 键(key)
CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。 CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 眼下包括的键值对数量。
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。

ref:http://www.cnblogs.com/tankaixiong/articles/4022646.html