镜像同步

指定文本中镜像同步

给定一个镜像列表 images-list.txt,其格式如下:

kubesphere/kube-apiserver:v1.20.6
kubesphere/kube-scheduler:v1.20.6
kubesphere/kube-proxy:v1.20.6
kubesphere/kube-controller-manager:v1.20.6
kubesphere/kube-apiserver:v1.19.8

同步的 shell 脚本 skopeo-copy.sh:

#!/bin/bash

GREEN_COL="\\033[32;1m"
RED_COL="\\033[1;31m"
NORMAL_COL="\\033[0;39m"

BASE_PATH=$(cd "$(dirname $0)" && pwd)

SOURCE_REGISTRY=$1
TARGET_REGISTRY=$2

# shell 变量赋值
# 当没有从命令行中传递值给 SOURCE_REGISTRY 和 TARGET_REGISTRY 变量时,便采用下述值进行覆盖
: "${IMAGES_LIST_FILE:="${BASE_PATH}/images-list.txt"}"
: "${SOURCE_REGISTRY:="docker.io"}"
: "${TARGET_REGISTRY:="192.168.31.100:5000"}"

set -eo pipefail

CURRENT_NUM=0
ALL_IMAGES=$(sed -n '/#/d;s/:/:/p' "${IMAGES_LIST_FILE}" | sort -u)
TOTAL_NUMS=$(wc -l  < "${IMAGES_LIST_FILE}" )

# skopeo 拷贝函数
skopeo_copy() {
    if skopeo copy \
        --insecure-policy --src-tls-verify=false --dest-tls-verify=false \
        --override-arch amd64 --override-os linux \
        -q docker://$1 docker://$2; then
        echo -e "$GREEN_COL Progress: ${CURRENT_NUM}/${TOTAL_NUMS} sync $1 to $2 successful $NORMAL_COL"
    else
        echo -e "$RED_COL Progress: ${CURRENT_NUM}/${TOTAL_NUMS} sync $1 to $2 failed $NORMAL_COL"
        exit 2
    fi
}

# 调用拷贝函数并记录当前执行序号
for image in ${ALL_IMAGES}; do
    (( CURRENT_NUM="${CURRENT_NUM}"+1 ))
    skopeo_copy "${SOURCE_REGISTRY}/${image}" "${TARGET_REGISTRY}/${image}"
done

执行命令和结果:

> bash skopeo-copy.sh

  Progress: 1/5 sync docker.io/kubesphere/kube-apiserver:v1.19.8 to 192.168.31.100:5000/kubesphere/kube-apiserver:v1.19.8 successful 
  Progress: 2/5 sync docker.io/kubesphere/kube-apiserver:v1.20.6 to 192.168.31.100:5000/kubesphere/kube-apiserver:v1.20.6 successful 
  Progress: 3/5 sync docker.io/kubesphere/kube-controller-manager:v1.20.6 to 192.168.31.100:5000/kubesphere/kube-controller-manager:v1.20.6 successful 
  Progress: 4/5 sync docker.io/kubesphere/kube-proxy:v1.20.6 to 192.168.31.100:5000/kubesphere/kube-proxy:v1.20.6 successful 
  Progress: 5/5 sync docker.io/kubesphere/kube-scheduler:v1.20.6 to 192.168.31.100:5000/kubesphere/kube-scheduler:v1.20.6 successful