三、单redis升级redis集群+哨兵

  • 三、单redis升级redis集群+哨兵已关闭评论
  • 163 次浏览
  • A+
所属分类:linux技术
摘要

针对假如已经是安装了redis,只是是单部署,需要把他切换成redis集群+哨兵模式,我因为偷懒,就写了个脚本来执行,各位看官,请品~你品~你细品~

针对假如已经是安装了redis,只是是单部署,需要把他切换成redis集群+哨兵模式,我因为偷懒,就写了个脚本来执行,各位看官,请品~你品~你细品~

首先准备个升级包,放到任意路径,内容如下:

三、单redis升级redis集群+哨兵

 

 第一个文件不用管,第二个跟第四个,是把里面的配置改好,如何配置请参考我之前写的redis集群一,

然后是upgrade.sh脚本内容:

  1 #!/bin/bash   2    3 base_path=/home/zmoon   4 redis_package_name=redis-5.0.12   5    6 #安装文件名称(安装文件位置=安装位置+安装文件名称)   7 redis_path=/redis-5.0.12   8 redis_conf=/etc/redis.conf   9 sentinel_conf=/etc/sentinel.conf  10 monitor_redis=/home/zmoon/bin  11   12   13   14 #判断redis是否安装  15 check_redis(){  16     if [ -d "$base_path$redis_path" ]; then  17         echo "redis 已安装,无需再次安装!"  18         return 1  19     else  20         return 0  21     fi  22 }  23   24 #判断etc  25 check_redis_conf(){  26     if [ -f "$base_path$redis_path$redis_conf" ]; then  27         echo "redis.conf 已存在,无需再次安装!"  28         return 1  29     else  30         return 0  31     fi  32 }  33   34 #判断etc redis.conf,sentinel.conf是否存在  35 check_sentinel_conf(){  36     if [ -f "$base_path$redis_path$sentinel_conf" ]; then  37         echo "sentinel.conf 已存在,无需再次安装!"  38         return 1  39     else  40         return 0  41     fi  42 }  43   44   45 copy_redis_conf(){  46     mkdir -p $base_path$redis_path/etc  47     cp redis.conf $base_path$redis_path/etc/redis.conf  48     return 2  49 }  50   51 copy_sentinel_conf(){  52     mkdir -p $base_path$redis_path/etc  53     cp sentinel.conf $base_path$redis_path/etc/sentinel.conf  54     return 2  55 }  56   57 copy_monitor_sh(){  58     cp install-zm-redis.sh $monitor_redis/install-zm-redis.sh  59     return 2  60 }  61   62 install_redis(){  63     echo "安装redis"  64       65     tar -zxvf $redis_package_name.tar.gz  66       67     echo "开始编译及安装。。。"   68     cd $redis_package_name  69       70     make  71     make PREFIX=$base_path$redis_path install  72       73     #切换到redis目录 新建etc文件夹  74     mkdir -p $base_path$redis_path/etc  75   76     cd ..  77       78     #把配置好的文件复制过来  79     cp redis.conf $base_path$redis_path/etc/redis.conf  80     cp sentinel.conf $base_path$redis_path/etc/sentinel.conf  81       82     return 2  83   84 }  85   86 main(){  87     echo "base_path:$base_path"  88       89     #判断当前用户是否为root用户,不是则终止部署  90     if [ `whoami` != "root" ]; then  91         echo "部署请将用户切换到root用户,安装终止"  92         exit 1  93     fi  94   95     if [ ! -d "$base_path" ]; then  96         mkdir $base_path   97     fi  98       99     #检查是否有redis进程 100     # 停止正在运行的services-应用 101     echo "检查是否有redis进程" 102     pids=`ps -ef|grep redis-server|grep -v 'grep'|awk '{print $2}'` 103     pidsentinel=`ps -ef|grep redis-sentinel|grep -v 'grep'|awk '{print $2}'` 104     pids=${pids%/*} 105     if [ -n "$pids" ];then 106         for pid in $pids 107         do  108             kill -9 $pid 109         done 110     fi 111  112     pidsentinel=${pidsentinel%/*} 113     if [ -n "$pidsentinel" ];then 114         for pid in $pidsentinel 115         do  116             kill -9 $pid 117         done 118     fi 119      120     echo "------------------------------------------------" 121     echo "Redis service stop" 122     echo "------------------------------------------------" 123      124     #安装redis 125     check_redis 126     if [ $? -eq 0 ]; then 127         install_redis 128          129         if [ $? -eq 2 ]; then 130             echo "redis 安装成功!" 131         else 132             echo "redis 安装失败,安装终止!" 133             exit 1 134         fi 135     fi 136      137     #安装redis 138     check_redis_conf 139     if [ $? -eq 0 ]; then 140         copy_redis_conf 141          142         if [ $? -eq 2 ]; then 143             echo "redis.conf copy成功!" 144         else 145             echo "redis.conf copy失败,copy终止!" 146             exit 1 147         fi 148     fi 149      150     #安装redis 151     check_sentinel_conf 152     if [ $? -eq 0 ]; then 153         copy_sentinel_conf 154          155         if [ $? -eq 2 ]; then 156             echo "sentinel.conf copy成功!" 157         else 158             echo "sentinel.conf copy失败,copy终止!" 159             exit 1 160         fi 161     fi 162      163     copy_monitor_sh 164          165         if [ $? -eq 2 ]; then 166             echo "看门猫redis脚本 copy成功!" 167         else 168             echo "看门猫redis脚本 copy失败,copy终止!" 169             exit 1 170         fi 171      172     #提示“请输入当前节点IP(xx.xx.xx.xx):”,把输入IP保存入变量selfIp中 173     read -p "请输入当前节点IP(xx.xx.xx.xx):" selfIp 174     #提示“请输入Redis主节点IP(xx.xx.xx.xx):”,把输入IP保存入变量nodeIp中 175     read -p "请输入当前主节点IP(xx.xx.xx.xx):" nodeIp 176  177  178     if [ $selfIp = $nodeIp ] 179     then 180         sed -i 's#.*'"sentinel monitor mymaster.*#sentinel monitor mymaster ${nodeIp} 6379 2"'#' /home/zmoon/redis-5.0.12/etc/sentinel.conf 181     else 182         echo -e "replicaof ${nodeIp} 6379" >>  /home/zmoon/redis-5.0.12/etc/redis.conf 183          184         sed -i 's#.*'"sentinel monitor mymaster.*#sentinel monitor mymaster ${nodeIp} 6379 2"'#' /home/zmoon/redis-5.0.12/etc/sentinel.conf 185     fi 186      187     #启动redis 188     $base_path$redis_path/bin/redis-server $base_path$redis_path/etc/redis.conf & 189     $base_path$redis_path/bin/redis-sentinel $base_path$redis_path/etc/sentinel.conf & 190 } 191 main $1 192 exit 0

里面首先判断是否有redis进程与sentinel进程,有则kill掉,

然后又判断是否有安装redis,

然后是etc下的两个配置文件是否存在,

进而是修改配置文件的内容,

最后启动redis服务与哨兵服务,redis升级完成。

请各位看官大大参与评论,指点不足~