redis 集群配置

redis 集群准备

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 拷贝环境,如果存在 dump.rdb,记得rm
[root@iZuf6iq8e7ya9v3ix71k0pZ local]# mkdir redis-cluster
[root@iZuf6iq8e7ya9v3ix71k0pZ local]# cp -r redis/bin redis-cluster/redis01
[root@iZuf6iq8e7ya9v3ix71k0pZ local]#

<!-- more -->

# 设置 redis.conf,修改 port,并开启集群模式
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# vim redis01/redis.conf
port 6379 改为 port 7001
cluster-enabled yes
...
...
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# ll
总用量 24
drwxr-xr-x 2 root root 4096 3月 5 23:58 redis01
drwxr-xr-x 2 root root 4096 3月 5 23:57 redis02
drwxr-xr-x 2 root root 4096 3月 5 23:57 redis03
drwxr-xr-x 2 root root 4096 3月 5 23:57 redis04
drwxr-xr-x 2 root root 4096 3月 5 23:57 redis05
drwxr-xr-x 2 root root 4096 3月 5 23:57 redis06

# 创建 .sh 批处理文件,批量启动
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# vim start-all.sh
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
cd ..
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# chmod +x start-all.sh

# 创建 .sh 批处理文件,批量停止
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# vim stop-all.sh
cd redis01
./redis-cli -p 7001 shutdown
cd ..
cd redis02
./redis-cli -p 7002 shutdown
cd ..
cd redis03
./redis-cli -p 7003 shutdown
cd ..
cd redis04
./redis-cli -p 7004 shutdown
cd ..
cd redis05
./redis-cli -p 7005 shutdown
cd ..
cd redis06
./redis-cli -p 7006 shutdown
cd ..
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# chmod +x stop-all.sh

# 启动server
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# ./start-all.sh
8907:C 06 Mar 00:07:10.397 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8907:C 06 Mar 00:07:10.397 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8907, just started
8907:C 06 Mar 00:07:10.397 # Configuration loaded
8909:C 06 Mar 00:07:10.401 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8909:C 06 Mar 00:07:10.401 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8909, just started
8909:C 06 Mar 00:07:10.401 # Configuration loaded
8911:C 06 Mar 00:07:10.406 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8911:C 06 Mar 00:07:10.406 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8911, just started
8911:C 06 Mar 00:07:10.406 # Configuration loaded
8919:C 06 Mar 00:07:10.410 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8919:C 06 Mar 00:07:10.411 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8919, just started
8919:C 06 Mar 00:07:10.411 # Configuration loaded
8924:C 06 Mar 00:07:10.415 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8924:C 06 Mar 00:07:10.415 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8924, just started
8924:C 06 Mar 00:07:10.415 # Configuration loaded
8926:C 06 Mar 00:07:10.419 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8926:C 06 Mar 00:07:10.419 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8926, just started
8926:C 06 Mar 00:07:10.419 # Configuration loaded
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# ps -aux|grep redis
root 8908 0.0 0.2 147300 9612 ? Ssl 00:07 0:00 ./redis-server *:7001 [cluster]
root 8910 0.0 0.2 147300 9608 ? Ssl 00:07 0:00 ./redis-server *:7002 [cluster]
root 8918 0.0 0.2 147300 9608 ? Ssl 00:07 0:00 ./redis-server *:7003 [cluster]
root 8920 0.0 0.2 147300 9612 ? Ssl 00:07 0:00 ./redis-server *:7004 [cluster]
root 8925 0.0 0.2 147300 9608 ? Ssl 00:07 0:00 ./redis-server *:7005 [cluster]
root 8930 0.0 0.2 147300 9608 ? Ssl 00:07 0:00 ./redis-server *:7006 [cluster]
root 8940 0.0 0.0 112664 972 pts/1 S+ 00:07 0:00 grep --color=auto redis
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]#

下载并配资后 ruby 搭建 redis 集群所依赖的包

https://rubygems.global.ssl.fastly.net/gems/redis-4.0.1.gem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sftp> put /Users/niuhesm/Downloads/redis-4.0.1.gem /root
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# yum install ruby
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# gem install redis-4.0.1.gem
ERROR: Error installing redis-4.0.1.gem:
​ redis requires Ruby version >= 2.2.2.

