我们使用一台机器,模拟6个redis实例来创建redis集群,其中3主3从。
创建目录,copy配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost bin]# cd /application/redis-3.2/
[root@localhost redis-3.2]# ls
bin conf dump.rdb
[root@localhost redis-3.2]# mkdir cluster
[root@localhost redis-3.2]# ls
bin cluster conf dump.rdb
[root@localhost redis-3.2]# cd cluster/
[root@localhost cluster]# ls
[root@localhost cluster]# ls
[root@localhost cluster]# mkdir {7000,7001,7002,7003,7004,7005}
[root@localhost cluster]# cp ../conf/redis.conf 7000
[root@localhost cluster]# cp ../conf/redis.conf 7001
[root@localhost cluster]# cp ../conf/redis.conf 7002
[root@localhost cluster]# cp ../conf/redis.conf 7003
[root@localhost cluster]# cp ../conf/redis.conf 7004
[root@localhost cluster]# cp ../conf/redis.conf 7005
|
2. 修改配置文件
1
2
3
4
5
6
7
8
9
10
|
[root@localhost cluster]# cd 7000
[root@localhost 7000]# vi redis.conf
#做如下修改
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
dir /application/redis-3.2/cluster/7000/
|
按照此方式修改7001~7005的配置文件,注意修改端口号。
3. 启动各个实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost cluster]# ls
7000 7001 7002 7003 7004 7005
[root@localhost cluster]# ../bin/redis-server 7000/redis.conf
[root@localhost cluster]# ../bin/redis-server 7001/redis.conf
[root@localhost cluster]# ../bin/redis-server 7002/redis.conf
[root@localhost cluster]# ../bin/redis-server 7003/redis.conf
[root@localhost cluster]# ../bin/redis-server 7004/redis.conf
[root@localhost cluster]# ../bin/redis-server 7005/redis.conf
[root@localhost cluster]# ps -ef | grep redis
root 31949 1 0 12:00 ? 00:00:00 ../bin/redis-server 127.0.0.1:7000 [cluster]
root 31954 1 0 12:00 ? 00:00:00 ../bin/redis-server 127.0.0.1:7001 [cluster]
root 31977 1 0 12:01 ? 00:00:00 ../bin/redis-server 127.0.0.1:7002 [cluster]
root 31982 1 0 12:01 ? 00:00:00 ../bin/redis-server 127.0.0.1:7003 [cluster]
root 31986 1 0 12:01 ? 00:00:00 ../bin/redis-server 127.0.0.1:7004 [cluster]
root 31991 1 0 12:01 ? 00:00:00 ../bin/redis-server 127.0.0.1:7005 [cluster]
root 31996 31349 0 12:01 pts/2 00:00:00 grep redis
|
4. 创建集群
现在我们已经有了六个正在运行中的 Redis 实例, 接下来我们需要使用这些实例来创建集群, 并为每个节点编写配置文件。
通过使用 Redis 集群命令行工具redis-trib,编写节点配置文件的工作可以非常容易地完成redis-trib位于Redis 源码的src文件夹中,它是一个 Ruby 程序,这个程序通过向实例发送特殊命令来完成创建新集群,检查集群,或者对集群进行重新分片(reshared)等工作。
我们需要执行以下命令来创建集群:
1
2
|
[root@localhost src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 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
/usr/bin/env: ruby: No such file or directory
|
系统中没有安装ruby,所以报上面的错误。
先安装ruby
[root@localhost yum.repos.d]# yum install ruby
[root@localhost yum.repos.d]# yum install rubygems
[root@localhost yum.repos.d]# gem install redis
Successfully installed redis-3.2.2
1 gem installed
Installing ri documentation for redis-3.2.2...
Installing RDoc documentation for redis-3.2.2...
再次创建集群
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
40
41
42
43
44
45
46
47
|
[root@localhost src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 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
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: ac733323e529323b04b57fb13ddcdeb7c7db23fd 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 8e96fae16b8d92911e579ade8050829567427c8b 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: 3c0f95416608ce682fa7a18ee782111a7c4effda 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
S: 855bdb49452a2be3a3ab0a72b5270d057a7f2c9f 127.0.0.1:7003
replicates ac733323e529323b04b57fb13ddcdeb7c7db23fd
S: 7ab4da5d4c822ab55a1dff1d24a1ff83a0c98260 127.0.0.1:7004
replicates 8e96fae16b8d92911e579ade8050829567427c8b
S: fbe78c944af98f9cfd75c4dae87201b9c2572c7a 127.0.0.1:7005
replicates 3c0f95416608ce682fa7a18ee782111a7c4effda
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: ac733323e529323b04b57fb13ddcdeb7c7db23fd 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 8e96fae16b8d92911e579ade8050829567427c8b 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: 3c0f95416608ce682fa7a18ee782111a7c4effda 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
M: 855bdb49452a2be3a3ab0a72b5270d057a7f2c9f 127.0.0.1:7003
slots: (0 slots) master
replicates ac733323e529323b04b57fb13ddcdeb7c7db23fd
M: 7ab4da5d4c822ab55a1dff1d24a1ff83a0c98260 127.0.0.1:7004
slots: (0 slots) master
replicates 8e96fae16b8d92911e579ade8050829567427c8b
M: fbe78c944af98f9cfd75c4dae87201b9c2572c7a 127.0.0.1:7005
slots: (0 slots) master
replicates 3c0f95416608ce682fa7a18ee782111a7c4effda
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
|
Redis自动选择主从。
5. 连接集群
redis-cli 也可以作为集群的客户端工具,要想访问集群,只需连接任意一个redis实例即可。使用-c参数
1
2
|
[root@localhost bin]# ./redis-cli -c -p 7000
127.0.0.1:7000>
|
1
2
3
4
5
|
127.0.0.1:7000> set name zhangsanfeng
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get name
"zhangsanfeng"
|
set 命令写数据,集群将数据写到7001实例上,当你使用get命令获取数据时,客户端即自动切换到7001端口。
redis-cli对集群的支持是非常基本的, 所以它总是依靠 Redis 集群节点来将它转向(redirect)至正确的节点。一个真正的(serious)集群客户端应该做得比这更好: 它应该用缓存记录起哈希槽与节点地址之间的映射(map), 从而直接将命令发送到正确的节点上面。