Redis5官方自带集群脚本create-cluster(三主三从)
1.下载并安装redis
环境:centos 7
$ 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
2.快速搭建脚本
2.1 官方提供
redis 5.0版本集群搭建不需要我们安装ruby就可以搭建成功,而且redis安装包内utils/create-cluster/ 目录下给我们提供了快速搭建脚本,目录结构如下图:
2.2 README
使用方式通过阅读README文件内容可以得知,内容如下:
$ cat utils/create-cluster/README
Create-custer is a small script used to easily start a big number of Redis
instances configured to run in cluster mode. Its main goal is to allow manual
testing in a condition which is not easy to replicate with the Redis cluster
unit tests, for example when a lot of instances are needed in order to trigger
a given bug.
The tool can also be used just to easily create a number of instances in a
Redis Cluster in order to experiment a bit with the system.
USAGE
---
To create a cluster, follow these steps:
1. Edit create-cluster and change the start / end port, depending on the
number of instances you want to create.
2. Use "./create-cluster start" in order to run the instances.
3. Use "./create-cluster create" in order to execute redis-cli --cluster create, so that
an actual Redis cluster will be created.
4. Now you are ready to play with the cluster. AOF files and logs for each instances are created in the current directory.
In order to stop a cluster:
1. Use "./create-cluster stop" to stop all the instances. After you stopped the instances you can use "./create-cluster start" to restart them if you change your mind.
2. Use "./create-cluster clean" to remove all the AOF / log files to restart with a clean environment.
Use the command "./create-cluster help" to get the full list of features.
2.3 create-cluster
执行utils/create-cluster/ 目录下的create-cluster脚本,就可以快速搭建,脚本内容如下:
$ cat utils/create-cluster/create-cluster
#!/bin/bash
# Settings
PORT=30000 #起始端口
TIMEOUT=2000 #超时时间
NODES=6 #总节点数
REPLICAS=1 #每个主节点有几个从节点,0为无从节点
# 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"
../../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}