1 min read

容器镜像搬运

容器镜像搬运

背景

正在使用的外部容器镜像仓库告之将不能使用了,需要将集群中运行的容器迁移到内部的容器镜像仓库系统。这时我们就需要做些搬运工作。

实现要点

获取集群所运行的容器镜像

$ kubectl -n $NAMESPACE get po -o jsonpath={..image}|tr -s '[[:space:]]' '\n'|sort -ru > img.list

搬运脚本

#!/usr/bin/env bash
# 文件名: image_migrate.sh
# 功  能: 容器镜像复制迁移

dest_registry=registry.xx.cn                               # 目标镜像仓库
dest_project=prod-fc2-xx-sfqglb-ypt-qzzcrjkfzpt-znyy       # 目标仓库项目

txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgre=${txtbld}$(tput setaf 2) # green
bldylw=${txtbld}$(tput setaf 3) # yellow
txtrst=$(tput sgr0)             # Reset
err=${bldred}ERROR${txtrst}
info=${bldgre}INFO${txtrst}
warn=${bldylw}WARNING${txtrst}

usage()
{
    cat << EOF
用法: $(basename $0) 文件名
EOF
}

if [ -z $1 ]; then
    printf "\n$err : 需要传入镜像文件参数\n"
    usage
    exit 2
fi

while read image
do
    printf "\n$info: [ ${image##*/} ] 镜像处理中...\n"
    /usr/local/bin/skopeo copy --insecure-policy docker://${image} docker://${dest_registry}/${dest_project}/${image##*/}
done < $1

正式搬运

# ./image_migrate.sh img.list

更新控制器中的镜像,示例:

$ kubectl get deploy XX -oyaml | sed  's@旧容器镜像镜像@新容器镜像镜像@' | kubectl apply -f -