前言
記得年方二八時候,數數還是數得過來的,如今年逾二十八,數都數不清了,前幾天在狗東哪裡整了《演算法導論》、《概率導論》兩本教科書看看學學,也好在研究機器學習的時候有解惑的根本。redis-cluster正好成功的引起了我的興趣,就當著等書之前的休閒折騰
redis早前沒有現成的叢集模組,如今流行起來了,功能也越來越強大了,下面我就來部署一下,當然這中間也會遇到很多問題的。不過沒關係,有問題我們們解決即可
手動部署
根據官網介紹,手動部署叢集需要進行一下操作
cd /usr/local/redis
mkdir -p cluster/7000
cp ./etc/redis.conf cluster/7000
vi cluster/7000/redis.conf
這裡我們針對關鍵的地方修改即可,具體情況如下
port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
修改完成後,複製
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
然後依次修改每個節點配置檔案的埠號,修改好配置,最後啟動所有的節點
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的驅動
yum install ruby
gem install redis
安裝失敗,redis依賴的ruby版本必須要大於2.2.0,下面我們來手動安裝ruby2.4.2
先清理yum安裝的ruby
yum remove ruby
下面通過RVM(Ruby Version Manager)來安裝ruby
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
還有watch
、tail
、clean
、clean-logs
命令,這裡就不一一說明了,這個教程就寫到這吧