k8s 镜像下载并上传
该脚本用于将多个版本的k8s官方的镜像及二进制文件下载到本地,并将镜像上传到自己的docker仓库中去,已经push成功的,会在当前目录下创建pusded.txt文件作为记录。该脚本默认会遍历k8s仓库的releases文件中的所有版本,如果想要下载指定版本的,只需要手动修改main函数中的releases变量值。如果你想要push镜像到其他镜像仓库,只需要修改imagename_tag变量即可。
自行修改变量docker_user为自己的docker用户名后,直接运行脚本即可(需要保障运行脚本的机器能够访问k8s镜像仓库,并且已经登陆了docker仓库)
bash upload_k8s_images_to_docker.sh
#!/bin/bash
# 请修改为自己在hub.docker.com中的用户名
docker_user="xwjh"
# set -x
function logger() {
TIMESTAMP=$(date +'%Y-%m-%d %H:%M:%S')
case "$1" in
debug)
echo -e "$TIMESTAMP \033[36mDEBUG\033[0m $2"
;;
info)
echo -e "$TIMESTAMP \033[32mINFO\033[0m $2"
;;
warn)
echo -e "$TIMESTAMP \033[33mWARN\033[0m $2"
;;
error)
echo -e "$TIMESTAMP \033[31mERROR\033[0m $2"
;;
*) ;;
esac
}
download_images() {
chmod +x kubeadm
images=($(./kubeadm config images list --kubernetes-version=${kubernetes_version}))
logger info "images: ${images}"
pusded=$(cat ../pusded.txt)
for imagename in ${images[@]}; do
if [[ ! "${pusded[*]}" =~ ${imagename} ]]; then
docker pull $imagename || {
logger error "pull 镜像 $imagename 失败!"
exit 1
}
imagename_tag=${docker_user}/$(echo $imagename | awk -F '/' '{print $NF}')
docker tag $imagename $imagename_tag
docker push $imagename_tag || {
logger error "上传镜像 $imagename_tag 失败!"
exit 1
}
echo $imagename >>../pusded.txt
docker rmi -f $imagename_tag
fi
done
}
download_k8s() {
file_name="kubeadm"
if [[ -f "${file_name}" && -f "${file_name}.sha256" ]]; then
echo "$(cat ${file_name}.sha256) ${file_name}" | sha256sum --check >/dev/null 2>&1 || {
logger error "${file_name} sha256sum failed."
exit 1
}
else
rm -f "${file_name}"{,.sha256}
wget -nv https://storage.googleapis.com/kubernetes-release/release/${kubernetes_version}/bin/linux/amd64/"${file_name}"{,.sha256}
fi
file_name="kubelet"
if [[ -f "${file_name}" && -f "${file_name}.sha256" ]]; then
echo "$(cat ${file_name}.sha256) ${file_name}" | sha256sum --check >/dev/null 2>&1 || {
logger error "${file_name} sha256sum failed."
exit 1
}
else
rm -f "${file_name}"{,.sha256}
wget -nv https://storage.googleapis.com/kubernetes-release/release/${kubernetes_version}/bin/linux/amd64/"${file_name}"{,.sha256}
fi
file_name="kubectl"
if [[ -f "${file_name}" && -f "${file_name}.sha256" ]]; then
echo "$(cat ${file_name}.sha256) ${file_name}" | sha256sum --check >/dev/null 2>&1 || {
logger error "${file_name} sha256sum failed."
exit 1
}
else
rm -f "${file_name}"{,.sha256}
wget -nv https://storage.googleapis.com/kubernetes-release/release/${kubernetes_version}/bin/linux/amd64/"${file_name}"{,.sha256}
fi
if [ -f /etc/debian_version ]; then
if [ ! -f kubelet.service ]; then
wget -nv "https://raw.githubusercontent.com/kubernetes/release/${kubernetes_release_version}/cmd/kubepkg/templates/latest/deb/kubelet/lib/systemd/system/kubelet.service"
fi
if [ ! -f 10-kubeadm.conf ]; then
wget -nv "https://raw.githubusercontent.com/kubernetes/release/${kubernetes_release_version}/cmd/kubepkg/templates/latest/deb/kubeadm/10-kubeadm.conf"
fi
elif [ -f /etc/redhat-release ]; then
if [ ! -f kubelet.service ]; then
wget -nv "https://raw.githubusercontent.com/kubernetes/release/${kubernetes_release_version}/cmd/kubepkg/templates/latest/rpm/kubelet/kubelet.service"
fi
if [ ! -f 10-kubeadm.conf ]; then
wget -nv "https://raw.githubusercontent.com/kubernetes/release/${kubernetes_release_version}/cmd/kubepkg/templates/latest/rpm/kubeadm/10-kubeadm.conf"
fi
else
echo "ERROR: no deb or rpm."
exit 1
fi
}
main() {
logger info "开始..."
which curl >/dev/null 2>&1 || yum install curl || {
logger error "安装 curl 失败!"
exit 1
}
jq --version >/dev/null 2>&1
if [[ $? != 0 ]]; then
jq_tag="$(curl -sSL https://api.github.com/repos/stedolan/jq/releases/latest | grep ' "tag_name": ' | awk -F'"' '{print $4}' || exit 1)" &&
curl --retry 5 -sSL "https://github.com/stedolan/jq/releases/download/${jq_tag}/jq-linux64" -o /usr/bin/jq && chmod +x /usr/bin/jq || {
logger error "安装 jq 失败!"
exit 1
}
fi
rm -rf releases
logger info "获取最新的releases文件(https://api.github.com/repos/kubernetes/kubernetes/releases)"
wget -nv https://api.github.com/repos/kubernetes/kubernetes/releases || {
logger error "获取releases失败!"
exit 1
}
releases=($(cat releases | jq -r '.[].tag_name' | grep -Pv 'alpha|beta|rc'))
# logger info "releases: ${releases}"
kubernetes_release_version="$(curl --retry 5 -sSL https://api.github.com/repos/kubernetes/release/releases/latest | jq -r '.tag_name' || exit 1)"
logger info "kubernetes_release_version: ${kubernetes_release_version}"
if [ ! -f pusded.txt ]; then
touch pusded.txt
fi
for version in ${releases[@]}; do
kubernetes_version=$version
logger info "kubernetes_version: ${kubernetes_version}"
mkdir -p $kubernetes_version
logger info "开始下载..."
cd $kubernetes_version && download_k8s
download_images
cd ..
done
}
main