当在x86或arm平台使用buildx构建多平台镜像时,会发现buildx在构建与docker所在宿主机异构平台镜像包时,构建速度慢的问题。
当你同时有x86和arm服务器的基础上,可以使用远程节点来构建多平台镜像包,使用远程节点构建多平台镜像的好处如下:
1.无兼容性问题:在x86平台构建arm或在arm平台构建x86镜像,有时会存在兼容性问题导致构建失败。
2.构建速度提升:在x86平台构建arm(或相反)使用的QEMU模拟技术,构建速度相对较慢。
实现过程如下:
环境:
x86: 192.168.1.1 # docker engine version >=20
arm: 192.168.1.2 # docker engine version >=20
在x86服务器执行:
# 配置免密登录
[root@www ~]# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): # 回车
Enter passphrase (empty for no passphrase): # 回车
Enter same passphrase again: # 回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LtKq7W8+LlJWK8prpzLKT4RFEDZaqZhqvoOJkvEcjb0 root@www.dehuinet.com
The key's randomart image is:
+---[RSA 4096]----+
| =+o |
|o.+ |
|oo . |
|+ o . |
|.. = . .S |
|o.+ *... |
|=B *.oo . |
|XoO.Eoo. |
|+*BO+*+. |
+----[SHA256]-----+
# 上述命令执行成功会在家目录的.ssh目录下生成"id_rsa"和"id_rsa.pub"两个文件
[root@www ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.10.2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established.
ECDSA key fingerprint is SHA256:g4eZZsXuQumekQE19vucvMA+oVNLTGC3WV+PMKSWIh0.
ECDSA key fingerprint is MD5:53:10:36:82:8f:68:6f:c2:08:a4:4d:8b:42:cc:a0:05.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.2's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.2'"
and check to make sure that only the key(s) you wanted were added.
# 执行上述命令成功后就可以在x86服务器免密登录arm服务器了。
# 创建builder
docker buildx create \
--name multi_arch_builder \
--node node_amd64 \
--platform linux/amd64
# 向builder中追加新节点(arm)
docker buildx create \
--name multi_arch_builder \
--append \
--node node_arm64 \
--platform linux/arm64,linux/arm64/v8,linux/arm/v7,linux/arm/v6,linux/arm/v5 \
ssh://root@192.168.1.2
# 启用新builder
docker buildx user multi_arch_builder
docker buildx inspect --bootstrap
[root@www ~]# docker buildx inspect --bootstrap
[+] Building 19.7s (1/2)
[+] Building 30.3s (2/2) FINISHED
=> [node_amd64 internal] booting buildkit 30.3s
=> => pulling image moby/buildkit:buildx-stable-1 27.8s
=> => creating container buildx_buildkit_node_amd64 2.5s
=> [node_arm64 internal] booting buildkit 11.4s
=> => pulling image moby/buildkit:buildx-stable-1 8.2s
=> => pulling failed, using local image moby/buildkit:buildx-stable-1 0.0s
=> => creating container buildx_buildkit_node_arm64 3.2s
Name: remote
Driver: docker-container
Nodes:
Name: node_amd64
Endpoint: unix:///var/run/docker.sock
Status: running
Platforms: linux/amd64*, linux/amd64/v2, linux/386
Name: node_arm64
Endpoint: ssh://root@192.168.1.2
Status: running
Platforms: linux/arm64*, linux/arm/v7*, linux/arm/v6*, linux/arm/v5*
# 此时在arm服务器上执行docker ps命令会看到新增了一个容器
[root@localhost lib]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e67cf241b477 moby/buildkit:buildx-stable-1 "buildkitd" 3 minutes ago Up 3 minutes buildx_buildkit_node_arm64
上述步骤后,就可以向之前一样使用buildx进行多平台镜像的构建了。
标签:buildx,arm,ssh,linux,镜像,docker,root,节点 From: https://www.cnblogs.com/linux-xiaofeixiang/p/18662427