【redis使用系列】单节点安装redis 3.2版本集群

本文参考资料

https://blog.csdn.net/qq_37595946/article/details/77800147
https://blog.csdn.net/ahzxj2012/article/details/73468119
https://www.jianshu.com/p/854d702e6153

背景交代

公司有些没权限的机器又抽风,于是想把部分服务放到有权限的机器上。比如redis cluster。

安装规划

采用docker安装redis node。
由redis-trib.rb构建集群。

安装过程

docker安装redisnode

准备配置文件

port 6381
bind 10.2.1.31
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

拉取镜像并启动

docker pull redis:3.2.0
docker run --net=host -m 100m --name=redis1 -v `pwd`/redis1.conf:/usr/local/etc/redis/redis.conf -d redis:3.2.0 redis-server /usr/local/etc/redis/redis.conf
docker run --net=host -m 100m --name=redis2 -v `pwd`/redis2.conf:/usr/local/etc/redis/redis.conf -d redis:3.2.0 redis-server /usr/local/etc/redis/redis.conf
docker run --net=host -m 100m --name=redis3 -v `pwd`/redis3.conf:/usr/local/etc/redis/redis.conf -d redis:3.2.0 redis-server /usr/local/etc/redis/redis.conf
docker run --net=host -m 100m --name=redis4 -v `pwd`/redis4.conf:/usr/local/etc/redis/redis.conf -d redis:3.2.0 redis-server /usr/local/etc/redis/redis.conf
docker run --net=host -m 100m --name=redis5 -v `pwd`/redis5.conf:/usr/local/etc/redis/redis.conf -d redis:3.2.0 redis-server /usr/local/etc/redis/redis.conf
docker run --net=host -m 100m --name=redis6 -v `pwd`/redis6.conf:/usr/local/etc/redis/redis.conf -d redis:3.2.0 redis-server /usr/local/etc/redis/redis.conf

下载redis源码并解压

执行下面的命令前,请确保ruby版本已经升级到2.2.0。
ps:yum install ruby时,centos只会安装ruby2.0版本。

wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar xvf redis-3.2.1.tar.gz
mv redis-3.2.1 redis
ruby redis/src/redis-trib.rb create --replicas 1 10.2.1.31:6381 10.2.1.31:6382 10.2.1.31:6383 10.2.1.31:6384 10.2.1.31:6385 10.2.1.31:6386

顺利的话,能看到如下信息:

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.2.1.31:6381
10.2.1.31:6382
10.2.1.31:6383
Adding replica 10.2.1.31:6384 to 10.2.1.31:6381
Adding replica 10.2.1.31:6385 to 10.2.1.31:6382
Adding replica 10.2.1.31:6386 to 10.2.1.31:6383
M: cb4258f0a219065492d519ebf4417833c397718a 10.2.1.31:6381
slots:0-5460 (5461 slots) master
M: 5e1c70538ec4ddd28b0dbc79bce491ac1d25e452 10.2.1.31:6382
slots:5461-10922 (5462 slots) master
M: 31c913e81bbb994e843d8874d8063debd2e82363 10.2.1.31:6383
slots:10923-16383 (5461 slots) master
S: 34750039ae8d7010a06a7691b95ae81f48e44cb9 10.2.1.31:6384
replicates cb4258f0a219065492d519ebf4417833c397718a
S: 48f5de18c92ebdc277c4eaee9d73ea6f1cc91c8e 10.2.1.31:6385
replicates 5e1c70538ec4ddd28b0dbc79bce491ac1d25e452
S: 1a349016ed6f326bb4118ec94951301eb3ca7815 10.2.1.31:6386
replicates 31c913e81bbb994e843d8874d8063debd2e82363
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 10.2.1.31:6381)
M: cb4258f0a219065492d519ebf4417833c397718a 10.2.1.31:6381
slots:0-5460 (5461 slots) master
M: 5e1c70538ec4ddd28b0dbc79bce491ac1d25e452 10.2.1.31:6382
slots:5461-10922 (5462 slots) master
M: 31c913e81bbb994e843d8874d8063debd2e82363 10.2.1.31:6383
slots:10923-16383 (5461 slots) master
M: 34750039ae8d7010a06a7691b95ae81f48e44cb9 10.2.1.31:6384
slots: (0 slots) master
replicates cb4258f0a219065492d519ebf4417833c397718a
M: 48f5de18c92ebdc277c4eaee9d73ea6f1cc91c8e 10.2.1.31:6385
slots: (0 slots) master
replicates 5e1c70538ec4ddd28b0dbc79bce491ac1d25e452
M: 1a349016ed6f326bb4118ec94951301eb3ca7815 10.2.1.31:6386
slots: (0 slots) master
replicates 31c913e81bbb994e843d8874d8063debd2e82363
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

但是一般没有那么顺利。

常见错误以及处理

出现”kernel_require.rb:55:in `require’: cannot load suc”

解决办法:gem install redis

出现”报错redis requires Ruby version -= 2.2.2”

解决办法:升级ruby版本。

curl -L get.rvm.io | bash -s stable
source /usr/local/rvm/scripts/rvm
rvm list known
rvm install 2.4.0
rvm remove 2.0.0
ruby --version
gem install redis

然后执行:ruby redis/src/redis-trib.rb create –replicas 1 10.2.1.31:6381 10.2.1.31:6382 10.2.1.31:6383 10.2.1.31:6384 10.2.1.31:6385 10.2.1.31:6386
就可以看到上面的提示信息了。

ruby版本失效

解决办法:
rvm use ruby-2.4.0

体验