简介

利用SHELL脚本快速部署 Redis Cluster 集群。(推荐是用redis6版本)

创建配置文件

CONFIGDIR="/opt/redis/conf"
DATADIR="/data/redis/data"
PORTS=(7001 7002 7003 7004 7005 7006 7007 7008 7009)
PASSWORD="OxYLZdfP1dvBpice"
if [ "${PASSWORD}" == "" ]; then
    # if password is not set , create random password
    arr=(a b c d e f g h i g k l m n o p q r s t u v w x y z
        A B C D E F G H I G K L M N O P Q R S T U V W X Y Z
        / . 0 1 2 3 4 5 6 7 8 9)
    for ((i = 0; i < 16; i++)); do
        PASSWORD=${PASSWORD}${arr[$RANDOM % ${#arr[@]}]}
    done
fi
mkdir -p ${DATADIR} ${CONFIGDIR}
echo $PORTS
for ((i = 0; i < ${#PORTS[@]}; i++)); do
    PORT=${PORTS[${i}]}
    cat >${CONFIGDIR}/${PORT}.conf <<EOF
bind 0.0.0.0
protected-mode yes
port ${PORT}
daemonize yes
pidfile ${DATADIR}/redis_${PORT}.pid
loglevel notice
logfile "${DATADIR}/${PORT}.log"
databases 16
cluster-enabled yes
cluster-config-file cluster_node_${PORT}.conf
cluster-node-timeout 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
dbfilename dump_${PORT}.rdb
dir ${DATADIR}
maxmemory 4gb
appendonly yes
appendfilename "appendonly_${PORT}.aof"
appendfsync everysec
requirepass ${PASSWORD}
masterauth ${PASSWORD}
EOF
done

运行节点

for i in `seq 7001 7090` ; do  /opt/redis/bin/redis-server /opt/redis/conf/$i.conf ; done

创建集群

如果有多台主机,节点应该平均分布。

/opt/redis/bin/redis-cli --cluster create 192.168.122.1:7001 192.168.122.1:7002 192.168.122.1:7003 192.168.122.1:7004 192.168.122.1:7005 192.168.122.1:7006 192.168.122.1:7007 192.168.122.1:7008 192.168.122.1:7009 --cluster-replicas 2 -a OxYLZdfP1dvBpice

输入yes就可以自动完成创建和分配slot

查看集群信息

查看信息

/opt/redis/bin/redis-cli -c -h 192.168.122.1 -p 7001 -a OxYLZdfP1dvBpice cluster info

查看节点

/opt/redis/bin/redis-cli -c -h 192.168.122.1 -p 7001 -a OxYLZdfP1dvBpice cluster nodes