码迷,mamicode.com
首页 > 其他好文 > 详细

菜单形式部署redis 集群的脚本

时间:2019-06-16 16:11:59      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:art   mit   author   commit   lag   pcre   bar   -name   mod   

为减轻Linux运维工作中重复执行的工作量,降低出错概率;同时保证脚本能应用在不同的场合以及其灵活性,特地编写此脚本,仅供参考!

?
#!/bin/bash
#
#Author: fanfubin
#QQ: 502422514
#Date: 2019-03-19
#FileName: redis.sh
#Description: The deploy redis script
#Copyright (C): 2019 All rights reserved
#

source /etc/init.d/functions

BLACK_COLOR=‘\E[1;30m‘       
RED_COLOR=‘\E[1;31m‘       
GREEN_COLOR=‘\E[1;32m‘       
YELLOW_COLOR=‘\E[1;33m‘       
BLUE_COLOR=‘\E[1;34m‘       
PINK_COLOR=‘\E[1;35m‘ #紫色       
SKYBLUE_COLOR=‘\E[1;36m‘ #天蓝色       
WHITE_COLOR=‘\E[1;37m‘ #白色       

SHAN=‘\E[31;5m‘ #字体闪烁       
RES=‘\E[0m‘

cpu_count=`lscpu|awk -F ‘ +‘ ‘/^CPU\(s\):/{print $2}‘`
if [ $cpu_count -le 2 ];then
    export cpu_num="1"
else
    let cpu_num=`lscpu|awk -F ‘ +‘ ‘/^CPU\(s\):/{print $2}‘`-1
    export cpu_num
fi

export httpd_user="nginx" 
export httpd_group="nginx"
export httpd_id="2000"
export redis_×××tall_ver="redis-4.0.14.tar.gz"
export master_default_passwd="123456"
export ruby_×××tall_ver="ruby-2.5.5.tar.gz"
export redis_gem_×××tall_ver="redis-4.1.2.gem"

f_main_title() {
        f_color green "`echo -e "\t服务器监控系统"`"
}

f_color() {
    case "$1" in
        black)
            echo -e "${PINK_COLOR}$2 ${RES}"
            ;;
        red)
            echo -e "${RED_COLOR}$2 ${RES}"
            ;;
        green)
            echo -e "${GREEN_COLOR}$2 ${RES}"
            ;;
        yellow)
            echo -e "${YELLOW_COLOR}$2 ${RES}"
            ;;
        blue)
            echo -e "${BLUE_COLOR}$2 ${RES}"
            ;;
        skyblue)
            echo -e "${PINK_COLOR}$2 ${RES}"
            ;;
        pink)
            echo -e "${PINK_COLOR}$2 ${RES}"
            ;;
        white)
            echo -e "${PINK_COLOR}$2 ${RES}"
            ;;
        shan)
            echo -e "${SHAN}$2 ${RES}"
            ;;
        *)
            exit    
            ;;
    esac
}

##创建通用的用户名和id(user:nginx  id:2000)
id $httpd_user                                                                                                                                                          
        if [ `echo $?` -ne 0 ];then
                useradd -s /sbin/nologin -M -u $httpd_id $httpd_user
        else
             test_httpd_id=`id $httpd_user | awk -F ‘[=(]‘ ‘{print $2}‘`
             test_httpd_gid=`id $httpd_user | awk -F ‘[=(]‘ ‘{print $4}‘`
             if [ $test_httpd_id -ne $httpd_id -o $test_httpd_gid -ne $httpd_id ];then
                     userdel -r $httpd_id
                     useradd -s /sbin/nologin -M -u $httpd_id $httpd_user
             fi
        fi

