อยากได้ container หรือ virtual machine บน Linux Mint ที่ใช้งานประจำ เพื่อจะได้ติดตั้งระบบต่างๆ ที่ต้องการทดลองเล่นเรียนรู้ เมื่อก่อนใช้ Virtual Box เพราะง่ายสะดวกดี แต่ก็ต้องแลกด้วยทรัพยากร ได้พบรู้จักคำว่า LXC ก็ตอนที่ทดลองใช้งาน Proxmox Virtual Environment

จากการค้นหาข้อมูลเบื้องต้นพบว่า LXC เป็น container ชนิดหนึ่งที่เบาและเร็ว ใช้ Kernel ร่วมกันกับ host แต่ทำการ isolate แยกการทำงานออกจากในระดับของ Linux และด้วยการใช้ cgroups จึงสามารถบริหารจัดการทรัพยากรได้โดยไม่จำเป็นที่จะต้อง restart container นั้นๆ

Linux Containers (LXC) is an operating-system-level virtualization method for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.

The Linux kernel provides the cgroups functionality that allows limitation and prioritization of resources (CPU, memory, block I/O, network, etc.) without the need for starting any virtual machines, and also the namespace isolation functionality that allows complete isolation of an application's view of the operating environment, including process trees, networking, user IDs and mounted file systems.[2]

LXC combines the kernel's cgroups and support for isolated namespaces to provide an isolated environment for applications. Early versions of Docker used LXC as the container execution driver, though LXC was made optional in v0.9 and support was dropped in Docker v1.10.[3][4] References to Linux containers commonly refer[5] to Docker containers running on Linux.

LXD เป็นตัวที่ช่วยบริหารจัดการ LXC ซึ่งการติดตั้ง LXD นั้นต้องติดตั้งผ่าน snapd แต่ Linux Mint ที่ใช้งานอยู่ ปิดการติดตั้ง SNAPD ไว้ (เปิดได้) โชคยังดีที่มี Raspberry Pi 4 8 GB พร้อม 1 TB HDD USB ใช้อยู่ จึงทำการติดตั้ง LXD/LXC บน RPi4 แทน

การติดตั้ง

  • ติดตั้ง snapd และ tools สำหรับ bridge network.
.
# apt update
# apt install -y snapd bridge-utils
.
  • ทำการติดตั้ง snapd core และ lxd พร้อมกับ initial lxd
    หากต้องการใช้งาน lxc ภายใต้ user profile ก็ให้ทำการเพิ่ม user นั้นเข้า group lxd
.
# snap install core lxd
# lxd init
# usermod -aG lxd choo
.
  • หากต้องการให้ container สามารถเข้าถึงได้จาก network ภายนอก ก็ต้องทำการสร้าง bridge network บนเครื่อง host และกำหนดให้ container นั้นใช้ network จาก bridge interface

    สร้างไฟล์ br0 ใน /etc/network/interfaces.d พร้อมกำหนดค่าต่าง ดั่งตัวอย่างนี้ เป็นการสร้าง network interface ที่ชื่อว่า br0 บน eth0 และรับ IP จาก DHCP แล้วทำการ restart netwokr-manager

    (eth0 นั่นคือ nic ชื่ออาจจะแตกต่างกันขึ้นอยู่กับแต่เครื่อง หรือ OS สามารถดูชื่อได้จาก ip a หรือ ifconfig)

auto br0
iface br0 inet dhcp
	bridge_ports eth0
.
# systemctl restart network-manager
.
  • กำหนดค่าเริ่มต้นให้กับ default profile ใช้ br0 สามารถ edit default ได้ด้วยคำสั่ง lxc profile edit default

คำสั่งพื้นฐาน และการใช้งานเบื้องต้น

  • lxc list เป็นการแสดง containers ที่ได้ติดตั้งใช้งานอยู่
  • lxc image list เป็นการดูข้อมูลว่ามี image อะไรอยู่บนเครื่องแล้วบ้าง
  • lxc image list images:debian เป็นคำสั่งค้นหา image
.
rpi4-01:~ $ lxc list
+-----------+---------+---------------------+------+-----------+-----------+
|   NAME    |  STATE  |        IPV4         | IPV6 |   TYPE    | SNAPSHOTS |
+-----------+---------+---------------------+------+-----------+-----------+
| ct-01     | STOPPED |                     |      | CONTAINER | 0         |
+-----------+---------+---------------------+------+-----------+-----------+
| ct-02     | RUNNING | 172.16.0.164 (eth0) |      | CONTAINER | 0         |
+-----------+---------+---------------------+------+-----------+-----------+
| ct-docker | STOPPED |                     |      | CONTAINER | 0         |
+-----------+---------+---------------------+------+-----------+-----------+

rpi4-01:~ $ lxc image list
+-------+--------------+--------+----------------------------------------+--------------+-----------+---------+------------------------------+
| ALIAS | FINGERPRINT  | PUBLIC |              DESCRIPTION               | ARCHITECTURE |   TYPE    |  SIZE   |         UPLOAD DATE          |
+-------+--------------+--------+----------------------------------------+--------------+-----------+---------+------------------------------+
|       | 745608884f64 | no     | Debian bullseye arm64 (20220929_05:25) | aarch64      | CONTAINER | 80.50MB | Sep 29, 2022 at 8:32am (UTC) |
+-------+--------------+--------+----------------------------------------+--------------+-----------+---------+------------------------------+
.
  • การสร้าง container นั้น ต้องรู้ก่อนว่าจะใช้ image อะไร เช่น debian, ubuntu, alpine, etc. ในตัวอย่างนี้จะใช้ debian 11 bulleye

    หากบนเครื่องฯ ไม่มี image นั้นอยู่ คำสั่งจะทำการ download image มาไว้บนแล้วสร้าง container
.
$ lxc launch images:debian/11 <name_of_container>
.
  • คำสั่ง start/stop container

ข้อมูลอ้างอิง

  • https://en.wikipedia.org/wiki/LXC
  • https://linuxcontainers.org/lxc/introduction/
  • https://ubuntu.com/blog/lxd-on-debian-using-snapd

Leave a Reply