使用redis4.0.1和redis-cluster搭建集羣並編寫重啓shell腳本

1.刪除機器上原有的redis2.8

關閉redis-serverhtml

killall -9 redis-server

查找redis文件所在目錄node

which redis

刪除相關文件redis

rm -rf redis
rm -rf redis-proxy/
rm -rf nutcracker/

刪除相關命令shell

rm -f /usr/local/bin/redis-*

2.下載、安裝redis

wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar  xzf redis-4.0.1.tar.gz 
mv redis-4.0.1 /usr/local/
cd /usr/local/redis-4.0.1/
make
make test

3.配置節點(新建6個以端口號命名的目錄,表示6個節點,3主3從)

cd /usr/local
mkdir cluster
cd cluster/
mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005

在上面新建的每一個目錄下新建redis.conf文件,並寫入如下內容:vim

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
protected-mode no
vim 7000/redis.conf
vim 7001/redis.conf
vim 7002/redis.conf
vim 7003/redis.conf
vim 7004/redis.conf
vim 7005/redis.conf

爲了方便使用把redis-server文件複製到/usr/local/cluster目錄下ruby

cp /usr/local/redis-4.0.1/src/redis-server  /usr/local/cluster

進入每一個節點的目錄,並用對應的配置文件啓動redisapp

cd /usr/local/cluster/7000
../redis-server ./redis.conf &

先測試一下是否啓動成功過,測試

/usr/local/redis-4.0.1/src/redis-cli -p 7000

若是能進去redis命令行表示啓動成功,再去啓動其餘端口ui

cd /usr/local/cluster/7001
../redis-server ./redis.conf &
cd /usr/local/cluster/7002
../redis-server ./redis.conf &
cd /usr/local/cluster/7003
../redis-server ./redis.conf &
cd /usr/local/cluster/7004
../redis-server ./redis.conf &
cd /usr/local/cluster/7005
../redis-server ./redis.conf &

接下來啓動cluster.net

cd /usr/local/redis-4.0.1/src/
./redis-trib.rb create --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005

4.常見錯誤及解決方法

若是系統上各類依賴比較齊全的話,這時就能啓動成功了,一般還可能遇到如下錯誤

錯誤1:

root@localhost src]# ./redis-trib.rb create --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005 
/usr/bin/env: ruby: No such file or directory

解決方案:

yum install ruby

image

錯誤2:

[root@localhost src]# ./redis-trib.rb create --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005 
./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
    from ./redis-trib.rb:24

解決方案:

yum install rubygems

image

錯誤3:

[root@localhost src]# ./redis-trib.rb creat --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005 
/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

image

解決方案:

gem install redis

image

yum install ruby
yum install rubygems
gem install redis

安裝完以上各類依賴以後,再次執行如下命令便可成功啓動cluster

./redis-trib.rb create --replicas 1 172.16.180.226:7000 172.16.180.226:7001 172.16.180.226:7002 172.16.180.227:7003 172.16.180.227:7004 172.16.180.227:7005

image

5.測試cluster集羣是否能正常使用

/usr/local/src/redis-cli  -c -h 172.16.180.226 -p 7000

上面的命令若是缺乏了「-c」會出現如下錯誤。

image

6.編寫重啓cluster的shell腳本

vim restart-cluster.sh

寫入如下內容:

#!/bin/sh
killall -9 redis-server
#刪掉之前運行的節點留下的配置信息
find /usr/local/cluster/ -name "appendonly.aof"|xargs rm -f;
find /usr/local/cluster/ -name "nodes.conf"|xargs rm -f;
find /usr/local/cluster/ -name "dump.rdb"|xargs rm -f;
#從新啓動redis
cd /usr/local/cluster/7000
../redis-server ./redis.conf &
cd /usr/local/cluster/7001
../redis-server ./redis.conf &
cd /usr/local/cluster/7002
../redis-server ./redis.conf &
cd /usr/local/cluster/7003
../redis-server ./redis.conf &
cd /usr/local/cluster/7004
../redis-server ./redis.conf &
cd /usr/local/cluster/7005
../redis-server ./redis.conf &
cd /usr/local/redis-4.0.1/src/
#從新建立cluster集羣
./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

shell文件保存以後須要賦予可執行權限。

chmod +x restart-cluster.sh

使用腳本重啓

./restart-cluster.sh

查看是否重啓成功

src/redis-cli -c -p 7000

本文參考瞭如下資料:

http://www.redis.cn/download.html
http://www.redis.cn/topics/cluster-tutorial.html
http://www.oschina.net/question/1259683_2133954
相關文章
相關標籤/搜索