必备的工具:
redis-3.0.0.tar
redis-3.0.0.gem(ruby和redis接口)
分析:
首先,集群数需要基数,这里搭建一个简单的redis集群(6个redis实例进行集群)。
在一台服务器上操作,因此仅需要6个不同的端口号即可。分别是:7001、7002、7003、7004、7005、7006。
步骤:
1、上传redis-3.0.0.tar到服务器(自己指定自己的软件目录),解压redis-3.0.0.tar。
2、安装c语言环境(安装centos之后,自带c语言环境)
yum install gcc-c++
3、进入到redis-3.0.0目录
make
4、安装redis到/usr/local/redis目录
make install PREFIX=/usr/local/redis
5、查看是否安装成功(出现bin目录即可)
6、前端启动redis(进入上图的bin目录)
./redis-server(开启)
./redis-cli shutdown
7、后端启动
需要将redis解压之后的源码包中(redis-3.0.0目录下)的redis.conf文件拷贝到bin目录下
修改redis.conf文件,将daemonize改为yes先要使用vim redis.conf
使用命令后端启动redis
bin目录下执行./redis-server redis.conf
查看是否启动成功
关闭后端启动的方式:
以上是单独的redis安装,接下来搭建集群!
安装ruby
yum install ruby
yum install rubygems
将以下文件上传到linux系统
进入该目录执行:gem install redis-3.0.0.gem
将redis-3.0.0包下src目录中的以下文件拷贝到/usr/local/redis/redis-cluster/
前提:先新建一个redis-cluster目录
搭建集群最少也得需要3台主机,如果每台主机再配置一台从机的话,则最少需要6台机器。
端口设计如下:7001-7006
第一步:复制出一个7001机器
进入/usr/local/redis/目录执行cp bin ./redis-cluster/7001–r
第二步:如果存在持久化文件,则删除
rm-rf appendonly.aof dump.rdb
第三步:设置集群参数
第四步:修改端口
第五步:复制出7002-7006机器
给出我桌面系统下的目录结构:
接下来设置一键启动全部redis或者关闭redis群:
首先,新建一个文件,可以用vim打开一个不存在的文件,然后保存就存在了。(上图的start-all.sh和shutdow-all.sh就是我新建的)
start-all的脚本内容:
shutdow-all.sh的脚本内容:
接下来修改那两个文件权限,设置其为可启动的脚本文件
chmod u +x start-all.sh
chmod u +x shutdown-all.sh
接下来用ruby连接这几个集群,进行管理
创建集群。
./redis-trib.rb create --replicas 1 192.168.188.131:7001 192.168.188.131:7002 192.168.188.131:7003 192.168.188.131:7004 192.168.188.131:7005 192.168.188.131:7006
出现:[root@bogon redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.188.131:7001 192.168.188.131:7002 192.168.188.131:7003 192.168.188.131:7004 192.168.188.131:7005 192.168.188.131:7006
>>> Creating cluster
Connecting to node 192.168.188.131:7001: OK
Connecting to node 192.168.188.131:7002: OK
Connecting to node 192.168.188.131:7003: OK
Connecting to node 192.168.188.131:7004: OK
Connecting to node 192.168.188.131:7005: OK
Connecting to node 192.168.188.131:7006: OK
>>> Performing hash slots allocation on 6 nodes... 连接6个节点成功
Using 3 masters: 7001-7003作为主节点
192.168.188.131:7001
192.168.188.131:7002
192.168.188.131:7003
Adding replica 192.168.188.131:7004 to 192.168.188.131:7001 7004作为7001的从节点
Adding replica 192.168.188.131:7005 to 192.168.188.131:7002 7005作为7002的从节点
Adding replica 192.168.188.131:7006 to 192.168.188.131:7003 7006作为7003的从节点
M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.188.131:7001
slots:0-5460 (5461 slots) master 给7001主节点分配0-5460 的槽
M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.188.131:7002
slots:5461-10922 (5462 slots) master 给7002主节点分配5461-10922 的槽
M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.188.131:7003
slots:10923-16383 (5461 slots) master 给7003主节点分配10923-16383 的槽
S: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.188.131:7004 7004,7005,7006是从节点槽和主节点的一样,从节点是备份用的
replicates 5a8523db7e12ca600dc82901ced06741b3010076
S: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.188.131:7005
replicates bf6f0929044db485dea9b565bb51e0c917d20a53
S: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.188.131:7006
replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca
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.188.131:7001)
M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.188.131:7001
slots:0-5460 (5461 slots) master
M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.188.131:7002
slots:5461-10922 (5462 slots) master
M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.188.131:7003
slots:10923-16383 (5461 slots) master
M: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.188.131:7004
slots: (0 slots) master
replicates 5a8523db7e12ca600dc82901ced06741b3010076
M: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.188.131:7005
slots: (0 slots) master
replicates bf6f0929044db485dea9b565bb51e0c917d20a53
M: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.188.131:7006
slots: (0 slots) master
replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Creating cluster
Connecting to node 192.168.188.131:7001: OK
Connecting to node 192.168.188.131:7002: OK
Connecting to node 192.168.188.131:7003: OK
Connecting to node 192.168.188.131:7004: OK
Connecting to node 192.168.188.131:7005: OK
Connecting to node 192.168.188.131:7006: OK
>>> Performing hash slots allocation on 6 nodes... 连接6个节点成功
Using 3 masters: 7001-7003作为主节点
192.168.188.131:7001
192.168.188.131:7002
192.168.188.131:7003
Adding replica 192.168.188.131:7004 to 192.168.188.131:7001 7004作为7001的从节点
Adding replica 192.168.188.131:7005 to 192.168.188.131:7002 7005作为7002的从节点
Adding replica 192.168.188.131:7006 to 192.168.188.131:7003 7006作为7003的从节点
M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.188.131:7001
slots:0-5460 (5461 slots) master 给7001主节点分配0-5460 的槽
M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.188.131:7002
slots:5461-10922 (5462 slots) master 给7002主节点分配5461-10922 的槽
M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.188.131:7003
slots:10923-16383 (5461 slots) master 给7003主节点分配10923-16383 的槽
S: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.188.131:7004 7004,7005,7006是从节点槽和主节点的一样,从节点是备份用的
replicates 5a8523db7e12ca600dc82901ced06741b3010076
S: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.188.131:7005
replicates bf6f0929044db485dea9b565bb51e0c917d20a53
S: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.188.131:7006
replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca
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.188.131:7001)
M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.188.131:7001
slots:0-5460 (5461 slots) master
M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.188.131:7002
slots:5461-10922 (5462 slots) master
M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.188.131:7003
slots:10923-16383 (5461 slots) master
M: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.188.131:7004
slots: (0 slots) master
replicates 5a8523db7e12ca600dc82901ced06741b3010076
M: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.188.131:7005
slots: (0 slots) master
replicates bf6f0929044db485dea9b565bb51e0c917d20a53
M: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.188.131:7006
slots: (0 slots) master
replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
则代表成功了!
测试一下,进入7001目录执行:./redis-cli -h 192.168.242.137 -p 7001 –c
注:连集群的时候必须加-c,否则set的时候会出现(error)MOVED的错误
图形化界面客户端
只支持单机版,不支持集群。可以在开发的时候连接单机版,生产环境换成集群