需要在master节点执行

ns为命名空间(按需修改)

获取镜像的命令需要根据实际情况调整

image=`kubectl get pod $p -n $ns -o yaml|grep "image:"|grep -v "{"|head -1|awk '{print $2}'` 

#!/bin/sh
 
# 报错停止运行
#set -e
 
function info(){
    DATE_N=`date "+%Y-%m-%d %H:%M:%S.%N" | cut -b 1-23`
    echo -e "$DATE_N|INFO|$@ "
}
 
function warning(){
    DATE_N=`date "+%Y-%m-%d %H:%M:%S.%N" | cut -b 1-23`
    echo -e "\033[33m$DATE_N|WARINIG|$@ \033[0m"
}
 
 
function success(){
    DATE_N=`date "+%Y-%m-%d %H:%M:%S.%N" | cut -b 1-23`
    echo -e "\033[32m$DATE_N|SUCCESS|$@ \033[0m"
}
 
function error(){
    DATE_N=`date "+%Y-%m-%d %H:%M:%S.%N" | cut -b 1-23`
    echo -e "\033[31m$DATE_N|ERROR|$@ \033[0m"
}

# 命名空间(需要配置)
ns="test"

pods=`kubectl get pod -n $ns  |grep -v NAME|awk '{print $1}'`
cat /dev/null > images_name
i=1
for p in ${pods[*]}
do
	image=`kubectl get pod $p -n $ns -o yaml|grep "image:"|grep -v "{"|head -1|awk '{print $2}'`
	docker pull $image
	f="$ns_$i.tar"
	if [ ! -f "$f" ];then
		docker save -o $f $image
		if [ $? -eq 0 ]; then
			success "保存 【$p】的镜像【$image】为【$f】成功"
		else
			image=`kubectl get pod $p -n $ns -o yaml|grep "image:"|grep -v "{"|head -1|awk '{print $3}'`
			docker pull $image
			docker save -o $f $image
			if [ $? -eq 0 ]; then
				success "保存 【$p】的镜像【$image】为【$f】成功"
			else
				error "保存 【$p】的镜像【$image】为【$f】失败"
			fi
		fi
		echo "$f ===> $p ===> $image" >> images_name
	else
		warning "【$f】文件已存在"
	fi
	i=$((i+1))
done


Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