一、使用腳本搭建Redis集群
二吼鳞、使用腳本搭建Redis容器集群
創(chuàng)建步驟
- ①創(chuàng)建一個(gè)docker redis子網(wǎng)
docker create network redis-net
- ②創(chuàng)建多個(gè)Redis容器:像搭建一個(gè)redis容器一樣重復(fù)N次纬向,并制定網(wǎng)絡(luò)為上述①的子網(wǎng)
PS: 此時(shí)創(chuàng)建的仍是單機(jī)Redis
參考:[20年第45周:docker安裝/部署/使用redis (Redis 配置)]
(http://www.reibang.com/p/3e89597d6272)
- 配置模板redis-cluster.tmpl
port ${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.18.0.2 # docker創(chuàng)建的子網(wǎng),后面要改的隨便寫
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
- 批量創(chuàng)建redis.conf配置的腳本
for port in `seq 9000 9005`; do \
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done
什么是envsubst铜犬?
>>參考envsubst
- 批量創(chuàng)建Redis容器的腳本
for port in `seq 9000 9005`; do \
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
-v /Users/xusanhuo/data/redis-cluster/${port}/conf/redis.conf:/etc/redis/redis.conf \
-v /Users/xusanhuo/data/redis-cluster/${port}/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis redis-server /etc/redis/redis.conf; \
done
- 生成集群創(chuàng)建命令的腳本
PS: 這個(gè)腳本要在本②中所創(chuàng)建的6個(gè)容器實(shí)例里面的任意一個(gè)內(nèi)執(zhí)行既可
docker exec -it redis-9000 bash
IP=2 ;\
PORT=0 ;\
CLUSTER_HOST=172.18.0. ;\ # 這里填寫你通過(guò)`docker inspect network redis-net`看到的子網(wǎng)
for i in `seq 0 5`; do \
IP=$((IP+${i})) ;\
PORT=$((9000+${i})) ;\
HOSTS="$HOSTS $CLUSTER_HOST$IP:$PORT" ;\
done; \
echo $HOSTS ;\
redis-cli --cluster create $HOSTS --cluster-replicas 1
- ③集群聯(lián)網(wǎng):創(chuàng)建一個(gè)集群荔茬,并將多個(gè)Redis容器掛載到容器集群上灼芭。
PS: 通過(guò)此步驟有额,將上述②中創(chuàng)建Redis實(shí)例掛載到集群上。
三姿鸿、參考資料
- Redis集群搭建手冊(cè)
-
create-cluster.sh文件內(nèi)容
#!/bin/bash
# Settings
BIN_PATH="../../src/"
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""
# 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"
$BIN_PATH/redis-server --port $PORT --protected-mode $PROTECTED_MODE --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 ${ADDITIONAL_OPTIONS}
done
exit 0
fi
if [ "$1" == "create" ]
then
HOSTS=""
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
HOSTS="$HOSTS $CLUSTER_HOST:$PORT"
done
$BIN_PATH/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS
exit 0
fi
if [ "$1" == "stop" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Stopping $PORT"
$BIN_PATH/redis-cli -p $PORT shutdown nosave
done
exit 0
fi
if [ "$1" == "watch" ]
then
PORT=$((PORT+1))
while [ 1 ]; do
clear
date
$BIN_PATH/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" == "tailall" ]
then
tail -f *.log
exit 0
fi
if [ "$1" == "call" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
$BIN_PATH/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|call]"
echo "start -- Launch Redis Cluster instances."
echo "create -- Create a cluster using redis-cli --cluster 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 "tailall -- Run tail -f for all the log files at once."
echo "clean -- Remove all instances data, logs, configs."
echo "clean-logs -- Remove just instances logs."
echo "call <cmd> -- Call a command (up to 7 arguments) on all nodes."