├── .gitignore ├── LICENSE ├── README.md ├── dispatch.sh ├── images-abc.txt ├── pull.sh └── target-hosts.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-image-loader 2 | 3 | docker-image-loader 可以将一系列指定的 docker 镜像(外网环境)加载到一系列指定的目标(通常是内网环境)机器上。 4 | 5 | > * docker-image-loader 被大量使用在 K8S 管理工具 [kuboard](https://kuboard.cn) 的内网安装场景下; 6 | > * 可以完成类似任务的有 [sealos](https://github.com/fanux/sealos) 。(Sealos 是一款非常优秀的 Kubernetes 离线安装工具)。 7 | 8 | docker-image-loader 项目假设您有一台机器 A 能够访问外网,有另外一台机器 B 能够访问所有的目标机器(通常是内网环境),且您有办法从机器 A 传输文件到机器 B 上(或者机器 A、机器 B 是同一台机器)。 9 | 10 | 前提条件: 11 | * 机器 A 以及所有的目标机器都安装了 docker 12 | 13 | docker-image-loader 工作过程: 14 | 1. 在机器 A 上执行 `./pull.sh imagets-abc.txt` 以下载 `imagets-abc.txt` 文件中所指定的 docker image,镜像将被保存到 `imagets-abc.tar.gz` 文件中; 15 | 2. 将 `./dispatch.sh` 文件和 `imagets-abc.tar.gz` 文件复制到机器 B; 16 | 3. 在机器 B 上执行 `./dispatch.sh imagets-abc.tar.gz target-hosts.txt`,将前面步骤中下载到的镜像分发到 `target-hosts.txt` 文件中所定义的所有目标机器上。 17 | 18 | 具体操作过程描述如下: 19 | 20 | ## 准备工作 21 | 22 | ### 下载 docker-image-loader 23 | 24 | * 在机器 A 上创建一个临时目录,并切换到该目录 25 | 26 | ```sh 27 | mkdir docker-image-loader 28 | cd docker-image-loader 29 | ``` 30 | 31 | * 下载 docker-image-loader 32 | 33 | ```sh 34 | wget https://addons.kuboard.cn/downloads/docker-image-loader/dispatch.sh 35 | wget https://addons.kuboard.cn/downloads/docker-image-loader/pull.sh 36 | chmod +x dispatch.sh 37 | chmod +x pull.sh 38 | ``` 39 | 40 | ### 设置机器 B 可以无密码 ssh 访问所有目标机器 41 | 42 | > 如果您的机器 A 可以同时访问外网和内网,则 A 和 B 可以是同一台机器 43 | 44 | 在机器 B 上执行 45 | 46 | * 生成 key 文件,如果您已经有 ssh key 文件,无需再次生成 47 | 48 | ```sh 49 | ssh-keygen -t rsa 50 | ``` 51 | 52 | * 设置无密码访问 53 | 54 | ```sh 55 | # 将 ** 替换为目标主机的 IP 地址 56 | # 针对每个目标主机都要执行一次,如此,机器 B 可以无密码 ssh 访问所有目标机器 57 | ssh-copy-id -f -i ~/.ssh/id_rsa.pub root@** 58 | ``` 59 | 60 | ## 在机器 A 上下载镜像 61 | 62 | 63 | * 创建 `imagets-abc.txt` 文件,内容如下所示: 64 | 65 | 文件中的每一行代表一个镜像(需包含 TAG) 66 | > * 文件名称可以自定义 67 | > * 文件内容请修改为您自己期望的任意数量的镜像 68 | ``` 69 | quay.io/k8scsi/csi-provisioner:v1.6.0 70 | quay.io/k8scsi/csi-resizer:v0.5.0 71 | quay.io/k8scsi/csi-snapshotter:v2.1.1 72 | quay.io/k8scsi/csi-attacher:v2.1.1 73 | quay.io/cephcsi/cephcsi:v3.1.0 74 | quay.io/k8scsi/csi-node-driver-registrar:v1.3.0 75 | ``` 76 | 77 | * 执行下载任务 78 | 79 | ``` sh 80 | ./pull.sh imagets-abc.txt 81 | ``` 82 | 83 | 镜像下载任务完成之后,会在同目录下生成一个压缩文件 `imagets-abc.tar.gz` (文件名称与 imagets-abc.txt 相同,后缀不同) 84 | 85 | 86 | ## 将文件复制到机器 B 87 | 88 | 用你自己的办法,将如下两个文件复制到机器 B 的某个目录,假设路径是 `~/docker-image-loader`: 89 | * `dispatch.sh` 90 | * `imagets-abc.tar.gz` (在前一个步骤中生成的文件) 91 | 92 | ## 从机器 B 分发镜像到目标机器 93 | 94 | 在机器 B 上执行: 95 | 96 | * 切换到 `~/docker-image-loader` 目录 97 | 98 | ```sh 99 | cd ~/docker-image-loader 100 | ``` 101 | 102 | * 创建 `target-hosts.txt` 文件,内容如下 103 | 104 | ``` {1} 105 | /root/.ssh/id_rsa 106 | root@192.168.32.11:22 107 | root@192.168.32.12:22 108 | root@192.168.32.13:22 109 | root@192.168.32.14:22 110 | root@192.168.32.15:22 111 | ``` 112 | * 第一行为 ssh key 文件的路径,我们在前面 **设置机器 B 可以无密码 ssh 访问所有目标机器** 的步骤中,已经将该 key 对应的 .pub 文件复制到所有的目标服务器上,后续将使用该 key 文件作为认证信息,远程在目标服务器上执行指令; 113 | > 此处 ssh key 文件的路径必须使用绝对路径 114 | * 后面的每一行为一个目标机器的登录用户、IP 地址以及 ssh 端口(如果使用默认 22 端口,也需要在此处列出)。 115 | 116 | * 执行分发任务 117 | ``` sh 118 | chmod +x dispatch.sh 119 | ./dispatch.sh imagets-abc.tar.gz target-hosts.txt 120 | ``` 121 | 122 | **至此,您已经成功地将指定的 docker image 分发到指定的目标机器上。** 123 | -------------------------------------------------------------------------------- /dispatch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ x${1} = x ]; then 4 | echo -e "\033[31m 请在第一个命令行参数指定 *-images.txt 文件 \033[0m" 5 | exit 6 | fi 7 | 8 | if [ ! -f "${1}" ]; then 9 | echo -e "\033[31m 文件 ${1} 不存在 \033[0m" 10 | exit 11 | fi 12 | 13 | if [ x${2} = x ]; then 14 | echo -e "\033[31m 请在第二个命令行参数指定 target-hosts.txt 文件 \033[0m" 15 | exit 16 | fi 17 | 18 | if [ ! -f "${2}" ]; then 19 | echo -e "\033[31m 文件 ${2} 不存在 \033[0m" 20 | exit 21 | fi 22 | 23 | read line < ${2} 24 | 25 | prvKey=$(echo ${line}) 26 | 27 | if [ ! -f "${prvKey}" ]; then 28 | echo -e "\033[31m 文件 '${prvKey}' 不存在,请在文件 ${2} 的第一行指定 ssh privateKey 的路径 \033[0m" 29 | exit 30 | fi 31 | 32 | while read line 33 | do 34 | let count++ 35 | if [ ${count} -gt 1 ]; then 36 | 37 | line=$(echo $line) 38 | 39 | if [ x${line} = x ]; then 40 | continue 41 | fi 42 | 43 | user=$(echo ${line%@*}) 44 | ipport=$(echo ${line#*@}) 45 | ip=$(echo ${ipport%:*}) 46 | port=$(echo ${ipport#*:}) 47 | 48 | if [ x${user} = x${line} -o x${ip} = x${ipport} -o x${port} = x${ipport} ]; then 49 | echo -e "\033[31m 文件 ${2} 的第 ${count} 行应该符合 user@192.168.2.10:22 的格式,当前该行内容为: \033[0m" 50 | echo ${line} 51 | exit 52 | fi 53 | 54 | hostIndex=`expr ${count} - 1` 55 | echo -e "\033[36m>>>>> 开始分发镜像到第 ${hostIndex} 个目标主机 ${ip} >>>>>\033[0m" 56 | scp -P ${port} -i ${prvKey} ${1} ${user}@${ip}:~/ 57 | 58 | ssh -p ${port} -i ${prvKey} ${user}@${ip} "rm -rf ${1%???????} || true 59 | echo -e \"\033[36mstep ${hostIndex}.1 解压缩\033[0m\" 60 | tar zxvf ${1} 61 | echo -e \"\033[36mstep ${hostIndex}.2 加载镜像\033[0m\" 62 | while read line 63 | do 64 | let c++ 65 | line=\$(echo \${line}) 66 | if [ x\${line} = x ]; then 67 | echo -e \"Step ${hostIndex}.2.\${c} \033[33m第 \${c} 行为空\033[0m\" 68 | continue 69 | fi 70 | echo -e \"Step ${hostIndex}.2.\${c} sudo docker load < ${1%???????}/\${line//\//_}.tar \\t \\c\" 71 | line=\$(echo \$line) 72 | sudo docker load < ${1%???????}/\${line//\//_}.tar 73 | done < ${1%???????}/images.txt 74 | 75 | echo -e \"\033[36m加载到目标主机 ${ip} 的镜像如下\033[0m\" 76 | echo -e \"IMAGE ID\t CREATED\t\tSIZE\t\t REPOSITORY:TAG\" 77 | while read line 78 | do 79 | line=\$(echo \${line}) 80 | if [ x\${line} = x ]; then 81 | continue 82 | fi 83 | tag=\$(echo \${line%:*}) 84 | version=\$(echo \${line%:*}) 85 | sudo docker images \${line} --format \"table {{.ID}}\t{{.CreatedSince}}\t{{.Size}}\t{{.Repository}}:{{.Tag}}\" | grep \${tag} | grep \${version} 86 | done < ${1%???????}/images.txt 87 | 88 | echo 89 | echo -e \"\033[36m清理目标主机 ${ip} 上的临时文件\033[0m\" 90 | rm -rf ${1%???????} || true 91 | rm -rf ${1} || true 92 | echo 93 | " < /dev/null 94 | 95 | echo -e "\033[32m<<<<< 已结束将镜像分发到第 ${hostIndex} 个目标主机 ${ip} <<<<<\033[0m" 96 | echo "" 97 | fi 98 | done < ${2} 99 | 100 | echo -e "\033[32m----- 已结束将镜像分发到 ${2} 文件中定义的所有主机 -----\033[0m" 101 | 102 | echo "" 103 | -------------------------------------------------------------------------------- /images-abc.txt: -------------------------------------------------------------------------------- 1 | eipwork/kuboard:latest 2 | busybox:latest 3 | -------------------------------------------------------------------------------- /pull.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | folder=${1%.*} 4 | 5 | if [ x"${folder}" = x ]; then 6 | echo -e "\033[31m 请指定 *-images.txt 文件 \033[0m" 7 | exit 8 | fi 9 | 10 | echo "" 11 | echo "创建临时文件夹 ${folder}" 12 | echo "" 13 | rm -rf ${folder} || true 14 | mkdir ${folder} 15 | 16 | while read line 17 | do 18 | let count++ 19 | line=$(echo $line) 20 | if [ x${line} = x ]; then 21 | echo -e "\033[33m第 ${count} 行为空\033[0m" 22 | echo 23 | continue 24 | fi 25 | echo ">>>>> 下载第 ${count} 个镜像 ${line} >>>>>" 26 | docker pull $line 27 | echo -e "\033[32m<<<<< 保存第 ${count} 个镜像到 ${folder}/${line//\//_}.tar \033[0m" 28 | docker save $line > ${folder}/${line//\//_}.tar 29 | echo "" 30 | done < ${1} 31 | 32 | echo "----- 创建压缩文件 ${folder}.tar.gz -----" 33 | cp ${1} ${folder}/images.txt 34 | tar -zcvf ${folder}.tar.gz ${folder}/*.tar ${folder}/images.txt 35 | 36 | echo -e "\033[32m----- 已压缩到文件 ${folder}.tar.gz ----- \033[0m" 37 | echo -e "文件大小为 \c" 38 | ls -hl ${folder}.tar.gz | awk '{print $5}' 39 | 40 | echo "" 41 | echo "清除临时文件夹 ${folder}" 42 | rm -rf ${folder} 43 | 44 | echo "" 45 | echo "请执行以下指令,将镜像分发到 ./target-hosts.txt 文件中定义的目标主机上。" 46 | echo -e "\033[36m./dispatch.sh ${folder}.tar.gz target-hosts.txt \033[0m" 47 | echo "" 48 | -------------------------------------------------------------------------------- /target-hosts.txt: -------------------------------------------------------------------------------- 1 | /root/.ssh/id_rsa_p 2 | root@192.168.2.80:22 3 | root@192.168.2.81:22 4 | root@192.168.2.82:22 5 | root@192.168.2.83:22 6 | --------------------------------------------------------------------------------