redis集群搭建
Posted 精品唯居
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis集群搭建相关的知识,希望对你有一定的参考价值。
为了保证可以进行投票,需要至少3个主节点。每个主节点都需要至少一个从节点,所以需要至少3个从节点所以一共需要6台redis服务器,为了模拟6个redis节点,我们可以使用6个redis实例作为节点。6个redis实例的端口号,7001~7006.
首先在我们的linux中安装一个redis,如果不会的同学,可以去看我之前的博客《redis安装-单机版》然后:
1. 把bin目录里面的rdb,和aof文件删除,准备干净的redis
[root@localhost bin]# rm -rf appendonly.aof
[root@localhost bin]# rm -f dump.rdb
完事效果如下:
退出到上级目录,将bin复制6份
把bin复制6份
[root@localhost bin]# cd ..
如果没有redis-cluster文件夹的话需要创建一个
[root@localhost redis]# mkdir redis-cluster
[root@localhost redis]# cp -r bin redis-cluster/redis1
[root@localhost redis]# cp -r bin redis-cluster/redis2
[root@localhost redis]# cp -r bin redis-cluster/redis3
[root@localhost redis]# cp -r bin redis-cluster/redis4
[root@localhost redis]# cp -r bin redis-cluster/redis5
[root@localhost redis]# cp -r bin redis-cluster/redis6
这个时候,我们进入redis-cluster目录下看到的结果是这样的:
现在,为了让这6个redis节点运行的时候不起节点的冲突,我们需要修改它们的节点端口号。将端口号一次修改为7001~7006
首先打开第一个节点的redis.conf 配置文件:[root@itcast-01 redis-cluster]# vim redis1/redis.conf
将端口号修改为7001(将这的6379改为7001)
剩下的代码用同样的方法修改即可。
分别启动6个redis节点的服务,或者是写一个脚本,然后给脚本附上执行权限,然后用脚本启动也可以。
准备集群安装环境
redis集群的管理工具使用的是ruby脚本语言,安装集群需要ruby环境。
安装ruby环境[root@itcast-01 redis-cluster]# yum install ruby
安装Ruby的打包系统[root@itcast-01 redis-cluster]# yum install rubygems
下载一个redis-3.0.0.gem的文件,然后将文件上传,再执行命令安转。
[root@itcast-01 ~]# gem install redis-3.0.0.gem
安装集群
修改这6个实例的redis.conf配置文件,开启redis-cluster
[root@localhost redis-cluster]# vim redis1/redis.conf
然后再次重启实例
集群管理工具在redis解压文件夹的src的文件夹中,使用redis-cluster的集群管理工具启动集群
先进入集群管理工具所在的路径:[root@localhost redis-cluster]# cd /root/redis-3.0.0/src/
再启动命令:[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.37.161:7001 192.168.37.161:7002 192.168.37.161:7003 192.168.37.161:7004 192.168.37.161:7005 192.168.37.161:7006
此时的启动信息如下:
>>> Creating cluster
Connecting to node 192.168.37.131:7001: OK
Connecting to node 192.168.37.131:7002: OK
Connecting to node 192.168.37.131:7003: OK
Connecting to node 192.168.37.131:7004: OK
Connecting to node 192.168.37.131:7005: OK
Connecting to node 192.168.37.131:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.37.131:7001
192.168.37.131:7002
192.168.37.131:7003
Adding replica 192.168.37.131:7004 to 192.168.37.131:7001
Adding replica 192.168.37.131:7005 to 192.168.37.131:7002
Adding replica 192.168.37.131:7006 to 192.168.37.131:7003
M: 955567f988830cdf4328854f815719ea28082ca8 192.168.37.131:7001
slots:0-5460 (5461 slots) master
M: 4f3eeced04b930aa48193699301745a05a70697a 192.168.37.131:7002
slots:5461-10922 (5462 slots) master
M: 871a684dbbc0f43dcc16107710d7bd2f4e6de76a 192.168.37.131:7003
slots:10923-16383 (5461 slots) master
S: baca3ce2223dfcd9c636a7193b12998b1dbb2431 192.168.37.131:7004
replicates 955567f988830cdf4328854f815719ea28082ca8
S: 0b599863ddf2e03b0326c75b874a1af8ae430d2e 192.168.37.131:7005
replicates 4f3eeced04b930aa48193699301745a05a70697a
S: 92f712d954c62f2743e2e572f6582a6ef9a163e0 192.168.37.131:7006
replicates 871a684dbbc0f43dcc16107710d7bd2f4e6de76a
Can I set the above configuration? (type \'yes\' to accept):
>>> Creating cluster
Connecting to node 192.168.37.131:7001: OK
Connecting to node 192.168.37.131:7002: OK
Connecting to node 192.168.37.131:7003: OK
Connecting to node 192.168.37.131:7004: OK
Connecting to node 192.168.37.131:7005: OK
Connecting to node 192.168.37.131:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.37.131:7001
192.168.37.131:7002
192.168.37.131:7003
Adding replica 192.168.37.131:7004 to 192.168.37.131:7001
Adding replica 192.168.37.131:7005 to 192.168.37.131:7002
Adding replica 192.168.37.131:7006 to 192.168.37.131:7003
M: 955567f988830cdf4328854f815719ea28082ca8 192.168.37.131:7001
slots:0-5460 (5461 slots) master
M: 4f3eeced04b930aa48193699301745a05a70697a 192.168.37.131:7002
slots:5461-10922 (5462 slots) master
M: 871a684dbbc0f43dcc16107710d7bd2f4e6de76a 192.168.37.131:7003
slots:10923-16383 (5461 slots) master
S: baca3ce2223dfcd9c636a7193b12998b1dbb2431 192.168.37.131:7004
replicates 955567f988830cdf4328854f815719ea28082ca8
S: 0b599863ddf2e03b0326c75b874a1af8ae430d2e 192.168.37.131:7005
replicates 4f3eeced04b930aa48193699301745a05a70697a
S: 92f712d954c62f2743e2e572f6582a6ef9a163e0 192.168.37.131:7006
replicates 871a684dbbc0f43dcc16107710d7bd2f4e6de76a
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 192.168.37.131:7001)
M: 955567f988830cdf4328854f815719ea28082ca8 192.168.37.131:7001
slots:0-5460 (5461 slots) master
M: 4f3eeced04b930aa48193699301745a05a70697a 192.168.37.131:7002
slots:5461-10922 (5462 slots) master
M: 871a684dbbc0f43dcc16107710d7bd2f4e6de76a 192.168.37.131:7003
slots:10923-16383 (5461 slots) master
M: baca3ce2223dfcd9c636a7193b12998b1dbb2431 192.168.37.131:7004
slots: (0 slots) master
replicates 955567f988830cdf4328854f815719ea28082ca8
M: 0b599863ddf2e03b0326c75b874a1af8ae430d2e 192.168.37.131:7005
slots: (0 slots) master
replicates 4f3eeced04b930aa48193699301745a05a70697a
M: 92f712d954c62f2743e2e572f6582a6ef9a163e0 192.168.37.131:7006
slots: (0 slots) master
replicates 871a684dbbc0f43dcc16107710d7bd2f4e6de76a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
这时,我们的集群就算是安装成功了。
测试
使用redis命令行客户端连接
[root@localhost bin]# ./redis-cli -h 192.168.37.131 -p 7006 -c
192.168.37.131:7006> set hello money
-> Redirected to slot [866] located at 192.168.37.131:7001
OK
一定要加-c参数,节点之间就可以互相跳转
这就表示我们的集群搭建成功了,而且,自动将访问7001端口的那个请求跳转到7006.并且保存成功。
以上是关于redis集群搭建的主要内容,如果未能解决你的问题,请参考以下文章