采集容器日志的shell脚本内容为:
点击查看代码
#!/bin/bash
export LANG=zh_CN.gb18030
. ~/.bash_profile
#日志放置目录
log_path=/aa/bb/cc/dd/ee
todaydate=$(date +%Y%m%d)
nowdate=$(date +%Y%m%d%H%M)
#pod列表
dube_pod_id='xx1-service xx2-service xx3-service xx4-service xx5-service'
#日志保留天数
reverseDay=7
#文件名
fileName=$(basename "${BASH_SOURCE[0]}")
#初始判断
init(){
#清理缓存
echo 3 > /proc/sys/vm/drop_caches
#备份目录不存在,创建备份目录
[ -d "${log_path}" ] || {
mkdir -p ${log_path} &> /dev/null
#创建备份目录失败,退出
[ $? -ne 0 ] && {
echo "创建日志目录 ${log_path}失败,退出..."
exit 222
}
}
}
#清理历史日志文件
cleaOldLog(){
for fileName in $(find ${log_path} -type f -mtime ${reverseDay});do
rm -f ${fileName}
done
}
#抓取日志
function fetchLog(){
for pod_id in ${dube_pod_id[*]}; do
# 对大于50M的文件进行分片
find ${log_path}/ -size +50M -name $pod_id==${todaydate}.log | xargs -i cp {} ${log_path}/$pod_id==${nowdate}.log && echo > ${log_path}/$pod_id==${todaydate}.log
#查看是否处于运行状态。如果是运行状态
docker ps | grep ${pod_id} > /dev/null 2>& 1
#如果运行状态,查看是否正在抓取日志
if [ $? -eq 0 ];then
#判断是否已经后台在抓取对应服务的日志。如果没抓取则抓取
ps -ef|grep -e cronolog |grep $pod_id > /dev/null 2>& 1
if [ $? -ne 0 ];then
nohup docker logs -f $pod_id |/usr/local/sbin/cronolog ${log_path}/$pod_id==%Y%m%d.log 2>&1&
fi
fi
done
}
#主函数
main(){
#判断是否已经运行该脚本。如果已经运行则不运行初始化和抓取工作,只是清理历史日志文件。方便配置定时任务
running_count=$(ps -ef |grep ${fileName} | grep -v "grep" | wc -l )
if [ ${running_count} -le 2 ];then
init
fetchLog
fi
#清理历史日志文件
cleaOldLog
}
main
对容器镜像进行清理的脚本未:
点击查看代码
#!/bin/sh
# 所有的基础镜像暂时不动: tomcat7.0.92:jdk8 tomcat:9.0.68 nginx:1.23
messages=/var/log/messages
month=$(tail -n 1 $messages | awk '{print $1}')
day=$(tail -n 1 $messages | awk '{print $2}')
time=$(date +"%H:%M:%S")
hostname=$(hostname)
needdelimg='xx1-service xx2-service xx3-service'
# delete标记为空: docker images -f dangling=true
function DELALLNOTAG() {
dlnum=$(docker images -f dangling=true | grep -vE 'REPOSITORY|11fc437dbfa2'|wc -l)
if [ $dlnum -gt 0 ]; then
docker images -f dangling=true|grep -vE 'REPOSITORY|11fc437dbfa2'|awk '{print $3}'|xargs docker rmi
if [ $? == 0 ]; then
echo "$month $day $time $hostname docker-rmi-images: dangling=true succeed" >> $messages
else
echo "$month $day $time $hostname docker-rmi-images: dangling=true failed" >> $messages
fi
else
echo "$month $day $time $hostname docker-rmi-images: dangling=true no images" >> $messages
fi
}
# 系统类容器镜像进行删除的方法
function DELETEIMG() {
# eg: $1 == sproc-service
cname=$1
imgnum=0
# 判断是否传入参数
[ -e $cname ] && exit 111
# 运行容器的镜像信息
rimg=$(docker inspect $cname|grep -i image |grep -i $cname|awk '{print $2}'|sed 's#"##g'|sed 's#,##g' )
imgnum=$(docker images xx1-service -f "before=$rimg"|grep -vE 'hours ago|2 days ago|REPOSITORY'|wc -l)
if [ $imgnum -gt 5 ]; then
docker images xx2-service -f "before=$rimg"|grep -vE 'hours ago|2 days ago|REPOSITORY'|awk '{print $3}'|xargs docker rmi
if [ $? == 0 ]; then
echo "$month $day $time $hostname docker-rmi-images: $cname succeed" >> $messages
else
echo "$month $day $time $hostname docker-rmi-images: $cname failed" >> $messages
fi
else
echo "$month $day $time $hostname docker-rmi-images: $cname imagenum=$imgnum" >> $messages
fi
}
# 执行删除镜像
function main() {
# delete all no tag images
DELALLNOTAG
# delete old images
for i in $needdelimg; do
DELETEIMG $i
done
# remove all unused volumes
# docker volume prune
}
main