初学乍练redis:主从复制及哨兵维护脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzy0623/article/details/82013226

目录

一、创建redis主从复制集群脚本

二、删除redis主从复制集群脚本

三、启动redis主从复制集群脚本

四、停止redis主从复制集群脚本

五、重启哨兵脚本


        环境:

103.244.233.166(master、sentinel)
103.244.233.167(slave、sentinel)
103.244.233.168(slave、sentinel)

        用脚本维护多个一主两从的redis复制环境,master name名称依次为redis1、redis2...,使用端口依次为20001、20002...,哨兵使用端口为30001,一组哨兵监控多套redis主从环境。

一、创建redis主从复制集群脚本

        master的create_redis_cluster.sh文件内容如下:

#!/bin/bash

maxdir=`ls -l /home/redis | grep "redisdata" | awk '{print $9}' | sort -k1.10n | tail -n1`
maxnum=`expr ${maxdir:9} + 1`
datadir='/home/redis/redisdata'${maxnum}
clustername='redis'${maxnum}
redis_port=$((20000+maxnum))

mkdir ${datadir}
cp /home/redis/redis.conf.templet ${datadir}/redis.conf

sed -i "s/20001/$redis_port/g" ${datadir}/redis.conf
sed -i "s/redisdata1/redisdata$maxnum/g" ${datadir}/redis.conf

cat /dev/null > /home/redis/tmp_sentinel_monitor
echo "sentinel monitor ${clustername} 103.244.233.166 ${redis_port} 2" >> /home/redis/tmp_sentinel_monitor
echo "sentinel set ${clustername} auth-pass 123456" >> /home/redis/tmp_sentinel_monitor
echo "sentinel set ${clustername} down-after-milliseconds 5000" >> /home/redis/tmp_sentinel_monitor
echo "sentinel set ${clustername} failover-timeout 10000" >> /home/redis/tmp_sentinel_monitor

