实践是检验真理的唯一标准,作为redis的初学者,在学习redis集群的时候,希望能够自己搭建一个redis集群,但只有一台电脑,所以想出使用虚拟机去模拟redis集群,相当于构建一个伪集群。
VM虚拟机(这里操作系统选用的是CentOs7)
安装redies
注:为避免权限问题,后期操作均使用root账户
为方便对文件的管理,新建一个集群目录
[root@centOSyangsensen redis]# mkdir redis-cluster/ [root@centOSyangsensen redis]# ls redis redis-cluster
至少3台主机才可以配置集群,如果每台主机再配置一台从机的话,那么就需要6台主机,这里创建6个redis实例来代替六台主机,每个实例使用不同的端口号,设计端口如下,黑色为主机,绿色为从机,端口号7001~7006
复制文件
[root@centOSyangsensen redis]# cp redis redis-cluster/7001 -r [root@centOSyangsensen redis]# cd redis-cluster/7001/ [root@centOSyangsensen 7001]# ls bin dump.rdb nodes.conf redis.conf
[root@centOSyangsensen 7001]# rm -rf dump.rdb nodes.conf [root@centOSyangsensen 7001]# ls bin redis.conf
需要修改三处,分别是是否接受集群(Cluster-enable),绑定ip(bind),端口号(port),以及一些文件目录
打开集群
设置IP,ip为虚拟机的IP
设置端口号
修改文件目录(这里不修改,待会可能打不开第二台机器)
保存退出
[root@centOSyangsensen redis-cluster]# cp 7001/ 7002 -r [root@centOSyangsensen redis-cluster]# cp 7001/ 7003 -r [root@centOSyangsensen redis-cluster]# cp 7001/ 7004 -r [root@centOSyangsensen redis-cluster]# cp 7001/ 7005 -r [root@centOSyangsensen redis-cluster]# cp 7001/ 7006 -r
修改7002~7006机器的端口号,并修改配置文件中对应的文件目录,与7001中配置一样的步骤
创建集群,无需在每一台机器都运行集群命令,只需在任一台机器执行redis-cli --cluster命令就够了
命令格式如下
./redis-cli --cluster create ip:port ip:port --cluster-replicas 1
其中–cluster-replicas表示为每台主机配置的从机数量
命令及命令执行过程如下
[root@centOSyangsensen redis-cluster]# cd 7001 [root@centOSyangsensen 7001]# cd bin [root@centOSyangsensen bin]# ./redis-cli --cluster create 192.168.12.128:7001 192.168.12.128:7002 192.168.12.128:7003 192.168.12.128:7004 192.168.12.128:7005 192.168.12.128: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.12.128:7005 to 192.168.12.128:7001 Adding replica 192.168.12.128:7006 to 192.168.12.128:7002 Adding replica 192.168.12.128:7004 to 192.168.12.128:7003 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 63966329ebb2405f38fa03cb1901d8a9bdd0eedb 192.168.12.128:7001 slots:[0-5460] (5461 slots) master M: 0bec30bff33dc635608c7cf48e66eaed6c867d5d 192.168.12.128:7002 slots:[5461-10922] (5462 slots) master M: 65a2c0d143fa8d9be9be1a70da76744ddfbf9a59 192.168.12.128:7003 slots:[10923-16383] (5461 slots) master S: 460bd1fe2564b31c302fe5431a7560e1773798cc 192.168.12.128:7004 replicates 65a2c0d143fa8d9be9be1a70da76744ddfbf9a59 S: e1cdf6e53ffef7361c0c722909b5bf37f36bb758 192.168.12.128:7005 replicates 63966329ebb2405f38fa03cb1901d8a9bdd0eedb S: 48df8c6d3e400a539614b6f418ba7a4004db2df6 192.168.12.128:7006 replicates 0bec30bff33dc635608c7cf48e66eaed6c867d5d 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.12.128:7001) M: 63966329ebb2405f38fa03cb1901d8a9bdd0eedb 192.168.12.128:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 65a2c0d143fa8d9be9be1a70da76744ddfbf9a59 192.168.12.128:7003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: e1cdf6e53ffef7361c0c722909b5bf37f36bb758 192.168.12.128:7005 slots: (0 slots) slave replicates 63966329ebb2405f38fa03cb1901d8a9bdd0eedb S: 48df8c6d3e400a539614b6f418ba7a4004db2df6 192.168.12.128:7006 slots: (0 slots) slave replicates 0bec30bff33dc635608c7cf48e66eaed6c867d5d M: 0bec30bff33dc635608c7cf48e66eaed6c867d5d 192.168.12.128:7002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 460bd1fe2564b31c302fe5431a7560e1773798cc 192.168.12.128:7004 slots: (0 slots) slave replicates 65a2c0d143fa8d9be9be1a70da76744ddfbf9a59 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
注:上面的信息可以看出有三台主机(7001,7002,7003),以及三台从机(7004,7005,7006),其中M表示主机,S表示从机。
命令(任一连接一台主机):
[root@centOSyangsensen bin]# ./redis-cli -h 192.168.12.128 -p 7001 -c 192.168.12.128:7001>
-c指的是集群连接
到这里redis集群就全部搭建完毕了,接下里可以存值测试,查看集群信息等等
存值测试
192.168.12.128:7001> set k1 k -> Redirected to slot [12706] located at 192.168.12.128:7003 OK 192.168.12.128:7003> get k -> Redirected to slot [7629] located at 192.168.12.128:7002 (nil) 192.168.12.128:7002> get k1 -> Redirected to slot [12706] located at 192.168.12.128:7003 "k"
可以看出虽然连接的是7001机器,当数据会根绝存储位置为你重新连接到其他机器。