首先我们进入到redis官网的下载页(https://redis.io/download)可以看到:
在中间部分的stable部分内容大概意思是什么呢?就是redis5的第一个正式版是在2018年10月发布,引入了流数据类型、RDB的LFU和LRU的信息、集群管理采用了redis-cli及其其他的一些改进。
最大的改进就是集群搭建不再使用ruby,我们都知道在redis3和redis4的版本在集群的创建和管理时候都需要引入ruby。但是从redis5开始去掉了这个依赖。
1、下载并编译
我的下载和安装目录是:/opt/redis ,同时下载用的wget,编译需要gcc,所以这两个依赖都需要安装。
$ mkdir /opt/redis
$ cd /opt/redis
$ yum install -y wget gcc
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar xzf redis-5.0.5.tar.gz
$ cd redis-5.0.5
$ make && make install
如果不安装gcc,编译会出问题,如果在编译过程中未安装gcc,编译失败后需要删除解压的文件目录,重新解压并安装gcc再去编译。
2、修改create-cluster脚本
在redis5.0中引入这么一个功能,官方提供一键创建集群的脚本(create-cluster),可以帮我们快速的生成集群,管理集群。
支持的命令有哪些呢?如下:
#启动Redis集群实例命令
$ /opt/redis/redis-5.0.5/utils/create-cluster/create-cluster start
#创建集群命令
$ /opt/redis/redis-5.0.5/utils/create-cluster/create-cluster create
#停止Redis集群实例命令
$ /opt/redis/redis-5.0.5/utils/create-cluster/create-cluster stop
#显示第一个节点的集群节点输出(前30行)。
$ /opt/redis/redis-5.0.5/utils/create-cluster/create-cluster watch
#查看日志信息(1代表第一个节点)
$ /opt/redis/redis-5.0.5/utils/create-cluster/create-cluster tail 1
#删除所有实例数据、日志和配置
$ /opt/redis/redis-5.0.5/utils/create-cluster/create-cluster clean
#只删除实例日志
$ /opt/redis/redis-5.0.5/utils/create-cluster/create-cluster clean-logs
进入redis的utils目录修改create-cluster脚本
$ cd /opt/redis/redis-5.0.5/utils
$ cd create-cluster #进入到create-cluster目录
$ vim create-cluster #进入create-cluster文件,进行相关的变量设置
#!/bin/bash
# Settings
PORT=20000 #开始端口为 20000+1
TIMEOUT=2000 #超时连接时间
NODES=6 #集群节点数量
REPLICAS=1 #表示为集群中的每一个主节点指定一个从节点,即一比一的复制
# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.
# 如果你想有一个缺省的上述配置信息,那么就在这个脚本同目录下,增加一个配置信息脚本config.sh,把缺省的上述配置添加到这个shell脚本中
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"
../../src/redis-server --port $PORT --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
done
exit 0
fi
if [ "$1" == "create" ]
then
HOSTS=""
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
HOSTS="$HOSTS 127.0.0.1:$PORT"
done
../../src/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"
../../src/redis-cli -p $PORT shutdown nosave
done
exit 0
fi
if [ "$1" == "watch" ]
then
PORT=$((PORT+1))
while [ 1 ]; do
clear
date
../../src/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" == "call" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
../../src/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]"
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 "clean -- Remove all instances data, logs, configs."
echo "clean-logs -- Remove just instances logs."
保存退出!
注意:这个脚本是在一台机器上搭建集群好使!!!如果你想在多台服务器上搭建集群的话此脚本就需要做出修改了。同时这个脚本使用的本台机器的默认ip(127.0.0.1),这个地方很尴尬,所以使用过程中需要修改这个IP的。
当然你也可以使用我修改之后的集群脚本,
#!/bin/bash
# Settings
#绑定IP,如果多台机器请设置不同IP
BIND=172.16.16.4
#BIND=127.0.0.1
#开始端口为 16378+1
PORT=16378
#超时时间
TIMEOUT=6000
#集群节点数量,如果多台机器上这个变量设置为1
NODES=6
#表示为集群中的每一个主节点指定一个从节点,即一比一的复制
REPLICAS=1
#redis5.0根目录
DIR=/opt/redis/redis-5.0.5
#redis5.0集群目录
CLUSTER_DIR=/opt/redis/redis-5.0.5/cluster
# 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
echo "check $CLUSTER_DIR"
if [ ! -d "$CLUSTER_DIR" ]
then
mkdir -p $CLUSTER_DIR
echo "mkdir $CLUSTER_DIR success"
fi
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "check $CLUSTER_DIR/$PORT"
if [ ! -d "$CLUSTER_DIR/$PORT" ]
then
mkdir -p $CLUSTER_DIR/$PORT
echo "mkdir $CLUSTER_DIR/$PORT success"
fi
echo "Starting $PORT"
$DIR/src/redis-server --bind $BIND --port $PORT --protected-mode no --cluster-enabled yes --dir $CLUSTER_DIR/$PORT --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --pidfile redis-$PORT.pid --daemonize yes
done
exit 0
fi
if [ "$1" == "create" ]
then
HOSTS=""
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
HOSTS="$HOSTS $BIND:$PORT"
done
$DIR/src/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"
$DIR/src/redis-cli -h $BIND -p $PORT shutdown nosave
done
exit 0
fi
if [ "$1" == "watch" ]
then
PORT=$((PORT+1))
while [ 1 ]; do
clear
date
$DIR/src/redis-cli -h $BIND -p $PORT cluster nodes | head -30
sleep 1
done
exit 0
fi
if [ "$1" == "tail" ]
then
INSTANCE=$2
PORT=$((PORT+INSTANCE))
tail -f $CLUSTER_DIR/${PORT}.log
exit 0
fi
if [ "$1" == "call" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
$DIR/src/redis-cli -h $BIND -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
done
exit 0
fi
if [ "$1" == "clean" ]
then
rm -rf $CLUSTER_DIR/*/*.log
rm -rf $CLUSTER_DIR/*/appendonly*.aof
rm -rf $CLUSTER_DIR/*/dump*.rdb
rm -rf $CLUSTER_DIR/*/nodes*.conf
exit 0
fi
if [ "$1" == "clean-logs" ]
then
rm -rf $CLUSTER_DIR/*/*.log
exit 0
fi
echo "Usage: $0 [start|create|stop|watch|tail|clean]"
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 "clean -- Remove all instances data, logs, configs."
echo "clean-logs -- Remove just instances logs."
它支持:
1、自定义集群搭建目录
2、自定义绑定IP地址
3、自动创建集群目录和文件
4、支持多台机器搭建集群
做到真正的一键。
下载地址:
链接:https://pan.baidu.com/s/15iMBNjPjS3iXmMS8_SJ3-g
提取码:wgrr
3、集群搭建命令
1、使用官方默认脚本搭建集群-启动集群节点实例
#使用默认的/opt/redis/redis-5.0.5/utils/create-cluster/create-cluster脚本
#启动集群节点实例
$/opt/redis/redis-5.0.5/utils/create-cluster/create-cluster start
当出现下方视图证明启动完成。
也可以使用命令:ps -ef|grep redis 查看redis节点启动情况。
2、使用官方默认脚本搭建集群-创建集群
#创建集群命令
$ /opt/redis/redis-5.0.5/utils/create-cluster/create-cluster create
当出现下方视图证明创建成功。
到这里集群已经搭建完成。
如果使用我修改后脚本在一台机器上搭建集群,命令跟上述类似,只不过会在集群目录(我的脚本中默认是/opt/redis/redis-5.0.5/cluster)中会以端口生成目录,每个节点的日志、pid、rdb、aof、节点配置文件单独存储。
如果使用我修改后脚本在多台机器上搭建集群(假设ip是172.16.16.10-15,端口是20001),只需要把NODES=1 即可,然后在6台机器上分别执行
$ ./opt/redis/redis-5.0.5/create-cluster.sh start
再执行命令
$ /opt/redis/redis-5.0.5/src/redis-cli --cluster create 172.16.16.10:20001 172.16.16.11:20001 172.16.16.12:20001 172.16.16.13:20001 172.16.16.14:20001 172.16.16.15:20001 --cluster-replicas 1
便可实现多台机器的集群搭建。