linux-redis学习经历四,redis集群初步体验

3 篇文章 0 订阅
2 篇文章 0 订阅
[2018/1/30 14:29:18] [root@bogon redis]# ll
[2018/1/30 14:29:18] 总用量 60
[2018/1/30 14:29:18] -rw-r--r--. 1 root root   110 1月  30 22:28 appendonly.aof
[2018/1/30 14:29:18] drwxr-xr-x. 2 root root  4096 1月  29 22:17 bin
[2018/1/30 14:29:18] -rw-r--r--. 1 root root   100 1月  30 22:29 dump.rdb
[2018/1/30 14:29:18] -rw-r--r--. 1 root root 46700 1月  30 22:18 redis.conf
//数据持久化方式,可以使用rdb,效率高,但保存间隔长,数据容易缺失。
//或者是aof方式,这个日志形式,比较频繁,安全,但相率相对较低。
//aof配置:
//编辑redis.conf;appendonly no默认关闭aof方式 我们修改成yes 就开启;
//always 是 只要发生修改,立即同步 (推荐实用 安全性最高)
//everysec 是 每秒同步一次;
//no是不同步。我们修改成always
//rdb设置:
//save 900 1 表示 每900秒内至少有1个kery发生变化,就持久化
//save 300 10表示 每300秒内至少有10个key发生变化,就持久化
//save 60 10000表示 每60秒内至少有10000个key发生变化,就持久化
[2018/1/30 14:29:24] [root@bogon redis]# vi appendonly.aof 
[2018/1/30 14:30:48] [root@bogon redis]# bin/redis-server redis.conf 
[2018/1/30 14:30:52] [root@bogon redis]# bin/redis-cli 
[2018/1/30 14:30:58] 127.0.0.1:6379> keys *
[2018/1/30 14:30:58] 1) "aa"
[2018/1/30 14:30:58] 2) "cc"
[2018/1/30 14:30:58] 3) "bb"

