交叉构建Docker镜像-arm64

Administrator
发布于 2020-10-10 / 517 阅读 / 0 评论 / 0 点赞

交叉构建Docker镜像-arm64

一、前言

目前CPU架构除了x86之外,常用的还有arm64v8、s390x、ppc64le,很多时候不能我们没有相应CPU的集群,就需要想办法,最近看到qemu-user-static这个开源项目还是不错的,很有启发,可以在x86的CPU中模拟各种架构的环境,解决适配各种架构的开发环境,以及测试兼容性问题,等等各种棘手问题。话不多说,开整。
∷目前该项目只能用在x86架构的机器上,不能用于其他架构的机器,就是说x86可以模拟任何架构,反过来行不通。

二、演示步骤

本文使用的x86环境为开源centos7.6,内核需要单独升级到4以上,用默认的3.10的时候注册环境失败,在进行之前,先升级centos的内核。

2.1 一键三连查看系统

[root@node01 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@node01 ~]# uname -a
Linux node01 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@node01 ~]# uname -r
3.10.0-957.el7.x86_64

2.2 启用 ELRepo 仓库

ELRepo 仓库是基于社区的用于企业级 Linux 仓库,提供对 RedHat Enterprise (RHEL) 和 其他基于 RHEL的 Linux 发行版(CentOS、Scientific、Fedora 等)的支持。
ELRepo 聚焦于和硬件相关的软件包,包括文件系统驱动、显卡驱动、网络驱动、声卡驱动和摄像头驱动等。

[root@node01 ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@node01 ~]# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

2.3 查看可用的系统版本内核

[root@node01 ~]# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available

image.png

2.4 升级到最新的

yum --enablerepo=elrepo-kernel install kernel-ml

2.5 查看系统中可用的内核

[root@node01 ~]# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.8.14-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (4.4.238-1.el7.elrepo.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)
3 : CentOS Linux (0-rescue-cdd526301e5346a9b20205508661b5b2) 7 (Core)

2.6 将新安装的内核设置成默认

[root@node01 ~]# grub2-set-default 0

2.7 重新生成grub2文件

[root@node01 ~]#  grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.8.14-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-5.8.14-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-4.4.238-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-4.4.238-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-cdd526301e5346a9b20205508661b5b2
Found initrd image: /boot/initramfs-0-rescue-cdd526301e5346a9b20205508661b5b2.img
done

2.8 重启机器生效,使用最新的内核验证升级是否成功

[root@node01 ~]# uname -r
5.8.14-1.el7.elrepo.x86_64
[root@node01 ~]# uname -a
Linux node01 5.8.14-1.el7.elrepo.x86_64 #1 SMP Mon Oct 5 19:02:22 EDT 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@node01 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

3 注册qemu-user-static

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

image.png

4 验证arm镜像运行情况

[root@node01 ~]# docker run --rm -t arm64v8/ubuntu uname -m
aarch64

可以看到运行的docker镜像架构是arm64的,同样在dockerfile中指定arm的基础镜像也可构建arm下的docker镜像

感谢 升级内核指引

感谢qemu-user-static开源 交叉构建


评论