前言
- ubuntu下搭建redis集群步骤
步骤
准备工作
- redis的集群管理工具依赖于ruby,先安装
sudo apt-get install ruby
- 上传redis和ruby的接口配置文件
redis-3.0.0.gem
- 安装gem
gem install ./redis-3.0.0.gem
在/usr/local下创建redis-cluster目录
- 在redis-cluster下面创建redis01-redis06 共6个子目录。
每一个子目录中拷贝一个redis的bin文件目录
cp -r bin /usr/local/redis-cluster/redis01
- 修改redis01下面的redis.conf配置
port 7001
cluster-enabled yes
- 编写脚本批量启动redis实例
#!/bin/bash
echo '开始配置redis集群'
cd redis01
redis-server ./redis.conf
echo "启动redis实例1"
cd ..
cd redis02
redis-server ./redis.conf
echo "启动redis实例2"
cd ..
cd redis03
redis-server ./redis.conf
echo "启动redis实例3"
cd ..
cd redis04
redis-server ./redis.conf
cd ..
echo "启动redis实例4"
cd redis05
redis-server ./redis.conf
cd ..
echo "启动redis实例5"
cd redis06
redis-server ./redis.conf
cd ..
echo "启动reddis实例6"
- 运行脚本
sh startall.sh
ps -ef|grep redis
- 查看redis启动情况
root@ubuntu:/usr/local/redis-cluster# ps -ef|grep redis
root 11173 1 0 05:05 ? 00:00:14 redis-server *:6379
root 13769 1 0 07:51 ? 00:00:00 redis-server *:7001 [cluster]
root 13773 1 0 07:51 ? 00:00:00 redis-server *:7002 [cluster]
root 13775 1 0 07:51 ? 00:00:00 redis-server *:7003 [cluster]
root 13777 1 0 07:51 ? 00:00:00 redis-server *:7004 [cluster]
root 13783 1 0 07:51 ? 00:00:00 redis-server *:7005 [cluster]
root 13785 1 0 07:51 ? 00:00:00 redis-server *:7006 [cluster]
root 13810 7920 0 07:52 pts/33 00:00:00 grep --color=auto redis
- 拷贝redis src下面的redis-trib.rb文件到集群目录
cp *rb /usr/local/redis-cluster
- 执行部署命令 ip为服务器的ip
./redis-trib.rb create --replicas 1 192.168.21.133:7001 192.168.21.133:7002 192.168.21.133:7003 192.168.21.133:7004 192.168.21.133:7005 192.168.21.133:7006
- 如果提示错误
- 将需要新增的节点下aof、rdb等本地备份文件删除;
[ERR] Node 192.168.21.133:7002 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
默认分配主从询问后,配置出错解决方式
192.168.21.133:7001
192.168.21.133:7002
192.168.21.133:7003
Adding replica 192.168.21.133:7004 to 192.168.21.133:7001
Adding replica 192.168.21.133:7005 to 192.168.21.133:7002
Adding replica 192.168.21.133:7006 to 192.168.21.133:7003
M: 57cd2fd63d76ea3bb85b49ab34c7a2fa2e92d35c 192.168.21.133:7001
slots:0-5460 (5461 slots) master
M: 55c50c08c44ca69b0e9db2d9a1dffcf4b97a8bad 192.168.21.133:7002
slots:5461-10922,15495 (5463 slots) master
M: a7f4ee2f7570b0c94e1ebbaf9c9a75208a224654 192.168.21.133:7003
slots:10923-16383 (5461 slots) master
S: 9ed6d356b835f7d1239f95eb61b0f90606f5f844 192.168.21.133:7004
replicates 57cd2fd63d76ea3bb85b49ab34c7a2fa2e92d35c
S: b8496267269c1c5670ed33ddb855d33a714cd876 192.168.21.133:7005
replicates 55c50c08c44ca69b0e9db2d9a1dffcf4b97a8bad
S: cf3b7d828fa6d392d5ebd9c905fcbe074e3b3f8d 192.168.21.133:7006
replicates a7f4ee2f7570b0c94e1ebbaf9c9a75208a224654
Can I set the above configuration? (type 'yes' to accept):
- 输入yes。没有创建成功
- 创建集群时报某个err slot 0 is already busy (redis:commanderror)
这是由于之间创建集群没有成功,需要将nodes.conf和dir里面的文件全部删除(注意不要删除了redis.conf)
- 创建集群时报某个err slot 0 is already busy (redis:commanderror)
- 进入每个集群节点,然后执行如下命令
127.0.0.1:7006> flushall
OK
127.0.0.1:7006> cluster reset
OK
错误:
/var/lib/gems/1.9.1/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is already busy (Redis::CommandError)
正确的输出
>> 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 192.168.21.133:7001)
M: 57cd2fd63d76ea3bb85b49ab34c7a2fa2e92d35c 192.168.21.133:7001
slots:0-5460 (5461 slots) master
M: 55c50c08c44ca69b0e9db2d9a1dffcf4b97a8bad 192.168.21.133:7002
slots:5461-10922 (5462 slots) master
M: a7f4ee2f7570b0c94e1ebbaf9c9a75208a224654 192.168.21.133:7003
slots:10923-16383 (5461 slots) master
M: 9ed6d356b835f7d1239f95eb61b0f90606f5f844 192.168.21.133:7004
slots: (0 slots) master
replicates 57cd2fd63d76ea3bb85b49ab34c7a2fa2e92d35c
M: b8496267269c1c5670ed33ddb855d33a714cd876 192.168.21.133:7005
slots: (0 slots) master
replicates 55c50c08c44ca69b0e9db2d9a1dffcf4b97a8bad
M: cf3b7d828fa6d392d5ebd9c905fcbe074e3b3f8d 192.168.21.133:7006
slots: (0 slots) master
replicates a7f4ee2f7570b0c94e1ebbaf9c9a75208a224654
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
测试集群
- 可以使用任意一个节点
redis-cli -h 192.168.21.133 -p 7001 -c
192.168.21.133:7001> set a 1000
-> Redirected to slot [15495] located at 192.168.21.133:7003
OK
192.168.21.133:7003> set b huangyunquan
-> Redirected to slot [3300] located at 192.168.21.133:7001
OK
192.168.21.133:7001> get b
"huangyunquan"
192.168.21.133:7001> get a
-> Redirected to slot [15495] located at 192.168.21.133:7003
"1000"
192.168.21.133:7003>