搭建redis集群环境

1
伪集群,单机上搭建,没多的机器

说明

1
2
3
Redis3.0版本之后支持Cluster
当前版本3.2.8
redis集群搭建最少6个节点其中3个主节点

修改配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
接上文redis环境搭建
redis1配置
打开集群配置
port 7001
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 5000
redis2配置
port 7002
cluster-enabled yes
cluster-config-file nodes-7002.conf
cluster-node-timeout 5000
redis3配置
port 7003
cluster-enabled yes
cluster-config-file nodes-7003.conf
cluster-node-timeout 5000

image

启动redis

1
2
3
4
5
6
cd redis1
./redis-server redis.conf
cd redis2
./redis-server redis.conf
cd redis3
./redis-server redis.conf

查看服务是否正常

1
ps -ef|grep redis 查看redis进程

image

创建集群

1
官方有提供工具redis-trib.rb,采用ruby编写,so安装ruby

安装ruby

1
yum -y install ruby ruby-devel rubygems rpm-build

通过ruby命令工具安装redis

1
gem install redis

通过redis-trib.rb 创建集群

查看命令参数

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
[root@vultr src]# ./redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
set-timeout host:port milliseconds
reshard host:port
--pipeline <arg>
--to <arg>
--yes
--slots <arg>
--from <arg>
--timeout <arg>
del-node host:port node_id
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
fix host:port
--timeout <arg>
help (show this help)
rebalance host:port
--pipeline <arg>
--simulate
--auto-weights
--use-empty-masters
--weight <arg>
--timeout <arg>
--threshold <arg>
check host:port
info host:port
create host1:port1 ... hostN:portN
--replicas <arg>
import host:port
--replace
--copy
--from <arg>
call host:port command arg arg .. arg
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

创建集群,出错

1
2
3
4
5
6
7
8
./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 1 replicas per node.
*** At least 6 nodes are required.
至少需要6个节点

开始创建节点
image

重新创建redis集群
image
image

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1.通过redis-cli链接redis redis-cli -c -p 7001
[root@vultr bin]# redis-cli -c -p 7001
127.0.0.1:7001> set name kite
-> Redirected to slot [5798] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get name
"kite"
127.0.0.1:7002> exit
[root@vultr bin]# redis-cli -c -p 7003
127.0.0.1:7003> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002
"kite"
127.0.0.1:7002>
已经进行同步

参考

Redis 3.2.1集群搭建