🏠hoatranlab.io.vn 💬Zalo: 0917516878 📞0917516878 ✉️[email protected]

Phase 6: LXC Containers + OCI Images

Bài 6.1: LXC vs KVM — khi nào dùng cái nào?

Lý thuyết cốt lõi

LXC = OS-level container (share kernel với host), KVM = full hardware virtualization.

Tiêu chí LXC KVM
Overhead Rất thấp Cao hơn 5–15%
Boot time <1 giây 10–60 giây
Kernel Share với host Riêng
OS Chỉ Linux Linux/Windows/BSD
Snapshot Đơn giản (ZFS/thin) Built-in
Isolation Yếu hơn Mạnh
Memory dynamic Tốt Balloon only

Dùng LXC khi: Linux microservice, web server, database, DNS, CI runner.
Dùng KVM khi: Windows, kernel-level workload (Docker-in-Docker, iptables custom), isolation cao.

Privileged vs Unprivileged:

  • Unprivileged (mặc định, an toàn): UID mapping 0→100000 trên host
  • Privileged (nguy hiểm): root container = root host → chỉ dùng khi cần (Docker in LXC)

Bài tập thực hành

Tạo LXC Ubuntu:

# 1. Download template
pveam update
pveam available | grep ubuntu-24
pveam download local ubuntu-24.04-standard_24.04-2_amd64.tar.zst

# 2. Tạo container
pct create 200 local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst \
    --hostname web-lxc-01 \
    --cores 2 --memory 1024 --swap 512 \
    --rootfs local-zfs:10 \
    --net0 name=eth0,bridge=vmbr1,tag=10,ip=10.0.10.60/24,gw=10.0.10.1 \
    --nameserver 8.8.8.8 \
    --password 'SecureP@ss123' \
    --unprivileged 1 \
    --features nesting=1

# 3. Start + enter
pct start 200
pct enter 200

Kết quả đầu ra

$ pct list
VMID       Status     Lock         Name
200        running                 web-lxc-01

$ pct exec 200 -- hostname
web-lxc-01

$ pct exec 200 -- ip -br addr
lo               UNKNOWN        127.0.0.1/8
eth0@if123       UP             10.0.10.60/24

Ứng dụng thực tế

Docker trong LXC unprivileged: set --features nesting=1,keyctl=1 → chạy Docker bên trong được (development only, prod nên dùng VM riêng).


Bài 6.2: OCI container images trong LXC (VE 9 tech preview)

Lý thuyết cốt lõi

VE 9 hỗ trợ pull image từ Docker Hub / GHCR rồi convert thành LXC template. Feature tech preview — không dùng production, chủ yếu cho lab/dev.

Workflow:

  1. Pull OCI image (skopeo hoặc docker pull)
  2. Convert sang LXC tarball
  3. Tạo LXC từ template đó

Bài tập thực hành

# 1. Install skopeo + umoci (thường có sẵn VE 9)
apt install -y skopeo umoci

# 2. Pull image nginx từ Docker Hub
mkdir -p /tmp/oci-nginx && cd /tmp/oci-nginx
skopeo copy docker://nginx:alpine oci:./nginx:alpine

# 3. Unpack
umoci unpack --image ./nginx:alpine ./nginx-bundle

# 4. Tar thành LXC template
cd nginx-bundle/rootfs
tar -czf /var/lib/vz/template/cache/nginx-oci.tar.gz .

# 5. Tạo LXC từ template này
pct create 201 local:vztmpl/nginx-oci.tar.gz \
    --hostname nginx-oci \
    --cores 1 --memory 256 \
    --rootfs local-zfs:1 \
    --net0 name=eth0,bridge=vmbr1,ip=10.0.10.61/24,gw=10.0.10.1 \
    --unprivileged 1

pct start 201
pct exec 201 -- nginx -g 'daemon off;' &

Kết quả đầu ra

$ curl http://10.0.10.61
<!DOCTYPE html>
<html>
<head><title>Welcome to nginx!</title></head>
...

Troubleshooting

  • Image không có init system: OCI image thường chỉ có 1 process → LXC phải override entrypoint
  • Volume mount: dùng pct set 201 --mp0 /host/path,mp=/container/path

Ứng dụng thực tế

Lab: test nhanh Nginx, Redis, PostgreSQL mà không cần build template từ đầu.

Cảnh báo: Production vẫn nên dùng full LXC (Debian/Ubuntu template) hoặc Kubernetes cho multi-container workload.


Bài 6.3: Backup + restore LXC

Lý thuyết cốt lõi

Backup LXC dùng vzdump giống VM. Có 3 mode:

  • Snapshot: nhanh, VM không pause (yêu cầu ZFS/LVM-thin)
  • Suspend: pause ~5 giây, an toàn
  • Stop: shutdown full, downtime

Format nén: lzo, gzip, zstd (mặc định VE 9, tốt nhất).

Bài tập thực hành

# Backup 1 container
vzdump 200 --mode snapshot --compress zstd --storage local \
    --notes-template "Manual backup {{guestname}} at {{time}}"

# Schedule backup (Web GUI: Datacenter > Backup > Add)
# Hoặc cron:
cat > /etc/cron.d/pve-backup-nightly <<'EOF'
# Backup all CT+VM hàng đêm 2h sáng
0 2 * * * root vzdump --all --compress zstd --storage backup-nfs --mode snapshot --prune-backups keep-daily=7,keep-weekly=4
EOF

# Restore
pct restore 210 /var/lib/vz/dump/vzdump-lxc-200-2026_04_22-02_00_00.tar.zst \
    --storage local-zfs --rootfs local-zfs:10

Kết quả đầu ra

INFO: starting new backup job: vzdump 200 --mode snapshot --compress zstd
INFO: Starting Backup of VM 200 (lxc)
INFO: backup mode: snapshot
INFO: creating vzdump archive '/var/lib/vz/dump/vzdump-lxc-200-2026_04_22-11_30_45.tar.zst'
INFO: Total bytes written: 524288000 (500MiB, 85MiB/s)
INFO: archive file size: 127MB
INFO: Finished Backup of VM 200 (00:00:06)

Ứng dụng thực tế

Dùng PBS (Proxmox Backup Server) thay vzdump file → dedup + incremental (xem Phase 8).