Redis集群配置与测试

环境:
系统:CentOS 7
Redis: 4.0.1

测试记录,按照操作流程随手记


创建几个测试目录,在里面放redis.conf文件

$ ll
总用量 1708
drwxr-xr-x 2 root root      23 5月  31 10:30 d6379
drwxr-xr-x 2 root root      23 5月  31 10:31 d6380
drwxr-xr-x 2 root root      23 5月  31 10:32 d6381
drwxr-xr-x 2 root root      23 5月  31 10:32 d6382
drwxr-xr-x 2 root root      23 5月  31 10:32 d6383
drwxr-xr-x 2 root root      23 5月  31 10:32 d6384
drwxr-xr-x 2 root root      23 5月  31 10:33 d6385

文件内容如下,每个文件的端口不同,最好与目录对应,方便记忆

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

启动6个redis实例
注意要分别进入到每个目录中启动,启动后会自动在目录下创建nodes.conf文件

cd d6379
/usr/local/redis/bin/redis-server redis.conf 

cd ..
cd d6380
/usr/local/redis/bin/redis-server redis.conf 
...
...

查看一下进程

# ps -ef | grep redis
root      9608     1  0 11:10 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379 [cluster]
root      9615     1  0 11:11 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6380 [cluster]
root      9620     1  0 11:11 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6381 [cluster]
root      9625     1  0 11:12 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6382 [cluster]
root      9630     1  0 11:12 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6383 [cluster]
root      9635     1  0 11:12 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6384 [cluster]
root      9640     1  0 11:12 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6385 [cluster]

使用这些实例来创建集群, 并为每个节点编写配置文件。

通过使用 Redis 集群命令行工具 redis-trib, 编写节点配置文件的工作可以非常容易地完成:redis-trib 位于 Redis 源码的 src 文件夹中,它是一个 Ruby 程序,这个程序通过向实例发送特殊命令来完成创建新集群,检查集群,或者对集群进行重新分片(reshared)等工作。

./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385

报错:

/usr/bin/env: ruby: 没有那个文件或目录

安装ruby

yum install ruby

报错:

