redis集群一键安装优势和功能

  1. 此脚本分有离线安装和联网安装的功能, 联网安装的用法就是直接运行这个脚本 sh .sh /或者./.sh
    而离线安装, 就是把安装包和这个脚本放在想安装在的目录, 然后执行sh .sh /或者./.sh即可,而无论选择哪一方式,都是包是否存在的问题,包在=离线,包不在=联网,下面说一下上面的全局变量的解释。
  2. onversion 这个是在联网安装的时候需要制定的版本,默认4.0.3,如果需要安装5.0.0的, 修改此参数。
  3. offversion 这个不用管它,这个是离线安装识别离线包版本的。
  4. cluster_name 这个就是安装集群的文件夹名字,就是把集群装在这个文件夹里面。
  5. number 这个就比较重要了, 节点。一台机器需要安装的节点,我这里是6,安装6个节点,可修改。
  6. numstaring 这个是端口的前缀,比如我这里安装6个节点,端口自动分配为7001,7002,7003…, 如果需要修改成6001,6002…这些端口,修改此属性为600便可。

如何获取离线包

https://rubygems.org/downloads/redis-3.2.0.gem
http://download.redis.io/releases/redis-${onversion}.tar.gz

shell脚本

#!/bin/bash
#redis-cluster install
#2018.12.10
#blog http://www.cnblogs.com/chenglee/
onversion="4.0.3"
offversion=`basename redis-*.tar.gz .tar.gz | awk -F '-' '{print$2}'`
installdir=$(cd `dirname $0`; pwd)
cluster_name="redis-cluster"
number=6
numstaring=700

function initize(){
    installdir=$(cd `dirname $0`; pwd)
}

function  checkroot(){
if [ $UID -ne  0 ]
  then
    echo;
    echo "[权限不足...请切换至root用户]"
    echo;
    exit;
fi
}

function judge(){
    echo
    offfile=`ls | grep redis-*.tar.gz`
    if [[ "$offfile" != "" ]]
    then
        echo;
        echo "[发现离线压包]"
        echo;
        /usr/bin/sleep 3
        offinstall
    else
        echo;
        echo "[未发现离线包]"
        echo;
        /usr/bin/sleep 3
        network
    fi
}

function offinstall(){
    echo;
    echo "[离线包安装中]"
    echo;
    tar -zxvf redis-${offversion}.tar.gz >/dev/null 2>&1
    redis="redis-${offversion}"
    cd ${redis}/src && make >/dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo "编译出错"
    else
        echo;
        echo "[文件编译完成]"
        echo;
        /usr/bin/sleep 3
        intend
    fi
}

function network(){
    httpcode=`curl -I -m 10 -o /dev/null -s -w %{http_code}'\n' http://www.baidu.com`
    net1=$(echo $httpcode | grep "200")
    if [[ "$net1" != "" ]]
    then
        echo;
        echo "[联网]"
        echo "[准备联网安装]"
        echo;
        /usr/bin/sleep 3
        online
    else
        echo;
        echo "[未联网,无离线安装包,准备退出]"
        echo;
        /usr/bin/sleep 3
        exit;
    fi
}
function online(){
    wget_v=`which wget`
    wget_vv=$(echo $wget_v | grep wget)
    if [[ "$wget_vv" != "" ]]
    then
        echo;
        echo;
        echo;
        wget http://download.redis.io/releases/redis-${onversion}.tar.gz
        installon
    else
        echo;
        echo "[检测到wget没有安装, 准备安装wget]"
        echo;
        yum install wget -y
        echo;
        echo;
        echo;
        wget http://download.redis.io/releases/redis-${onversion}.tar.gz
        installon
    fi
}

function installon(){
    echo;
    echo "[在线安装包中]"
    echo;
    tar -zxvf redis-${onversion}.tar.gz >/dev/null 2>&1
    redis="redis-${onversion}"
    cd ${redis}/src && make >/dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo "编译出错"
    else
        echo;
        echo "[文件编译完成]"
        echo;
        /usr/bin/sleep 3
        intend
    fi
}

