定时清理镜像
方式一 - crictl(弃用):
cat > /etc/cron.hourly/clean-unuse-images.sh << 'EOF'
#!/usr/bin/env bash
main () {
CRICTL=$(which crictl)
if [[ -z "$CRICTL" ]]; then
echo "crictl not found. Please install it and ensure it is in the PATH." >&2
exit 1
fi
($CRICTL img | grep "<none>") && ($CRICTL img | grep "<none>" | xargs $CRICTL rmi)
images=( $($CRICTL img | grep "dev.flyrise.cn" | awk '{print $3}') )
pods=( $($CRICTL ps | grep -v IMAGE | awk '{print $2}') )
for item in "${images[@]}"
do
if [[ ! " ${pods[@]} " =~ " $item " ]]; then
$CRICTL rmi $item
fi
done
}
main 2>&1 | tee /var/log/clean-unuse-images.log
EOF
# 必须添加执行权限,否则不会执行
chmod +x /etc/cron.hourly/clean-unuse-images.sh
方式二 - crictl(推荐):
cat > /etc/cron.hourly/clean-unuse-images.sh << 'EOF'
#!/usr/bin/env bash
main() {
CRICTL=$(which crictl)
if [[ -z "$CRICTL" ]]; then
echo "crictl not found. Please install it and ensure it is in the PATH." >&2
exit 1
fi
# 删除悬空镜像
$CRICTL images | grep dev.flyrise | grep "<none>" | awk '{print $3}' | xargs $CRICTL rmi
# 获取所有 dev.flyrise.cn 镜像
filtered_images=$($CRICTL images | grep "dev.flyrise.cn" | awk '{print $1":"$2}')
# 遍历镜像
for image in $filtered_images; do
running=$($CRICTL ps -a --image "$image" --quiet)
if [[ -n $running ]]; then
echo "已保留镜像: $image"
else
$CRICTL rmi "$image"
echo "已删除镜像: $image"
fi
done
}
main 2>&1 | tee -a /var/log/clean-unuse-images.log
EOF
# 必须添加执行权限,否则不会执行
chmod +x /etc/cron.hourly/clean-unuse-images.sh
方式三 - docker:
cat > /etc/cron.hourly/clean-unuse-images.sh << 'EOF'
#!/usr/bin/env bash
# 获取 docker 的实际路径
DOCKER=$(which docker)
if [[ -z "$DOCKER" ]]; then
echo "Docker not found in PATH. Please ensure Docker is installed and accessible." >&2
exit 1
fi
main() {
# 清理未使用的悬空镜像(<none>)
($DOCKER images | grep "<none>") && ($DOCKER images | grep "<none>" | xargs $DOCKER rmi)
# 获取 dev.flyrise.cn 相关镜像和运行的容器
images=($($DOCKER images | grep "dev.flyrise.cn" | awk '{print $3}'))
#pods=( $($DOCKER ps | grep -v IMAGE | awk '{print $1}') )
pods=($($DOCKER ps --no-trunc | awk 'NR>1 {print $2}'))
# 遍历镜像,清理未被使用的镜像
for image in "${images[@]}"; do
if [[ ! " ${pods[*]} " =~ " $image " ]]; then
echo "Removing unused image: $image"
$DOCKER rmi "$image"
else
echo "Image $image is in use by a container."
fi
done
}
main 2>&1 | tee -a /var/log/clean-unuse-images.log
EOF
# 必须添加执行权限,否则不会执行
chmod +x /etc/cron.hourly/clean-unuse-images.sh
清理docker log
clean_docker_log.sh
#!/bin/bash
echo "开始清理docker日志"
# 容器的路径 默认的就是下面的,如果是自定义的docker的路径就改自己定的路径
CONTAINER_DIR=/var/lib/docker/containers
# 获取所有的容器日志文件
CONTAINER_LOGS=$(find $CONTAINER_DIR -name "*-json.log")
# 清理每个容器的日志
for LOG in $CONTAINER_LOGS; do
LOG_FILE=${LOG##*/}
echo "容器的ID:${LOG_FILE/-json.log/} 清理的日志文件:$LOG_FILE"
cat /dev/null >"$LOG"
# 也可以使用-> echo "" >"$LOG"
done
执行
sh clean_docker_log.sh
备份并清理过期的镜像
backup-useless-images.sh
#!/bin/bash
# 设定命名空间和保存目录
NAMESPACE="pai-cloud"
# 确保保存目录存在
SAVE_DIR="/data/backup/useless-images"
mkdir -p $SAVE_DIR
# 优先使用 docker,如果 docker 不存在,则使用 crictl
if command -v docker > /dev/null 2>&1; then
IMAGE_CMD="docker"
else
IMAGE_CMD="crictl"
fi
$IMAGE_CMD image prune -f
# 获取所有正在使用的deployment镜像名称和版本
used_images=$(kubectl -n $NAMESPACE get deployments -o=jsonpath='{range .items[*]}{range .spec.template.spec.containers[*]}{.image}{"\n"}{end}{end}')
# 遍历所有镜像
for image in $used_images; do
# 提取镜像名称和版本 (忽略仓库地址,默认版本使用latest)
image_base=$(echo $image | awk -F'/' '{print $NF}') # 只保留最后的镜像名称和版本部分
image_name=$(echo $image_base | cut -d ':' -f 1)
image_version=$(echo $image_base | cut -d ':' -f 2)
if [ -z "$image_version" ]; then
image_version="latest"
fi
# 使用 crictl 找到本地所有同名的镜像
local_images=$($IMAGE_CMD images | grep -E "/$image_name(\s)" | awk '{print $1 ":" $2}')
for local_image in $local_images; do
if [ "$local_image" != "$image" ]; then
local_image_base=$(echo $local_image | awk -F'/' '{print $NF}') # 只保留最后的镜像名称和版本部分
# 如果本地镜像的版本不是当前正在使用的版本,导出它
local_image_name=$(echo $local_image_base | cut -d ':' -f 1)
local_image_version=$(echo $local_image_base | cut -d ':' -f 2)
tar_name="${local_image_name}-${local_image_version}.tar"
echo "导出镜像: $local_image 到 $SAVE_DIR/$tar_name"
ctr images export $SAVE_DIR/$tar_name $local_image
if [ $? -eq 0 ]; then
# 导出成功后删除该镜像
echo "导出成功后删除镜像: $local_image"
$IMAGE_CMD rmi $local_image
else
echo "导出失败: $local_image"
fi
fi
done
done
文档更新时间: 2024-12-19 09:02 作者:姚连洲