ubtuntu redis 集群部署/搭建(官方原始方案)
1. 安装ruby环境
apt-get install ruby gem install redis
2. 安装redis
3. 创建集群文件及文件夹
mkdir redis-cluster cd redis-cluster mkdir 9000 9001 9002 9003 9004 9005
结果如下图所示:
4. 拷贝redis.conf
ubuntu redis配置文件路径
cd /etc/redis
进入redis配置文件目录,将redis.conf复制redis-cluster文件夹中
cp redis.conf /home/user/redis-cluster
4.1主要修改信息如下:
port 9000 # 端口号和文件夹名一样 bind 127.0.0.1 # 绑定的ip 可以使用127.0.0.1 也可以使用192.168.0.1 dir /home/user/redis-cluster/9000 # 备份文件名存储位置 cluster-enabled yes # 开启集群 cluster-config-file nodes-9000.conf # 配置文件名 appendonly yes # 开启
不同信息在redis.conf位置:
4.2 ip绑定 绑定本机ip
4.2修改端口号
4.3 开启
4.4 数据存储位置
dbfilename 备份文件名
4.5 开启集群
4.6 配置文件名
然后分别将redis0cluster中redis.conf配置文件复制到新建的6个文件夹中(9000,9001,9002,9003,9004,9005)
复制redis.conf配置文件
cp redis.conf /home/user/redis-cluster/9000 cp redis.conf /home/user/redis-cluster/9001 cp redis.conf /home/user/redis-cluster/9002 cp redis.conf /home/user/redis-cluster/9003 cp redis.conf /home/user/redis-cluster/9004 cp redis.conf /home/user/redis-cluster/9005
5. 分别启动redis-server
redis-server /home/user/redis-cluster/9000/redis.conf redis-server /home/user/redis-cluster/9001/redis.conf redis-server /home/user/redis-cluster/9002/redis.conf redis-server /home/user/redis-cluster/9003/redis.conf redis-server /home/user/redis-cluster/9004/redis.conf redis-server /home/user/redis-cluster/9005/redis.conf
6. 查看redis-server是否正常启动
ps -ef|grep redis-server
结果如下
7. 创建redis集群
redis-trib.rb(能否创建集群主要靠它)存放的路径
cd /usr/share/doc/redis-tools/examples/
复制redis-trib.rb到指定目录
cp redis-trib.rb /home/user/redis-cluster/
redis集群启动
启动集群命令
./redis-trib.rb create --replicas 1 127.0.0.1:9000 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9004 127.0.0.1:9005
结果如下:
>>> ./redis-trib.rb create --replicas 1 127.0.0.1:9000 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9004 127.0.0.1:9005 >>> Creating cluster # 自动输入的命令 >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:9000 127.0.0.1:9001 127.0.0.1:9002 Adding replica 127.0.0.1:9004 to 127.0.0.1:9000 Adding replica 127.0.0.1:9005 to 127.0.0.1:9001 Adding replica 127.0.0.1:9003 to 127.0.0.1:9002 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 56b766b9e4b8447c2f0645e16b82283f6c7c11d7 127.0.0.1:9000 slots:0-5460 (5461 slots) master M: 89af1208bca79d22f9c051bb86d40de1ebaf92b4 127.0.0.1:9001 slots:5461-10922 (5462 slots) master M: c95629b3e17abaf3b0cef022359b49f4498a4d8a 127.0.0.1:9002 slots:10923-16383 (5461 slots) master S: 736a7abdbec3fd16bd610900cfed8ecd7f9b008b 127.0.0.1:9003 replicates 56b766b9e4b8447c2f0645e16b82283f6c7c11d7 S: 5f94b10bfe0ba249767da82161bdfdb14b5092aa 127.0.0.1:9004 replicates 89af1208bca79d22f9c051bb86d40de1ebaf92b4 S: 58481f44f290203a644503946b3d6f2b9bd53916 127.0.0.1:9005 replicates c95629b3e17abaf3b0cef022359b49f4498a4d8a 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:9000) M: 56b766b9e4b8447c2f0645e16b82283f6c7c11d7 127.0.0.1:9000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 736a7abdbec3fd16bd610900cfed8ecd7f9b008b 127.0.0.1:9003 slots: (0 slots) slave replicates 56b766b9e4b8447c2f0645e16b82283f6c7c11d7 S: 5f94b10bfe0ba249767da82161bdfdb14b5092aa 127.0.0.1:9004 slots: (0 slots) slave replicates 89af1208bca79d22f9c051bb86d40de1ebaf92b4 S: 58481f44f290203a644503946b3d6f2b9bd53916 127.0.0.1:9005 slots: (0 slots) slave replicates c95629b3e17abaf3b0cef022359b49f4498a4d8a M: 89af1208bca79d22f9c051bb86d40de1ebaf92b4 127.0.0.1:9001 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: c95629b3e17abaf3b0cef022359b49f4498a4d8a 127.0.0.1:9002 slots:10923-16383 (5461 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.
8. 连接集群
redis-cli -c -h 127.0.0.1 -p 9000
注意:如果遇到MOVED 5798错误
请阅读:http://www.chenxm.cc/post/695.html
总结:
这是一篇完整的Redis集群部署,只要按照文章中的步骤做,那么保证你能成功搭建一个redis集群。
接下来可能就是动态扩容、增加节点和减少节点,重新分配槽大小等,当然,还有最重要的就是怎么和我们程序结合起来,以及如何更好的把 Redis 缓存集群发挥出应有的效果,这些才是最重要的。