Redis Cluster - 外网访问redis集群 - create-cluster脚本

本文介绍了如何在外网环境下安全访问Redis 6.0.6集群,强调了不推荐直接开放外网访问,并提供了设置密码进行调试的建议。详细内容包括Shell脚本、相关命令和注意事项。

Version

redis 6.0.6


Shell script

cd /redis-6.0.6/utils/create-cluster
vi create-cluster
#!/bin/bash

# Settings
BIN_PATH="../../src/"
#CLUSTER_HOST=127.0.0.1
CLUSTER_HOST=YOUR_REMOTE_IP
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
#PROTECTED_MODE=yes
PROTECTED_MODE=no
#ADDITIONAL_OPTIONS=""
ADDITIONAL_OPTIONS="--requirepass YOUR_PASSWORD --masterauth YOUR_PASSWORD --cluster-announce-ip YOUR_REMOTE_IP"

PASSWORD=YOUR_PASSWORD
#MASTERAUTH=YOUR_PASSWORD

# 云服务器上部署需指定公网ip
#cluster-announce-ip YOUR_REMOTE_IP

# 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
    $BIN_PATH/redis-cli -a $PASSWORD --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
        $BIN_PATH/redis-cli -a $PASSWORD -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
        $BIN_PATH/redis-cli -a $PASSWORD -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
        $BIN_PATH/redis-cli -a $PASSWORD -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."


Command

cd /redis-6.0.6/utils/create-cluster

./create-cluster start

./create-cluster create

./create-cluster stop

./create-cluster clean

Attention

redis server不建议外网访问

有时开发环境需要外网访问测试环境redis,进行调试时,建议设置密码


Reference

https://redis.io/topics/cluster-tutorial

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值