[root@iZuf6iq8e7ya9v3ix71k0pZ ~]#

# 由于不想更新 yum 源,换个方式更新 ruby 版本
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# yum remove ruby
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# wget https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# tar zxvf ruby-2.2.3.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# cd ruby-2.2.3
[root@iZuf6iq8e7ya9v3ix71k0pZ ruby-2.2.3]# ./configure
[root@iZuf6iq8e7ya9v3ix71k0pZ ruby-2.2.3]# make
[root@iZuf6iq8e7ya9v3ix71k0pZ ruby-2.2.3]# sudo make install
[root@iZuf6iq8e7ya9v3ix71k0pZ ruby-2.2.3]# ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
# 重新安装依赖包
[root@iZuf6iq8e7ya9v3ix71k0pZ ~]# gem install redis-4.0.1.gem

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# cd /usr/local/redis/redis-4.0.8/src/
[root@iZuf6iq8e7ya9v3ix71k0pZ src]# cp redis-trib.rb /usr/local/redis-cluster/
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# ./redis-trib.rb create --replicas 1 106.15.191.27:7001 106.15.191.27:7002 106.15.191.27:7003 106.15.191.27:7004 106.15.191.27:7005 106.15.191.27:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
106.15.191.27:7001
106.15.191.27:7002
106.15.191.27:7003
Adding replica 106.15.191.27:7005 to 106.15.191.27:7001
Adding replica 106.15.191.27:7006 to 106.15.191.27:7002
Adding replica 106.15.191.27:7004 to 106.15.191.27:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: ec761eb0418892ec5ddee2c9360d09e28174236a 106.15.191.27:7001
slots:0-5460 (5461 slots) master
M: 5ae347530fee2c4569fb2fc9b0c34a9e17d48399 106.15.191.27:7002
slots:5461-10922 (5462 slots) master
M: d1fef0edb0a6efe7b2c25d1628fa01e7aa31200d 106.15.191.27:7003
slots:10923-16383 (5461 slots) master
S: be0615cbdb26b634adeed9290557750e3bda7c5c 106.15.191.27:7004
replicates ec761eb0418892ec5ddee2c9360d09e28174236a
S: caa925f9e8074f7ba23ee9e73d0c1d67eb8d829b 106.15.191.27:7005
replicates 5ae347530fee2c4569fb2fc9b0c34a9e17d48399
S: 274b0e14d2cb0413c026cbb9606ed9b60d44f7fa 106.15.191.27:7006
replicates d1fef0edb0a6efe7b2c25d1628fa01e7aa31200d
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.....................................................................................................................................................^C./redis-trib.rb:653:in `sleep': Interrupt
from ./redis-trib.rb:653:in `wait_cluster_join'
from ./redis-trib.rb:1436:in `create_cluster_cmd'
from ./redis-trib.rb:1830:in `<main>'
[root@iZuf6iq8e7ya9v3ix71k0pZ]#

# 出现此问题(WRNING] Some slaves are in the same host as their master)
解决一:修改 redis.conf 下的 bind ip 的地址改为服务器实际地址
解决二:(由于只有一台服务器,也可以)
./redis-trib.rb create --replicas 1 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 127.0.0.1:7006

# 成功启动
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# ./redis-trib.rb create --replicas 1 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 127.0.0.1:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 8495f96bb7cecaa753af70b0bd76c060d1a69a77 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 701c093b7045eedc542f68a1cbfb1c59f1b36c1a 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 4cf1720b03f3db54abb8f0f1eeb743f7af7eacc9 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
S: a1d3258ac24b74d72a5fe6d3a255bf6dbd337684 127.0.0.1:7004
replicates 701c093b7045eedc542f68a1cbfb1c59f1b36c1a
S: 892d5474496613876cbc78ac5f903f39db94e8de 127.0.0.1:7005
replicates 4cf1720b03f3db54abb8f0f1eeb743f7af7eacc9
S: fc2dfc9a44179023f2743c87c0bd7f9387728728 127.0.0.1:7006
replicates 8495f96bb7cecaa753af70b0bd76c060d1a69a77
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:7001)
M: 8495f96bb7cecaa753af70b0bd76c060d1a69a77 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: a1d3258ac24b74d72a5fe6d3a255bf6dbd337684 127.0.0.1:7004
slots: (0 slots) slave
replicates 701c093b7045eedc542f68a1cbfb1c59f1b36c1a
M: 4cf1720b03f3db54abb8f0f1eeb743f7af7eacc9 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: fc2dfc9a44179023f2743c87c0bd7f9387728728 127.0.0.1:7006
slots: (0 slots) slave
replicates 8495f96bb7cecaa753af70b0bd76c060d1a69a77
M: 701c093b7045eedc542f68a1cbfb1c59f1b36c1a 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 892d5474496613876cbc78ac5f903f39db94e8de 127.0.0.1:7005
slots: (0 slots) slave
replicates 4cf1720b03f3db54abb8f0f1eeb743f7af7eacc9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]#

