linux kvm lxc docker - hanyong/note GitHub Wiki

linux 虚拟机与容器


KVM/QEMU


运行 qemu

qemu-system-x86_64


qemu-system-x86_64 -smp 1 -m 1G -cdrom ubuntu-14.04.4-server-amd64.iso


qemu 命令行

  • 优点: 简单直接, 不需要 root 权限
  • 缺点
    • 命令输入繁琐
    • 手工维护网络结构和 MAC 地址
    • 前台运行, 缺乏维护管理, 原始工具

libvirt

libvirt: The virtualization API, https://libvirt.org/

  • 标准化 VM API, 支持 KVM/QEMU , Xen, VMware, VirtualBox, LXC
  • 网络管理, 自动生成 MAC 地址
  • xml 格式配置文件
  • 后台 daemon (后台管理 qemu 进程)
  • 命令行工具: virsh
  • openstack 等云计算平台的底层工具

libvirt 网络配置


libvirt 网络配置: bridge


自定义 bridge:

!xml
<network>
  <name>local</name>
  <bridge name="br0"/>
  <forward mode="bridge"/>
</network>

libvirt 网络配置: macvlan (bridge mode)


!xml
<network>
  <name>vlan</name>
  <forward mode="bridge">
	<interface dev="eth0"/>
  </forward>
</network>
  • 区别: 去掉指定网桥, 添加桥接设备.

wireless 限制

  • 无线安全性默认只允许通过一个 MAC 地址, 有线网络也可以进行安全设置
  • fallback: NAT, 不能直接对外互通
  • fallback: Proxy ARP 3 层网桥, 不支持 2 层协议 dhcp、mDNS 等.
  • vmware, virtualbox: MAC 地址转换, 内核模块支持(?)

kvm 虚拟机配置

http://libvirt.org/formatnetwork.html

基础配置:

!xml
<domain type='kvm'>
  <name>node1</name>
  <os>
	<type arch='x86_64' machine='pc'>hvm</type>
	<boot dev='hd'/>
	<boot dev='cdrom'/>
  </os>
  <features>
	<acpi/>
	<apic/>
  </features>
  <clock offset='localtime'/>
  <cpu mode='host-passthrough'/>
  <vcpu placement='static'>1</vcpu>
  <memory unit='MiB'>1024</memory>
  ... ...
</domain>

设备配置:

!xml
<domain>
  ... ...
  <devices>
	<console type='pty'>
	  <target type='virtio'/>
	</console>
	<graphics type='vnc'/>
	<interface type='network'>
	  <source network='local'/>
	  <model type='virtio'/>
	</interface>
	<disk type='block' device='disk'>
	  <driver name='qemu' type='raw' cache='none'/>
	  <source dev='/dev/vg/node1'/>
	  <target dev='vda' bus='virtio'/>
	</disk>
	<disk type='file' device='cdrom'>
	  <driver name='qemu' type='raw' cache='none'/>
	  <source file='/repo/software/ubuntu/ubuntu-14.04.4-server-amd64.iso'/>
	  <target dev='hdc'/>
	</disk>
  </devices>
</domain>

虚拟机操作

  • virsh create node1.xml
  • virsh define node1.xml
  • virsh edit node1
  • virsh start node1
  • virsh console node1
  • virsh shutdown node1
  • virsh destroy node1
  • virsh net-* local

使用 VNC 连接虚拟机

  • vncviewer localhost:0
  • VNC 端口从 5900 开始, ":0" 表示 DISPLAY 0, 即 5900 端口
  • 需要查询和记住每个 vm 的 VNC 端口号?


virt-manager


主窗口


vm 控制台


vm 配置


SPICE: 图形桌面集成




KVM 小结

  • KVM
  • QEMU
  • libvirt
  • spice
  • virt-manager, 整合 + GUI

linux KVM 高级功能

纯命令行方式使用 vm, 直接连接 console

$ virsh console node1
Connected to domain node1
Escape character is ^]

Ubuntu 15.10 node1 hvc0

node1 login: hanyong
Password: 
Last login: Thu Apr 21 13:49:39 CST 2016 on tty1
Welcome to Ubuntu 15.10 (GNU/Linux 4.2.0-16-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
hanyong@node1:~$ 

linux KVM 高级功能

透传宿主机文件系统到 vm

 

!xml
<filesystem type='mount' accessmode='passthrough'>
  <driver type='path' wrpolicy='immediate'/>
  <source dir='/repo/vm/node1'/>
  <target dir='/host'/>
</filesystem>

linux KVM 高级功能

直接启动 linux 内核

  • QEMU -> BIOS -> MBR -> grub -> kernel -> init -> app
  • QEMU ------------------------> kernel -> init -> app

 

!xml
<os>
  <kernel>/repo/vm/node1/boot/vmlinuz-4.2.0-16-generic</kernel>
  <initrd>/repo/vm/node1/boot/initrd.img-4.2.0-16-generic</initrd>
  <cmdline>root=/dev/mapper/vg-root console=hvc0</cmdline>
</os>

$ virsh start --console node1
... ...
[  OK  ] Reached target Host and Network Name Lookups.
[  OK  ] Started Update UTMP about System Runlevel Changes.

Ubuntu 15.10 node1 hvc0

node1 login: hanyong
Password: 
Last login: Thu Apr 21 15:37:38 CST 2016 on hvc0
hanyong@node1:~$ cat /proc/cmdline 
root=/dev/mapper/vg-root console=hvc0
hanyong@node1:~$ 

LXC

  • 透传文件系统
  • 直接启动 init
  • cgroup 资源管控

总结

  • VM 1: QEMU -> BIOS -> MBR -> grub -> kernel -> init -> app
  • VM 2: QEMU ------------------------> kernel -> init -> app
  • LXC: LXC ------------------------------------> init -> app
  • docker: docker-daemon -------------------------------> app
⚠️ **GitHub.com Fallback** ⚠️