[BEGIN] 2018/1/30 14:46:28
[2018/1/30 14:46:28] Disconnected from remote host(centos) at 14:35:49.
Type `help' to learn how to use Xshell prompt.
[d:\~]$ 
Connecting to 192.168.0.154:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Tue Jan 30 17:42:25 2018 from 192.168.0.122
//redis实验集群
[2018/1/30 14:47:07] [root@bogon ~]# cd /usr/local/
[2018/1/30 14:47:08] [root@bogon local]# ll
[2018/1/30 14:47:08] 总用量 0
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 bin
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 etc
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 games
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 include
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 lib
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 lib64
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 libexec
[2018/1/30 14:47:08] drwxr-xr-x. 3 root root 69 1月  30 22:45 redis
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 sbin
[2018/1/30 14:47:08] drwxr-xr-x. 5 root root 46 1月  24 19:34 share
[2018/1/30 14:47:08] drwxr-xr-x. 2 root root  6 6月  10 2014 src
[2018/1/30 14:47:27] [root@bogon local]# cd bin
//将redis文件下的redis-trib.rb复制到/usr/local/bin/下,使能运行。
//redis-trib.rb是redis官方推出的管理redis集群的工具,集成在redis的源码src目录下,是基于redis提供的集群命令封装成简单、便捷、实用的操作工具。
//redis-trib.rb是redis作者用ruby完成的。
[2018/1/30 14:48:48] [root@bogon bin]# cp /root/redis-3.2.11/src/redis-trib.rb /usr/local/bin/
[2018/1/30 14:48:51] [root@bogon bin]# ll
[2018/1/30 14:48:51] 总用量 60
[2018/1/30 14:48:51] -rwxr-xr-x. 1 root root 60852 1月  30 22:47 redis-trib.rb
//创建集群文件夹,这里以端口命名,可以连续创建
[2018/1/30 14:51:21] [root@bogon bin]# mkdir /root/redis_cluster
[2018/1/30 14:52:22] [root@bogon redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006
[2018/1/30 14:52:24] [root@bogon redis_cluster]# ll
[2018/1/30 14:52:24] 总用量 0
[2018/1/30 14:52:24] drwxr-xr-x. 2 root root 6 1月  30 22:51 7001
[2018/1/30 14:52:24] drwxr-xr-x. 2 root root 6 1月  30 22:51 7002
[2018/1/30 14:52:24] drwxr-xr-x. 2 root root 6 1月  30 22:51 7003
[2018/1/30 14:52:24] drwxr-xr-x. 2 root root 6 1月  30 22:51 7004
[2018/1/30 14:52:24] drwxr-xr-x. 2 root root 6 1月  30 22:51 7005
[2018/1/30 14:52:24] drwxr-xr-x. 2 root root 6 1月  30 22:51 7006
//将redis配置文件复制到7001文件下
[2018/1/30 14:54:10] [root@bogon ~]# copy redis-3.2.11/redis.conf redis_cluster/7001/
//修改配置文件如下:
//port 7001  //六个节点配置文件分别是7001-7006
//daemonize yes        //redis后台运行
//pidfile /var/run/redis_7001.pid   //pidfile文件对应7001-7006
//cluster-enabled yes   //开启集群
//cluster-config-file nodes_7001.conf  //保存节点配置,自动创建,自动更新对应7001-7006
//cluster-node-timeout 5000    //集群超时时间,节点超过这个时间没反应就断定是宕机
//appendonly yes   //存储方式,aof,将写操作记录保存到日志中
[2018/1/30 14:55:17] [root@bogon ~]# vi redis_cluster/7001/redis.conf 
[root@bogon redis_cluster]# cp 7001/redis.conf  7002/
[root@bogon redis_cluster]# cp 7001/redis.conf  7003/
[root@bogon redis_cluster]# cp 7001/redis.conf  7004/
[root@bogon redis_cluster]# cp 7001/redis.conf  7005/
[root@bogon redis_cluster]# cp 7001/redis.conf  7006/
[root@bogon redis_cluster]# vi 7002/redis.conf 
[root@bogon redis_cluster]# vi 7003/redis.conf 
[root@bogon redis_cluster]# vi 7004/redis.conf 
[root@bogon redis_cluster]# vi 7005/redis.conf 
[root@bogon redis_cluster]# vi 7006/redis.conf 
//启动六个节点
[2018/1/30 15:15:15] [root@bogon redis_cluster]# /usr/local/redis/bin/redis-server 7001/redis.conf 
[2018/1/30 15:15:15] [root@bogon redis_cluster]# /usr/local/redis/bin/redis-server 7002/redis.conf 
[2018/1/30 15:15:18] [root@bogon redis_cluster]# /usr/local/redis/bin/redis-server 7003/redis.conf 
[2018/1/30 15:15:21] [root@bogon redis_cluster]# /usr/local/redis/bin/redis-server 7004/redis.conf 
[2018/1/30 15:15:24] [root@bogon redis_cluster]# /usr/local/redis/bin/redis-server 7005/redis.conf 
[2018/1/30 15:15:27] [root@bogon redis_cluster]# /usr/local/redis/bin/redis-server 7006/redis.conf 
//查看是否启动成功
[2018/1/30 15:15:46] [root@bogon redis_cluster]# ps -ef |grep -i redis
[2018/1/30 15:15:46] root       3023      1  0 22:30 ?        00:00:08 bin/redis-server *:6379
[2018/1/30 15:15:46] root       3126      1  0 23:12 ?        00:00:00 /usr/local/redis/bin/redis-server *:7001 [cluster]
[2018/1/30 15:15:46] root       3136      1  0 23:14 ?        00:00:00 /usr/local/redis/bin/redis-server *:7002 [cluster]
[2018/1/30 15:15:46] root       3140      1  0 23:14 ?        00:00:00 /usr/local/redis/bin/redis-server *:7003 [cluster]
[2018/1/30 15:15:46] root       3144      1  0 23:14 ?        00:00:00 /usr/local/redis/bin/redis-server *:7004 [cluster]
[2018/1/30 15:15:46] root       3148      1  0 23:14 ?        00:00:00 /usr/local/redis/bin/redis-server *:7005 [cluster]
[2018/1/30 15:15:46] root       3152      1  0 23:14 ?        00:00:00 /usr/local/redis/bin/redis-server *:7006 [cluster]
[2018/1/30 15:15:46] root       3156   3037  0 23:14 pts/0    00:00:00 grep --color=auto -i redis
//会生成对应配置文件
[2018/1/30 15:17:28] [root@bogon redis_cluster]# ll
[2018/1/30 15:17:28] 总用量 28
[2018/1/30 15:17:28] drwxr-xr-x. 2 root root  23 1月  30 23:12 7001
[2018/1/30 15:17:28] drwxr-xr-x. 2 root root  23 1月  30 23:12 7002
[2018/1/30 15:17:28] drwxr-xr-x. 2 root root  23 1月  30 23:13 7003
[2018/1/30 15:17:28] drwxr-xr-x. 2 root root  23 1月  30 23:13 7004
[2018/1/30 15:17:28] drwxr-xr-x. 2 root root  23 1月  30 23:13 7005
[2018/1/30 15:17:28] drwxr-xr-x. 2 root root  23 1月  30 23:13 7006
[2018/1/30 15:17:28] -rw-r--r--. 1 root root   0 1月  30 23:10 appendonly.aof
[2018/1/30 15:17:28] -rw-r--r--. 1 root root  77 1月  30 23:10 dump.rdb
[2018/1/30 15:17:28] -rw-r--r--. 1 root root 112 1月  30 23:10 nodes-7001.conf
[2018/1/30 15:17:28] -rw-r--r--. 1 root root 112 1月  30 23:14 nodes-7002.conf
[2018/1/30 15:17:28] -rw-r--r--. 1 root root 112 1月  30 23:14 nodes-7003.conf
[2018/1/30 15:17:28] -rw-r--r--. 1 root root 112 1月  30 23:14 nodes-7004.conf
[2018/1/30 15:17:28] -rw-r--r--. 1 root root 112 1月  30 23:14 nodes-7005.conf
[2018/1/30 15:17:28] -rw-r--r--. 1 root root 112 1月  30 23:14 nodes-7006.conf
//因为redis-trib.rb工具是由ruby语言写的,所有使用前要先安装ruby
[2018/1/30 15:18:09] [root@bogon redis_cluster]# cd 
[2018/1/30 15:19:11] [root@bogon ~]# yum -y install ruby ruby-devel rubygems rpm-build
[2018/1/30 15:19:13] 已加载插件:fastestmirror
[2018/1/30 15:19:49] http://mirrors.tuna.tsinghua.edu.cn/centos/7.4.1708/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://mirrors.tuna.tsinghua.edu.cn/centos/7.4.1708/os/x86_64/repodata/repomd.xml: (28, 'Resolving timed out after 30417 milliseconds')
[2018/1/30 15:19:49] 正在尝试其它镜像。
…………
[2018/1/30 15:21:43] 完毕!
//gem是ruby的命令,类似yum,可以理解为从一个庞大的包库里下载需要的包和依赖。
//这里报错:意思为ruby版本太低
[2018/1/30 15:23:04] [root@bogon ~]# gem install redis
[2018/1/30 15:23:06] Fetching: redis-4.0.1.gem (100%)
[2018/1/30 15:23:06] ERROR:  Error installing redis:
[2018/1/30 15:23:06] 	redis requires Ruby version >= 2.2.2.
[2018/1/30 15:24:26] [root@bogon ~]# redis-trib.rb create --replicas 1  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 127.0.0.1:7006
[2018/1/30 15:24:26] /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
[2018/1/30 15:24:26] 	from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
[2018/1/30 15:24:26] 	from /usr/local/bin/redis-trib.rb:25:in `<main>'
//这因为没有正常安装ruby的redis所以报错
[2018/1/30 15:28:06] [root@bogon ~]# redis-trib.rb create --replicas 1  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 127.0.0.1:7006
[2018/1/30 15:28:06] /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
[2018/1/30 15:28:06] 	from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
[2018/1/30 15:28:06] 	from /usr/local/bin/redis-trib.rb:25:in `<main>'
//安装rvm:Ruby Version Manager,Ruby版本管理器,
[2018/1/30 15:47:24] [root@bogon rvm]# gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
[2018/1/30 15:47:24] gpg: 钥匙环‘/root/.gnupg/secring.gpg’已建立
[2018/1/30 15:47:24] gpg: 下载密钥‘D39DC0E3’,从 hkp 服务器 keys.gnupg.net
[2018/1/30 15:47:27] gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库
[2018/1/30 15:47:27] gpg: 密钥 D39DC0E3:公钥“Michal Papis (RVM signing) <mpapis@gmail.com>”已导入
[2018/1/30 15:47:27] gpg: 没有找到任何绝对信任的密钥
[2018/1/30 15:47:27] gpg: 合计被处理的数量:1
[2018/1/30 15:47:27] gpg:           已导入:1  (RSA: 1)
[2018/1/30 15:47:37] [root@bogon rvm]# curl -L get.rvm.io | bash -s stable
[2018/1/30 15:47:37]   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
[2018/1/30 15:47:37]                                  Dload  Upload   Total   Spent    Left  Speed
[2018/1/30 15:47:38] 100   194  100   194    0     0    192      0  0:00:01  0:00:01 --:--:--   192
[2018/1/30 15:47:39] 100 24090  100 24090    0     0  11714      0  0:00:02  0:00:02 --:--:-- 23365
[2018/1/30 15:47:42] Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
[2018/1/30 15:48:27] Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
[2018/1/30 15:48:30] gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
[2018/1/30 15:48:30] gpg: 完好的签名,来自于“Michal Papis (RVM signing) <mpapis@gmail.com>”
[2018/1/30 15:48:30] gpg:               亦即“Michal Papis <michal.papis@toptal.com>”
[2018/1/30 15:48:30] gpg:               亦即“[jpeg image of size 5015]”
[2018/1/30 15:48:30] gpg: 警告:这把密钥未经受信任的签名认证!
[2018/1/30 15:48:30] gpg:       没有证据表明这个签名属于它所声称的持有者。
[2018/1/30 15:48:30] 主钥指纹: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3
[2018/1/30 15:48:30] 子钥指纹: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17
[2018/1/30 15:48:30] GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
[2018/1/30 15:48:30] Creating group 'rvm'
[2018/1/30 15:48:31] 
[2018/1/30 15:48:31] Installing RVM to /usr/local/rvm/
[2018/1/30 15:48:35] Installation of RVM in /usr/local/rvm/ is almost complete:
[2018/1/30 15:48:35] 
[2018/1/30 15:48:35]   * First you need to add all users that will be using rvm to 'rvm' group,
[2018/1/30 15:48:35]     and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.
[2018/1/30 15:48:35] 
[2018/1/30 15:48:35]   * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
[2018/1/30 15:48:35]     in all your open shell windows, in rare cases you need to reopen all shell windows.
//查看安装的路径
[2018/1/30 15:48:59] [root@bogon rvm]# find / -name rvm -print
[2018/1/30 15:49:00] /usr/local/rvm
[2018/1/30 15:49:00] /usr/local/rvm/src/rvm
[2018/1/30 15:49:00] /usr/local/rvm/src/rvm/bin/rvm
[2018/1/30 15:49:00] /usr/local/rvm/src/rvm/lib/rvm
[2018/1/30 15:49:00] /usr/local/rvm/src/rvm/scripts/rvm
[2018/1/30 15:49:00] /usr/local/rvm/bin/rvm
[2018/1/30 15:49:00] /usr/local/rvm/lib/rvm
[2018/1/30 15:49:00] /usr/local/rvm/scripts/rvm
//读取并执行指定文件
[2018/1/30 15:49:12] [root@bogon rvm]# source /usr/local/rvm/scripts/rvm
//查看rvm库中有的版本
[2018/1/30 15:50:13] [root@bogon rvm]# rvm list known
[2018/1/30 15:50:15] # MRI Rubies
[2018/1/30 15:50:15] [ruby-]1.8.6[-p420]
[2018/1/30 15:50:15] [ruby-]1.8.7[-head] # security released on head
[2018/1/30 15:50:15] [ruby-]1.9.1[-p431]
[2018/1/30 15:50:15] [ruby-]1.9.2[-p330]
[2018/1/30 15:50:15] [ruby-]1.9.3[-p551]
[2018/1/30 15:50:15] [ruby-]2.0.0[-p648]
[2018/1/30 15:50:15] [ruby-]2.1[.10]
[2018/1/30 15:50:15] [ruby-]2.2[.7]
[2018/1/30 15:50:15] [ruby-]2.3[.4]
[2018/1/30 15:50:15] [ruby-]2.4[.1]
[2018/1/30 15:50:15] ruby-head
…………
//安装ruby
[2018/1/30 15:50:37] [root@bogon rvm]# rvm install 2.3.4
[2018/1/30 15:50:39] Searching for binary rubies, this might take some time.
[2018/1/30 15:50:40] Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.3.4.tar.bz2
…………
//更换ruby并设置为默认
[2018/1/30 15:55:17] [root@bogon rvm]# rvm use 2.3.4 --default
[2018/1/30 15:55:18] Using /usr/local/rvm/gems/ruby-2.3.4
//查看是否更换成功
[2018/1/30 15:55:36] [root@bogon rvm]# ruby -version
[2018/1/30 15:55:36] ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux]
[2018/1/30 15:55:36] -e:1:in `<main>': undefined local variable or method `rsion' for main:Object (NameError)
//用新的ruby下载
[2018/1/30 15:56:29] [root@bogon rvm]# gem install redis
[2018/1/30 15:56:31] Fetching: redis-4.0.1.gem (100%)
[2018/1/30 15:56:31] Successfully installed redis-4.0.1
……
//创建集群,注意如果对应端口下数据不少清空的也报错,nodes-7001.conf等配置文件里有错误信息的也可能报错
[2018/1/30 15:56:44] [root@bogon rvm]# redis-trib.rb create --replicas 1  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 127.0.0.1:7006
//创建集群
[2018/1/30 15:56:44] >>> Creating cluster
//有六个节点,至少要三个,不然不能创建
[2018/1/30 15:56:44] >>> Performing hash slots allocation on 6 nodes...
//设置三个为主节点
[2018/1/30 15:56:44] Using 3 masters:
[2018/1/30 15:56:44] 127.0.0.1:7001
[2018/1/30 15:56:44] 127.0.0.1:7002
[2018/1/30 15:56:44] 127.0.0.1:7003
//给主节点分配奴隶(副)节点
[2018/1/30 15:56:44] Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
[2018/1/30 15:56:44] Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
[2018/1/30 15:56:44] Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
//显示哈希槽分配情况,如果集群运行时哈希槽断掉会报错
//M表示主节点,S表里奴隶节点(cluster)
[2018/1/30 15:56:44] M: 03faf924369fb2622aa851b846a797fd613c451d 127.0.0.1:7001
[2018/1/30 15:56:44]    slots:0-5460 (5461 slots) master
[2018/1/30 15:56:44] M: a33ff4fcc56ecb840a5c957e46ea547556531f57 127.0.0.1:7002
[2018/1/30 15:56:44]    slots:5461-10922 (5462 slots) master
[2018/1/30 15:56:44] M: 715a175dfe8b30ad1304787d47ca917d6ef5d1be 127.0.0.1:7003
[2018/1/30 15:56:44]    slots:10923-16383 (5461 slots) master
[2018/1/30 15:56:44] S: 675abfe3278df935ef8a9766c3fe5120ac7f40b1 127.0.0.1:7004
[2018/1/30 15:56:44]    replicates 03faf924369fb2622aa851b846a797fd613c451d
[2018/1/30 15:56:44] S: 6aa9f5b62591b508ae0576c6f769c75e8c86bb42 127.0.0.1:7005
[2018/1/30 15:56:44]    replicates a33ff4fcc56ecb840a5c957e46ea547556531f57
[2018/1/30 15:56:44] S: 6e13358c766de76177e4641c55bc7a246f7dc8e3 127.0.0.1:7006
[2018/1/30 15:56:44]    replicates 715a175dfe8b30ad1304787d47ca917d6ef5d1be
[2018/1/30 15:56:56] Can I set the above configuration? (type 'yes' to accept): yes
[2018/1/30 15:56:56] >>> Nodes configuration updated
[2018/1/30 15:56:56] >>> Assign a different config epoch to each node
[2018/1/30 15:56:56] >>> Sending CLUSTER MEET messages to join the cluster
[2018/1/30 15:56:59] Waiting for the cluster to join..
[2018/1/30 15:56:59] >>> Performing Cluster Check (using node 127.0.0.1:7001)
[2018/1/30 15:56:59] M: 03faf924369fb2622aa851b846a797fd613c451d 127.0.0.1:7001
[2018/1/30 15:56:59]    slots:0-5460 (5461 slots) master
[2018/1/30 15:56:59]    1 additional replica(s)
[2018/1/30 15:56:59] M: a33ff4fcc56ecb840a5c957e46ea547556531f57 127.0.0.1:7002
[2018/1/30 15:56:59]    slots:5461-10922 (5462 slots) master
[2018/1/30 15:56:59]    1 additional replica(s)
[2018/1/30 15:56:59] S: 6aa9f5b62591b508ae0576c6f769c75e8c86bb42 127.0.0.1:7005
[2018/1/30 15:56:59]    slots: (0 slots) slave
[2018/1/30 15:56:59]    replicates a33ff4fcc56ecb840a5c957e46ea547556531f57
[2018/1/30 15:56:59] S: 6e13358c766de76177e4641c55bc7a246f7dc8e3 127.0.0.1:7006
[2018/1/30 15:56:59]    slots: (0 slots) slave
[2018/1/30 15:56:59]    replicates 715a175dfe8b30ad1304787d47ca917d6ef5d1be
[2018/1/30 15:56:59] S: 675abfe3278df935ef8a9766c3fe5120ac7f40b1 127.0.0.1:7004
[2018/1/30 15:56:59]    slots: (0 slots) slave
[2018/1/30 15:56:59]    replicates 03faf924369fb2622aa851b846a797fd613c451d
[2018/1/30 15:56:59] M: 715a175dfe8b30ad1304787d47ca917d6ef5d1be 127.0.0.1:7003
[2018/1/30 15:56:59]    slots:10923-16383 (5461 slots) master
[2018/1/30 15:56:59]    1 additional replica(s)
[2018/1/30 15:56:59] [OK] All nodes agree about slots configuration.
[2018/1/30 15:56:59] >>> Check for open slots...
[2018/1/30 15:56:59] >>> Check slots coverage...
[2018/1/30 15:56:59] [OK] All 16384 slots covered.
[2018/1/30 16:03:29] [root@bogon rvm]# cd
//集群使用时要追加-c表示集群,-p表示用集群的哪个端口
[2018/1/30 16:03:52] [root@bogon ~]# /usr/local/redis/bin/redis-cli -c -p 7001
[2018/1/30 16:04:18] 127.0.0.1:7001> set aa 11
[2018/1/30 16:04:18] OK
//设置内容根据集群算法自带分配,这里7001的内容根据算法得出哈希槽,然后分配给了7002,连接也同时跳转过去
[2018/1/30 16:04:35] 127.0.0.1:7001> set bb "hihi"
[2018/1/30 16:04:35] -> Redirected to slot [8620] located at 127.0.0.1:7002
[2018/1/30 16:04:35] OK
[2018/1/30 16:05:19] 127.0.0.1:7002> quit
[2018/1/30 16:05:24] [root@bogon ~]# /usr/local/redis/bin/redis-cli -c -p 7005
//同理获取数据也根据算法来获取是哪个哈希槽,再确定端口
[2018/1/30 16:05:28] 127.0.0.1:7005> get aa
[2018/1/30 16:05:28] -> Redirected to slot [1180] located at 127.0.0.1:7001
[2018/1/30 16:05:28] "11"
[2018/1/30 16:05:48] 127.0.0.1:7001> quit
//杀死主节点,-9表示无条件强制杀死进程,3136是进程id,这里模拟宕机
[2018/1/30 16:06:02] [root@bogon ~]# ps -ef | grep -i redis
[2018/1/30 16:06:02] root       3023      1  0 1月30 ?       00:00:13 bin/redis-server *:6379
[2018/1/30 16:06:02] root       3126      1  0 1月30 ?       00:00:05 /usr/local/redis/bin/redis-server *:7001 [cluster]
[2018/1/30 16:06:02] root       3136      1  0 1月30 ?       00:00:05 /usr/local/redis/bin/redis-server *:7002 [cluster]
[2018/1/30 16:06:02] root       3140      1  0 1月30 ?       00:00:04 /usr/local/redis/bin/redis-server *:7003 [cluster]
[2018/1/30 16:06:02] root       3144      1  0 1月30 ?       00:00:04 /usr/local/redis/bin/redis-server *:7004 [cluster]
[2018/1/30 16:06:02] root       3148      1  0 1月30 ?       00:00:04 /usr/local/redis/bin/redis-server *:7005 [cluster]
[2018/1/30 16:06:02] root       3152      1  0 1月30 ?       00:00:04 /usr/local/redis/bin/redis-server *:7006 [cluster]
[2018/1/30 16:06:02] root      16593   3037  0 00:04 pts/0    00:00:00 grep --color=auto -i redis
[2018/1/30 16:07:53] [root@bogon ~]# kill -9 3136
[2018/1/30 16:07:54] [root@bogon ~]# ps -ef | grep -i redis
[2018/1/30 16:07:54] root       3023      1  0 1月30 ?       00:00:13 bin/redis-server *:6379
[2018/1/30 16:07:54] root       3126      1  0 1月30 ?       00:00:05 /usr/local/redis/bin/redis-server *:7001 [cluster]
[2018/1/30 16:07:54] root       3140      1  0 1月30 ?       00:00:04 /usr/local/redis/bin/redis-server *:7003 [cluster]
[2018/1/30 16:07:54] root       3144      1  0 1月30 ?       00:00:05 /usr/local/redis/bin/redis-server *:7004 [cluster]
[2018/1/30 16:07:54] root       3148      1  0 1月30 ?       00:00:05 /usr/local/redis/bin/redis-server *:7005 [cluster]
[2018/1/30 16:07:54] root       3152      1  0 1月30 ?       00:00:05 /usr/local/redis/bin/redis-server *:7006 [cluster]
[2018/1/30 16:07:54] root      16596   3037  0 00:06 pts/0    00:00:00 grep --color=auto -i redis
//可以发现原本的主节点7002被强制杀死后消失,对应的子节点变成了主节点。
//如果再次杀死7005,则slots:5461-10922这段哈希槽空缺,报错。
[2018/1/30 16:08:40] [root@bogon ~]# redis-trib.rb check 127.0.0.1:7001
[2018/1/30 16:08:40] >>> Performing Cluster Check (using node 127.0.0.1:7001)
[2018/1/30 16:08:40] M: 03faf924369fb2622aa851b846a797fd613c451d 127.0.0.1:7001
[2018/1/30 16:08:40]    slots:0-5460 (5461 slots) master
[2018/1/30 16:08:40]    1 additional replica(s)
[2018/1/30 16:08:40] M: 6aa9f5b62591b508ae0576c6f769c75e8c86bb42 127.0.0.1:7005
[2018/1/30 16:08:40]    slots:5461-10922 (5462 slots) master
[2018/1/30 16:08:40]    0 additional replica(s)
[2018/1/30 16:08:40] S: 6e13358c766de76177e4641c55bc7a246f7dc8e3 127.0.0.1:7006
[2018/1/30 16:08:40]    slots: (0 slots) slave
[2018/1/30 16:08:40]    replicates 715a175dfe8b30ad1304787d47ca917d6ef5d1be
[2018/1/30 16:08:40] S: 675abfe3278df935ef8a9766c3fe5120ac7f40b1 127.0.0.1:7004
[2018/1/30 16:08:40]    slots: (0 slots) slave
[2018/1/30 16:08:40]    replicates 03faf924369fb2622aa851b846a797fd613c451d
[2018/1/30 16:08:40] M: 715a175dfe8b30ad1304787d47ca917d6ef5d1be 127.0.0.1:7003
[2018/1/30 16:08:40]    slots:10923-16383 (5461 slots) master
[2018/1/30 16:08:40]    1 additional replica(s)
[2018/1/30 16:08:40] [OK] All nodes agree about slots configuration.
[2018/1/30 16:08:40] >>> Check for open slots...
[2018/1/30 16:08:40] >>> Check slots coverage...
[2018/1/30 16:08:40] [OK] All 16384 slots covered.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值