现行K8s中Etcd集群运行状况;
status-etcd.sh
#!/bin/bash
bash_path=$(cd $(dirname $0); pwd)
theday=`date +%F`
if [ ! -d "${rundir}/log" ]; then
mkdir ${rundir}/log
fi
rm -rf $bash_path/log/etcd-check-${theday}.txt >> /dev/null 2>&1
endpoint=`kubectl get pod -n kube-system -o wide \
|awk '$1~"etcd"&&$3=="Running"{print $6":2379"}' \
|awk BEGIN{RS=EOF}'{gsub(/\n/,",");print}'`
#endpoint_1="https://10.149.23.13:2379"
#endpoint_2="https://10.149.23.13:2379"
#endpoint_3="https://10.149.23.13:2379"
CL_0="ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt \
--key=/etc/kubernetes/pki/etcd/healthcheck-client.key \
--endpoints=\"${endpoint}\""
CL_1="endpoint status --write-out=table"
CL_2="member list --write-out=table"
CL_3="get /registry/deployments/iotplat/ --prefix --keys-only"
Outtxt=">> /home/etcd-check-${theday}.txt"
CMDLine="${CL_0} ${CL_1} ${Outtxt} && ${CL_0} ${CL_2} ${Outtxt} && ${CL_0} ${CL_3} ${Outtxt}"
#echo "$CMDLine"
docker run -it --rm -v ${bash_path}/log:/home -v /etc/kubernetes/pki/etcd:/etc/kubernetes/pki/etcd k8s.gcr.io/etcd:3.3.10 /bin/sh -c "${CMDLine}"
sed -i '/^$/d' $bash_path/log/etcd-check-${theday}.txt
echo -e "\n=======${theday} 当前集群Etcd数据库检查=======\n" >> $bash_path/log/etcd-check.txt
cat $bash_path/log/etcd-check-${theday}.txt >> $bash_path/log/etcd-check.txt
tail -n 36 $bash_path/log/etcd-check.txt
运行
验证Etcd数据;
etcd-check.sh
#!/bin/bash
rundir=$(cd $(dirname $0); pwd)
theday=`date +%F`
echo -e "=======${theday} Etcd备份验证======="
#清除上次验证数据
dbtmpdir="${rundir}/dbtmp/etcd"
rm -rf ${dbtmpdir} && mkdir ${dbtmpdir} -p
#如果没有参数则搜索当前运行目录下的
if [ "$1" ];then
baktar=$1
else
baktar=$(find ${rundir} -name "*snapshot*.db" | sort -rn | head -1)
fi
if [ ! -e "${baktar}" ];then
echo "没有找到备份snapshot*.db!!!"
exit 0
fi
echo -e "1.Etcd备份文件是:\n ${baktar}"
echo -e "2.还原数据库的临时目录:\n ${dbtmpdir}"
echo -e "3.开始还原数据库"
cp -f ${baktar} ${dbtmpdir}/snapshot.db
docker run -it --rm -v ${dbtmpdir}:/home k8s.gcr.io/etcd:3.3.10 /bin/sh \
-c 'ETCDCTL_API=3 etcdctl snapshot restore /home/snapshot.db --data-dir=/home/db' \
>> /dev/null 2>&1
echo -e " 还原的数据库:${dbtmpdir}/db"
echo -e "\n======================================" >> ${rundir}/log/etcd-check.txt
echo -e "=======${theday} Etcd备份验证=======" >> ${rundir}/log/etcd-check.txt
echo -e "======================================" >> ${rundir}/log/etcd-check.txt
echo -e "备份文件为:${baktar}" >> ${rundir}/log/etcd-check.txt
echo -e "输出/registry/deployments/iotplat/下的所有key:" >> ${rundir}/log/etcd-check.txt
echo -e "4.检验Etcd数据"
docker run -it --name etcd-check -v ${dbtmpdir}/db:/var/lib/etcd -d k8s.gcr.io/etcd:3.3.10 /bin/sh \
-c 'etcd --data-dir=/var/lib/etcd' \
>> /dev/null 2>&1
docker exec -it etcd-check /bin/sh \
-c 'ETCDCTL_API=3 etcdctl get /registry/deployments/iotplat/ --prefix --keys-only' \
>> ${rundir}/log/etcd-check.txt
docker stop etcd-check >> /dev/null 2>&1
docker rm etcd-check >> /dev/null 2>&1
sed -i 's/
//g' ${rundir}/log/etcd-check.txt
sed -i '/^$/d' ${rundir}/log/etcd-check.txt
#cat ${rundir}/log/etcd-check.txt | tr -s "\n" > ${rundir}/log/etcd-check.txt
tail -n 24 ${rundir}/log/etcd-check.txt
echo -e "验证结果输出:${rundir}/log/etcd-check.txt"
运行