linux kvm lxc docker - hanyong/note GitHub Wiki
- KVM (Kernel-based Virtual Machine): http://www.linux-kvm.org/
- QEMU (Quick Emulator): http://wiki.qemu.org/Main_Page
- linux 下 qemu 默认支持 KVM
- 不需要 root 权限 (取决于
/dev/kvm
权限)
qemu-system-x86_64
qemu-system-x86_64 -smp 1 -m 1G -cdrom ubuntu-14.04.4-server-amd64.iso
- 优点: 简单直接, 不需要 root 权限
- 缺点
- 命令输入繁琐
- 手工维护网络结构和 MAC 地址
- 前台运行, 缺乏维护管理, 原始工具
libvirt: The virtualization API, https://libvirt.org/
- 标准化 VM API, 支持 KVM/QEMU , Xen, VMware, VirtualBox, LXC 等
- 网络管理, 自动生成 MAC 地址
- xml 格式配置文件
- 后台 daemon (后台管理 qemu 进程)
- 命令行工具: virsh
- openstack 等云计算平台的底层工具
- http://events.linuxfoundation.jp/sites/events/files/slides/LinuxConJapan2014_makita_0.pdf
- http://libvirt.org/formatnetwork.html
自定义 bridge:
!xml
<network>
<name>local</name>
<bridge name="br0"/>
<forward mode="bridge"/>
</network>
!xml
<network>
<name>vlan</name>
<forward mode="bridge">
<interface dev="eth0"/>
</forward>
</network>
- 区别: 去掉指定网桥, 添加桥接设备.
- 无线安全性默认只允许通过一个 MAC 地址, 有线网络也可以进行安全设置
- fallback: NAT, 不能直接对外互通
- fallback: Proxy ARP 3 层网桥, 不支持 2 层协议 dhcp、mDNS 等.
- vmware, virtualbox: MAC 地址转换, 内核模块支持(?)
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
- vncviewer localhost:0
- VNC 端口从 5900 开始, ":0" 表示 DISPLAY 0, 即 5900 端口
- 需要查询和记住每个 vm 的 VNC 端口号?
- virt-manager: https://virt-manager.org/
- 图形化管理工具, 直观显示
- virt-viewer
主窗口
vm 控制台
vm 配置
- SPICE: http://www.spice-space.org/
- 类似 vmware/virtualbox 的图形桌面集成体验
- server, guest, client 结构
- KVM
- QEMU
- libvirt
- spice
- virt-manager, 整合 + GUI
$ 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:~$
- http://www.linux-kvm.org/page/9p_virtio
- vm 添加 9p 模块:
echo 9p >> /etc/modules
!xml
<filesystem type='mount' accessmode='passthrough'>
<driver type='path' wrpolicy='immediate'/>
<source dir='/repo/vm/node1'/>
<target dir='/host'/>
</filesystem>
- 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:~$
- 透传文件系统
- 直接启动 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