版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1433158
目录
一、创建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主从环境。 master的create\_redis\_cluster.sh文件内容如下:#!/bin/bash
maxdir=`ls -l /data1 | grep "redisdata" | awk '{print $9}' | sort -k1.10n | tail -n1`
maxnum=`expr ${maxdir:9} + 1`
datadir='/data1/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
chown -R redis:redis ${datadir}
chmod 775 ${datadir}
chmod 664 ${datadir}/*
# 添加哨兵监控
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
scp /home/redis/tmp_sentinel_monitor 103.244.233.167:/home/redis/
scp /home/redis/tmp_sentinel_monitor 103.244.233.168:/home/redis/
/root/sentinel_monitor.sh
ssh root@103.244.233.167 /root/sentinel_monitor.sh
ssh root@103.244.233.168 /root/sentinel_monitor.sh
# 启动redis实例
sudo -u redis /home/redis/redis-5.0.3/src/redis-server ${datadir}/redis.conf
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文件:#!/bin/bash
maxdir=`ls -l /data1 | grep "redisdata" | awk '{print $9}' | sort -k1.10n | tail -n1`
maxnum=`expr ${maxdir:9} + 1`
datadir='/data1/redisdata'${maxnum}
clustername='redis'${maxnum}
redis_port=$((20000+maxnum))
export clustername redis_port
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
chown -R redis:redis ${datadir}
chmod 775 ${datadir}
chmod 664 ${datadir}/*
sudo -u redis /home/redis/redis-5.0.3/src/redis-server ${datadir}/redis.conf 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
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 2gb 1gb 300 slave的redis.conf.templet模板文件比master的只是在最后增加如下一行:slaveof 103.244.233.166 20001 sentinel\_monitor.sh文件内容如下:#!/bin/bash
cat /home/redis/tmp_sentinel_monitor | sudo -u redis /home/redis/redis-3.2.3/src/redis-cli -p 30001 -a 123456 sentinel.conf文件内容如下:port 30001
protected-mode no
dir "/home/redis/sentinel" 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 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 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