Introduction for AWS Firecracker

栏目: 服务器 · 发布时间: 6年前

内容简介:首先贴两段官网对 Firecracker 的定义:实际上 Firecracker 就是一个 VMM,可以运行轻量级的虚拟机,也就是所谓的由于 Firecracker 还处于 0.1x 阶段,在使用上还是非常简单和技术化。 运行 Firecracker 有两个先决条件:

AWS Firecracker 和 KataContainer 初探(一)

AWS Firecracker

首先贴两段官网对 Firecracker 的定义:

Firecracker is an open source virtualization technology that is purpose-built for creating and managing secure, multi-tenant container and function-based services that provide serverless operational models. Firecracker runs workloads in lightweight virtual machines, called microVMs, which combine the security and isolation properties provided by hardware virtualization technology with the speed and flexibility of containers.
The main component of Firecracker is a virtual machine monitor (VMM) that uses the Linux Kernel Virtual Machine (KVM) to create and run microVMs. Firecracker has a minimalist design. It excludes unnecessary devices and guest-facing functionality to reduce the memory footprint and attack surface area of each microVM. This improves security, decreases the startup time, and increases hardware utilization. Firecracker currently supports Intel CPUs, with planned AMD and Arm support. Firecracker will also be integrated with popular container runtimes.

实际上 Firecracker 就是一个 VMM,可以运行轻量级的虚拟机,也就是所谓的 microVM 。Firecracker 底层还是使用 KVM 做 CPU 和内存的硬件虚拟化,然后配合 KVM 实现其他设备的虚拟化。它对标的是 qemu,更确切的说是魔改的 qemu-lite。因为去除了很多累赘设备和做了优化,配合精简的 Linux Kernel,可以将虚拟机启动时间缩短到秒级,甚至毫秒级。Firecracker 是 AWS Lambda 和 AWS Fargate 的后端支持,背景可见一斑。不过众所周知,开源代码和内部生产必然有很大差距,大家秉承 PoC 的精神即可 :)。

如何使用

由于 Firecracker 还处于 0.1x 阶段,在使用上还是非常简单和技术化。 运行 Firecracker 有两个先决条件:

  • Linux 4.14+
  • 支持 KVM

可以使用以下脚本检验:

err=""; \
[ "$(uname) $(uname -m)" = "Linux x86_64" ] \
  || err="ERROR: your system is not Linux x86_64."; \
[ -r /dev/kvm ] && [ -w /dev/kvm ] \
  || err="$err\nERROR: /dev/kvm is innaccessible."; \
(( $(uname -r | cut -d. -f1)*1000 + $(uname -r | cut -d. -f2) >= 4014 )) \
  || err="$err\nERROR: your kernel version ($(uname -r)) is too old."; \
dmesg | grep -i "hypervisor detected" \
  && echo "WARNING: you are running in a virtual machine. Firecracker is not well tested under nested virtualization."; \
[ -z "$err" ] && echo "Your system looks ready for Firecracker!" || echo -e "$err"

在正式环境中,官方推荐使用 jailer 运行 Firecracker。由于我们只是验证环境,故直接使用 Firecracker 二进制运行:

先下载最新版本:

latest=v0.13.0 && curl -LOJ https://github.com/firecracker-microvm/firecracker/releases/download/v${latest}/firecracker-v${latest}

注:由于 v0.14.0 会出现 Firecracker API hang 问题,所以我这里还是选择 v0.13.0。

接下来启动进程:

rm -f /tmp/firecracker.socket; ./firecracker --api-sock /tmp/firecracker.socket

开启另一个 shell,下载 kernel 和 rootfs:

curl -fsSL -o hello-vmlinux.bin https://s3.amazonaws.com/spec.ccfc.min/img/hello/kernel/hello-vmlinux.bin
curl -fsSL -o hello-rootfs.ext4 https://s3.amazonaws.com/spec.ccfc.min/img/hello/fsfiles/hello-rootfs.ext4

调用 Firecracker API,设置虚拟机 kernel 和 rootfs:

curl --unix-socket /tmp/firecracker.socket -i \
    -X PUT 'http://localhost/boot-source'   \
    -H 'Accept: application/json'           \
    -H 'Content-Type: application/json'     \
    -d '{
        "kernel_image_path": "./hello-vmlinux.bin",
        "boot_args": "console=ttyS0 reboot=k panic=1 pci=off"
    }'

curl --unix-socket /tmp/firecracker.socket -i \
    -X PUT 'http://localhost/drives/rootfs' \
    -H 'Accept: application/json'           \
    -H 'Content-Type: application/json'     \
    -d '{
        "drive_id": "rootfs",
        "path_on_host": "./hello-rootfs.ext4",
        "is_root_device": true,
        "is_read_only": false
    }'

启动虚拟机:

curl --unix-socket /tmp/firecracker.socket -i \
    -X PUT 'http://localhost/actions'       \
    -H  'Accept: application/json'          \
    -H  'Content-Type: application/json'    \
    -d '{
        "action_type": "InstanceStart"
     }'

这时回头看第一个 shell 窗口,已经连接了一个 TTY 设备到虚拟机。使用 root/root 登陆即可。

最后

笔者实际是想写 KataContainer 和 AWS Firecracker 的结合,以及它们之间会碰撞出怎么样的火花。不过总觉得需要先做些铺垫,简单介绍下基本原理和使用,故有了这篇充斥拿来主义的“水”文。先挖个坑,后面如果有时间会深入阐述 Firecracker 的实现原理。至于源码分析就算了,Rust 实在看的头疼。有经验的读者无需浪费时间,直接移步 github:https://github.com/firecracker-microvm/firecracker/blob/master/docs。

《未完待续》


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

启示录

启示录

[美] Marty Cagan / 七印部落 / 华中科技大学出版社 / 2011-5 / 36.00元

为什么市场上那么多软件产品无人问津,成功的产品凤毛麟角?怎样发掘有价值的产品?拿什么说服开发团队接受你的产品设计?如何将敏捷方法融入产品开发?过去二十多年,Marty Cagan作为高级产品经理人为多家一流企业工作过,包括惠普、网景、美国在线、eBay。他亲历了个人电脑 、互联网、 电子商务的起落沉浮。《启示录:打造用户喜爱的产品》从人员、流程、产品三个角度介绍了现代软件(互联网)产品管理的实践经......一起来看看 《启示录》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具