hoatranlab.io.vn Zalo: 0917516878 Hotline: 0917516878 [email protected]
HoaTranLab Logo HoaTranLab
Linux Bible 11th Edition • Chapter 28

Using Linux for Cloud Computing

Thiết lập môi trường cloud với KVM/QEMU hypervisor, quản lý máy ảo với virt-manager và virsh, cấu hình shared storage và live migration.

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)

Terminal - Cài đặt KVM/QEMU trên Fedora

# 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

Terminal - Các lệnh virsh cơ bản

# 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

Terminal - Tạo VM mới 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

Terminal - Cấu hình NFS Storage Pool

# 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.

Terminal - Live migration VM giữa 2 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.

1

Kiểm tra hardware hỗ trợ virtualization

Xác nhận CPU có hỗ trợ VT-x (Intel) hoặc AMD-V:

Terminal

$ grep -E 'vmx|svm' /proc/cpuinfo | head -1

flags : ... vmx ept vpid ...

# vmx = Intel VT-x, svm = AMD-V

2

Cài đặt KVM và virt-manager

Terminal

$ 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

3

Tải Fedora Cloud Image

Terminal

$ 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

4

Tạo VM với virt-install

Terminal

$ 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 &

5

Quản lý VM với virsh

Terminal

$ 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]

6

Snapshot và Clone VM

Terminal

$ 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

1

KVM là gì và vai trò của nó trong virtualization?

KVM (Kernel-based Virtual Machine) là module kernel Linux cho phép Linux kernel hoạt động như hypervisor. KVM cho phép VMs tương tác trực tiếp với hardware của host, đạt hiệu năng gần native. KVM làm việc cùng QEMU (emulator phần cứng) và libvirt (management layer).
2

Sự khác biệt giữa virsh và virt-manager?

virsh là CLI tool — dùng lệnh để quản lý VMs (start, stop, list, migrate, snapshot). Phù hợp để automation và scripting. virt-manager là GUI application — cung cấp giao diện đồ họa để tạo, cấu hình và quản lý VMs. Cả hai đều giao tiếp với libvirtd daemon.
3

Live migration yêu cầu điều kiện gì?

Live migration cần: (1) Shared storage (NFS, iSCSI, hoặc Ceph) giữa cả hai hypervisors để disk image của VM có thể truy cập từ cả hai; (2) SSH connectivity giữa các hypervisors; (3) Cùng CPU architecture; (4) libvirtd chạy trên cả hai hosts; (5) Network connectivity đủ tốt.
4

Storage pool trong libvirt là gì?

Storage pool là vùng lưu trữ được định nghĩa cho libvirt sử dụng để tạo và quản lý disk images của VMs. Có thể là: local directory (default: /var/lib/libvirt/images/), NFS share, iSCSI target, hoặc LVM volume group. Dùng virsh pool-list --all để xem.
5

Lệnh nào kiểm tra xem CPU có hỗ trợ hardware virtualization không?

grep -E 'vmx|svm' /proc/cpuinfovmx 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.
6

Format qcow2 của disk image khác raw như thế nào?

qcow2 (QEMU Copy-On-Write v2): hỗ trợ snapshots, thin provisioning (chỉ dùng dung lượng thực), compression, encryption. File nhỏ hơn khi mới tạo. raw: không có overhead, tốc độ I/O tối đa nhưng không có tính năng nâng cao, file luôn full size.
7

Tại sao cần NFS shared storage trong cloud environment?

NFS shared storage cho phép tất cả hypervisors truy cập cùng VM disk images. Điều này cần thiết cho: (1) Live migration — VM có thể chạy trên bất kỳ hypervisor nào; (2) High availability — nếu một hypervisor fail, VMs có thể restart trên hypervisor khác; (3) Quản lý tập trung một bộ images.
8

Lệnh virsh snapshot-create-as dùng để làm gì?

Tạo snapshot của VM tại thời điểm hiện tại. Cú pháp: 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.