使用ruby自带工具进行redis集群搭建

使用源码安装redis,在官网上下载3.0以后的版本。

(1)确保系统安装zlib,否则gem install会报(no such file to load -- zlib)

  1. #download:zlib-1.2.6.tar  

  2. ./configure  

  3. make  

  4. make install  

(2)安装ruby

  1. # ruby1.9.2   

  2. cd /home/searat/ruby  

  3. ./configure -prefix=/usr/local/ruby  

  4. make  

  5. make install  

  6. cp ruby /usr/local/bin  

(3)安装rubygem

  1. # rubygems-1.8.16.tgz  

  2. cd /home/searat/gem  

  3. ruby setup.rb  

  4. cp bin/gem /usr/local/bin  

(4)安装gem-redis

  1. gem install redis --version 3.0.0  



wget http://rubygems.org/gems/redis/versions/3.0.0  

  1. gem install -l /home/searat/redis-3.0.0.gem  

接下对redis目录做处理

使用源码安装后目录如下:

-bash-4.1$ tree -L 2 /home/searat/redis

/home/searat/redis

├── bin

│   ├── dump.rdb

│   ├── redis-benchmark

│   ├── redis-check-aof

│   ├── redis-check-dump

│   ├── redis-cli

│   ├── redis-sentinel -> redis-server

│   └── redis-server

├── conf

│   ├── dump.rdb

│   └── redis.conf

├── log

│   └── redis.log

└── redis_6379

从源码包中复制redis-trib.rb到/home/searat/redis/bin下,另外从源码包中找到create-cluster脚本,复制到/home/searat/redis/bin下。

若在单台服务器上使用create-cluster创建redis集群,直接执行create-cluster脚本即可。

若在两台或者更多台服务器上创建redis集群,可以改写create-cluster脚本

-bash-4.1$ cat create-cluster 
#!/bin/bash
PORT=$2   #端口号可以自己随意指定
PORTNUM=3 #定义端口号的个数
REPLICAS=1
REDIS_HOME=/home/searat/redis
HOST1=192.168.1.101
HOST2=192.168.1.102
HOSTS="$HOST1 $HOST2"
declare -A HOSTGROUP  #定义一个数组用来存放IP:PORT形式的数据,在创建集群时使用
i=1
if [ "$1" == "start" ]
then
    while [ $PORTNUM -gt "0" ]; do
        echo "Starting 127.0.0.1:$PORT"
        $REDIS_HOME/bin/redis-server ../conf/redis_$PORT.conf
PORT=$((PORT+1))
PORTNUM=$((PORTNUM-1))
    done
    exit 0
fi
if [ "$1" == "stop" ]
then
    while [ $PORTNUM -gt "0" ]; do
        echo "Stopping 127.0.0.1:$PORT"
        $REDIS_HOME/bin/redis-cli -p $PORT shutdown nosave
        PORT=$((PORT+1))
        PORTNUM=$((PORTNUM-1))
    done
    exit 0
fi
if [ "$1" == "create" ]
then
    while [ $PORTNUM -gt "0" ]; do
        for host in $HOSTS
            do
                HOSTGROUP[$i]=$host:$PORT
                i=`expr $i + 1`
            done
        PORT=$((PORT+1))
        PORTNUM=$((PORTNUM-1))
    done
    echo ${HOSTGROUP[*]}
    $REDIS_HOME/bin/redis-trib.rb create --replicas 1 ${HOSTGROUP[*]}
    exit 0
fi
if [ "$1" == "clean" ]
then
    rm -rf ../log/*.log
    rm -rf ../data/appendonly*.aof
    rm -rf ../data/dump*.rdb
    rm -rf ../data/nodes*.conf
    exit 0
fi
echo "Usage: $0 [start|create|stop|clean"
echo "start       -- Launch Redis Cluster instances."
echo "create      -- Create a cluster using redis-trib create."
echo "stop        -- Stop Redis Cluster instances."
echo "clean       -- Remove all instances data, logs, configs."

至于redis_6379.conf、redis_6380.conf这些配置文件,可以提前配置好,也可以在create-cluster中继续追加自动生成配置文件的代码。

redis集群启停:/home/searat/redis/bin/create-cluster start/stop 6379

创建集群:/home/searat/redis/bin/create-cluster create 6379

如果提示如下信息,则证明创建成功:

[OK] All nodes agree about slotsconfiguration.

>>>Check for open slots...

>>>Check slots coverage...

[OK] All 16384 slots covered.

验证Master、Slave信息:

/home/searat/redis/bin/redis-cli -p $PORT cluster nodes | head -30

0784283914d088acd66c458f6ee32db337bc3daf192.168.0.101:6379 master- 0 1453248628585 2 connected 5461-10922

4eebf65f5b1f4ac4c547c47fd19f442b30f27396192.168.0.102:6379myself,master - 0 0 1 connected 0-5460

f0763f4d57ce2996f95fdc0693680a36bb004c98192.168.0.102:6381 slave0784283914d088acd66c458f6ee32db337bc3daf 0 1453248627976 5 connected

d7461d15c6347a960a686530d3b72602a1fd855b192.168.0.101:6380 slave4eebf65f5b1f4ac4c547c47fd19f442b30f27396 0 1453248627976 4 connected

0977a10502ea3897c943ea88afe6133170ed09a8192.168.0.101:6381 master- 0 1453248628585 7 connected 10923-16383

836cdfbf7938c983f4bae07197a413fc43f7a155192.168.0.102:6380 slave0977a10502ea3897c943ea88afe6133170ed09a8 0 1453248627976 7 connected


Yes!这样就通过create-cluster的方式创建了redis集群,管理方便,扩展方便。



你可能感兴趣的:(redis,集群,shell)