Redis集群的伪分布式搭建

Redis集群中至少应该有三个节点。要保证集群的高可用,需要每个节点有一个备份机。
Redis集群至少需要6台服务器。
搭建伪分布式。可以使用一台CentOS虚拟机运行6个redis实例。需要修改redis的端口号7001-7006

1、使用ruby脚本搭建集群。需要ruby的运行环境

1
2
3
安装ruby
um install ruby
um install rubygems

2、安装ruby脚本运行使用的包。

1
2
3
4
5
6
7
8
9
[root@localhost ~]# gem install redis-3.0.0.gem 
uccessfully installed redis-3.0.0
gem installed
nstalling ri documentation for redis-3.0.0...
nstalling RDoc documentation for redis-3.0.0...
root@localhost ~]#
root@localhost ~]# cd redis-3.0.0/src
root@localhost src]# ll *.rb
rwxrwxr-x. 1 root root 48141 Apr 1 2015 redis-trib.rb

3、修改redis.conf配置文件。配置文件中还需要把cluster-enabled yes前的注释去掉。

http://ov1nop9io.bkt.clouddn.com/%E5%B1%B1%E4%B8%9C.png

4、创建集群实例文件夹

1
[root@localhost redis]# mkdir ../redis-cluster

5、拷贝redis到集群文件夹创建实例redis01

1
2
3
[root@localhost local]# cp redis/bin redis-cluster/redis01 -r
cp: omitting directory `redis/bin'

6、删除appendonly.aof,dump.rdb

1
2
[root@localhost redis01]# rm -f appendonly.aof 
[root@localhost redis01]# rm -f dump.rdb

7、更改端口为7001和cluster-enabled yes前的注释去掉

http://ov1nop9io.bkt.clouddn.com/%E5%B1%B1%E4%B8%9C.png

8、复制redis01到集群文件夹创建其余实例,更改端口

1
2
3
4
5
[root@localhost redis-cluster]# cp -r redis01/ redis02
[root@localhost redis-cluster]# cp -r redis01/ redis03
[root@localhost redis-cluster]# cp -r redis01/ redis04
[root@localhost redis-cluster]# cp -r redis01/ redis05
[root@localhost redis-cluster]# cp -r redis01/ redis06

9、编写启动批处理shell文件start-all.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf

10、赋予权限

1
[root@localhost redis-cluster]# chmod u+x start-all.sh

11、启动

1
2
3
4
5
6
7
8
9
10
11
[root@localhost redis-cluster]# ./start-all.sh 

[root@localhost redis-cluster]# ps aux|grep redis
root 5893 0.0 0.3 33936 1696 ? Ssl 21:04 0:07 ./redis-server *:6379
root 6385 0.0 0.3 33936 1960 ? Ssl 23:36 0:00 ./redis-server *:7001 [cluster]
root 6387 0.0 0.3 33936 1960 ? Ssl 23:36 0:00 ./redis-server *:7002 [cluster]
root 6391 0.1 0.3 33936 1960 ? Ssl 23:36 0:00 ./redis-server *:7003 [cluster]
root 6395 0.1 0.3 33936 1960 ? Ssl 23:36 0:00 ./redis-server *:7004 [cluster]
root 6401 0.0 0.3 33936 1960 ? Ssl 23:36 0:00 ./redis-server *:7005 [cluster]
root 6403 0.1 0.3 33936 1960 ? Ssl 23:36 0:00 ./redis-server *:7006 [cluster]
root 6411 0.0 0.1 4356 732 pts/1 S+ 23:37 0:00 grep redis

12、复制redis下的src目录中的redis-trib.rb到集群

1
2
3
4
root@localhost redis-3.0.0]# cd src
[root@localhost src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr 1 2015 redis-trib.rb
[root@localhost src]# cp redis-trib.rb /usr/local/redis-cluster/

13、使用ruby脚本搭建集群。

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
48
49
50
51
52
53
54
55
56
 [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005  192.168.25.153:7006
>>> Creating cluster
necting to node 192.168.25.153:7001: OK
necting to node 192.168.25.153:7002: OK
necting to node 192.168.25.153:7003: OK
necting to node 192.168.25.153:7004: OK
necting to node 192.168.25.153:7005: OK
necting to node 192.168.25.153:7006: OK
>>> Performing hash slots allocation on 6 nodes...
ng 3 masters:
192.168.25.153:7001
192.168.25.153:7002
192.168.25.153:7003
ing replica 192.168.25.153:7004 to 192.168.25.153:7001
ing replica 192.168.25.153:7005 to 192.168.25.153:7002
ing replica 192.168.25.153:7006 to 192.168.25.153:7003
2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001
lots:0-5460 (5461 slots) master
8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002
lots:5461-10922 (5462 slots) master
2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003
lots:10923-16383 (5461 slots) master
74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004
eplicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3
42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005
eplicates 8cd93a9a943b4ef851af6a03edd699a6061ace01
8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006
eplicates 2935007902d83f20b1253d7f43dae32aab9744e6
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
ting for the cluster to join.....
>>> Performing Cluster Check (using node 192.168.25.153:7001)
2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001
lots:0-5460 (5461 slots) master
8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002
lots:5461-10922 (5462 slots) master
2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003
lots:10923-16383 (5461 slots) master
74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004
lots: (0 slots) master
eplicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3
42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005
lots: (0 slots) master
eplicates 8cd93a9a943b4ef851af6a03edd699a6061ace01
8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006
lots: (0 slots) master
eplicates 2935007902d83f20b1253d7f43dae32aab9744e6
] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
] All 16384 slots covered.
ot@localhost redis-cluster]#


14、启动每个redis实例。

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

15、创建关闭集群的脚本。

1
2
3
4
5
6
7
8
[root@localhost redis-cluster]# vim shutdow-all.sh
edis01/redis-cli -p 7001 shutdown
edis01/redis-cli -p 7002 shutdown
edis01/redis-cli -p 7003 shutdown
edis01/redis-cli -p 7004 shutdown
edis01/redis-cli -p 7005 shutdown
edis01/redis-cli -p 7006 shutdown
root@localhost redis-cluster]# chmod u+x shutdow-all.sh

16、 集群的使用方法,Redis-cli连接集群

1
[root@localhost redis-cluster]# redis01/redis-cli -p 7002 -c

-c:代表连接的是redis集群