20年第45周(docker部署Redis集群)

一、使用脚本搭建Redis集群 20年第45周(docker部署Redis集群)
文章图片
image.png 二、使用脚本搭建Redis容器集群 创建步骤

  • ①创建一个docker redis子网
docker create network redis-net
  • ②创建多个Redis容器:像搭建一个redis容器一样重复N次,并制定网络为上述①的子网
PS: 此时创建的仍是单机Redis
参考:[20年第45周:docker安装/部署/使用redis (Redis 配置)]
(https://www.jianshu.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创建的子网,后面要改的随便写 cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT} appendonly yes

  • 批量创建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
  • 批量创建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

  • 生成集群创建命令的脚本
PS: 这个脚本要在本②中所创建的6个容器实例里面的任意一个内执行既可docker exec -it redis-9000 bash
IP=2 ; \ PORT=0 ; \ CLUSTER_HOST=172.18.0. ; \# 这里填写你通过`docker inspect network redis-net`看到的子网 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

  • ③集群联网:创建一个集群,并将多个Redis容器挂载到容器集群上。
【20年第45周(docker部署Redis集群)】PS: 通过此步骤,将上述②中创建Redis实例挂载到集群上。

20年第45周(docker部署Redis集群)
文章图片
image.png
三、参考资料
  • Redis集群搭建手册
  • create-cluster.sh文件内容
    20年第45周(docker部署Redis集群)
    文章图片
    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 fiif [ "$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 fiif [ "$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 fiif [ "$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 fiif [ "$1" == "tail" ] then INSTANCE=$2 PORT=$((PORT+INSTANCE)) tail -f ${PORT}.log exit 0 fiif [ "$1" == "tailall" ] then tail -f *.log exit 0 fiif [ "$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 fiif [ "$1" == "clean" ] then rm -rf *.log rm -rf appendonly*.aof rm -rf dump*.rdb rm -rf nodes*.conf exit 0 fiif [ "$1" == "clean-logs" ] then rm -rf *.log exit 0 fiecho "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 -- 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 -- Call a command (up to 7 arguments) on all nodes."

    推荐阅读