Rancher 离线安装

当需要安装Rancher的主机只有内网,而没有外网通信的时候,Rancher 的安装就成为一大难题,这里记录下Rancher离线安装的步骤,让你的主机没有互联网也能安装Rancher。
1、首先找到 Rancher 相应版本 的 rancher-images.txt;
https://github.com/rancher/rancher/releases
【Rancher 离线安装】2、通过RKE生成镜像清单
./rke config --system-images -all >> ./rancher-images.txt
rke链接:https://pan.baidu.com/s/18uP0FREHXiAkw1CsX33PNw提取码:2ih2
3、解析模板以获取镜像详细信息
./linux-amd64/helm template ./cert-manager-v0.6.7.tgz | grep -oP '(?<=image: ").*(?=")' >> ./rancher-images.txt
helm 链接:https://pan.baidu.com/s/1r1TBpTlTdOTUrwF2t8I2Gw提取码:0is3
cert-manager 链接:https://pan.baidu.com/s/1w7QLBWI438js6M_GrPm9vQ提取码:65z6

4、对镜像列表进行排序和去重,以去除重复的镜像
sort -u rancher-images.txt -o rancher-images.txt
5、复制脚本文件
rancher-save-images.sh

#!/bin/bash # 定义日志 workdir='../pwd' log_file=${workdir}/sync_images_$(date +"%Y-%m-%d").loglogger() { log=$1 cur_time='['$(date +"%Y-%m-%d %H:%M:%S")']' echo ${cur_time} ${log} | tee -a ${log_file} }list="../rancher-images.txt" #images="rancher-images.tar.gz"POSITIONAL=() while [[ $# -gt 0 ]]; do key="$1" case $key in -i|--images) images="$2" shift # past argument shift # past value ; ; -l|--image-list) list="$2" shift # past argument shift # past value ; ; -h|--help) help="true" shift ; ; esac doneusage () { echo "USAGE: $0 [--image-list rancher-images.txt] [--images rancher-images.tar.gz]" echo "[-l|--images-list path] text file with list of images. 1 per line." echo "[-l|--images path] tar.gz generated by docker save." echo "[-h|--help] Usage message" }if [[ $help ]]; then usage exit 0 fiset -e -xmkdir -p rancher-images-$(date +"%Y-%m-%d") cd rancher-images-$(date +"%Y-%m-%d")for i in $(cat ${list}); do docker pull ${i}if [ $? -ne 0 ]; then logger "${i} pull failed." else logger "${i} pull successfully." fidocker save ${i} | gzip > $(echo $i | sed "s#/#-#g; s#:#-#g").tgzif [ $? -ne 0 ]; then logger "${i} save failed." else logger "${i} save successfully." fi done

rancher-load-images.sh
#!/bin/bash# 定义日志 workdir='../pwd' log_file=${workdir}/sync_images_$(date +"%Y-%m-%d").loglogger() { log=$1 cur_time='['$(date +"%Y-%m-%d %H:%M:%S")']' echo ${cur_time} ${log} | tee -a ${log_file} }POSITIONAL=() while [[ $# -gt 0 ]]; do key="$1" case $key in -i|--images-path) images_path="$2" shift # past argument shift # past value ; ; -l|--image-list) list="$2" shift # past argument shift # past value ; ; -h|--help) help="true" shift ; ; esac doneusage () { echo "USAGE: $0 [--image-list rancher-images.txt] [--images rancher-images.tar.gz]" echo "[-l|--images-list path] text file with list of images. 1 per line." echo "[-l|--images path] tar.gz generated by docker save." echo "[-h|--help] Usage message" }if [[ $help ]]; then usage exit 0 fiset -e -x# 镜像压缩文件列表 images=$(ls $images_path | grep ".tgz") cd $images_path# 导入镜像 docker_load () { for imgs in $(echo ${images}); do gunzip -c ${imgs} | docker loadif [ $? -ne 0 ]; then logger "${imgs} load failed." else logger "${imgs} load successfully." fi done }docker_load

rancher-push-images.sh
#!/bin/bash## 镜像上传说明 # 需要先在镜像仓库中创建 rancher 项目 # 根据实际情况更改以下私有仓库地址# 定义日志 workdir='../pwd' log_file=${workdir}/sync_images_$(date +"%Y-%m-%d").loglogger() { log=$1 cur_time='['$(date +"%Y-%m-%d %H:%M:%S")']' echo ${cur_time} ${log} | tee -a ${log_file} }images_hub() {while true; do read -p "输入镜像仓库地址(不加http/https): " registry read -p "输入镜像仓库用户名: " registry_user read -p "输入镜像仓库用户密码: " registry_password echo "您设置的仓库地址为: ${registry},用户名: ${registry_user},密码: xxx" read -p "是否确认(Y/N): " confirmif [ $confirm != Y ] && [ $confirm != y ] && [ $confirm == '' ]; then echo "输入不能为空,重新输入" else break fi done }images_hubecho "镜像仓库 $(docker login -u ${registry_user} -p ${registry_password} ${registry})"images=$(docker images -a | grep -v TAG | awk '{print $1 ":" $2}') namespace=rancherdocker_push() { for imgs in $(echo ${images}); do n=$(echo ${imgs} | awk -F"/" '{print NF-1}') #如果镜像名中没有/,那么此镜像一定是library仓库的镜像; if [ ${n} -eq 0 ]; then img_tag=${imgs} #namespace=rancher #重命名镜像 docker tag ${imgs} ${registry}/${namespace}/${img_tag} #删除原始镜像 #docker rmi ${imgs} #上传镜像 docker push ${registry}/${namespace}/${img_tag} #如果镜像名中有一个/,那么/左侧为项目名,右侧为镜像名和tag elif [ ${n} -eq 1 ]; then img_tag=$(echo ${imgs} | awk -F"/" '{print $2}') #namespace=$(echo ${imgs} | awk -F"/" '{print $1}') #重命名镜像 docker tag ${imgs} ${registry}/${namespace}/${img_tag} #删除旧镜像 #docker rmi ${imgs} #上传镜像 docker push ${registry}/${namespace}/${img_tag} #如果镜像名中有两个/, elif [ ${n} -eq 2 ]; then img_tag=$(echo ${imgs} | awk -F"/" '{print $3}') #namespace=$(echo ${imgs} | awk -F"/" '{print $2}') #重命名镜像 docker tag ${imgs} ${registry}/${namespace}/${img_tag} #删除旧镜像 #docker rmi ${imgs} #上传镜像 docker push ${registry}/${namespace}/${img_tag} else #标准镜像为四层结构,即:仓库地址/项目名/镜像名:tag,如不符合此标准,即为非有效镜像。 echo "No available images" fi done }docker_push

6、rancher-save-images.sh 赋权限并执行备份镜像
chmod +x rancher-save-images.sh./rancher-save-images.sh

7、离线安装Rancher镜像文件,rancher-load-images.sh
chmod +x rancher-load-images.sh./rancher-load-images.sh --images-path rancher-images-2019-06-07/

8、此外,还可以将镜像上传至私有镜像仓库
chmod +x rancher-push-images.sh ./rancher-push-images.sh


    推荐阅读