Redis5.0以上跳过ruby环境搭建
环境准备
安装ruby环境
yum -y install ruby
yum -y install rubygems
使用filezilla上传redis-4.1.2.gem至/usr/local下
开始安装
gem install /usr/local/redis-4.1.2.gem
解决redis requires ruby version 2.3.0
1.在/usr/local/redis路径创建redis-cluster目录
mkdir redis-cluster
2.把redis源码src文件夹中的redis-trib.rb复制到/usr/local/redis/redis-cluster目录下
cp redis-trib.rb /usr/local/redis/redis-cluster/ -r
搭建集群最少也得需要3台主机,如果每台主机再配置一台从机的话,则最少需要6台机器。
端口设计如下:7001-7006
1.复制出一个7001机器
cp bin ./redis-cluster/7001 –r
2.如果存在持久化文件,则删除
rm -rf appendonly.aof dump.rdb
3.设置集群参数,删除cluster-enabled前面的#
4.修改端口
5.复制出7002-7006机器
[root@localhost redis-cluster]# cp 7001/ 7002 -r
[root@localhost redis-cluster]# cp 7001/ 7003 -r
[root@localhost redis-cluster]# cp 7001/ 7004 -r
[root@localhost redis-cluster]# cp 7001/ 7005 -r
[root@localhost redis-cluster]# cp 7001/ 7006 -r
6.修改7002-7006机器的端口
7.启动7001-7006这六台机器
在/usr/local/redis/redis-cluster创建startall.sh,并写入以下内容
cd 7001
./redis-server redis.conf
cd ..
cd 7002
./redis-server redis.conf
cd ..
cd 7003
./redis-server redis.conf
cd ..
cd 7004
./redis-server redis.conf
cd ..
cd 7005
./redis-server redis.conf
cd ..
cd 7006
./redis-server redis.conf
cd ..
修改startall.sh权限
chmod u+x startall.sh
启动
./startall.sh
查看redis启动
8.创建集群
./redis-trib.rb create --replicas 1 192.168.153.129:7001 192.168.153.129:7002 192.168.153.129:7003 192.168.153.129:7004 192.168.153.129:7005 192.168.153.129:7006
Redis 5.0开始不再使用ruby搭建集群,而是直接使用客户端命令 redis-cli 来创建。
如果用ruby命令在>5.0.0版本上创建会报如下提示信息
[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.153.129:7001 192.168.153.129:7002 192.168.153.129:7003 192.168.153.129:7004 192.168.153.129:7005 192.168.153.129:7006
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 192.168.153.129:7001 192.168.153.129:7002 192.168.153.129:7003 192.168.153.129:7004 192.168.153.129:7005 192.168.153.129:7006 --cluster-replicas 1
To get help about all subcommands, type:
redis-cli --cluster help
按照上面提示,使用以下命令
[root@localhost ~]# cd /usr/local/redis/bin
[root@localhost bin]# ./redis-cli --cluster create 192.168.153.129:7001 192.168.153.129:7002 192.168.153.129:7003 192.168.153.129:7004 192.168.153.129:7005 192.168.153.129:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.153.129:7005 to 192.168.153.129:7001
Adding replica 192.168.153.129:7006 to 192.168.153.129:7002
Adding replica 192.168.153.129:7004 to 192.168.153.129:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 39ceef49447e877e9ead03452755bd5ab4501d23 192.168.153.129:7001
slots:[0-5460] (5461 slots) master
M: b51d558a71fefc1f146efd0ff37987e832c42458 192.168.153.129:7002
slots:[5461-10922] (5462 slots) master
M: c072fbfe71864bc295d261c48f9b6cfdf3aaeec3 192.168.153.129:7003
slots:[10923-16383] (5461 slots) master
S: 261a97b6a79179ca5bd1e0bf3deb96f7697d3227 192.168.153.129:7004
replicates c072fbfe71864bc295d261c48f9b6cfdf3aaeec3
S: bb8faec495e4123d48c38a9c35ddcae3850aebff 192.168.153.129:7005
replicates 39ceef49447e877e9ead03452755bd5ab4501d23
S: caaa0b46bb54a2e63d0fc80308c740657cd2eaba 192.168.153.129:7006
replicates b51d558a71fefc1f146efd0ff37987e832c42458
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.153.129:7001)
M: 39ceef49447e877e9ead03452755bd5ab4501d23 192.168.153.129:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: bb8faec495e4123d48c38a9c35ddcae3850aebff 192.168.153.129:7005
slots: (0 slots) slave
replicates 39ceef49447e877e9ead03452755bd5ab4501d23
S: 261a97b6a79179ca5bd1e0bf3deb96f7697d3227 192.168.153.129:7004
slots: (0 slots) slave
replicates c072fbfe71864bc295d261c48f9b6cfdf3aaeec3
S: caaa0b46bb54a2e63d0fc80308c740657cd2eaba 192.168.153.129:7006
slots: (0 slots) slave
replicates b51d558a71fefc1f146efd0ff37987e832c42458
M: b51d558a71fefc1f146efd0ff37987e832c42458 192.168.153.129:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: c072fbfe71864bc295d261c48f9b6cfdf3aaeec3 192.168.153.129:7003
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.
9.验证集群
[root@localhost 7001]# ./redis-cli -h 127.0.0.1 -p 7001 -c
127.0.0.1:7001> set test 123
-> Redirected to slot [6918] located at 192.168.153.129:7002
OK
192.168.153.129:7002> get test
"123"
192.168.153.129:7002> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:188
cluster_stats_messages_pong_sent:195
cluster_stats_messages_meet_sent:4
cluster_stats_messages_sent:387
cluster_stats_messages_ping_received:194
cluster_stats_messages_pong_received:192
cluster_stats_messages_meet_received:1
cluster_stats_messages_received:387
192.168.153.129:7002> cluster nodes
bb8faec495e4123d48c38a9c35ddcae3850aebff 192.168.153.129:7005@17005 slave 39ceef49447e877e9ead03452755bd5ab4501d23 0 1559630098121 1 connected
c072fbfe71864bc295d261c48f9b6cfdf3aaeec3 192.168.153.129:7003@17003 master - 0 1559630098000 3 connected 10923-16383
39ceef49447e877e9ead03452755bd5ab4501d23 192.168.153.129:7001@17001 master - 0 1559630097000 1 connected 0-5460
261a97b6a79179ca5bd1e0bf3deb96f7697d3227 192.168.153.129:7004@17004 slave c072fbfe71864bc295d261c48f9b6cfdf3aaeec3 0 1559630099129 4 connected
caaa0b46bb54a2e63d0fc80308c740657cd2eaba 192.168.153.129:7006@17006 slave b51d558a71fefc1f146efd0ff37987e832c42458 0 1559630097113 6 connected
b51d558a71fefc1f146efd0ff37987e832c42458 192.168.153.129:7002@17002 myself,master - 0 1559630097000 2 connected 5461-10922