REDIES集群脚本

 
转至元数据结尾
 
转至元数据起始
 

一、dockerfile 及其中的脚本

1.创建集群的cluster.sh

#/bin/bash
cat /mnt/redishosts|awk '{print $1}'|grep -v 127.0.0.1 > ips
cat ips
sed -i 's/$/:6379/g' ips
echo yes|/usr/local/redis/src/redis-trib.rb create --replicas 1 `cat ips`

 

2.supervisord配置文件supervisord.conf
[supervisord]
nodaemon=true

[program:rsyslog]
command=/usr/sbin/rsyslogd -n

[program:redis]
command=/usr/local/bin/redis-server /etc/redis.conf

[program:shell]
command=/usr/bin/cluster.sh

3、redis的dockerfile 

FROM 192.168.0.45:5000/basenojdk
MAINTAINER lstar 442132099@qq.com
ADD redis-3.2.8.tar.gz .
ADD redis-3.3.3.gem ./
RUN groupadd -r redis && useradd -r -g redis redis
RUN apt-get update&& apt-get install -y gcc make tcl rsyslog && \
mv redis-3.2.8 /usr/local/redis && \
cd /usr/local/redis && make && make install && \
mkdir /var/log/redis && mkdir /etc/redis && gem install -l redis-3.3.3.gem && \
cp /usr/local/redis/src/redis-trib.rb /usr/bin && \
rm -rf /var/cache/apt/archives/*
COPY *.conf /etc/
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
EXPOSE 6379
VOLUME [/etc/redis]
ADD cluster.sh /usr/bin/cluster.sh
ENTRYPOINT ["/usr/bin/supervisord"]

 

二、初始化redis集群的脚本redis-init.sh

#!/bin/bash
kubectl create -f /repo/kube-conf/redis-rc.yaml
i=`kubectl get po -o wide|grep redis|awk '{print $1}'`
for a in $i;do
i2="kubectl exec -it $a redis-cli<<EOF cluster nodes; exit; exit;EOF"
echo $i2|bash|tee init-redis
if [ `cat init-redis|wc -l` -ge 6 ];then
exit
fi
done
echo 'Please wait, init cluster for redis........'
sleep 60
kubectl get po -o wide|grep redis|awk '{print $6,$1}'|sort -k2 > redishosts
kubectl get po -o wide|grep redis|awk '{print $1,$6}'|sort -k2 > /opt/redis.ck
kubectl cp redishosts $a:/mnt/redishosts
kubectl exec $a bash /usr/bin/cluster.sh

三、pod重启自动添加节点的脚本redis-check.sh

添加redis-check.sh到crontab中

echo '*/5 * * * *   bash /home/jiang/redis-check.sh'  >> /var/spool/cron/crontabs/root

如果已添加不用重复添加

#/bin/bash
set -e
i=`kubectl get po -o wide|grep redis|grep -v "none"|wc -l`
if [ $i -lt 6 ];then
echo "please check redis pods running"
exit
fi


#the file /opt/redis.ck is exist or not
if [ ! -f /opt/redis.ck ];then
kubectl get po -o wide|grep redis|awk '{print $1,$6}'|sort -k2 > /opt/redis.ck
exit
fi
#the file /opt/redis.ck is empty or not
i=`cat /opt/redis.ck|wc -l`
if [ ! -n $i ];then
kubectl get po -o wide|grep redis|awk '{print $1,$6}'|sort -k2 > /opt/redis.ck
exit
fi

#get the new ip
#a1="kubectl exec -it redis-vsh60 redis-cli<<EOF cluster nodes; exit; exit;EOF"
#echo $a1|bash|tee /tmp/ccredis
#cat /tmp/ccredis|awk '{print $2}'
kubectl get po -o wide|grep redis|awk '{print $1,$6}'|sort -k2 > /opt/redis.newck

#get different ip
n="`diff /opt/redis.newck /opt/redis.ck|grep "^<"|awk '{print $3}'`"
if [ "$n"="" ];then

#get shutdown ips
#get enable ip for redis cluster
m1="`diff /opt/redis.newck /opt/redis.ck|grep "^>"|awk '{print $3}'`"
echo $m1|sed 's? ?|?g' > b.txt
m2=`cat /opt/redis.ck|grep -E -v "cat b.txt"|awk 'NR==1{print $1}'`

#get redis master ip
m3="kubectl exec -it $m2 redis-cli<<EOF cluster nodes; exit; exit;EOF"
echo $m3|bash|tee /tmp/ccredis
mnum=`cat /tmp/ccredis|grep master|grep -v fail|grep -v handshake|wc -l`
mip=`cat /tmp/ccredis|grep master|grep -v fail|grep -v handshake|awk 'NR==1{print $2}'|cut -d ":" -f1`
mpodname=`cat /opt/redis.ck|grep $mip|awk '{print $1}'`

#add new node to exist redis cluster
for a in $n;do
if [ $mnum -lt 3 ];then
echo "/usr/local/redis/src/redis-trib.rb add-node $a:6379 $mip:6379" > /tmp/add.sh
chmod +x /tmp/add.sh
kubectl cp /tmp/add.sh $mpodname:/bin/add.sh
kubectl exec $mpodname bash /bin/add.sh
sleep 15
else
echo "/usr/local/redis/src/redis-trib.rb add-node --slave $a:6379 $mip:6379" > /tmp/add.sh
chmod +x /tmp/add.sh
kubectl cp /tmp/add.sh $mpodname:/bin/add.sh
kubectl exec $mpodname bash /bin/add.sh
sleep 15
fi
rm -f /tmp/add.sh
done

rm -f b.txt
#change the new file to base file
mv /opt/redis.newck /opt/redis.ck
fi

 

四、删除错误的集群主机

 

待测试

posted @ 2023-04-25 15:14  hanwei666  阅读(25)  评论(0编辑  收藏  举报
……