20年第45周:docker部署Redis集群

一、使用腳本搭建Redis集群

image.png

二、使用腳本搭建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.itdecent.cn/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),后面要改的隨便寫(xiě)
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. ;\  # 這里填寫(xiě)你通過(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í)例掛載到集群上。


image.png

三、參考資料

#!/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."
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容