f_menu_×××tall_server_redis_list() {
    local path
    local redis_×××tall_dir
    local num
    local choose_num
    local passwd_choose_num
    local locate_server_num
    local find_server_num
    local new_master_passwd
    local master_passwd
    local redis_gem_×××tall_dir
    local gem_passwd

    echo
    f_main_title
    echo "==================================" 
    cat <<-EOF
    【1. 】 Show ×××tall redis version
    【2. 】 Source ×××tall redis && cluster 
    【3. 】 Show ×××tall ruby && gem version
    【4. 】 Source ×××tall ruby && gem
    【0. 】 Exit

    EOF
    echo "=================================="

    read -p "Pls input a num:" num
    f_is_num $num
    [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_×××tall_server_redis_list;}

    case "$num" in 
        1)
            echo
            echo -e "\t$redis_×××tall_ver"
            f_menu_×××tall_server_redis_list
            ;;
        ###################################################### ×××tall redis && cluster
        2)
            echo
            #redis  .tar. is exist?
            yum -y ×××tall mlocate &> /dev/null
            updatedb &> /dev/null
            locate_server_num=`locate ${redis_×××tall_ver}| wc -l`
            if [ ! -e $redis_×××tall_ver ];then
                if [ $locate_server_num -eq 1 ];then
                    \cp `locate ${redis_×××tall_ver}` . -a
                elif [ $locate_server_num -ge 2 ];then
                    \cp `locate ${redis_×××tall_ver} | head -n1` . -a
                else
                    find_server_num=`find / -name ${redis_×××tall_ver}| wc -l`
                    if [ $find_server_num -eq 1 ];then
                        \cp `find / -name ${redis_×××tall_ver}` . -a
                    elif [ $find_server_num -ge 2 ];then
                        \cp `find / -name ${redis_×××tall_ver} | head -n1` . -a
                    else
                        f_color red "`echo "${redis_×××tall_ver} is not exist!"`"
                        f_menu_×××tall_server_redis_list
                    fi
                fi
            fi

            redis_×××tall_dir=`echo $redis_×××tall_ver | awk -F ‘.tar.‘ ‘{print $1}‘`

            read -p "Pls input 1-×××tall or 2-quit : " choose_num
            f_is_num $choose_num
            [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_×××tall_server_redis_list;}

            case "$choose_num" in
                1)
                    echo
                    yum -y ×××tall gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel &> /dev/null

                    read -p "Whether use default masterauth=requirepass  1-yes or 2-no : " passwd_choose_num
                    f_is_num $passwd_choose_num
                    [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_×××tall_server_redis_list;}
                    case $passwd_choose_num in
                        1)
                            new_master_passwd=$master_default_passwd
                            f_color shan "`echo "Default masterauth=password is: $new_master_passwd "`"
                            ;;
                        2)
                            while true
                            do
                                read -p "Pls input masterauth=requirepass password: " master_passwd
                                if [ -z "$master_passwd" ];then
                                    f_color red "`echo "password can not be empty !"`"  
                                else
                                    new_master_passwd=$master_passwd
                                    f_color shan "`echo "Masterauth=requirepass password is: $new_master_passwd "`"
                                    break
                                fi
                            done
                            ;;
                        *)
                            f_menu_×××tall_server_redis_list
                    esac

                    read -p "Pls input ×××tall Path or null: " path

                    if [ -z "$path" ];then
                        \rm -rf $redis_×××tall_dir
                        tar xf $redis_×××tall_ver
                        cd $redis_×××tall_dir
                        make PREFIX=/apps/$redis_×××tall_dir ×××tall
                        mkdir -p /apps/$redis_×××tall_dir/{etc,logs,data,run}
                        \cp redis.conf /apps/$redis_×××tall_dir/etc
                        \cp src/redis-trib.rb /usr/bin/
                        sed -ri ‘s/bind 127.0.0.1/bind 0.0.0.0/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s/daemonize no/daemonize yes/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s/supervised no/supervised systemd/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s#/var/run/redis_6379.pid#/apps/‘$redis_×××tall_dir‘/run/redis_6379.pid#‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sleep 0.5
                        sed -ri ‘s#logfile ""#logfile "/apps/‘$redis_×××tall_dir‘/logs/redis_6379.log"#‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s/stop-writes-on-bgsave-error yes/stop-writes-on-bgsave-error no/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s#dbfilename dump.rdb#dbfilename dump_6379.rdb#‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s#dir \./#dir /apps/‘$redis_×××tall_dir‘/data#‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s/# masterauth <master-password>/masterauth "‘$new_master_passwd‘"/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sleep 0.5
                        sed -ri ‘s/# requirepass foobared/requirepass ‘$new_master_passwd‘/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s/# maxclients 10000/maxclients 65536/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s/appendonly no/appendonly yes/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s/appendfilename "appendonly.aof"/appendfilename "appendonly_6379.aof"/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sleep 0.5
                        ##cluster
                        sed -ri ‘s/# cluster-enabled yes/ cluster-enabled yes/‘ /apps/$redis_×××tall_dir/etc/redis.conf
                        sed -ri ‘s/# cluster-config-file nodes-6379.conf/ cluster-config-file nodes-6379.conf/‘ /apps/$redis_×××tall_dir/etc/redis.conf

                        total_mem=`free -m | grep ‘Mem‘ |awk ‘{print $2}‘`
                        let mem_result=$total_mem/2*1024*1024
                        sed -ri ‘s/# maxmemory <bytes>/maxmemory ‘$mem_result‘/‘ /apps/$redis_×××tall_dir/etc/redis.conf

                        ln -s /apps/$redis_×××tall_dir/bin/* /usr/bin/
                        sleep 1
                        cat >/usr/lib/systemd/system/redis.service<<-EOF
                            [Unit]
                            Description=Redis persistent key-value database
                            After=network.target
                            After=network-online.target
                            Wants=network-online.target

                            [Service]
                            ExecStart=/apps/$redis_×××tall_dir/bin/redis-server /apps/$redis_×××tall_dir/etc/redis.conf --supervised systemd
                            ExecReload=/bin/kill -s HUP \$MAINPID
                            ExecStop=/bin/kill -s QUIT \$MAINPID
                            Type=notify
                            #User=$httpd_user
                            #Group=$httpd_user
                            User=root
                            Group=root
                            RuntimeDirectory=redis
                            RuntimeDirectoryMode=0755

                            [Install]
                            WantedBy=multi-user.target
                        EOF

                        #chown -R ${httpd_user}.${httpd_user} /apps/$redis_×××tall_dir
                        cat >>/etc/sysctl.conf<<-EOF
                            net.core.somaxconn = 512
                            vm.overcommit_memory = 1
                        EOF
                        sysctl -p
                        sleep 1
                        cat >>/etc/rc.d/rc.local<<-EOF
                            echo never > /sys/kernel/mm/transparent_hugepage/enabled
                        EOF
                        chmod a+x /etc/rc.d/rc.local
                        systemctl daemon-reload
                        sleep 1
                        systemctl start redis
                        cd -

                        f_color shan "`echo "redis ×××tall path is: /apps/${redis_×××tall_dir}"`"

                    else
                        \rm -rf $redis_×××tall_dir
                        tar xf $redis_×××tall_ver
                        cd $redis_×××tall_dir
                        make PREFIX=$path ×××tall
                        mkdir -p ${path}/{etc,logs,data,run}
                        \cp redis.conf ${path}/etc
                        \cp src/redis-trib.rb /usr/bin/
                        sed -ri ‘s/bind 127.0.0.1/bind 0.0.0.0/‘ $path/etc/redis.conf
                        sed -ri ‘s/daemonize no/daemonize yes/‘ $path/etc/redis.conf
                        sed -ri ‘s/supervised no/supervised systemd/‘ $path/etc/redis.conf
                        sed -ri ‘s#/var/run/redis_6379.pid#/apps/‘$redis_×××tall_dir‘/run/redis_6379.pid#‘ $path/etc/redis.conf
                        sleep 0.5
                        sed -ri ‘s#logfile ""#logfile "/apps/‘$redis_×××tall_dir‘/logs/redis_6379.log"#‘ $path/etc/redis.conf
                        sed -ri ‘s/stop-writes-on-bgsave-error yes/stop-writes-on-bgsave-error no/‘ $path/etc/redis.conf
                        sed -ri ‘s#dbfilename dump.rdb#dbfilename dump_6379.rdb#‘ $path/etc/redis.conf
                        sed -ri ‘s#dir \./#dir /apps/‘$redis_×××tall_dir‘/data#‘ $path/etc/redis.conf
                        sed -ri ‘s/# masterauth <master-password>/masterauth "‘$new_master_passwd‘"/‘ $path/etc/redis.conf
                        sleep 0.5
                        sed -ri ‘s/# requirepass foobared/# requirepass ‘$new_master_passwd‘/‘ $path/etc/redis.conf
                        sed -ri ‘s/# maxclients 10000/maxclients 65536/‘ $path/etc/redis.conf
                        sed -ri ‘s/appendonly no/appendonly yes/‘ $path/etc/redis.conf
                        sed -ri ‘s/appendfilename "appendonly.aof"/appendfilename "appendonly_6379.aof"/‘ $path/etc/redis.conf
                        sleep 0.5
                        ##cluster
                        sed -ri ‘s/# cluster-enabled yes/ cluster-enabled yes/‘ $path/etc/redis.conf
                        sed -ri ‘s/# cluster-config-file nodes-6379.conf/ cluster-config-file nodes-6379.conf/‘ $path/etc/redis.conf

                        total_mem=`free -m | grep ‘Mem‘ |awk ‘{print $2}‘`
                        let mem_result=$total_mem/2*1024*1024
                        sed -ri ‘s/# maxmemory <bytes>/maxmemory ‘$mem_result‘/‘ $path/etc/redis.conf

                        ln -s ${path}/bin/* /usr/bin/
                        sleep 1
                        cat >/usr/lib/systemd/system/redis.service<<-EOF
                            [Unit]
                            Description=Redis persistent key-value database
                            After=network.target
                            After=network-online.target
                            Wants=network-online.target

                            [Service]
                            ExecStart=${path}/bin/redis-server ${path}/etc/redis.conf --supervised systemd
                            ExecReload=/bin/kill -s HUP \$MAINPID
                            ExecStop=/bin/kill -s QUIT \$MAINPID
                            Type=notify
                            #User=$httpd_user
                            #Group=$httpd_user
                            User=root
                            Group=root
                            RuntimeDirectory=redis
                            RuntimeDirectoryMode=0755

                            [Install]
                            WantedBy=multi-user.target
                        EOF

                        #chown -R ${httpd_user}.${httpd_user} $path
                        cat >>/etc/sysctl.conf<<-EOF
                            net.core.somaxconn = 512
                            vm.overcommit_memory = 1
                        EOF
                        sysctl -p
                        sleep 1
                        cat >>/etc/rc.d/rc.local<<-EOF
                            echo never > /sys/kernel/mm/transparent_hugepage/enabled
                        EOF
                        chmod a+x /etc/rc.d/rc.local
                        systemctl daemon-reload
                        sleep 1
                        systemctl start redis
                        cd -

                        f_color shan "`echo "redis ×××tall path is: ${path}"`"
                        sleep 1
                    fi

                    f_menu_×××tall_server_redis_list
                    ;;
                2)
                    f_menu_×××tall_server_redis_list
                    ;;
                *)      
                    f_menu_×××tall_server_redis_list
                    ;;
            esac
            ;;
        ####################################################  show ruby && redis_gem version
        3)
            echo
            echo -e "\t$ruby_×××tall_ver"
            echo -e "\t$redis_gem_×××tall_ver"
            f_menu_×××tall_server_redis_list
            ;;
        #####################################################  ×××tal ruby && redis_gem 
        4)
            echo
            read -p "Pls input 1-×××tall or 2-quit : " choose_num
            f_is_num $choose_num
            [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_×××tall_server_redis_list;}

            yum -y ×××tall gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel &> /dev/null

            read -p "Whether use default masterauth=requirepass  1-yes or 2-no : " passwd_choose_num
            f_is_num $passwd_choose_num
            [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_×××tall_server_redis_list;}
            case $passwd_choose_num in
                1)
                    new_master_passwd=$master_default_passwd
                    f_color shan "`echo "Default masterauth=requirepass is: $new_master_passwd "`"
                    ;;
                2)
                    f_color shan "`echo "You input passwd must be -eq redis cluster requirepass, or else create cluster error!!! "`"
                    read -p "You affirm chooge passwd ? 1-yes or 2-no :" $gem_choose_num
                    case $gem_choose_num in
                        1)
                            while true
                            do
                                read -p "Pls input gem password: " gem_passwd
                                if [ -z "$gem_passwd" ];then
                                    f_color red "`echo "password can not be empty !"`"  
                                else
                                    new_master_passwd=$gem_passwd
                                    f_color shan "`echo "Gem password is: $new_master_passwd "`"
                                    break
                                fi
                            done
                            ;;
                        2)
                            f_menu_×××tall_server_redis_list
                            ;;
                        *)
                            f_menu_×××tall_server_redis_list
                    esac
                    ;;
                *)
                    f_menu_×××tall_server_redis_list
            esac

            # ruby  .tar. is exist?
            yum -y ×××tall mlocate &> /dev/null
            updatedb &> /dev/null
            locate_server_num=`locate ${ruby_×××tall_ver}| wc -l`
            if [ ! -e $ruby_×××tall_ver ];then
                if [ $locate_server_num -eq 1 ];then
                    \cp `find / -name ${ruby_×××tall_ver}` . -a
                elif [ $locate_server_num -ge 2 ];then
                    \cp `find / -name ${ruby_×××tall_ver} | head -n1` . -a
                else
                    find_server_num=`find / -name ${ruby_×××tall_ver}| wc -l`
                    if [ $find_server_num -eq 1 ];then
                        \cp `find / -name ${ruby_×××tall_ver}` . -a
                    elif [ $find_server_num -ge 2 ];then
                        \cp `find / -name ${ruby_×××tall_ver} | head -n1` . -a
                    else
                        f_color red "`echo "${ruby_×××tall_ver} is not exist!"`"
                        f_menu_×××tall_server_redis_list
                    fi
                fi
            fi

            # redis_gem exist?
            locate_server_num=`locate ${redis_gem_×××tall_ver}| wc -l`
            if [ ! -e $redis_gem_×××tall_ver ];then
                if [ $locate_server_num -eq 1 ];then
                    \cp `find / -name ${redis_gem_×××tall_ver}` . -a
                elif [ $locate_server_num -ge 2 ];then
                    \cp `find / -name ${redis_gem_×××tall_ver} | head -n1` . -a
                else
                    find_server_num=`find / -name ${redis_gem_×××tall_ver}| wc -l`
                    if [ $find_server_num -eq 1 ];then
                        \cp `find / -name ${redis_gem_×××tall_ver}` . -a
                    elif [ $find_server_num -ge 2 ];then
                        \cp `find / -name ${redis_gem_×××tall_ver} | head -n1` . -a
                    else
                        f_color red "`echo "${redis_gem_×××tall_ver} is not exist!"`"
                        f_menu_×××tall_server_redis_list
                    fi
                fi
            fi

            ruby_×××tall_dir=`echo $ruby_×××tall_ver | awk -F ‘.tar.‘ ‘{print $1}‘`
            redis_gem_×××tall_dir=`echo $redis_gem_×××tall_ver | awk -F ‘.gem‘ ‘{print $1}‘`
            \rm -rf $ruby_×××tall_dir
            tar xvf $ruby_×××tall_ver
            cd $ruby_×××tall_dir
            ./configure
            make -j $cpu_num
            make ×××tall
            sleep 1
            cd -
            /usr/local/bin/gem ×××tall -l $redis_gem_×××tall_ver
            sed -ri s‘/password => nil/password => ‘$new_master_passwd‘/g‘ /usr/local/lib/ruby/gems/2.5.0/gems/$redis_gem_×××tall_dir/lib/redis/client.rb 
            f_menu_×××tall_server_redis_list
            ;;
        0)
            exit 1
            ;;
        *)
            echo
            f_menu_×××tall_server_redis_list
    esac    
    unset path
    unset redis_×××tall_dir
    unset num
    unset choose_num
    unset locate_server_num
    unset passwd_choose_num
    unset find_server_num
    unset new_master_passwd
    unset master_passwd
    unset redis_gem_×××tall_dir
    unset gem_passwd

}

Flag=true
while $Flag
do
    f_menu_×××tall_server_redis_list
        if [ $? -eq 0 ];then
                Flag=false
        fi
done

 

最后需要手动登陆集群中任意主机,进行创建集群,命令示例如下:

redis-trib.rb create  --replicas 1 172.18.200.101:6379  172.18.200.102:6379 172.18.200.103:6379 172.18.200.104:6379 172.18.200.105:6379 172.18.200.106:6379

菜单形式部署redis 集群的脚本

标签:art   mit   author   commit   lag   pcre   bar   -name   mod   

原文地址:https://blog.51cto.com/13560168/2409657

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!