启动一个redis
[root@VM_0_17_centos 7000]# redis-server redis.conf 22936:C 28 Sep 15:42:06.241 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 22936:C 28 Sep 15:42:06.241 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=22936, just started 22936:C 28 Sep 15:42:06.241 # Configuration loaded
查看redis状态
[root@VM_0_17_centos 7000]# ps -ef|grep redis root 22351 1 0 Aug31 ? 00:23:47 ./bin/redis-server 127.0.0.1:7005 root 22414 1 0 Aug31 ? 00:23:36 ./7001/bin/redis-server 127.0.0.1:7001 root 22453 1 0 Aug31 ? 00:23:42 ./7002/bin/redis-server 127.0.0.1:7002 root 22473 1 0 Aug31 ? 00:23:54 ./7003/bin/redis-server 127.0.0.1:7003 root 22491 1 0 Aug31 ? 00:23:43 ./7004/bin/redis-server 127.0.0.1:7004 root 22937 1 0 15:42 ? 00:00:00 redis-server 127.0.0.1:7000 root 22945 21072 0 15:42 pts/0 00:00:00 grep --color=auto redis
赋值一个文件夹到另一个文件夹里(-r 联通文件夹中内容一起复制)
cp -r bin /usr/local/softInstall/redis-server/redis_cluster/7000
列出已知的rvm版本
rvm list known
安装一个 Ruby 版本(这里安装了最新的 2.2.0, rvm list known
列表里面的都可以拿来安装。)
rvm install 2.2.0 --disable-binary
切换 Ruby 版本
rvm use 2.2.0
如果想设置为默认版本,这样一来以后新打开的控制台默认的 Ruby 就是这个版本
rvm use 2.2.0 --default
查询已经安装的ruby
rvm list
卸载一个已安装版本
rvm remove 1.8.7
配置完6个config之后重启
逐一重启
[root@VM_0_17_centos 7002]# cd /usr/local/softInstall/redis-server/redis_cluster/7001 [root@VM_0_17_centos 7001]# redis-server redis.conf 25745:C 28 Sep 20:29:46.490 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 25745:C 28 Sep 20:29:46.490 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=25745, just started 25745:C 28 Sep 20:29:46.490 # Configuration loaded
查看状态
[root@VM_0_17_centos 7001]# ps -ef|grep redis root 25571 1 0 20:28 ? 00:00:00 redis-server 127.0.0.1:7000 [cluster] root 25612 1 0 20:28 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster] root 25629 1 0 20:28 ? 00:00:00 redis-server 127.0.0.1:7004 [cluster] root 25660 1 0 20:28 ? 00:00:00 redis-server 127.0.0.1:7005 [cluster] root 25708 1 0 20:29 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster] root 25834 1 0 20:30 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster] root 25843 21072 0 20:30 pts/0 00:00:00 grep --color=auto redis
创建集群(执行命令前要先找到src路径)
[root@VM_0_17_centos ~]# cd /usr/local/softInstall/redis-server/redis_cluster/redis-4.0.1/src
[root@VM_0_17_centos src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 Adding replica 127.0.0.1:7003 to 127.0.0.1:7000 Adding replica 127.0.0.1:7004 to 127.0.0.1:7001 Adding replica 127.0.0.1:7005 to 127.0.0.1:7002 M: 5be3761a1d6c72f6266406685dbe5ef9a9801362 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: 26c8f2a26467214ed228852ba00cb88f1af0ce6f 127.0.0.1:7001 slots:5461-10922 (5462 slots) master M: a4ac19b1f49d72af45dcafc56d6077ce2e68c690 127.0.0.1:7002 slots:10923-16383 (5461 slots) master S: 299d4e818831b8aa8d62b753033c257ef8e7df94 127.0.0.1:7003 replicates 5be3761a1d6c72f6266406685dbe5ef9a9801362 S: 456b89187106f8c0011b66d463092b54095289cd 127.0.0.1:7004 replicates 26c8f2a26467214ed228852ba00cb88f1af0ce6f S: c4da7f847993e309d62e7425a33b83d0bf175329 127.0.0.1:7005 replicates a4ac19b1f49d72af45dcafc56d6077ce2e68c690 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:7000) M: 5be3761a1d6c72f6266406685dbe5ef9a9801362 127.0.0.1:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 456b89187106f8c0011b66d463092b54095289cd 127.0.0.1:7004 slots: (0 slots) slave replicates 26c8f2a26467214ed228852ba00cb88f1af0ce6f S: 299d4e818831b8aa8d62b753033c257ef8e7df94 127.0.0.1:7003 slots: (0 slots) slave replicates 5be3761a1d6c72f6266406685dbe5ef9a9801362 M: 26c8f2a26467214ed228852ba00cb88f1af0ce6f 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: a4ac19b1f49d72af45dcafc56d6077ce2e68c690 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: c4da7f847993e309d62e7425a33b83d0bf175329 127.0.0.1:7005 slots: (0 slots) slave replicates a4ac19b1f49d72af45dcafc56d6077ce2e68c690 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@VM_0_17_centos src]#
三、redis集群的测试
1、测试存取值
注意,进入redis首先要进入其src目录才能执行命令,ctrl+c是退出命令
[root@VM_0_17_centos ~]# cd /usr/local/softInstall/redis-server/redis-4.0.1/src/ [root@VM_0_17_centos src]# ./redis-cli -c -p 7001 127.0.0.1:7001> [root@VM_0_17_centos src]#
客户端连接集群redis-cli需要带上 -c ,redis-cli -c -p 端口号
[root@localhost redis01]# ./redis-cli -c -p 7001 127.0.0.1:7001> set name andy -> Redirected to slot [5798] located at 127.0.0.1:7002 OK 127.0.0.1:7002> get name "andy" 127.0.0.1:7002>
根据redis-cluster的key值分配,name应该分配到节点7002[5461-10922]上,上面显示redis cluster自动从7001跳转到了7002节点。
我们可以测试一下7006从节点获取name值
[root@localhost redis06]# ./redis-cli -c -p 7006 127.0.0.1:7006> get name -> Redirected to slot [5798] located at 127.0.0.1:7002 "andy" 127.0.0.1:7002>
查看redis中所有的key
127.0.0.1:7001> keys * 1) "name" 2) "sexMan"
3) "sexMan"
列出匹配的key
127.0.0.1:7001> keys sex* 1) "sexGirl" 2) "sexMan" 127.0.0.1:7001> keys *me 1) "name"
四、集群节点选举
现在模拟将7001节点挂掉,按照redis-cluster原理会选举会将 7001的从节点7004选举为主节点。
[root@VM_0_17_centos src]# ps -ef|grep redis root 10351 9328 0 10:17 pts/1 00:00:00 grep --color=auto redis root 25571 1 0 Sep28 ? 00:00:45 redis-server 127.0.0.1:7000 [cluster] root 25612 1 0 Sep28 ? 00:00:45 redis-server 127.0.0.1:7003 [cluster] root 25629 1 0 Sep28 ? 00:00:46 redis-server 127.0.0.1:7004 [cluster] root 25660 1 0 Sep28 ? 00:00:44 redis-server 127.0.0.1:7005 [cluster] root 25708 1 0 Sep28 ? 00:00:45 redis-server 127.0.0.1:7002 [cluster] root 25834 1 0 Sep28 ? 00:00:45 redis-server 127.0.0.1:7001 [cluster]
杀掉7001
[root@VM_0_17_centos src]# kill 25834
在查看集群中的7001节点
[root@VM_0_17_centos src]# ./redis-trib.rb check 127.0.0.1:7001 [ERR] Sorry, can't connect to node 127.0.0.1:7001
此时7001节点已经被干掉我们试一下取值,
[root@VM_0_17_centos src]# ./redis-cli -c -p 7000 127.0.0.1:7000> get name -> Redirected to slot [5798] located at 127.0.0.1:7004 "yuandalian"
原先在7001中的值已经切换到7004,我们来看一下7004的状态是否已经切换到主节点
[root@VM_0_17_centos src]# ./redis-trib.rb check 127.0.0.1:7004 >>> Performing Cluster Check (using node 127.0.0.1:7004) M: a4ac19b1f49d72af45dcafc56d6077ce2e68c690 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: c4da7f847993e309d62e7425a33b83d0bf175329 127.0.0.1:7005 slots: (0 slots) slave replicates a4ac19b1f49d72af45dcafc56d6077ce2e68c690 M: 456b89187106f8c0011b66d463092b54095289cd 127.0.0.1:7004 slots:5461-10922 (5462 slots) master 0 additional replica(s) M: 5be3761a1d6c72f6266406685dbe5ef9a9801362 127.0.0.1:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 299d4e818831b8aa8d62b753033c257ef8e7df94 127.0.0.1:7003 slots: (0 slots) slave replicates 5be3761a1d6c72f6266406685dbe5ef9a9801362
7004已经切换到master,而7001已经被干掉
现在我们将7001节点恢复,看是否会自动加入集群中以及充当的M还是S节点。
[root@VM_0_17_centos 7002]# cd ../7001 [root@VM_0_17_centos 7001]# redis-server redis.conf 10823:C 29 Sep 10:24:38.549 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 10823:C 29 Sep 10:24:38.549 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=10823, just started 10823:C 29 Sep 10:24:38.549 # Configuration loaded [root@VM_0_17_centos 7001]# cd ../../ [root@VM_0_17_centos redis-server]# cd redis-4.0.1/src/ [root@VM_0_17_centos src]# ./redis-trib.rb check 127.0.0.1:7001 >>> Performing Cluster Check (using node 127.0.0.1:7001) S: 26c8f2a26467214ed228852ba00cb88f1af0ce6f 127.0.0.1:7001 slots: (0 slots) slave replicates 456b89187106f8c0011b66d463092b54095289cd M: 5be3761a1d6c72f6266406685dbe5ef9a9801362 127.0.0.1:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 299d4e818831b8aa8d62b753033c257ef8e7df94 127.0.0.1:7003 slots: (0 slots) slave replicates 5be3761a1d6c72f6266406685dbe5ef9a9801362 M: a4ac19b1f49d72af45dcafc56d6077ce2e68c690 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: c4da7f847993e309d62e7425a33b83d0bf175329 127.0.0.1:7005 slots: (0 slots) slave replicates a4ac19b1f49d72af45dcafc56d6077ce2e68c690 M: 456b89187106f8c0011b66d463092b54095289cd 127.0.0.1:7004 slots:5461-10922 (5462 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. [root@VM_0_17_centos src]#
可以看到7001节点变成了456b89187106f8c0011b66d463092b54095289cd7004的从节点。
进一步测试
我们来测一下节点掉线之后在上线是否会更新最新数据?
1.把主节点7004干掉,然后7001应该会成为主节点,
2.这个时候我们往7001中写值,set name falali(7004被干掉的时候他的name值还是andy)
3.恢复7004,成为7001的从节点
4.干掉7001,到70004中取出name值
5.观察name值是andy还是falali
1.把主节点7004干掉,然后7001应该会成为主节点,
[root@VM_0_17_centos src]# ps -ef|grep redis root 10824 1 0 10:24 ? 00:00:01 redis-server 127.0.0.1:7001 [cluster] root 12226 9328 0 10:45 pts/1 00:00:00 grep --color=auto redis root 25571 1 0 Sep28 ? 00:00:47 redis-server 127.0.0.1:7000 [cluster] root 25612 1 0 Sep28 ? 00:00:47 redis-server 127.0.0.1:7003 [cluster] root 25629 1 0 Sep28 ? 00:00:48 redis-server 127.0.0.1:7004 [cluster] root 25660 1 0 Sep28 ? 00:00:46 redis-server 127.0.0.1:7005 [cluster] root 25708 1 0 Sep28 ? 00:00:47 redis-server 127.0.0.1:7002 [cluster] [root@VM_0_17_centos src]# kill 25629 [root@VM_0_17_centos src]# ./redis-trib.rb check 127.0.0.1:7004 [ERR] Sorry, can't connect to node 127.0.0.1:7004
2.这个时候我们往7001中写值,set name falali(7004被干掉的时候他的name值还是andy)
127.0.0.1:7000> get name -> Redirected to slot [5798] located at 127.0.0.1:7001 "andy"(7004被干掉的时候name=andy) [root@VM_0_17_centos src]# ./redis-cli -c -p 7000 127.0.0.1:7002> set name falali -> Redirected to slot [5798] located at 127.0.0.1:7001 OK
3.恢复7004,成为7001的从节点
[root@VM_0_17_centos src]# cd ../../ [root@VM_0_17_centos redis-server]# cd redis_cluster/7004 [root@VM_0_17_centos 7004]# redis-server redis.conf 12650:C 29 Sep 10:51:50.313 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12650:C 29 Sep 10:51:50.313 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=12650, just started 12650:C 29 Sep 10:51:50.313 # Configuration loaded
4.干掉7001,到700
[root@VM_0_17_centos src]# ./redis-cli -c -p 7004 127.0.0.1:7004> get name -> Redirected to slot [5798] located at 127.0.0.1:7001 "falali"
5.我们看到7004的值已经重andy变成了falali。说明redis会自动同步主从节点间的数据
我们来看一下这6个节点分别存了哪些key
[root@VM_0_17_centos src]# ./redis-cli -c -p 7000 127.0.0.1:7000> keys * 1) "name2" 2) "sex" 127.0.0.1:7000> [root@VM_0_17_centos src]# ./redis-cli -c -p 7001 127.0.0.1:7001> keys * 1) "name" 2) "sexGirl" 3) "sexMan" 127.0.0.1:7001> [root@VM_0_17_centos src]# ./redis-cli -c -p 7002 127.0.0.1:7002> keys * 1) "money" 127.0.0.1:7002> [root@VM_0_17_centos src]# ./redis-cli -c -p 7003 127.0.0.1:7003> keys * 1) "sex" 2) "name2" 127.0.0.1:7003> [root@VM_0_17_centos src]# ./redis-cli -c -p 7004 127.0.0.1:7004> keys * 1) "sexGirl" 2) "name" 3) "sexMan" 127.0.0.1:7004> [root@VM_0_17_centos src]# ./redis-cli -c -p 7005 127.0.0.1:7005> keys * 1) "money" 127.0.0.1:7005>
可以看出M与S节点之间的数据是一样的。
很好的一个搭建测试例子:http://blog.sina.com.cn/s/blog_53b45c4d0102wg10.html