/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
        from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
        from ./redis-trib.rb:25:in `<main>'

使用RubyGems安装ruby脚本所需要的redis包

gem install redis

报错:

Fetching: redis-4.0.1.gem (100%)
ERROR:  Error installing redis:
        redis requires Ruby version >= 2.2.2.

看了下当前安装的ruby版本

# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

CentOS7 yum库中ruby的版本只支持到2.0.0,使用RVM(Ruby Version Manager)程序升级Ruby的版本
先安装RVM程序

curl -L get.rvm.io | bash -s stable 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    308      0 --:--:-- --:--:-- --:--:--   307
100 24361  100 24361    0     0  12772      0  0:00:01  0:00:01 --:--:-- 89410
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 已创建目录‘/root/.gnupg’
gpg: 新的配置文件‘/root/.gnupg/gpg.conf’已建立
gpg: 警告:在‘/root/.gnupg/gpg.conf’里的选项于此次运行期间未被使用
gpg: 钥匙环‘/root/.gnupg/pubring.gpg’已建立
gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 无法检查签名:没有公钥
Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -

the key can be compared with:

    https://rvm.io/mpapis.asc
    https://keybase.io/mpapis

NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.

无法验证签名,按照提示下载公钥

说明:
gpg2 --recv-keys : 该命令从密钥服务器下载一个或多个公钥。每个key-id都是一个密钥ID。该命令需要使用keyserver选项来指定gpg应该从哪个keyserver下载密钥。

# gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
gpg: 下载密钥‘D39DC0E3’,从 hkp 服务器 keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库
gpg: 密钥 D39DC0E3:公钥“Michal Papis (RVM signing) <mpapis@gmail.com>”已导入
gpg: 没有找到任何绝对信任的密钥
gpg: 合计被处理的数量:1
gpg:           已导入:1  (RSA: 1)

再次安装RVM

]# curl -L get.rvm.io | bash -s stable 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    303      0 --:--:-- --:--:-- --:--:--   302
100 24361  100 24361    0     0  11395      0  0:00:02  0:00:02 --:--:-- 41430
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 完好的签名,来自于“Michal Papis (RVM signing) <mpapis@gmail.com>”
gpg:               亦即“Michal Papis <michal.papis@toptal.com>”
gpg:               亦即“[jpeg image of size 5015]”
gpg: 警告:这把密钥未经受信任的签名认证!
gpg:       没有证据表明这个签名属于它所声称的持有者。
主钥指纹: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3
子钥指纹: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17
GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

设置下环境变量,在查看下rvm版本

[root@wwh214 redis]# source /etc/profile.d/rvm.sh
[root@wwh214 redis]# rvm --version
rvm 1.29.3 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

使用RVM升级Ruby版本
查看rvm库中已知的ruby版本

$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
ruby-head

安装2.4版本的ruby

$ rvm install 2.4.1
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.4.1.tar.bz2
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: libffi-devel, readline-devel, sqlite-devel, libyaml-devel............
Requirements installation successful.
ruby-2.4.1 - #configure
ruby-2.4.1 - #download
...
...

在查看一下

[root@wwh214 redis]# rvm list

rvm rubies

=* ruby-2.4.1 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

[root@wwh214 redis]# ruby --version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

之前通过yum安装的ruby并没有在rvm管理中,将其卸载掉

yum remove ruby

再次使用RubyGems安装ruby脚本所需要的redis包

# gem install redis
Fetching: redis-4.0.1.gem (100%)
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 0 seconds
1 gem installed

再次使用命令创建集群

./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6379
127.0.0.1:6381
127.0.0.1:6382
Adding replica 127.0.0.1:6383 to 127.0.0.1:6379
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
Adding replica 127.0.0.1:6385 to 127.0.0.1:6382
M: b2ba9914a49563dabdf022c1f8031577e4b30e44 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: d38eaf2cf3a5ed2a4d4b0bafcb174748fac009dc 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: b54907e579a7daf1fb7155a4f25f5ae485374636 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
S: 602191b3b062238e10c1284343b4677b51fd31a3 127.0.0.1:6383
   replicates b2ba9914a49563dabdf022c1f8031577e4b30e44
S: aa4feef4e0c426ead799d4b34288cb3c5b9baa58 127.0.0.1:6384
   replicates d38eaf2cf3a5ed2a4d4b0bafcb174748fac009dc
S: a642a61c228ea354d94c695ca9dcaa048750590e 127.0.0.1:6385
   replicates b54907e579a7daf1fb7155a4f25f5ae485374636
Can I set the above configuration? (type 'yes' to accept): 

这个命令在这里用于创建一个新的集群, 选项–replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。之后跟着的其他参数则是这个集群实例的地址列表,3个master3个slave,redis-trib 会打印出一份预想中的配置给你看, 如果你觉得没问题的话, 就可以输入 yes , redis-trib 就会将这份配置应用到集群当中,让各个节点开始互相通讯,最后可以得到如下信息:

>>> 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:6379)
M: b2ba9914a49563dabdf022c1f8031577e4b30e44 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: a642a61c228ea354d94c695ca9dcaa048750590e 127.0.0.1:6385
   slots: (0 slots) slave
   replicates b54907e579a7daf1fb7155a4f25f5ae485374636
S: aa4feef4e0c426ead799d4b34288cb3c5b9baa58 127.0.0.1:6384
   slots: (0 slots) slave
   replicates d38eaf2cf3a5ed2a4d4b0bafcb174748fac009dc
M: d38eaf2cf3a5ed2a4d4b0bafcb174748fac009dc 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: b54907e579a7daf1fb7155a4f25f5ae485374636 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 602191b3b062238e10c1284343b4677b51fd31a3 127.0.0.1:6383
   slots: (0 slots) slave
   replicates b2ba9914a49563dabdf022c1f8031577e4b30e44
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

再对比一下node.conf配置文件:
以d6381目录为例子,redis启动时创建的缺省文件是:

d38eaf2cf3a5ed2a4d4b0bafcb174748fac009dc :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0

运行脚本之后,文件变成了:

602191b3b062238e10c1284343b4677b51fd31a3 127.0.0.1:6383@16383 slave b2ba9914a49563dabdf022c1f8031577e4b30e44 0 1527749631645 4 connected
d38eaf2cf3a5ed2a4d4b0bafcb174748fac009dc 127.0.0.1:6381@16381 myself,master - 0 1527749631000 2 connected 5461-10922
b54907e579a7daf1fb7155a4f25f5ae485374636 127.0.0.1:6382@16382 master - 0 1527749631144 3 connected 10923-16383
aa4feef4e0c426ead799d4b34288cb3c5b9baa58 127.0.0.1:6384@16384 slave d38eaf2cf3a5ed2a4d4b0bafcb174748fac009dc 0 1527749631244 5 connected
b2ba9914a49563dabdf022c1f8031577e4b30e44 127.0.0.1:6379@16379 master - 0 1527749630140 1 connected 0-5460
a642a61c228ea354d94c695ca9dcaa048750590e 127.0.0.1:6385@16385 slave b54907e579a7daf1fb7155a4f25f5ae485374636 0 1527749631000 6 connected
vars currentEpoch 6 lastVoteEpoch 0

使用redis-cli登录测试一下:

# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> info
......
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6383,state=online,offset=742,lag=0
......

在往集群中某个redis中放入值的时候会计算key的hash所 属的槽,然后再计算槽是不是属于当前实例,如果不是会返回error,并且返回正确的节点地址和端口,如下:

127.0.0.1:6379> put aaa aaa
(error) ERR unknown command 'put'
127.0.0.1:6379> set aaa aaa
(error) MOVED 10439 127.0.0.1:6381

而在6381上可以设值成功

# /usr/local/redis/bin/redis-cli -p 6381
127.0.0.1:6381> set aaa aaa
OK
127.0.0.1:6381> 

不同的键值对存在不同的节点上,keys * 只会返回当前节点的所有key

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容