function intend(){
    echo;
    echo "[开始迁移文件]"
    echo;
    cd ${installdir}
    redis="redis-${onversion}"
    cd ${redis}/src
    if [ -f "redis-server" ];then
        echo;
        echo "[准备分配数据]"
        echo;
    else
        echo;
        echo "[失败重新编译]"
        echo;
        /usr/bin/sleep 2
        make >/dev/null 2>&1
    fi
    mkdir -p ${installdir}/${cluster_name}/{logs,nodes,conf,bin}
    cp redis-cli redis-server ${installdir}/${cluster_name}/bin
    cp redis-trib.rb ${installdir}/${cluster_name}
    for((i=1;i<=${number};i++));
        do
        port=${numstaring}${i}
        confile="redis-${numstaring}${i}.conf"
        cp ../redis.conf ${installdir}/${cluster_name}/conf/${confile}
    done

    echo;
    echo "[数据迁移完成]"
    echo;
    /usr/bin/sleep 2
    echo;
    echo "[清理多余文件]"
    echo;
    #finish
    /usr/bin/sleep 2
    echo;
    echo "[配置快捷启动]"
    echo;
    service
    /usr/bin/sleep 2
    echo;
    echo "[修改配置文件]"
    echo;
    configfile
    /usr/bin/sleep 3
}
function finish(){
    echo
    rm -rf ${installdir}/redis-*
}
#------------------------------------------------------------------------------------------------------------------#
#|                                   service     复制多份redis.conf        conf 目录                              |#
#|                                   configfile  对整改的内容做个整合                                             |#
#|                                   background  后台运行                                                         |#
#|                                   updateport  端口                                                             |#
#|                                   logpath     日志输出目录文件          logs 目录                              |#
#|                                   colony      开启集群模式                                                     |#
#|                                   clusterconf 集群数据输出目录文件      nodes目录                              |#
#|                                   trib        创建集群 连接所有节点     redis-cluster.log                      |#
#------------------------------------------------------------------------------------------------------------------#
function service(){
    cd ${installdir}/${cluster_name}
    for((i=1;i<=${number};i++));
        do
        port=${numstaring}${i}
        confile="redis-${numstaring}${i}.conf"
        echo "./bin/redis-server conf/${confile}" >> start.sh
    done
    chmod +x start.sh
}
function configfile(){
    cd ${installdir}/${cluster_name}
    pathdir=`pwd`
    cd ${installdir}/${cluster_name}/conf
        for((i=1;i<=${number};i++));
            do
            port=${numstaring}${i}
            confile="redis-${numstaring}${i}.conf"
            #后台
            background
            #端口
            updateport
            #日志输出文件
            logpath
            #开启集群
            colony
            #集群数据文件
            clusterconf
        done
}
function background(){
    cd ${installdir}/${cluster_name}/conf
    for((i=1;i<=${number};i++));
        do
        confile="redis-${numstaring}${i}.conf"
        sed -i 's/daemonize no/daemonize yes/' ${confile}
    done 
}
function updateport(){
    cd ${installdir}/${cluster_name}/conf
    for((i=1;i<=${number};i++));
        do
        port=${numstaring}${i}
        confile="redis-${numstaring}${i}.conf"
        sed -i "s|port 6379|port ${port}|g" ${confile}
    done
}
function colony(){
    cd ${installdir}/${cluster_name}/conf
    for((i=1;i<=${number};i++));
        do
        confile="redis-${numstaring}${i}.conf"
        sed -i 's/# cluster-enabled yes/cluster-enabled yes/' ${confile}
    done
}
function logpath(){
    cd ${installdir}/${cluster_name}
    pathdir=`pwd`
    cd ${installdir}/${cluster_name}/conf
        for((i=1;i<=${number};i++));
            do
            port=${numstaring}${i}
            confile="redis-${numstaring}${i}.conf"
            logfileold=`cat "${confile}" | grep logfile | awk -F '"' '{print$2}'`
            logfile="redis-${numstaring}${i}.logs"
            logfilenew=${pathdir}/logs/${logfile}
            if [ ! $logfileold ];then
                sed -i 's/logfile ""/logfile "judgement"/' ${confile}
                logfileold=`cat "${confile}" | grep logfile | awk -F '"' '{print$2}'`
                if [ ! $logfileold ];then
                    echo
                else
                    sed -i "s|$logfileold|$logfilenew|g" ${confile}
                fi
                sed -i "s|$logfileold|$logfilenew|g" ${confile}
            else
                sed -i "s|$logfileold|$logfilenew|g" ${confile}
            fi
        done
}
function clusterconf(){
    cd ${installdir}/${cluster_name}
    pathdir=`pwd`
    cd ${installdir}/${cluster_name}/conf
    for((i=1;i<=${number};i++));
        do
        port=${numstaring}${i}
        confile="redis-${numstaring}${i}.conf"
        nodes="nodes-${numstaring}${i}.conf"
        clusternumber=`cat -n ${confile} | grep cluster-config-file | awk '{print$1}' | head -n 1`
        clusterconfnew="cluster-config-file ${pathdir}/nodes/${nodes}"
        clusterNM=`cat -n ${confile} | grep cluster-config-file | grep -v '#' | wc -l`
        if [ ${clusterNM} -ge 1 ];then
            number=`cat -n ${confile} | grep cluster-config-file | grep -v '#' | awk '{print$1}' | head -n 1`
            sed -i "${number}d" ${confile}
        else
            sed -i "${clusternumber}i ${clusterconfnew}" ${confile}
        fi
    done
}
function trib(){
    cd ${installdir}/${cluster_name}
    echo;
    echo "[启动集群进程]"
    echo;
    ./start.sh
    /usr/bin/sleep 2
    echo;
    echo "[进程  已启动]"
    echo;

    bindip=`ps aux | grep "redis" | awk '{print$12}' | grep -v "auto" | head -n ${number}`
    echo;
    echo "[安装 gem环境]"
    echo;
    /usr/bin/sleep 2
    wget https://rubygems.org/downloads/redis-3.2.0.gem
    gem install redis-*.gem
    echo;
    echo "[gem 环境已装]"
    echo;
    /usr/bin/sleep 2
    echo;
    echo "[搭建节点连接]"
    echo;
    ./redis-trib.rb create --replicas 1 ${bindip} > redis-cluster.log > /dev/null 2>&1
}
function main(){
    gem=`which gem`
    gemv=$(echo $gem | grep gem)
    if [[ $gemv != "" ]]
    then
        echo;
        echo "[gem 环境存在]"
        echo;
        checkroot
        judge
        trib
    else
        echo;
        echo "[gem 环境未装]"
        echo;
        yum install ruby rubygems -y
        wget https://rubygems.org/downloads/redis-3.2.0.gem
        gem install redis-*.gem
        /usr/bin/sleep 2
        echo;
        echo "[gem 环境已装]"
        echo;
        checkroot
        judge
        trib
    fi
}
main
文档更新时间: 2018-12-18 09:13   作者:RuM