Multipass 介绍与应用

发布时间: 更新时间: 总字数:1146 阅读时间:3m 作者: IP上海 分享 网址

Multipass是一款支持在LinuxWindowsMacOS上运行的轻量级虚拟机管理器。

介绍

Multipass 在不同平台上使用不同的虚拟化技术运行虚拟机:

  • Linux 上通过 KVM
  • Windows 上通过 Hyper-V
  • MacOS 上通过 HyperKit
    • HyperKit 是一个具有 hyperisor 能力的工具集
    • Dockermultipass 均依赖 HyperKit,安装他们会默认安装 HyperKit

安装

Mac

Mac 上有多种方式:

brew install multipass

使用

help

$ multipass --help
Usage: multipass [options] <command>
Create, control and connect to Ubuntu instances.

This is a command line utility for multipass, a
service that manages Ubuntu instances.

Options:
  -h, --help     Displays help on commandline options.
  --help-all     Displays help including Qt specific options.
  -v, --verbose  Increase logging verbosity. Repeat the 'v' in the short option
                 for more detail. Maximum verbosity is obtained with 4 (or more)
                 v's, i.e. -vvvv.

Available commands:
  delete    Delete instances
  exec      Run a command on an instance
  find      Display available images to create instances from
  get       Get a configuration setting
  help      Display help about a command
  info      Display information about instances
  launch    Create and start an Ubuntu instance
  list      List all available instances
  mount     Mount a local directory in the instance
  networks  List available network interfaces
  purge     Purge all deleted instances permanently
  recover   Recover deleted instances
  restart   Restart instances
  set       Set a configuration setting
  shell     Open a shell on a running instance
  start     Start instances
  stop      Stop running instances
  suspend   Suspend running instances
  transfer  Transfer files between the host and instances
  umount    Unmount a directory from an instance
  version   Show version details

启动命令:

$ multipass help launch
Usage: multipass launch [options] [[<remote:>]<image> | <url>]
Create and start a new instance.

Options:
  -h, --help           Display this help
  -v, --verbose        Increase logging verbosity, repeat up to three times for
                       more detail
  -c, --cpus <cpus>    Number of CPUs to allocate
  -d, --disk <disk>    Disk space to allocate. Positive integers, in bytes, or
                       with K, M, G suffix. Minimum: 512M.
  -m, --mem <mem>      Amount of memory to allocate. Positive integers, in
                       bytes, or with K, M, G suffix. Mimimum: 128M.
  -n, --name <name>    Name for the instance
  --cloud-init <file>  Path to a user-data cloud-init configuration, or '-' for
                       stdin

Arguments:
  image                Optional image to launch. If omitted, then the default
                       Ubuntu LTS will be used.
                       <remote> can be either ‘release’ or ‘daily‘. If <remote>
                       is omitted, ‘release’ will be used.
                       <image> can be a partial image hash or an Ubuntu release
                       version, codename or alias.
                       <url> is a custom image URL that is in http://, https://,
                       or file:// format.

启动实例

若在 windows 建议在PowerShell中执行

$ multipass launch -c 1 -d 5G -m 256m -n nginx
$ multipass launch -c 4 -d 20G -m 4G -n ubuntu 20.04

说明:

  • -n : 指定启动实例名字
  • -c : 分配 CPU 数量
  • -d : 设置磁盘容量
  • -m : 设置内存容量

查看实例

# 查看
multipass list

登录实例

# 登录 shell
multipass shell <podman-name>

# 配置源
ubuntu@nginx:~$ . /etc/os-release
ubuntu@nginx:~$ echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
ubuntu@nginx:~$ echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
ubuntu@nginx:~$ curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/Release.key | sudo apt-key add -
ubuntu@nginx:~$ sudo apt-get update
ubuntu@nginx:~$ sudo apt-get -y upgrade

# 为ubuntu安装软件
ubuntu@nginx:~$ sudo apt install nginx -y

磁盘挂载

在windows下默认挂载是禁用的, 手动开启命令

multipass set local.privileged-mounts=true

挂载盘,在宿主机执行:

$ multipass mount /Users/xiexianbin/work/code/github.com/xiexianbin/xiexianbin.github.io/public nginx:/var/www/html

$ multipass info nginx
Name:           nginx
State:          Running
IPv4:           172.17.115.100
                172.17.0.1
Release:        Ubuntu 20.04.5 LTS
Image hash:     ef2f29df600c (Ubuntu 20.04 LTS)
Load:           0.00 0.00 0.00
Disk usage:     8.2G out of 19.2G
Memory usage:   278.8M out of 3.8G
Mounts:         D:/code/share => /data
                    UID map: -2:default
                    GID map: -2:default

$ multipass unmount nginx

文件传输

# multipass transfer 主机文件 容器名:容器目录
$ multipass transfer all.xiexianbin.cn.bundle.crt nginx:/home/ubuntu/
transfer failed: [sftp push] open failed: 'SFTP server: Failure'

错误原因:位置不对,/home/ubuntu/ 不是sftp的共享目录,可以临时放到 /tmp/ 目录下在从系统内移动到合适的位置

cloud-init

multipass 提供了 --cloud-init 选项进行初始化配置自动化:

multipass launch --name ubuntu --cloud-init config.yaml

config.yaml 示例文件如下:

#cloud-config
runcmd:
  - curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
  - sudo apt-get install -y nodejs

注意:凡是用户自定义的 cloud-init 的配置文件,必须以 #cloud-config 开头,这是cloud-init识别它的方式

F&Q

multipass shell 闪退

在 Windows 安装 multipass 后开启 multipass shell 闪退,原因是没有启动 Hyper-V 组件,启动脚本如下,使用管理员权限执行:

  • setup-hyper-v.bat
pushd "%~dp0"

dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt

for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"

del hyper-v.txt

Dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /LimitAccess /ALL

windows启动失败

  • 未安装 Hyper-V,将上面的脚本

  • Hyper-V 未启动

$ multipass launch -c 4 -d 20G -m 4G -n ubuntu 20.04
launch failed: The Hyper-V Windows feature is disabled. Please enable by using the following
command in an Administrator Powershell and reboot:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

使用Administrator在Powershell中执行

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数