redis多节点集群搭建和使用:
环境:
ubuntu14.04
redis-3.0.4
七台物理机器
已经配置好ssh无密钥登录等基础条件
1.下载安装编译
wget http://download.redis.io/releases/redis-3.0.4.tar.gz
tar -zxvf redis-3.0.4.tar.gz
cp -R redis-3.0.4 ~/cloud
cd redis-3.0.4
make
make install
每个节点都需要进行安装
2.配置集群所需的工具:
sudo apt-get install ruby
sudo apt-get install rubygems-integration
sudo gem install redis
如果不配置会导致后面报错,错误类似于参考资料【2】
3.修噶redis.conf分发到每个节点
包括redis-server脚本,我把redis.conf和redis-server放在cluster文件夹下,cluster放在redis目录下。
redis.conf修改一下几个地方:
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
logfile ./redis.log
syslog-enabled yes
然后在每个节点的cluster文件夹运行:
./redis-server ./redis.conf
用ps -aux | grep redis会看到相对应的进程
4.启动集群:
在一个节点运行
./redis-trib.rb create --replicas 1 192.168.1.1:7000 192.168.1.2:7000 192.168.1.3:7000 192.168.1.4:7000 192.168.1.5:7000 192.168.1.6:7000 192.168.1.7:7000
会有参考资料【2】第“6.安装成功”部分类似的结果。再次不详细贴出
然后再运行:
cd redis-3.0.3/utils/create-cluster
create-cluster start
create-cluster create
create-cluster stop
查看:
hadoop@Master:~$ redis-cli -c -p 7000 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:7
cluster_my_epoch:1
cluster_stats_messages_sent:5959
cluster_stats_messages_received:5959
显示集群中有七个节点。
运行结果类似于参考资料【2】第“7.运行测试”部分的结果
本文于参考资料【2】很类似,所以不赘述。
5.数据测试:
自动配置好的信息
hadoop@Master:~/cloud/redis-3.0.4/src$ cd ../utils/
hadoop@Master:~/cloud/redis-3.0.4/utils$ ls
build-static-symbols.tcl generate-command-help.rb lru redis_init_script speed-regression.tcl
cluster_fail_time.tcl hyperloglog mkrelease.sh redis_init_script.tpl whatisdoing.sh
create-cluster install_server.sh redis-copy.rb redis-sha1.rb
hadoop@Master:~/cloud/redis-3.0.4/utils$ cd create-cluster/
hadoop@Master:~/cloud/redis-3.0.4/utils/create-cluster$ ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
hadoop@Master:~/cloud/redis-3.0.4/utils/create-cluster$ ./create-cluster create
>>> Creating cluster
Connecting to node 127.0.0.1:30001: OK
Connecting to node 127.0.0.1:30002: OK
Connecting to node 127.0.0.1:30003: OK
Connecting to node 127.0.0.1:30004: OK
Connecting to node 127.0.0.1:30005: OK
Connecting to node 127.0.0.1:30006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:30001
127.0.0.1:30002
127.0.0.1:30003
Adding replica 127.0.0.1:30004 to 127.0.0.1:30001
Adding replica 127.0.0.1:30005 to 127.0.0.1:30002
Adding replica 127.0.0.1:30006 to 127.0.0.1:30003
M: 30158345e444e08268c2099cbe2abb90efa4585d 127.0.0.1:30001
slots:0-5460 (5461 slots) master
M: 3b37b1d2fb4e2147f4e380c5c249907030001f32 127.0.0.1:30002
slots:5461-10922 (5462 slots) master
M: 8b0e72fb39e28bd1434f9664074193f5936ab786 127.0.0.1:30003
slots:10923-16383 (5461 slots) master
S: a06a0f09efa4da674ee147b16938ac7d53e83902 127.0.0.1:30004
replicates 30158345e444e08268c2099cbe2abb90efa4585d
S: 14626ac528e076aa69d84b3921bd7d91836be5ba 127.0.0.1:30005
replicates 3b37b1d2fb4e2147f4e380c5c249907030001f32
S: ed1c3e8dad49fde8bb07617604c1d5c85c0fcfcd 127.0.0.1:30006
replicates 8b0e72fb39e28bd1434f9664074193f5936ab786
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:30001)
M: 30158345e444e08268c2099cbe2abb90efa4585d 127.0.0.1:30001
slots:0-5460 (5461 slots) master
M: 3b37b1d2fb4e2147f4e380c5c249907030001f32 127.0.0.1:30002
slots:5461-10922 (5462 slots) master
M: 8b0e72fb39e28bd1434f9664074193f5936ab786 127.0.0.1:30003
slots:10923-16383 (5461 slots) master
M: a06a0f09efa4da674ee147b16938ac7d53e83902 127.0.0.1:30004
slots: (0 slots) master
replicates 30158345e444e08268c2099cbe2abb90efa4585d
M: 14626ac528e076aa69d84b3921bd7d91836be5ba 127.0.0.1:30005
slots: (0 slots) master
replicates 3b37b1d2fb4e2147f4e380c5c249907030001f32
M: ed1c3e8dad49fde8bb07617604c1d5c85c0fcfcd 127.0.0.1:30006
slots: (0 slots) master
replicates 8b0e72fb39e28bd1434f9664074193f5936ab786
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
hadoop@Master:~/cloud/redis-3.0.4/utils/create-cluster$ ps -aux |grep redis
hadoop 12472 0.0 0.1 42308 9092 ? Ssl 20:06 0:00 ./redis-server *:7000 [cluster]
hadoop 12540 0.1 0.1 42312 8872 ? Ssl 20:08 0:00 ../../src/redis-server *:30001 [cluster]
hadoop 12542 0.1 0.1 42312 8948 ? Ssl 20:08 0:00 ../../src/redis-server *:30002 [cluster]
hadoop 12544 0.1 0.1 42312 8968 ? Ssl 20:08 0:00 ../../src/redis-server *:30003 [cluster]
hadoop 12546 0.1 0.1 42312 9056 ? Ssl 20:08 0:00 ../../src/redis-server *:30004 [cluster]
hadoop 12548 0.1 0.1 42312 9068 ? Ssl 20:08 0:00 ../../src/redis-server *:30005 [cluster]
hadoop 12550 0.1 0.1 42312 9064 ? Ssl 20:08 0:00 ../../src/redis-server *:30006 [cluster]
hadoop 12601 0.0 0.0 15948 2352 pts/38 S+ 20:09 0:00 grep --color=auto redis
hadoop@Master:~/cloud/redis-3.0.4/utils/create-cluster$
在集群写入一些信息,然后查看那分布:
192.168.1.7 别名 Master
192.168.1.6 别名 Mcnode6
192.168.1.5 别名 Mcnode5
192.168.1.4 别名 Mcnode4
192.168.1.3 别名 Mcnode3
192.168.1.2 别名 Mcnode2
192.168.1.1 别名 Mcnode1
hadoop@Master:~/cloud/redis-3.0.4/utils/create-cluster$ redis-cli -c -p 7000
127.0.0.1:7000> keys *
1) "x3"
127.0.0.1:7000>
hadoop@Mcnode1:~$ redis-cli -c -p 7000
127.0.0.1:7000> keys *
1) "x3"
127.0.0.1:7000>
hadoop@Mcnode2:~/cloud/redis-3.0.4$ redis-cli -c -p 7000
127.0.0.1:7000> keys *
1) "x2"
2) "x1"
3) "xubo1"
127.0.0.1:7000>
hadoop@Mcnode3:~/cloud/redis-3.0.4$ redis-cli -c -p 7000
127.0.0.1:7000> keys *
1) "x4"
127.0.0.1:7000>
hadoop@Mcnode4:~/cloud/redis-3.0.4$ redis-cli -c -p 7000
127.0.0.1:7000> keys *
1) "x3"
127.0.0.1:7000>
hadoop@Mcnode5:~/cloud/redis-3.0.4$ redis-cli -c -p 7000
127.0.0.1:7000> keys *
1) "x1"
2) "x2"
3) "xubo1"
127.0.0.1:7000>
可以看出来符合自动配置的。
有点值得注意:7个节点,replicas 是1,也就是每个master有一个slave,但是Mcnode2有两个slave,包括Mcnode1和Master。
hadoop@Mcnode6:~/cloud/redis-3.0.4$ redis-cli -c -p 7000
127.0.0.1:7000> keys *
1) "x4"
127.0.0.1:7000>
6.错误:
除了参考资料【1】和【2】出现的问题,还出现了;
S: 58fbf75d87c71c17adfa721811e6d4563f71a7b7 127.0.0.1:7000
replicates d8de85877ad04c693eaac285036c0452d39116d3
Can I set the above configuration? (type 'yes' to accept): yes
/var/lib/gems/1.9.1/gems/redis-3.2.1/lib/redis/client.rb:113:in `call': ERR Slot 0 is already busy (Redis::CommandError)
from /var/lib/gems/1.9.1/gems/redis-3.2.1/lib/redis.rb:2556:in `block in method_missing'
from /var/lib/gems/1.9.1/gems/redis-3.2.1/lib/redis.rb:37:in `block in synchronize'
from /usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /var/lib/gems/1.9.1/gems/redis-3.2.1/lib/redis.rb:37:in `synchronize'
from /var/lib/gems/1.9.1/gems/redis-3.2.1/lib/redis.rb:2555:in `method_missing'
from ./redis-trib.rb:205:in `flush_node_config'
from ./redis-trib.rb:657:in `block in flush_nodes_config'
from ./redis-trib.rb:656:in `each'
from ./redis-trib.rb:656:in `flush_nodes_config'
from ./redis-trib.rb:997:in `create_cluster_cmd'
from ./redis-trib.rb:1373:in `<main>'
原因:因为是从单节点的集群直接拷贝过来的cluster文件夹,里面有运行过的nodes.conf等文件。解决办法:删除不相干的文件,重新操作运行即可成功。
参考资料
【1】 http://blog.csdn.net/xubo245/article/details/48318735
【2】http://blog.csdn.net/xubo245/article/details/48349043
【3】 http://redis.io/topics/cluster-tutorial
【4】 http://redis.io/topics/cluster-spec
【5】 http://blog.csdn.net/xu470438000/article/details/42971091