1. 程式人生 > >redis 叢集安裝指令碼,redis-cluster

redis 叢集安裝指令碼,redis-cluster

#安裝
wget http://download.redis.io/releases/redis-3.0.0.tar.gz 
tar zxvf redis-3.0.0.tar.gz
cd redis-3.0.0
src_redis_3=`pwd`
make clean && make -j2 
cd src
make PREFIX=/usr/local/redis3 install

#配置
mkdir /usr/local/redis3/data
cd /usr/local/redis3/data
for p in 7000 7001 7002 7003 7004 7005 ;
do
    mkdir $p
    cp $src_redis_3
/redis.conf ./$p/ sed -i 's/port 6379/port $p/g' ./$p/redis.conf sed -i 's/daemonize no/daemonize yes/g' ./$p/redis.conf sed -i 's/# cluster-enabled yes/cluster-enabled yes/g' ./$p/redis.conf sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes.conf/g' ./$p/redis.conf sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/g'
./$p/redis.conf sed -i 's/appendonly no/appendonly yes/g' ./$p/redis.conf done #配置redis叢集命令 cp $src_redis_3/src/redis-trib.rb /usr/local/bin/redis-trib #啟動redis的各例項 for p in `ls .` ;do cd $p && /usr/local/bin/redis-server redis.conf && cd ..; done; #安裝ruby yum install ruby #安裝gem redis gem install redis -v 3.0
.0 #自動建立redis叢集,並顯示叢集方案,輸入yes即可 redis-trib 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 #連線測試,-c表式連線到叢集。 redis-cli -c -p 7000 #注意:普通的redis-client 不可以連線叢集,需要特殊redis-cluster-client,官網目前好像沒有PHP版本的。

如下是一個開源界大牛寫的叢集管理指令碼:make-cluser.sh

#!/bin/bash

# make-cluster.sh
# This is a simple script used to automatically spin up a Redis cluster instance
# simplifying the process of running unit tests.
#
# Usage:
#   ./make-cluster.sh start [host]
#   ./make-cluster.sh stop [host]
#

BASEDIR=`pwd`
NODEDIR=$BASEDIR/nodes
MAPFILE=$NODEDIR/nodemap

# Host, nodes, replicas, ports, etc.  Change if you want different values
HOST="192.168.1.117"
NODES=122
REPLICAS=3
START_PORT=7000
END_PORT=`expr $START_PORT + $NODES`

# Helper to determine if we have an executable
checkExe() {
    if ! hash $1 > /dev/null 2>&1; then
        echo "Error:  Must have $1 on the path!"
        exit 1
    fi
}

# Run a command and output what we're running
verboseRun() {
    echo "Running: [email protected]"
    [email protected]
}

# Spawn a specific redis instance, cluster enabled
spawnNode() {
    # Attempt to spawn the node
    verboseRun redis-server --cluster-enabled yes --dir $NODEDIR --port $PORT \
        --cluster-config-file node-$PORT.conf --daemonize yes --save \'\' \
        --bind $HOST --dbfilename node-$PORT.rdb

    # Abort if we can't spin this instance
    if [ $? -ne 0 ]; then
        echo "Error:  Can't spawn node at port $PORT."
        exit 1
    fi
}

# Spawn nodes from start to end port
spawnNodes() {
    for PORT in `seq $START_PORT $END_PORT`; do
        # Attempt to spawn the node
        spawnNode $PORT

        # Add this host:port to our nodemap so the tests can get seeds
        echo "$HOST:$PORT" >> $MAPFILE
    done
}

# Check to see if any nodes are running
checkNodes() {
    echo -n "Checking port availability "

    for PORT in `seq $START_PORT $END_PORT`; do
        redis-cli -p $PORT ping > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "FAIL"
            echo "Error:  There appears to be an instance running at port $PORT"
            exit 1
        fi
    done

    echo "OK"
}


 # Create our 'node' directory if it doesn't exist and clean out any previous
 # configuration files from a previous run.
 cleanConfigInfo() {
     verboseRun mkdir -p $NODEDIR
     verboseRun rm -f $NODEDIR/*
 }

 # Initialize our cluster with redis-trib.rb
 initCluster() {
     TRIBARGS=""
     for PORT in `seq $START_PORT $END_PORT`; do
         TRIBARGS="$TRIBARGS $HOST:$PORT"
     done

     verboseRun redis-trib.rb create --replicas $REPLICAS $TRIBARGS

     if [ $? -ne 0 ]; then
         echo "Error:  Couldn't create cluster!"
         exit 1
     fi
 }

 # Attempt to spin up our cluster
 startCluster() {
     # Make sure none of these nodes are already running
     checkNodes

     # Clean out node configuration, etc
     cleanConfigInfo

     # Attempt to spawn the nodes
     spawnNodes

     # Attempt to initialize the cluster
     initCluster
 }

 # Shut down nodes in our cluster
 stopCluster() {
     for PORT in `seq $START_PORT $END_PORT`; do
         verboseRun redis-cli -p $PORT SHUTDOWN NOSAVE > /dev/null 2>&1
     done
 }

 # Make sure we have redis-server and redis-trib.rb on the path
 checkExe redis-server
 checkExe redis-trib.rb

 # Override the host if we've got $2
 if [[ ! -z "$2" ]]; then
    HOST=$2
 fi

 # Main entry point to start or stop/kill a cluster
 case "$1" in
     start)
         startCluster
         ;;
     stop)
         stopCluster
         ;;
     *)
         echo "Usage $0 <start|stop> [host]"
         ;;
 esac