lzh2nix / articles

用 issue 来管理个人博客
https://github.com/lzh2nix/articles
62 stars 13 forks source link

使用multipass在本地快速构建虚拟化环境 #125

Open lzh2nix opened 3 years ago

lzh2nix commented 3 years ago

在本地构建vm有四种方式

  1. 安装vritualBox/vmware等工具来构建vm
  2. 使用libvritd/kvm手动构建vm
  3. 使用ubuntu母公司出品的multipass
  4. 使用Vagrant进行构建

3是最近发现的一种构建方式,也是一种很快的构件方式. 启动一个vm只需要39s

qndell15@qndell15:~$ time multipass launch file:///home/qndell15/hirsute-server-cloudimg-amd64.img
Launched: equipped-marsupial

real    0m39.112s
user    0m0.040s
sys 0m0.064s

这样本地启动一个集群就特别方便了

查询可以使用image

qndell15@qndell15:~$ multipass find
Image                       Aliases           Version          Description
snapcraft:core18                              20201111         Snapcraft builder for Core 18
snapcraft:core20                              20201111         Snapcraft builder for Core 20
snapcraft:core                                20210430         Snapcraft builder for Core 16
core                        core16            20200818         Ubuntu Core 16
core18                                        20200812         Ubuntu Core 18
18.04                       bionic            20210604         Ubuntu 18.04 LTS
20.04                       focal,lts         20210622         Ubuntu 20.04 LTS
20.10                       groovy            20210622.1       Ubuntu 20.10
21.04                       hirsute           20210623         Ubuntu 21.04
daily:21.10                 devel,impish      20210711         Ubuntu 21.10
appliance:adguard-home                        20200812         Ubuntu AdGuard Home Appliance
appliance:mosquitto                           20200812         Ubuntu Mosquitto Appliance
appliance:nextcloud                           20200812         Ubuntu Nextcloud Appliance
appliance:openhab                             20200812         Ubuntu openHAB Home Appliance
appliance:plexmediaserver                     20200812         Ubuntu Plex Media Server Appliance

创建一个vm

qndell15@qndell15:~$ multipass launch -c 4  -m 8G file:///home/qndell15/hirsute-server-cloudimg-amd64.img
Launched: ornamental-yeti

其中可以通过 -c 指定核数, -m 指定内存大小, -d 指定磁盘大小

ssh到某个vm里

qndell15@qndell15:~$ multipass shell ornamental-yeti
Welcome to Ubuntu 21.04 (GNU/Linux 5.11.0-22-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Mon Jul 12 06:32:02 UTC 2021

  System load:  0.08              Processes:             122
  Usage of /:   27.8% of 4.67GB   Users logged in:       0
  Memory usage: 2%                IPv4 address for ens4: 10.24.205.12
  Swap usage:   0%

0 updates can be applied immediately.

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@ornamental-yeti:~$

挂载本地目录到某个vm

qndell15@qndell15:~$ multipass mount ./xx ornamental-yeti:/home/ubuntu/xx
qndell15@qndell15:~$ multipass info ornamental-yeti
Name:           ornamental-yeti
State:          Running
IPv4:           10.24.205.12
Release:        Ubuntu 21.04
Image hash:     4a6326003505 (Ubuntu 21.04)
Load:           0.10 0.09 0.05
Disk usage:     1.3G out of 4.7G
Memory usage:   160.5M out of 7.8G
Mounts:         /home/qndell15/xx => /home/ubuntu/xx
                    UID map: 1000:default
                    GID map: 1000:default

从vm拷贝文件到本地

qndell15@qndell15:~$ multipass transfer ornamental-yeti:/home/ubuntu/ttx .
qndell15@qndell15:~$ ls ttx
ttx

list 本地vm 列表

qndell15@qndell15:~$ multipass list
Name                    State             IPv4             Image
discerning-skylark      Running           10.24.205.162    Ubuntu 21.04
                                          10.24.205.211
equipped-marsupial      Running           10.24.205.57     Ubuntu 21.04
honorary-snailfish      Running           10.24.205.41     Ubuntu 21.04
                                          10.24.205.212
perky-accentor          Running           10.24.205.84     Ubuntu 21.04
                                          10.24.205.99

远程执行命令

qndell15@qndell15:~$ multipass exec ornamental-yeti -- df -h
Filesystem          Size  Used Avail Use% Mounted on
tmpfs               796M  972K  795M   1% /run
/dev/sda1           4.7G  1.4G  3.4G  28% /
tmpfs               3.9G     0  3.9G   0% /dev/shm
tmpfs               5.0M     0  5.0M   0% /run/lock
tmpfs               4.0M     0  4.0M   0% /sys/fs/cgroup
/dev/sda15          105M  5.2M  100M   5% /boot/efi
tmpfs               796M  4.0K  796M   1% /run/user/1000
:/home/qndell15/xx 1000G     0 1000G   0% /home/ubuntu/xx

以上参数基本满足了日常操作, 详细命令可以参考 multipass 官网:

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