優雅搭建redis-cluster

前言

記得年方二八時候,數數仍是數得過來的,現在年逾二十八,數都數不清了,前幾天在狗東哪裏整了《算法導論》、《機率導論》兩本教科書看看學學,也好在研究機器學習的時候有解惑的根本。redis-cluster正好成功的引發了個人興趣,就當着等書以前的休閒折騰node

redis早前沒有現成的集羣模塊,現在流行起來了,功能也愈來愈強大了,下面我就來部署一下,固然這中間也會遇到不少問題的。不過不要緊,有問題我們解決便可redis

手動部署

根據官網介紹,手動部署集羣須要進行一下操做算法

cd /usr/local/redis
mkdir -p cluster/7000
cp ./etc/redis.conf  cluster/7000
vi cluster/7000/redis.conf

這裏咱們針對關鍵的地方修改便可,具體狀況以下ruby

port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes

修改完成後,複製bash

cp -r cluster/7000 cluster/7001
cp -r cluster/7000 cluster/7002
cp -r cluster/7000 cluster/7003
cp -r cluster/7000 cluster/7004
cp -r cluster/7000 cluster/7005

而後依次修改每一個節點配置文件的端口號,修改好配置,最後啓動全部的節點app

redis-server cluster/7000/redis.conf
redis-server cluster/7001/redis.conf
redis-server cluster/7002/redis.conf
redis-server cluster/7003/redis.conf
redis-server cluster/7004/redis.conf
redis-server cluster/7005/redis.conf

這個時候雖然全部節點啓動了,可是還不能稱之爲集羣。下面咱們要使用ruby的輔助工具redis-trib來將全部的節點聯繫起來,這個工具就是咱們的redis安裝源文件的src目錄下的redis-trib.rb文件,可是這個文件運行須要ruby版redis的驅動curl

yum install ruby
gem install redis

安裝失敗,redis依賴的ruby版本必需要大於2.2.0,下面咱們來手動安裝ruby2.4.2
先清理yum安裝的ruby機器學習

yum remove ruby

下面經過RVM(Ruby Version Manager)來安裝rubyide

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E37D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
rvm install 2.4.2
gem install redis

安裝成功後工具

./src/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

自動部署

以上是手動建立集羣的方法,下面還有自動啓動節點,建立集羣,中止集羣的服務,文件在redis安裝目錄下,爲utils/create-cluster/create-cluster

cp utils/create-cluster/create-cluster /etc/init.d/create-cluster
vi /etc/init.d/create-cluster

修改PROT並添加ROOT=/usr/local/redis

#!/bin/bash
# Settings
PORT=6999
TIMEOUT=2000
NODES=6
REPLICAS=1
ROOT=/usr/local/redis
# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.

if [ -a config.sh ]
then
    source "config.sh"
fi

# Computed vars
ENDPORT=$((PORT+NODES))

if [ "$1" == "start" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Starting $PORT"
        $ROOT/bin/redis-server --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes
    done
    exit 0
fi

if [ "$1" == "create" ]
then
    HOSTS=""
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        HOSTS="$HOSTS 127.0.0.1:$PORT"
    done
    $ROOT/src/redis-trib.rb create --replicas $REPLICAS $HOSTS
    exit 0
fi

if [ "$1" == "stop" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Stopping $PORT"
        $ROOT/bin/redis-cli -p $PORT shutdown nosave
    done
    exit 0
fi

if [ "$1" == "watch" ]
then
    PORT=$((PORT+1))
    while [ 1 ]; do
        clear
        date
        $ROOT/bin/redis-cli -p $PORT cluster nodes | head -30
        sleep 1
    done
    exit 0
fi

if [ "$1" == "tail" ]
then
    INSTANCE=$2
    PORT=$((PORT+INSTANCE))
    tail -f ${PORT}.log
    exit 0
fi

if [ "$1" == "call" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        $ROOT/bin/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
    done
    exit 0
fi

if [ "$1" == "clean" ]
then
    rm -rf *.log
    rm -rf appendonly*.aof
    rm -rf dump*.rdb
    rm -rf nodes*.conf
    exit 0
fi

if [ "$1" == "clean-logs" ]
then
    rm -rf *.log
    exit 0
fi

echo "Usage: $0 [start|create|stop|watch|tail|clean]"
echo "start       -- Launch Redis Cluster instances."
echo "create      -- Create a cluster using redis-trib create."
echo "stop        -- Stop Redis Cluster instances."
echo "watch       -- Show CLUSTER NODES output (first 30 lines) of first node."
echo "tail <id>   -- Run tail -f of instance at base port + ID."
echo "clean       -- Remove all instances data, logs, configs."
echo "clean-logs  -- Remove just instances logs."

啓動節點,建立集羣就能夠用一下命令來實現了

service create-cluster start
service create-cluster create

中止集羣

service create-cluster stop

還有watchtailcleanclean-logs命令,這裏就不一一說明了,這個教程就寫到這吧

相關文章
相關標籤/搜索