Mục tiêu học tập
KVM/QEMU Hypervisor
Hiểu kiến trúc KVM (Kernel-based Virtual Machine) và QEMU, cách chúng hoạt động cùng nhau để tạo môi trường ảo hóa.
Virtual Machine Manager
Sử dụng virt-manager GUI và virsh CLI để cài đặt, cấu hình và quản lý virtual machines trên hypervisor.
virt-install Command
Tạo và cấu hình VM bằng lệnh virt-install từ command line với các options cho RAM, vCPU, disk, network.
Live Migration
Di chuyển virtual machines đang chạy giữa các hypervisor mà không làm gián đoạn dịch vụ sử dụng shared storage.
Storage Pools
Cấu hình NFS shared storage pools cho hypervisors, tạo storage volumes cho virtual machines.
Virtual Networks & Bridges
Cấu hình virtual network bridges để VMs có thể giao tiếp với nhau và với mạng ngoài.
Lý thuyết: Linux Cloud Computing
1. Kiến trúc Cloud cơ bản
Một private cloud cơ bản gồm các thành phần: Hypervisors (KVM/QEMU), Virtual Machines, Shared Storage (NFS/iSCSI/Ceph), Virtual Networks, và Cloud Controller (virt-manager/OpenStack).
KVM (Kernel-based Virtual Machine)
Module kernel Linux cho phép VMs tương tác trực tiếp với hardware. Mỗi VM chạy như một tiến trình Linux riêng biệt.
QEMU (Quick Emulator)
Một tiến trình qemu chạy cho mỗi VM, cung cấp hardware emulation (CPU, RAM, disk, network) cho VM.
libvirtd
Daemon quản lý VMs trên mỗi hypervisor. Nhận requests từ virt-manager/virsh/OpenStack để start/stop/manage VMs.
virt-viewer
Công cụ mở console window để tương tác với VM, cung cấp giao diện đồ họa hoặc terminal cho VM đang chạy.
2. Cloud Storage
NFS (đơn giản)
Shared filesystem qua network. Dễ thiết lập cho môi trường thử nghiệm. Mount cùng mount point trên nhiều hypervisors.
Ceph (enterprise)
Open source distributed storage. Hỗ trợ block và object storage. Fault tolerant, scalable. Phổ biến trong OpenStack.
Cloud Provider Storage
AWS: S3/EBS/EFS. Azure: Blob/Disks/Files. GCP: Cloud Storage/Persistent Disks/Filestore.
3. Cài đặt Hypervisor (KVM)
# Cài đặt các gói KVM, QEMU, libvirt, virt-manager
$ sudo dnf install @virtualization -y
Installing group/module packages:
virt-install qemu-kvm libvirt virt-manager virt-viewer
$ sudo systemctl enable --now libvirtd
Created symlink /etc/systemd/system/multi-user.target.wants/libvirtd.service
# Kiểm tra KVM có được hỗ trợ không
$ lsmod | grep kvm
kvm_intel 245760 0
kvm 970752 1 kvm_intel
# Thêm user vào group libvirt
$ sudo usermod -aG libvirt $(whoami)
4. Lệnh virsh quản lý VMs
# Liệt kê tất cả VMs
$ virsh list --all
Id Name State
-----------------------------
1 fedora42-01 running
- ubuntu22-web shut off
# Bật/tắt VM
$ virsh start ubuntu22-web
Domain 'ubuntu22-web' started
$ virsh shutdown fedora42-01
Domain 'fedora42-01' is being shutdown
$ virsh destroy fedora42-01
Domain 'fedora42-01' destroyed (force stop)
# Xem thông tin VM
$ virsh dominfo fedora42-01
Name: fedora42-01
UUID: 4a3e5b2c-1d8f-4e7a-9c2b-3f5d6a7e8b9c
OS Type: hvm
State: running
CPU(s): 2
Max memory: 4194304 KiB
# Suspend và resume VM
$ virsh suspend fedora42-01
$ virsh resume fedora42-01
# Xem console VM
$ virsh console fedora42-01
5. Tạo VM với virt-install
# Tạo Fedora VM từ ISO
$ sudo virt-install \
--name fedora42-vm \
--ram 4096 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/fedora42.qcow2,size=20 \
--cdrom /tmp/Fedora-42-x86_64-dvd.iso \
--os-variant fedora42 \
--network network=default \
--graphics spice
Starting install...
Creating domain... 0 B 00:00
Domain is still running. Installation may be complete.
# Import từ existing qcow2 image
$ sudo virt-install \
--import \
--name cloud-vm-01 \
--ram 2048 --vcpus 1 \
--disk path=/var/lib/libvirt/images/fedora-cloud.qcow2,format=qcow2 \
--os-variant fedora42 \
--network network=default
6. Storage Pools và NFS Shared Storage
# Trên NFS Server - export thư mục shared
$ sudo mkdir -p /var/lib/libvirt/shared
$ echo "/var/lib/libvirt/shared 192.168.122.0/24(rw,sync,no_root_squash)" | sudo tee -a /etc/exports
$ sudo systemctl enable --now nfs-server
$ sudo exportfs -a
# Trên Hypervisor - tạo storage pool từ NFS
$ virsh pool-define-as --name shared-nfs \
--type netfs \
--source-host 192.168.122.100 \
--source-path /var/lib/libvirt/shared \
--target /var/lib/libvirt/shared
$ virsh pool-start shared-nfs
$ virsh pool-autostart shared-nfs
$ virsh pool-list --all
Name State Autostart
----------------------------------
default active yes
shared-nfs active yes
7. Live Migration
Live migration cho phép di chuyển VM đang chạy từ hypervisor này sang hypervisor khác mà không ngắt dịch vụ. Yêu cầu shared storage giữa các hypervisors.
# Migrate VM từ hypervisor1 sang hypervisor2
$ virsh migrate --live fedora42-01 \
qemu+ssh://hypervisor2/system \
--verbose
Migration: [100 %]
Domain 'fedora42-01' migrated successfully.
# Kiểm tra VM đã được migrate sang hypervisor2
$ virsh -c qemu+ssh://hypervisor2/system list
Id Name State
-----------------------------
2 fedora42-01 running
8. Khái niệm Cloud (Authentication, Controllers)
Cloud Authentication
- • AWS: IAM (Identity and Access Management)
- • Azure: Entra ID (formerly Azure AD)
- • Google Cloud: IAM và Service Accounts
- • Private Cloud: FreeIPA, Kerberos, LDAP
Cloud Controllers
- • virt-manager: GUI cho mini cloud
- • OpenStack: Full-blown cloud platform
- • Red Hat RHV: Enterprise virtualization
- • oVirt: Open source RHV upstream
Lab Thực Hành
Thiết lập hypervisor KVM, tạo và quản lý virtual machine với virt-install, cấu hình network bridge cho VMs.
Kiểm tra hardware hỗ trợ virtualization
Xác nhận CPU có hỗ trợ VT-x (Intel) hoặc AMD-V:
$ grep -E 'vmx|svm' /proc/cpuinfo | head -1
flags : ... vmx ept vpid ...
# vmx = Intel VT-x, svm = AMD-V
Cài đặt KVM và virt-manager
$ sudo dnf install @virtualization -y
$ sudo systemctl enable --now libvirtd
$ sudo usermod -aG libvirt $USER
# Logout và login lại để áp dụng group membership
Tải Fedora Cloud Image
$ cd /var/lib/libvirt/images/
$ sudo wget https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2
Fedora-Cloud-Base...qcow2 100% 432MB 45.2MB/s in 9.6s
Tạo VM với virt-install
$ sudo qemu-img create -f qcow2 \
-o backing_file=Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2 \
-F qcow2 myvm-01.qcow2
Formatting 'myvm-01.qcow2', fmt=qcow2, cluster_size=65536
$ sudo virt-install --import --name myvm-01 \
--ram 2048 --vcpus 2 \
--disk path=myvm-01.qcow2,format=qcow2,bus=virtio \
--os-variant fedora42 --network network=default &
Quản lý VM với virsh
$ virsh list --all
Id Name State
1 myvm-01 running
$ virsh domifaddr myvm-01
Name MAC address Protocol Address
vnet0 52:54:00:ab:cd:ef ipv4 192.168.122.45/24
$ ssh [email protected]
Snapshot và Clone VM
$ virsh snapshot-create-as myvm-01 snap-01 \
--description "Before web install"
Domain snapshot snap-01 created
$ virsh snapshot-list myvm-01
Name Creation Time State
snap-01 2025-04-01 09:15:32 +0000 running
$ virt-clone --original myvm-01 --name myvm-02 --auto-clone
Cloning 'myvm-01'... 100% [===================]
Câu hỏi ôn tập
KVM là gì và vai trò của nó trong virtualization?
Sự khác biệt giữa virsh và virt-manager?
Live migration yêu cầu điều kiện gì?
Storage pool trong libvirt là gì?
virsh pool-list --all để xem.
Lệnh nào kiểm tra xem CPU có hỗ trợ hardware virtualization không?
grep -E 'vmx|svm' /proc/cpuinfo — vmx là Intel VT-x, svm là AMD-V. Hoặc lsmod | grep kvm để kiểm tra module đã load. Ngoài ra virt-host-validate kiểm tra toàn diện system cho virtualization.
Format qcow2 của disk image khác raw như thế nào?
Tại sao cần NFS shared storage trong cloud environment?
Lệnh virsh snapshot-create-as dùng để làm gì?
virsh snapshot-create-as <domain> <name> --description "...". Snapshot lưu trạng thái RAM, CPU, và disk, cho phép revert về trạng thái đó nếu cần. Dùng virsh snapshot-revert để khôi phục.