Redis3.2.10集群搭建

Redis3.2.10集群搭建

前言
开发比较喜欢老点的包,所以如果你拿到的是5.0之前版本的包,又不想用ruby搭建集群,就可以学我。由于5.0版本之前的redis-cli没有--cluster选项,又不想安装ruby环境,使用redis-trib.rb,所以就顺便下载了5.0.2版本的包,然后将5.0.2中的redis-cli文件复制到了3.2.10的环境里。


  • 环境
    centos7.2
    node1:192.168.72.150
    node2:192.168.72.150
    node3:192.168.72.150
    官方建议集群环境最少6节点,3主3从,这里我们部署9节点,3主6从,每个主节点有2个从节点

下面所有操作都在node1上,node2和node3操作相同,除了启动集群操作

  • 下载包
wget http://download.redis.io/releases/redis-3.2.10.tar.gz
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
  • 1.安装
    这里只写了3.2.10版本的安装,5.0.2版本随便安装个目录,然后将src目录下的redis-cli文件复制到3.2.10中就行
tar zxvf redis-3.2.10.tar.gz -C /work/
cd /work/redis-3.2.10/
make && make install
  • 2.配置
$ vi deploy.sh
#!/bin/bash
#
#set -ex
CURRENT_DIR=$(cd `dirname $0`;pwd)
echo $CURRENT_DIR
echo $CONF_FILE
mkdir -pv $CURRENT_DIR/redis-cluster-conf/700{1,2,3}
cd $CURRENT_DIR/redis-cluster-conf
for i in `ls ./`;do
CONF_FILE=${CURRENT_DIR}/redis-cluster-conf/${i}/redis.conf
cp -f ../redis.conf ${i}/
sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" ${CONF_FILE}
sed -i "s/daemonize no/daemonize yes/g" ${CONF_FILE}
sed -i "s/redis_6379.pid/redis_${i}.pid/g" ${CONF_FILE}
sed -i "s/port 6379/port ${i}/g" ${CONF_FILE}
sed -i "s/# cluster-enabled yes/cluster-enabled yes/" ${CONF_FILE}
sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-${i}.conf/" ${CONF_FILE}
sed -i "s/# cluster-node-timeout 15000/cluster-node-timeout 5000/" ${CONF_FILE}
./deploy.sh
  • 3.启动
vi redis
#!/bin/bash
#
Host1=192.168.72.152
Host2=192.168.72.150
Host3=192.168.72.151
CURRENT_DIR=$(cd `dirname $0`;pwd)
CONF_BASE=${CURRENT_DIR}/redis-cluster-conf
cd $CURRENT_DIR/src
function start_redis() {
        for i in `ls ${CONF_BASE}`
        do
        ./redis-server ${CONF_BASE}/${i}/redis.conf
        done
}
function redis_status() {
        ps aux|grep redis
        netstat -tnlp|grep redis
}
function create_cluster() {
        src/redis-cli --cluster create --cluster-replicas 2 \
        ${Host1}:7001 \
        ${Host1}:7002 \
        ${Host1}:7003 \
        ${Host2}:7001 \
        ${Host2}:7002 \
        ${Host2}:7003 \
        ${Host3}:7001 \
        ${Host3}:7002 \
        ${Host3}:7003 \
}
## Start Redis
if [ "$1" == "start" ]
then
        echo "Starting Redis..."
        start_redis
  if [ "$?" == "0" ]
    then
        sleep 1
        echo "######################"
        echo "Redis Instances Status"
        ps aux|grep redis|grep -v grep|awk '{print $11,$12" Successful"}'
    fi
        exit 0
fi
## Stop Redis
if [ "$1" == "stop" ]
then
        echo "Stoping Redis..."
        pkill redis
        exit 0
fi
## Redis status
if [ "$1" == "status" ]
then
        echo "Status Redis..."
        redis_status
        exit 0
fi
## Create cluster
if [ "$1" == "create" ]
then
        echo "Create cluster"
        create_cluster
        exit 0
fi
echo "Usage: $0 [start|status|stop|]"
echo "start       -- Launch Redis instances."
echo "create      -- Create a cluster using redis-cli --cluster create."
echo "stop        -- Stop Redis instances."
echo "status      -- Show status of the redis instances."
./redis start
  • 4.启动集群
    上面配置好了节点1,其他俩个节点操作一样,下载,解压,安装,然后将deploy.sh和redis传到其他节点的对应位置(/work/redis-3.2.10/),然后执行./deploy.sh部署,然后./redis start启动redis实例。
    三个节点共九个实例都起来后,在某个节点使用./redis create来启动集群
    记得redis-cli文件需要用5.0版本的
    • node1
cp /usr/local/redis-5.0.2/src/redis-cli /work/redis-3.2.10/src/
./redis create
  • 5.测试
    客户端连接并写入
src/redis-cli -c -p 7001
set name "zhangsan"
cluster info  查看集群信息
cluster nodes  查看节点信息

redis5.0以上版本中的utils文件夹里有个create-cluster目录,里面的create-cluster脚本是一个创建集群的脚本,我上面的redis脚本就是参照它写的,我写的虽然很丑陋,但也能用。

你可能感兴趣的:(Redis3.2.10集群搭建)