至少准备6台redis服务:
[root@localhost ~]# cd /var/redis/
[root@localhost redis]# ls
6379 6380 6381
[root@localhost redis]# mkdir 6001 6002 6003 6004 6005 6006
[root@localhost redis]# ls
6001 6002 6003 6004 6005 6006 6379 6380 6381
[root@localhost redis]# cd /etc/redis/
[root@localhost redis]# ls
6379.conf 6380.conf 6381.conf
[root@localhost redis]# mkdir 6001 6002 6003 6004 6005 6006
[root@localhost redis]# cp 6379.conf 6001/redis.conf
[root@localhost redis]# cp 6379.conf 6002/redis.conf
[root@localhost redis]# cp 6379.conf 6003/redis.conf
[root@localhost redis]# cp 6379.conf 6004/redis.conf
[root@localhost redis]# cp 6379.conf 6005/redis.conf
[root@localhost redis]# cp 6379.conf 6006/redis.conf
[root@localhost redis]# vim 6001/redis.conf //分别修改redis端口号
[root@localhost redis]# vim 6002/redis.conf
[root@localhost redis]# vim 6003/redis.conf
[root@localhost redis]# vim 6004/redis.conf
[root@localhost redis]# vim 6005/redis.conf
[root@localhost redis]# vim 6006/redis.conf
[root@localhost redis]# vim 6005/redis.conf
[root@localhost redis]# redis-server 6001/redis.conf
[root@localhost redis]# redis-server 6002/redis.conf
[root@localhost redis]# redis-server 6003/redis.conf
[root@localhost redis]# redis-server 6004/redis.conf
[root@localhost redis]# redis-server 6005/redis.conf
[root@localhost redis]# redis-server 6006/redis.conf
[root@localhost redis]# ps -ef | grep redis
root 13104 1 0 16:25 ? 00:00:00 redis-server *:6379
root 13115 1 0 16:27 ? 00:00:00 redis-server *:6002 [cluster]
root 13119 1 0 16:27 ? 00:00:00 redis-server *:6003 [cluster]
root 13123 1 0 16:27 ? 00:00:00 redis-server *:6004 [cluster]
root 13127 1 0 16:28 ? 00:00:00 redis-server *:6005 [cluster]
root 13131 1 0 16:28 ? 00:00:00 redis-server *:6006 [cluster]
root 13135 6204 0 16:28 pts/0 00:00:00 grep redis
[root@localhost redis]# /usr/src/redis-3.2.6/src/redis-trib.rb create --
/usr/bin/env: ruby: No such file or directory
[root@localhost redis]# yum install ruby
[root@localhost redis]# /usr/src/redis-3.2.6/src/redis-trib.rb create --
/usr/src/redis-3.2.6/src/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from /usr/src/redis-3.2.6/src/redis-trib.rb:24
[root@localhost redis]# yum install rubygems
[root@localhost redis]# /usr/src/redis-3.2.6/src/redis-trib.rb create -
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/src/redis-3.2.6/src/redis-trib.rb:25
[root@localhost redis]# gem install redis
Successfully installed redis-3.3.3
1 gem installed
Installing ri documentation for redis-3.3.3...
Installing RDoc documentation for redis-3.3.3...
创建redis cluster:
[root@localhost redis]# /usr/src/redis-3.2.6/src/redis-trib.rb create --replicas 1 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006
>>> Creating cluster
[ERR] Sorry, can't connect to node 127.0.0.1:6001
//设置redis密码后需要修改ruby里面的redis/client.rb文件
[root@localhost redis]# vim /usr/lib/ruby/gems/1.8/gems/redis-3.3.3/lib/redis/client.rb
:password => "pwd123",
[root@localhost redis]# /usr/src/redis-3.2.6/src/redis-trib.rb create --replicas 1 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6001
127.0.0.1:6002
127.0.0.1:6003
Adding replica 127.0.0.1:6004 to 127.0.0.1:6001
Adding replica 127.0.0.1:6005 to 127.0.0.1:6002
Adding replica 127.0.0.1:6006 to 127.0.0.1:6003
M: af968aae6088b3567fe710f67f28e256905a93ad 127.0.0.1:6001
slots:0-5460 (5461 slots) master
M: b2e52bcd8c1c8eedd21c4480388892d1123083e4 127.0.0.1:6002
slots:5461-10922 (5462 slots) master
M: 0b3aa8f44e82c8edbe54b0d0ae0c2af335404607 127.0.0.1:6003
slots:10923-16383 (5461 slots) master
S: 85fc1084f0505147c19b247eac87f962a3e971d8 127.0.0.1:6004
replicates af968aae6088b3567fe710f67f28e256905a93ad
S: 4cdb41f3f8d7555b77c6ece45fbb107214dbfe08 127.0.0.1:6005
replicates b2e52bcd8c1c8eedd21c4480388892d1123083e4
S: b40ae6a26c1b0b3ee00e99249e2a11cb4c74d54a 127.0.0.1:6006
replicates 0b3aa8f44e82c8edbe54b0d0ae0c2af335404607
Can I set the above configuration? (type 'yes' to accept): yes //输入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:6001)
M: af968aae6088b3567fe710f67f28e256905a93ad 127.0.0.1:6001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 85fc1084f0505147c19b247eac87f962a3e971d8 127.0.0.1:6004
slots: (0 slots) slave
replicates af968aae6088b3567fe710f67f28e256905a93ad
S: b40ae6a26c1b0b3ee00e99249e2a11cb4c74d54a 127.0.0.1:6006
slots: (0 slots) slave
replicates 0b3aa8f44e82c8edbe54b0d0ae0c2af335404607
M: 0b3aa8f44e82c8edbe54b0d0ae0c2af335404607 127.0.0.1:6003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 4cdb41f3f8d7555b77c6ece45fbb107214dbfe08 127.0.0.1:6005
slots: (0 slots) slave
replicates b2e52bcd8c1c8eedd21c4480388892d1123083e4
M: b2e52bcd8c1c8eedd21c4480388892d1123083e4 127.0.0.1:6002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看cluster状态:
[root@localhost redis]# redis-cli -c -p 6001 -a pwd123
127.0.0.1:6001> info
# Server
redis_version:3.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:28793ea525035c6f
redis_mode:cluster
[root@localhost redis]# redis-cli -c -p 6001 -a pwd123 cluster nodes | grep master
af968aae6088b3567fe710f67f28e256905a93ad 127.0.0.1:6001 myself,master - 0 0 1 connected 0-5460
0b3aa8f44e82c8edbe54b0d0ae0c2af335404607 127.0.0.1:6003 master - 0 1486371963996 3 connected 10923-16383
b2e52bcd8c1c8eedd21c4480388892d1123083e4 127.0.0.1:6002 master - 0 1486371964498 2 connected 5461-10922
验证redis群集:
[root@localhost redis]# redis-cli -c -p 6001 -a pwd123
127.0.0.1:6001> set key val
-> Redirected to slot [12539] located at 127.0.0.1:6003
OK
127.0.0.1:6003> quit
[root@localhost redis]# redis-cli -c -p 6002 -a pwd123
127.0.0.1:6002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:6001
OK
127.0.0.1:6001> get hello
"world"
127.0.0.1:6001> quit
[root@localhost redis]# redis-cli -c -p 6001 -a pwd123
127.0.0.1:6001> get hello
"world"
127.0.0.1:6001> get key
-> Redirected to slot [12539] located at 127.0.0.1:6003
"val"
127.0.0.1:6003> quit
[root@localhost redis]# redis-cli -c -p 6002 -a pwd123
127.0.0.1:6002> get key
-> Redirected to slot [12539] located at 127.0.0.1:6003
"val"