# 测试
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# redis01/redis-cli -p 7001 -c
127.0.0.1:7001> set key1 aaa
-> Redirected to slot [9189] located at 127.0.0.1:7002
OK
127.0.0.1:7002> keys *
1) "key1"

# 查看集群的信息
127.0.0.1:7002> cluster info

# 查看节点信息
127.0.0.1:7002> cluster nodes

相关错误

1
2
3
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# ./redis-trib.rb create --replicas 1 106.15.191.27:7001 106.15.191.27:7002 106.15.191.27:7003 106.15.191.27:7004 106.15.191.27:7005 106.15.191.27:7006
>>> Creating cluster
[ERR] Node 106.15.191.27:7002 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

解决:http://blog.csdn.net/vtopqx/article/details/50235737

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
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-cluster]# ./redis-trib.rb create --replicas 1 106.15.191.27:7001 106.15.191.27:7002 106.15.191.27:7003 106.15.191.27:7004 106.15.191.27:7005 106.15.191.27:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
106.15.191.27:7001
106.15.191.27:7002
106.15.191.27:7003
Adding replica 106.15.191.27:7005 to 106.15.191.27:7001
Adding replica 106.15.191.27:7006 to 106.15.191.27:7002
Adding replica 106.15.191.27:7004 to 106.15.191.27:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: f2387ef8a1ff7770623ae9545bea3ad9a632da52 106.15.191.27:7001
slots:0-5460 (5461 slots) master
M: 58ab432ad1e25b9699a2552442e6e1b84d07473c 106.15.191.27:7002
slots:5461-10922 (5462 slots) master
M: 8a8276597cc83b02d39f845f7a0bf51e0a7a27b3 106.15.191.27:7003
slots:10923-16383 (5461 slots) master
S: c292b2833e1c76d0b69da96ebf35d9113262f458 106.15.191.27:7004
replicates 58ab432ad1e25b9699a2552442e6e1b84d07473c
S: 2b94f8ede1a83302a719d6839c7b9ce79669e33f 106.15.191.27:7005
replicates 8a8276597cc83b02d39f845f7a0bf51e0a7a27b3
S: 16e3c1535950a7bc02bb45993a70250d3402db05 106.15.191.27:7006
replicates f2387ef8a1ff7770623ae9545bea3ad9a632da52
Can I set the above configuration? (type 'yes' to accept): yes
/usr/local/lib/ruby/gems/2.2.0/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Slot 9189 is already busy (Redis::CommandError)
from /usr/local/lib/ruby/gems/2.2.0/gems/redis-4.0.1/lib/redis.rb:2764:in `block in method_missing'
from /usr/local/lib/ruby/gems/2.2.0/gems/redis-4.0.1/lib/redis.rb:45:in `block in synchronize'
from /usr/local/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
from /usr/local/lib/ruby/gems/2.2.0/gems/redis-4.0.1/lib/redis.rb:45:in `synchronize'
from /usr/local/lib/ruby/gems/2.2.0/gems/redis-4.0.1/lib/redis.rb:2763:in `method_missing'
from ./redis-trib.rb:212:in `flush_node_config'
from ./redis-trib.rb:906:in `block in flush_nodes_config'
from ./redis-trib.rb:905:in `each'
from ./redis-trib.rb:905:in `flush_nodes_config'
from ./redis-trib.rb:1426:in `create_cluster_cmd'
from ./redis-trib.rb:1830:in `<main>'

解决:删除相关的 dump.rdb,nodes-700x.conf