一、安装背景
由于工作需要,要求在测试环境安装redis-cluster集群,但是测试环境又不能访问外网,只能通过下载一些离线安装包进行安装,环境准备中提供所有离线安装所需的文件的下载地址,包括ruby-2.4.1.tar.gz,rubygems-2.7.6.tgz,zlib-1.2.11.tar.gz,redis-4.0.2.gem,tcl8.6.8-src.tar.gz
二、环境准备
- 系统版本 CentOS 7.2
- 软件版本 redis-4.0.2.tar.gz
- 所需的依赖包
通过 cat /etc/redhat-release 可查看系统版本
# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
三、开始安装
1. 离线安装ruby
将ruby-2.4.1.tar.gz上传至服务器,解压安装包
# tar -zxvf ruby-2.4.1.tar.gz
进入ruby-2.4.1文件,并配置安装目录
# cd ruby-2.4.1
# ./configure --prefix=/usr/local/ruby
通过echo $?命令查看是否执行完成,0表示完成,其他值为未完成
# echo $?
0
开始编译安装
# make && make install
安装完成后,将ruby加入到系统路径里,通过source命令让/etc/profile立即生效
# echo "PATH=$PATH:/usr/local/ruby/bin" >> /etc/profile
# source /etc/profile
为ruby创建软连接
# which ruby
/usr/local/ruby/bin/ruby
# rm -rf /usr/bin/ruby
# ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby
# which ruby
/usr/bin/ruby
此时ruby便安装完成,可通过ruby -v查看版本
# ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
2. 离线安装rubygems
将rubygems-2.7.6.tgz上传至服务器,解压
# tar -xvf rubygems-2.7.6.tgz
# chown -R root.root rubygems-2.7.6
# mv rubygems-2.7.6 /usr/local/
安装rubygems
# cd /usr/local/rubygems-2.7.6/
# ruby setup.rb
此时可能会出现以下报错
# ruby setup.rb
Bundler 1.16.1 installed
RubyGems 2.7.6 installed
/usr/local/rubygems-2.7.6/lib/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- zlib (LoadError)
from /usr/local/rubygems-2.7.6/lib/rubygems/core_ext/kernel_require.rb:59:in `require'
from /usr/local/rubygems-2.7.6/lib/rubygems/package.rb:47:in `<top (required)>'
from /usr/local/rubygems-2.7.6/lib/rubygems/core_ext/kernel_require.rb:59:in `require'
from /usr/local/rubygems-2.7.6/lib/rubygems/core_ext/kernel_require.rb:59:in `require'
from /usr/local/rubygems-2.7.6/lib/rubygems/commands/pristine_command.rb:3:in `<top (required)>'
from /usr/local/rubygems-2.7.6/lib/rubygems/core_ext/kernel_require.rb:59:in `require'
from /usr/local/rubygems-2.7.6/lib/rubygems/core_ext/kernel_require.rb:59:in `require'
from /usr/local/rubygems-2.7.6/lib/rubygems/commands/setup_command.rb:583:in `regenerate_binstubs'
from /usr/local/rubygems-2.7.6/lib/rubygems/commands/setup_command.rb:155:in `execute'
from /usr/local/rubygems-2.7.6/lib/rubygems/command.rb:313:in `invoke_with_build_args'
from /usr/local/rubygems-2.7.6/lib/rubygems/command_manager.rb:171:in `process_args'
from /usr/local/rubygems-2.7.6/lib/rubygems/command_manager.rb:141:in `run'
from /usr/local/rubygems-2.7.6/lib/rubygems/gem_runner.rb:59:in `run'
from setup.rb:46:in `<main>'
从报错信息来看,大概意思是无法加载zlib文件,怀疑是ruby在编译安装的时候没有编译这个zlib,于是决定把ruby重新编译安装。
# cd /usr/local/src/redis-cluster/ruby-2.4.1/ext/zlib/
# ruby extconf.rb
# make
/usr/local/src/redis-cluster/ruby-2.4.1/为reby的路径,当执行make命令时,可能又会报以下错误
# make
make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'. Stop.
此时,可修改当前目录下的Makefile文件
# vi Makefile
#zlib.o: $(top_srcdir)/include/ruby.h #把这一行替换成下面一行(278行)
zlib.o: ../../include/ruby.h
重新编译,即可成功
# make
# make install
此时zlib便加载成功了,然后在回到rubygems继续安装
# cd /usr/local/rubygems-2.7.6/
# ruby setup.rb
这时,应该是可以成功安装完成的。然后设置系统路径
# echo "PATH=$PATH:/usr/local/rubygems-2.7.6" >> /etc/profile
# source /etc/profile
这样,rubygems便是安装完成了,可查看当前版本
# gem -v
2.7.6
3. 离线配置rubygems的redisapi
将redis-4.0.2.gem上传至服务器,可先查看系统是否已经安装过
# gem list redis
*** LOCAL GEMS ***
我这里是没有安装过,执行安装命令
# gem install -l redis-4.0.2.gem
Successfully installed redis-4.0.2
Parsing documentation for redis-4.0.2
Installing ri documentation for redis-4.0.2
Done installing documentation for redis after 1 seconds
1 gem installed
这个便安装成功了,查看详情
# gem list redis
*** LOCAL GEMS ***
redis (4.0.2)
4. 离线安装tcl
上传tcl8.6.8-src.tar.gz到服务器,解压,指定安装路径,安装
# tcl8.6.8-src.tar.gz
# cd tcl8.6.8/unix/
# ./configure --prefix=/usr/local/tcl
# echo $?
# make
# make install
# echo $?
# make install-private-headers
设置软连接,赋权
# ln -v -sf tclsh8.6 /usr/local/tcl/bin/tclsh
# chmod -v 755 /usr/local/tcl/lib/libtcl8.6.so
# echo "PATH=$PATH:/usr/local/tcl/bin" >> /etc/profile
# source /etc/profile
5. 离线安装redis
上传redis-4.0.2.tar.gz至服务器,解压,安装
# tar -zxf redis-4.0.2.tar.gz
# cd redis-4.0.2
# make
# echo $?
至此,redis变安装成功了,接下来我们便开始部署集群,由于redis-cluster至少需要3个主节点,若每个主节点配置一个从节点,则至少需要六个,因此我们在这里创建6个文件夹,当然,如果有多台服务器,可分布在各台服务器上
# mkdir /usr/local/src/redis-cluster/redis-4.0.2/redis-cluster
# cd /usr/local/src/redis-cluster/redis-4.0.2/redis-cluster
# mkdir {6500,6501,6502,6503,6504,6505}
然后,为每个节点创建配置文件,以6500为例,部署172.130.132.128:6500
# cat <<EOF>./6500/redis.conf
> port 6500
> bind 172.130.132.128
> daemonize yes
> pidfile /var/run/redis_6500.pid
> cluster-enabled yes
> cluster-config-file nodes_6500.conf
> cluster-node-timeout 10100
> appendonly yes
> EOF
同理,部署172.130.132.128:6501,只需修改下端口即可
# cat <<EOF>./6500/redis.conf
> port 6501
> bind 172.130.132.128
> daemonize yes
> pidfile /var/run/redis_6501.pid
> cluster-enabled yes
> cluster-config-file nodes_6501.conf
> cluster-node-timeout 10100
> appendonly yes
> EOF
以此类推,依次创建6502,6503,6504,6505的配置文件,配置完成后,启动所有节点
[root@i-epr36ck8 redis-cluster]# for((i=0;i<=5;i++)); do /usr/local/src/redis-cluster/redis-4.0.2/src/redis-server /usr/local/src/redis-cluster/redis-4.0.2/redis-cluster/650$i/redis.conf; done
e20304:C 06 Mar 16:45:31.488 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20304:C 06 Mar 16:45:31.488 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=20304, just started
20304:C 06 Mar 16:45:31.488 # Configuration loaded
20306:C 06 Mar 16:45:31.510 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20306:C 06 Mar 16:45:31.510 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=20306, just started
20306:C 06 Mar 16:45:31.510 # Configuration loaded
20308:C 06 Mar 16:45:31.515 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20308:C 06 Mar 16:45:31.515 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=20308, just started
20308:C 06 Mar 16:45:31.515 # Configuration loaded
20313:C 06 Mar 16:45:31.520 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20313:C 06 Mar 16:45:31.520 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=20313, just started
20313:C 06 Mar 16:45:31.520 # Configuration loaded
20318:C 06 Mar 16:45:31.545 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20318:C 06 Mar 16:45:31.545 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=20318, just started
20318:C 06 Mar 16:45:31.545 # Configuration loaded
20323:C 06 Mar 16:45:31.559 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20323:C 06 Mar 16:45:31.559 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=20323, just started
20323:C 06 Mar 16:45:31.559 # Configuration loaded
查看状态
[root@i-epr36ck8 redis-cluster]# ps -ef |grep redis-server
root 20305 1 0 16:45 ? 00:00:00 /usr/local/src/redis-cluster/redis-4.0.2/src/redis-server 172.130.132.128:6500 [cluster]
root 20307 1 0 16:45 ? 00:00:00 /usr/local/src/redis-cluster/redis-4.0.2/src/redis-server 172.130.132.128:6501 [cluster]
root 20312 1 0 16:45 ? 00:00:00 /usr/local/src/redis-cluster/redis-4.0.2/src/redis-server 172.130.132.128:6502 [cluster]
root 20317 1 0 16:45 ? 00:00:00 /usr/local/src/redis-cluster/redis-4.0.2/src/redis-server 172.130.132.128:6503 [cluster]
root 20319 1 0 16:45 ? 00:00:00 /usr/local/src/redis-cluster/redis-4.0.2/src/redis-server 172.130.132.128:6504 [cluster]
root 20324 1 0 16:45 ? 00:00:00 /usr/local/src/redis-cluster/redis-4.0.2/src/redis-server 172.130.132.128:6505 [cluster]
root 20742 17205 0 16:52 pts/0 00:00:00 grep --color=auto redis-server
redis.conf的配置说明
- port 6500 配置集群的端口
- bind 本机的IP 这里的默认配置是127.0.0.1改为内网ip
- daemonsize yes 允许redis在后台运行
- pidfile /var/run/redis_6500.pidbind 本机的IP 改成和端口一致
- cluster-enabled yes 开启集群 把注释去掉
- cluster-config-file node_6500.conf 集群的配置,和端口一致
- cluster-node-timeout 15000 请求超时,默认为15秒
- appendonly yes aof日志开启,有需要就开启,每一次写操作都会记录一条日志。
压轴戏来了,创建集群
# cd /usr/local/src/redis-cluster/redis-4.0.2/src/
# ./redis-trib.rb create --replicas 1 172.130.132.128:6500 172.130.132.128:6501 172.130.132.128:6502 172.130.132.128:6503 172.130.132.
128:6504 172.130.132.128:6505
可能会出现以下报错(若无此错误,可忽略)
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 5 nodes and 172 replicas per node.
*** At least 519 nodes are required.
于是,使用/redis-trib.rb check检查节点
[root@i-epr36ck8 src]# ./redis-trib.rb check 172.130.132.128:6500
>>> Performing Cluster Check (using node 172.130.132.128:6500)
M: 7e26d3b838e91ab5e6424ddd857d5593b6a1bfa1 172.130.132.128:6500
slots: (0 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
这个往往是由于主node移除了,但是并没有移除node上面的slot,从而导致了slot总数没有达到16384,其实也就是slots分布不正确。所以在删除节点的时候一定要注意删除的是否是Master主节点。
此时,可使用./redis-trib.rb fix来修复每个端口
# ./redis-trib.rb fix 172.130.132.128:6500
# ./redis-trib.rb fix 172.130.132.128:6501
# ./redis-trib.rb fix 172.130.132.128:6502
# ./redis-trib.rb fix 172.130.132.128:6503
# ./redis-trib.rb fix 172.130.132.128:6504
# ./redis-trib.rb fix 172.130.132.128:6505
如果出现了以下错误,
/usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-4.0.2/lib/redis/client.rb:119:in `call': ERR Slot 0 is already busy (Redis::CommandError)
from /usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-4.0.2/lib/redis.rb:2854:in `block in method_missing'
from /usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-4.0.2/lib/redis.rb:45:in `block in synchronize'
from /usr/local/ruby/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
from /usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-4.0.2/lib/redis.rb:45:in `synchronize'
from /usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-4.0.2/lib/redis.rb:2853:in `method_missing'
from ./redis-trib.rb:212:in `flush_node_config'
from ./redis-trib.rb:776:in `block in flush_nodes_config'
from ./redis-trib.rb:775:in `each'
from ./redis-trib.rb:775:in `flush_nodes_config'
from ./redis-trib.rb:1296:in `create_cluster_cmd'
from ./redis-trib.rb:1700:in `<main>'
错误提示是,
slot插槽被占用了(这是搭建集群前时,以前redis的旧数据和配置信息没有清理干净。)
用redis-cli 登录到每个节点执行 flushall 和 cluster reset 就可以了。
[root@i-epr36ck8 src]# ./redis-cli -h 172.130.132.128 -c -p 6500
172.130.132.128:6500> flushall
OK
172.130.132.128:6500> cluster reset
OK
再次执行集群创建命令,即可安装完成
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
四、验证集群
# cd /usr/local/src/redis-cluster/redis-4.0.2/src
# ./redis-cli -h 172.130.132.128 -c -p 6500
172.130.132.128:6500> set test 1111
ok
172.130.132.128:6500> get test
"1111"
172.130.132.128:6500> quit
# ./redis-cli -h 172.130.132.128 -c -p 6501
172.130.132.128:6501> get test
"1111"
大功告成,完美收工~