redis单机快速搭建集群

新版本的redis 提供了快速搭建redis集群的脚本:这里以redis-6.2.6为例做演示:

https://download.redis.io/releases/redis-6.2.6.tar.gz

下载完成后,解压到/usr/local/redis

接下来开始编译和安装。

bash 复制代码
make && make install

编译安装完成后:

bash 复制代码
cd /usr/local/redis/utils/create-cluster

可以看到一个 名称为 create-cluster 的可执行脚本,编辑该脚本:

bash 复制代码
#!/bin/bash

# Settings
BIN_PATH="../../src/"
CLUSTER_HOST=你的ip
# 表示每个节点端口号的前3为是多少
PORT=7000
TIMEOUT=2000
# 表示需要创建几个节点
NODES=6
REPLICAS=1
PROTECTED_MODE=no
# 默认为空字符串,这里你可以设置密码
ADDITIONAL_OPTIONS="--requirepass 123456 --masterauth 123456  --cluster-announce-ip 你的ip"
PASSWORD=123456

# 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
    # 客户端连接redis的一些参数配置,如果设置了密码,则需要添加
    OPT_ARG="-a 123456"
    if [ "$2" == "-f" ]; then
        OPT_ARG="--cluster-yes"
    fi
    $BIN_PATH/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS $OPT_ARG
    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 [-f] -- 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."

编辑完成后,我们执行

bash 复制代码
 ./create-cluster start

此步骤会启动设定的各个redis节点

此时查看当前路径,已经多了许多文件,但是还没有完,目前只是启动了实例,但还没有完成集群创建。

bash 复制代码
./create-cluster create

执行以上命令,将创建集群

注意此命令是一个交互型命令。在执行的过程中会询问你是否通过把 xx、xx、xx作为为主节点,把 xx、xx、xx作为它们的从节点,输入 yes 后会执行完成。

我们可以先使用 redis-cli 连接到集群,命令如下:

bash 复制代码
redis-cli -c -p 7001 -a 123456

使用如下命令查看集群下各节点信息

bash 复制代码
cluster nodes

create-cluster 搭建的方式虽然速度很快,但是该方式搭建的集群主从节点数量固定以及槽位分配模式固定,并且安装在同一台服务器上,所以只能用于测试环境。

我们测试完成之后,可以使用以下命令,关闭并清理集群:

bash 复制代码
./create-cluster stop# 关闭集群
./create-cluster clean # 清理集群
相关推荐
张璐月2 小时前
mysql join语句、全表扫描 执行优化与访问冷数据对内存命中率的影响
数据库·mysql
全干engineer4 小时前
ClickHouse 入门详解:它到底是什么、优缺点、和主流数据库对比、适合哪些场景?
数据库·clickhouse
Hellyc6 小时前
基于模板设计模式开发优惠券推送功能以及对过期优惠卷进行定时清理
java·数据库·设计模式·rocketmq
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
{⌐■_■}10 小时前
【Kafka】登录日志处理的三次阶梯式优化实践:从同步写入到Kafka多分区批处理
数据库·分布式·mysql·kafka·go
isNotNullX10 小时前
数据中台架构解析:湖仓一体的实战设计
java·大数据·数据库·架构·spark
睿思达DBA_WGX13 小时前
由 DB_FILES 参数导致的 dg 服务器无法同步问题
运维·数据库·oracle
袋鼠云数栈14 小时前
使用自然语言体验对话式MySQL数据库运维
大数据·运维·数据库·后端·mysql·ai·数据治理·数栈·data+ai
阿里云大数据AI技术14 小时前
数据 + 模型 驱动 AI Native 应用发展
大数据·数据库·人工智能