chown -R redis:redis ${datadir}
chmod 775 ${datadir}
chmod 664 ${datadir}/*

sudo -u redis /home/redis/redis-3.2.3/src/redis-server ${datadir}/redis.conf
cat /home/redis/tmp_sentinel_monitor | sudo -u redis /home/redis/redis-3.2.3/src/redis-cli -p 30001 -a 123456

ssh root@103.244.233.167 /root/create_redis_cluster.sh
ssh root@103.244.233.168 /root/create_redis_cluster.sh

info="集群名称:${clustername}  |哨兵:103.244.233.166 30001, 103.244.233.167 30001, 103.244.233.168 30001 |口令:123456"

echo ${info} | sed 's/|/\n    /g'

        slave的create_redis_cluster.sh文件比master的只是去掉了如下所示的最后部分:

ssh root@103.244.233.167 /root/create_redis_cluster.sh
ssh root@103.244.233.168 /root/create_redis_cluster.sh

info="集群名称:${clustername}  |哨兵:103.244.233.166 30001, 103.244.233.167 30001, 103.244.233.168 30001 |口令:123456"

echo ${info} | sed 's/|/\n    /g'

        master的redis.conf.templet模板文件如下:

rename-command flushAll ""
daemonize yes
port 20001
dir "/home/redis/redisdata1"
pidfile "/home/redis/redisdata1/redis.pid"
logfile "/home/redis/redisdata1/redis.log"
dbfilename "dump.rdb"
save 900 1
appendonly no
appendfilename "appendonly.aof"
appendfsync always
maxmemory 8gb
maxmemory-policy volatile-lru
maxmemory-samples 3
slowlog-log-slower-than 10000
repl-backlog-size 64mb
timeout 0
repl-timeout 240
 
requirepass "123456"
masterauth "123456"
protected-mode no

        slave的redis.conf.templet模板文件比master的只是在最后增加如下一行:

slaveof 103.244.233.166 20001

二、删除redis主从复制集群脚本

        master的remove_redis_cluster.sh文件内容如下:

#!/bin/bash
if [ ! -n "$1" ] ;then
    echo "请输入集群编号!"
    exit
fi

maxnum=$1
datadir='/home/redis/redisdata'${maxnum}
redis_port=$((20000+maxnum))
clustername='redis'${maxnum}

# 根据端口号查询对应的pid
pid=$(netstat -nlp | grep $redis_port | awk '{print $7}' | awk -F"/" '{ print $1 }' | uniq);
# 杀掉对应的进程,如果pid不存在,则不执行
if [  -n  "$pid"  ];  then
    kill -9 $pid
fi

sudo -u redis /home/redis/redis-3.2.3/src/redis-cli -p 30001 -a 123456 sentinel remove ${clustername}

rm -rf $datadir

ssh root@103.244.233.167 /root/remove_redis_cluster.sh $1
ssh root@103.244.233.168 /root/remove_redis_cluster.sh $1

        slave的remove_redis_cluster.sh文件比master的只是去掉了如下所示的最后部分:

ssh root@103.244.233.167 /root/remove_redis_cluster.sh $1
ssh root@103.244.233.168 /root/remove_redis_cluster.sh $1

三、启动redis主从复制集群脚本

        master的start_redis_cluster.sh文件内容如下:

#!/bin/bash
if [ ! -n "$1" ] ;then
    echo "请输入集群编号!"
    exit
fi

maxnum=$1
datadir='/home/redis/redisdata'${maxnum}
clustername='redis'${maxnum}
redis_port=$((20000+maxnum))

cat /dev/null > /home/redis/tmp_sentinel_monitor
echo "sentinel monitor ${clustername} 103.244.233.166 ${redis_port} 2" >> /home/redis/tmp_sentinel_monitor
echo "sentinel set ${clustername} auth-pass 123456" >> /home/redis/tmp_sentinel_monitor
echo "sentinel set ${clustername} down-after-milliseconds 5000" >> /home/redis/tmp_sentinel_monitor
echo "sentinel set ${clustername} failover-timeout 10000" >> /home/redis/tmp_sentinel_monitor

sudo -u redis /home/redis/redis-3.2.3/src/redis-server ${datadir}/redis.conf
cat /home/redis/tmp_sentinel_monitor | sudo -u redis /home/redis/redis-3.2.3/src/redis-cli -p 30001 -a 123456 

ssh root@103.244.233.167 /root/start_redis_cluster.sh $1
ssh root@103.244.233.168 /root/start_redis_cluster.sh $1

        slave的start_redis_cluster.sh文件比master的只是去掉了如下所示的最后部分:

ssh root@103.244.233.167 /root/start_redis_cluster.sh $1
ssh root@103.244.233.168 /root/start_redis_cluster.sh $1

四、停止redis主从复制集群脚本

        master上的shutdown_redis_cluster.sh文件内容如下:

#!/bin/bash
if [ ! -n "$1" ] ;then
    echo "请输入集群编号!"
    exit
fi

maxnum=$1
redis_port=$((20000+maxnum))
clustername='redis'${maxnum}

sudo -u redis /home/redis/redis-3.2.3/src/redis-cli -p ${redis_port} -a 123456 shutdown
sudo -u redis /home/redis/redis-3.2.3/src/redis-cli -p 30001 -a 123456 sentinel remove ${clustername}

ssh root@103.244.233.167 /root/shutdown_redis_cluster.sh $1
ssh root@103.244.233.168 /root/shutdown_redis_cluster.sh $1

        slave的shutdown_redis_cluster.sh文件比master的只是去掉了如下所示的最后部分:

ssh root@103.244.233.167 /root/shutdown_redis_cluster.sh $1
ssh root@103.244.233.168 /root/shutdown_redis_cluster.sh $1

五、重启哨兵脚本

        master的restart_sentinel.sh文件内容如下:

#!/bin/bash

sudo -u redis /home/redis/redis-3.2.3/src/redis-cli -p 30001 -a 123456 shutdown
sudo -u redis /home/redis/redis-3.2.3/src/redis-sentinel /home/redis/sentinel/sentinel.conf > /home/redis/sentinel/sentinel.log 2>&1 
&

ssh root@103.244.233.167 /root/restart_sentinel.sh
ssh root@103.244.233.168 /root/restart_sentinel.sh

        slave的restart_sentinel.sh文件比master的只是去掉了如下所示的最后部分:

ssh root@103.244.233.167 /root/restart_sentinel.sh
ssh root@103.244.233.168 /root/restart_sentinel.sh

 

阅读更多
换一批

没有更多推荐了,返回首页