安装redis集群
第一步:安装相关依赖包
1):安装redis-cluster依赖:redis-cluster的依赖库在使用时有兼容问题,在reshard时会遇到各种错误,请按指定版本安装.
(1)确保系统安装zlib,否则geminstall会报(no such file to load -- zlib) #download:zlib-1.2.6.tar ./configure make make install 或者 yum install zlib
(1)安装ruby
cd /path/ruby ./configure -prefix=/usr/local/ruby make make install sudo cp ruby/usr/local/bin 或者 yum install ruby
(2)安装
rubygem:version(1.8.16)
#rubygems-1.8.16.tgz cd /path/gem sudo ruby setup.rb sudo cp bin/gem/usr/local/bin 或者 yum install rubygem
(3)安装gem-redis:version(3.0.0)
geminstall redis --version 3.0.0 #由于源的原因,可能下载失败,就手动下载下来安装 #download地址:http://rubygems.org/gems/redis/versions/3.0.0 geminstall -l /data/soft/redis-3.0.0.gem
第二步:安装redis-3.2.0
tar xf redis-3.2.0.tar.gz cd redis-3.2.0 make make install cd src/ cp redis-server /usr/local/bin cp redis-cli /usr/local/bin cp redis-trib.rb /usr/local/bin
创建集群需要的目录
[[email protected] /]mkdir -p /redis/{6000..6005} [[email protected] /]#cd /redis/ [[email protected] redis]# ll total 28 drwxr-xr-x. 2 root root 4096 May 9 03:39 6000 drwxr-xr-x. 2 root root 4096 May 9 03:54 6001 drwxr-xr-x. 2 root root 4096 May 9 03:39 6002 drwxr-xr-x. 2 root root 4096 May 9 03:39 6003 drwxr-xr-x. 2 root root 4096 May 9 03:51 6004 drwxr-xr-x. 2 root root 4096 May 9 03:39 6005 [[email protected] redis]#
修改配置文件redis.conf
cp /home/lvnian/tools/redis-3.2.0/redis.conf /redis vi /redis/redis.conf
##修改配置文件中的下面选项
port 6000 daemonize yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes
##修改完redis.conf配置文件中的这些配置项之后把这个配置文件分别拷贝到6000/6001/6002/6003/6004/6005目录下面
cp /redis/redis.conf /redis/6000/ cp /redis/redis.conf /redis/6001/ cp /redis/redis.conf /redis/6002/ cp /redis/redis.conf /redis/6003/ cp /redis/redis.conf /redis/6004/ cp /redis/redis.conf /redis/6005/
##注意:拷贝完成之后要修改6000/6001/6002/6003/6004/6005目录下面redis.conf文件中的port参数,分别改为对应的文件夹的名称
启动redis的每个实例
cd /redis/6000 redis-server redis.conf & cd /redis/6001 redis-server redis.conf & cd /redis/6002 redis-server redis.conf & cd /redis/6003 redis-server redis.conf & cd /redis/6004 redis-server redis.conf & cd /redis/6005 redis-server redis.conf & [[email protected] redis]# netstat -lntp|grep redis tcp 0 0 127.0.0.1:6000 0.0.0.0:* LISTEN 11046/redis-server tcp 0 0 127.0.0.1:6001 0.0.0.0:* LISTEN 11041/redis-server tcp 0 0 127.0.0.1:6002 0.0.0.0:* LISTEN 11049/redis-server tcp 0 0 127.0.0.1:6003 0.0.0.0:* LISTEN 11052/redis-server tcp 0 0 127.0.0.1:6004 0.0.0.0:* LISTEN 11055/redis-server tcp 0 0 127.0.0.1:6005 0.0.0.0:* LISTEN 11058/redis-server tcp 0 0 127.0.0.1:16000 0.0.0.0:* LISTEN 11046/redis-server tcp 0 0 127.0.0.1:16001 0.0.0.0:* LISTEN 11041/redis-server tcp 0 0 127.0.0.1:16002 0.0.0.0:* LISTEN 11049/redis-server tcp 0 0 127.0.0.1:16003 0.0.0.0:* LISTEN 11052/redis-server tcp 0 0 127.0.0.1:16004 0.0.0.0:* LISTEN 11055/redis-server tcp 0 0 127.0.0.1:16005 0.0.0.0:* LISTEN 11058/redis-server [[email protected] redis]#
1执行redis的创建集群命令创建集群
cd /home/lvnian/tools/redis-3.2.0/src ./redis-trib.rb create --replicas 1 127.0.0.1:6000 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005
2执行上面的命令的时候会报错,因为是执行的ruby的脚本,需要ruby的环境
错误内容:
/usr/bin/env: ruby: No such file or directory
所以需要安装ruby的环境,这里推荐使用yum install ruby安装
yum install ruby
3然后再执行第1步的创建集群命令,还会报错,提示缺少rubygems组件,使用yum安装
错误内容:
./redis-trib.rb:24:in `require‘: no such file to load -- rubygems (LoadError)from ./redis-trib.rb:24 yum install rubygems
4再次执行第1步的命令,还会报错,提示不能加载redis,是因为缺少redis和ruby的接口,使用gem 安装
错误内容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require‘: no such file to load -- redis (LoadError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require‘from ./redis-trib.rb:25 gem install redis
5.再次执行第1步的命令,正常执行
[[email protected] src]# ./redis-trib.rb create --replicas1 127.0.0.1:6000 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003127.0.0.1:6004 127.0.0.1:6005 >>> Creating cluster >>> Performing hash slots allocation on 6nodes... Using 3 masters: 127.0.0.1:6000 127.0.0.1:6001 127.0.0.1:6002 Adding replica 127.0.0.1:6003 to 127.0.0.1:6000 Adding replica 127.0.0.1:6004 to 127.0.0.1:6001 Adding replica 127.0.0.1:6005 to 127.0.0.1:6002 M: d968e771065b571e832b32b793ae235269ae7c6c127.0.0.1:6000 slots:0-5460 (5461 slots) master M: 7d8a4d617f2ada37f0efbdc1ce23accd9930a853127.0.0.1:6001 slots:5461-10922 (5462 slots) master M: 75a450d2d95eb5849aa33f50b1c6c6fac701aebf127.0.0.1:6002 slots:10923-16383 (5461 slots) master S: 2ec788dd51b6fee6fae76b1fa7c009bcfd1bafff127.0.0.1:6003 replicatesd968e771065b571e832b32b793ae235269ae7c6c S: cc2ce728ad7a8dee323dcd6ec5c0f21927bef71a127.0.0.1:6004 replicates7d8a4d617f2ada37f0efbdc1ce23accd9930a853 S: 6668aae6f3ce06cf4269977f82163876630abec8127.0.0.1:6005 replicates75a450d2d95eb5849aa33f50b1c6c6fac701aebf Can I set the above configuration? (type ‘yes‘ toaccept): yes >>> Nodes configuration updated >>> Assign a different config epoch toeach node 11046:M 09 May 03:39:21.630 # configEpoch set to 1via CLUSTER SET-CONFIG-EPOCH 11041:M 09 May 03:39:21.630 # configEpoch set to 2via CLUSTER SET-CONFIG-EPOCH 11049:M 09 May 03:39:21.631 # configEpoch set to 3via CLUSTER SET-CONFIG-EPOCH 11052:M 09 May 03:39:21.631 # configEpoch set to 4via CLUSTER SET-CONFIG-EPOCH 11055:M 09 May 03:39:21.631 # configEpoch set to 5via CLUSTER SET-CONFIG-EPOCH 11058:M 09 May 03:39:21.632 # configEpoch set to 6via CLUSTER SET-CONFIG-EPOCH >>> Sending CLUSTER MEET messages to jointhe cluster 11046:M 09 May 03:39:21.660 # IP address for thisnode updated to 127.0.0.1 11049:M 09 May 03:39:21.736 # IP address for thisnode updated to 127.0.0.1 11055:M 09 May 03:39:21.736 # IP address for thisnode updated to 127.0.0.1 11041:M 09 May 03:39:21.736 # IP address for thisnode updated to 127.0.0.1 11058:M 09 May 03:39:21.736 # IP address for thisnode updated to 127.0.0.1 11052:M 09 May 03:39:21.736 # IP address for thisnode updated to 127.0.0.1 Waiting for the cluster to join... 11052:S 09 May 03:39:25.655 # Cluster statechanged: ok 11055:S 09 May 03:39:25.656 # Cluster statechanged: ok 11058:S 09 May 03:39:25.657 # Cluster statechanged: ok >>> Performing Cluster Check (using node127.0.0.1:6000) M: d968e771065b571e832b32b793ae235269ae7c6c127.0.0.1:6000 slots:0-5460 (5461 slots) master M: 7d8a4d617f2ada37f0efbdc1ce23accd9930a853127.0.0.1:6001 slots:5461-10922 (5462 slots) master M: 75a450d2d95eb5849aa33f50b1c6c6fac701aebf127.0.0.1:6002 slots:10923-16383 (5461 slots) master M: 2ec788dd51b6fee6fae76b1fa7c009bcfd1bafff127.0.0.1:6003 slots: (0slots) master replicatesd968e771065b571e832b32b793ae235269ae7c6c M: cc2ce728ad7a8dee323dcd6ec5c0f21927bef71a127.0.0.1:6004 slots: (0slots) master replicates7d8a4d617f2ada37f0efbdc1ce23accd9930a853 M: 6668aae6f3ce06cf4269977f82163876630abec8127.0.0.1:6005 slots: (0slots) master replicates75a450d2d95eb5849aa33f50b1c6c6fac701aebf [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
至此redis集群即搭建成功!
使用redis-cli命令进入集群环境
[[email protected] redis-3.2.0]# redis-cli -c -p 6000 127.0.0.1:6000>
时间: 2024-10-01 12:06:42