本地内网,使用 Amazon Linux 2 镜像,模拟 AWS 线上系统环境

Amazon Linux 2 为每个受支持的虚拟化平台提供不同的 VM 镜像,如 KVM 、VMware vSphere 、Oracle
VirtualBox 、Microsoft Hyper-V 等主流虚拟化平台,便于我们进行本地开发和测试

环境介绍

  • 宿主机:Ubuntu 20.04.6 LTS (kernel 5.4.0-166)
  • kvm 虚拟化组件:libvirt-daemon 6.0 ,qemu-system-x86_64 4.2.1

安装 kvm 虚拟化软件包

apt install qemu-kvm libvirt-daemon-system libvirt-clients virtinst virt-manager bridge-utils -y

# 启动虚机管理进程
systemctl start libvirtd.service && systemctl enable libvirtd.service

准备启动镜像

启动镜像 seed.iso 包含启动新 VM 所需的初始配置信息,例如网络配置、主机名、用户数据,但不包括 Amazon Linux 2 操作系统文件

要生成 seed.iso ,需准备如下两个配置文件:

  • meta-data – 该文件包含虚拟机的主机名、静态网络设置
  • user-data – 该文件配置用户帐户,并指定其密码、访问机制等系进行。默认情况下,Amazon Linux 2 VM 镜像创建一个默认账号 ec2-user,您可以使用该配置文件来设置默认帐户的密码

创建 meta-data

1、静态配置虚机网络信息

mkdir seedconfig && cd seedconfig

cat > meta-data << EOF
local-hostname: t3
network-interfaces: |
  auto eth0
  iface eth0 inet static
  address 192.168.31.93
  network 192.168.31.0
  netmask 255.255.255.0
  broadcast 192.168.31.255
  gateway 192.168.31.1
EOF

创建 user-data

1、设置默认账号 ec2-user 密码为 password@123 ,且该账号具有 sudo 权限

cat > user-data << EOF
#cloud-config
#vim:syntax=yaml
users:
  - default
chpasswd:
  list: |
    ec2-user:password@123
EOF

2、使用 meta-data 和 user-data 创建启动映像 seed.iso

genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data

# 转化为 qcow2 格式的镜像,便于虚机创建快照
qemu-img convert -p -f raw -O qcow2 seed.img seed-t3.qcow2

虚机配置 t3.xml

1、创建虚机配置 t3.xml

<domain type='kvm'>
    <name>t3</name>
    <vcpu>4</vcpu>
    <memory>4194304</memory><!-- 4 GB内存。 -->
    <cpu mode='host-passthrough'>
    </cpu>
    <os>
        <type arch='x86_64'>hvm</type><!-- 如果是ARM64架构的服务器,则需设置为arch='aarch64'。 -->
        <boot dev='hd'/>
    </os>
    <clock sync="localtime"/>
    <on_poweroff>destroy</on_poweroff>
    <on_reboot>restart</on_reboot>
    <on_crash>restart</on_crash>
    <!-- 通过 virsh shutdown 关闭该虚机 -->
    <features>
        <acpi/>
        <apic/>
        <pae/>
    </features>
    <devices>
        <emulator>/usr/bin/kvm</emulator><!-- 请根据不同的操作系统设置对应的kvm路径。例如:Ubuntu对应的kvm路径是/usr/bin/kvm。 -->
        <disk type='file' device='disk'><!-- 请根据镜像格式设置下面的 type 参数:qcow2 对应 type='qcow2'、vhd 对应 type='vpc'。 -->
            <driver name='qemu' type='qcow2' cache='none' dataplane='off' io='native'/> <!-- 如果要创建 qcow2 快照,需要关闭 dataplane。 -->
            <source file='/mnt/vm/iso/amzn2-kvm-2.0.20231101.0-x86_64.xfs.gpt.qcow2'/> <!-- 填写 Amazon Linux 2 镜像的绝对路径。 -->
            <target dev='vda' bus='virtio'/>
        </disk>
        <!-- 加入 seed.qcow2 的信息。 -->
        <disk type='file' device='disk'>
            <driver name='qemu' type='qcow2'/>   <!-- raw 对应磁盘格式 img -->
            <source file='/mnt/vm/iso/seed/seed-t3.qcow2'/> <!-- 填写 seed 镜像的绝对路径。 -->
            <target dev='vdb' bus='virtio'/>
        </disk>
        <!-- 虚机网卡配置 -->
        <interface type='bridge'>
            <source bridge='br1'/>      <!-- 桥接到宿主机网络,需先创建网桥 br1  -->
            <model type='virtio'/>
        </interface>
        <console type='pty'>
            <target type='virtio' port='0'/>
        </console>
        <video>
            <model type='cirrus' vram='9216' heads='1'/>
            <alias name='video0'/>
        </video>
        <input type='tablet' bus='usb'/>
        <input type='mouse' bus='ps2'/>
        <!-- 通过 vnc 登录控制台,启用 sshd 服务的密码验证 -->
        <graphics type='vnc' port='59093' autoport='no' listen='0.0.0.0'>
            <listen type='address' address='0.0.0.0'/>
        </graphics>
    </devices>
</domain>

创建虚机

1、下载虚机镜像

wget https://cdn.amazonlinux.com/os-images/2.0.20231101.0/kvm/amzn2-kvm-2.0.20231101.0-x86_64.xfs.gpt.qcow2

2、根据如上虚机配置及镜像,启动并连接到新虚拟机 t3

virsh define t3.xml
virsh start t3

3、 编辑 /etc/ssh/sshd_config 开启 sshd 服务密码验证,重启 sshd

PasswordAuthentication yes

4、查看默认运行服务及内核版本

[ec2-user@t3 ~]$ sudo netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2176/rpcbind
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2846/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2556/master
tcp6       0      0 :::111                  :::*                    LISTEN      2176/rpcbind
tcp6       0      0 :::22                   :::*                    LISTEN      2846/sshd

[ec2-user@t3 ~]$ uname -r
4.14.327-246.539.amzn2.x86_64

内核升级

1、将 Amazon Linux 2 默认内核从 4.14.x 升级到 Amazon Linux Extras 内核 5.4.x

# 验证 amazon-linux-extras 存储库提供的内核版本
sudo amazon-linux-extras |grep kernel
#> 49  kernel-5.4               available    [ =stable ]
#> 55  kernel-5.10              available    [ =stable ]
#> 62  kernel-5.15              available    [ =stable ]

sudo amazon-linux-extras install kernel-5.4 -y

2、将 Amazon Linux 2 (Amazon Linux Extras) 内核从 5.4.x 升级到 5.10.x

sudo amazon-linux-extras disable kernel-5.4
sudo amazon-linux-extras install kernel-5.10 -y

3、重启虚机实例,以让新内核生效

virsh restart t3

Amazon Linux 2023 (AL2023) 是一种基于 RPM 的 Linux 通用发行版,也是 Amazon Linux 2 的后继者,每两年发布一个新的 Amazon Linux 主要版本,包括次要季度版本,并提供五年的长期支持

码字不易,若觉得本文对你有用,欢迎点赞 👍、分享 🚀 ,相关技术热点时时看🔥🔥🔥​​​…

参考

Run Amazon Linux 2 as a virtual machine on premises
cloud-init configuration for Amazon Linux 2023 on KVM

  • 21
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值