Install LXD Linux Container on Ubuntu 24.04

Install LXD Linux Container pada Ubuntu 24.04

  • Update repository ubuntu
apt update && apt upgrade -y
  • Install Snap
apt install snap -y
  • Install LXD dengan snap
snap install lxd
  • Cek versi lxd
lxd version
---<output>---
5.21.1 LTS

Initialize LXD

  • Jalankan lxd init
lxd init
---<output>---
Would you like to use LXD clustering? (yes/no) [default=no]: no
Do you want to configure a new storage pool? (yes/no) [default=yes]: yes
Name of the new storage pool [default=default]: sys-ops.id
Name of the storage backend to use (powerflex, zfs, btrfs, ceph, dir, lvm) [default=zfs]: lvm
Create a new LVM pool? (yes/no) [default=yes]: yes
Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]: no
Size in GiB of the new loop device (1GiB minimum) [default=8GiB]: 30GiB
Would you like to connect to a MAAS server? (yes/no) [default=no]: no
Would you like to create a new local network bridge? (yes/no) [default=yes]: yes
What should the new bridge be called? [default=lxdbr0]: lxdbr0
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 192.168.10.1/24
Would you like LXD to NAT IPv4 traffic on your bridge? [default=yes]: yes
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: none
Would you like the LXD server to be available over the network? (yes/no) [default=no]: no
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]: yes
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: no
  • Verifikasi LXD
lxc profile list
---<output>---
+---------+---------------------+---------+
|  NAME   |     DESCRIPTION     | USED BY |
+---------+---------------------+---------+
| default | Default LXD profile | 0       |
+---------+---------------------+---------+

lxc profile show default
---<output>---
name: default
description: Default LXD profile
config: {}
devices:
  eth0:
    name: eth0
    network: lxdbr0
    type: nic
  root:
    path: /
    pool: sys-ops.id
    type: disk
used_by: []

lxc network list
---<output>---
+--------+----------+---------+-----------------+------+-------------+---------+---------+
|  NAME  |   TYPE   | MANAGED |      IPV4       | IPV6 | DESCRIPTION | USED BY |  STATE  |
+--------+----------+---------+-----------------+------+-------------+---------+---------+
| ens160 | physical | NO      |                 |      |             | 0       |         |
+--------+----------+---------+-----------------+------+-------------+---------+---------+
| lxdbr0 | bridge   | YES     | 192.168.10.1/24 | none |             | 1       | CREATED |
+--------+----------+---------+-----------------+------+-------------+---------+---------+

lxc network show lxdbr0
---<output>---
name: lxdbr0
description: ""
type: bridge
managed: true
status: Created
config:
  ipv4.address: 192.168.10.1/24
  ipv4.nat: "true"
  ipv6.address: none
used_by:
- /1.0/profiles/default
locations:
- none

lxc storage list
---<output>---
+------------+--------+-----------------------------------------------+-------------+---------+---------+
|    NAME    | DRIVER |                    SOURCE                     | DESCRIPTION | USED BY |  STATE  |
+------------+--------+-----------------------------------------------+-------------+---------+---------+
| sys-ops.id | lvm    | /var/snap/lxd/common/lxd/disks/sys-ops.id.img |             | 1       | CREATED |
+------------+--------+-----------------------------------------------+-------------+---------+---------+

lxc storage show sys-ops.id
---<output>---
name: sys-ops.id
description: ""
driver: lvm
status: Created
config:
  lvm.thinpool_name: LXDThinPool
  lvm.vg_name: sys-ops.id
  size: 30GiB
  source: /var/snap/lxd/common/lxd/disks/sys-ops.id.img
used_by:
- /1.0/profiles/default
locations:
- none
  • Perintah dasar lxc
lxc <commands>
--------------------
Available Commands:
  alias       Manage command aliases
  auth        Manage user authorization
  cluster     Manage cluster members
  config      Manage instance and server configuration options
  console     Attach to instance consoles
  copy        Copy instances within or in between LXD servers
  delete      Delete instances and snapshots
  exec        Execute commands in instances
  export      Export instance backups
  file        Manage files in instances
  help        Help about any command
  image       Manage images
  import      Import instance backups
  info        Show instance or server information
  init        Create instances from images
  launch      Create and start instances from images
  list        List instances
  monitor     Monitor a local or remote LXD server
  move        Move instances within or in between LXD servers
  network     Manage and attach instances to networks
  operation   List, show and delete background operations
  pause       Pause instances
  profile     Manage profiles
  project     Manage projects
  publish     Publish instances as images
  query       Send a raw query to LXD
  rebuild     Rebuild instances
  remote      Manage the list of remote servers
  rename      Rename instances and snapshots
  restart     Restart instances
  restore     Restore instances from snapshots
  snapshot    Create instance snapshots
  start       Start instances
  stop        Stop instances
  storage     Manage storage pools and volumes
  version     Show local and remote versions
  warning     Manage warnings

Menambahkan LXC Container

  • Cek lxc images
lxc image list images:
lxc image list ubuntu:
lxc image list ubuntu:22.04
lxc image list ubuntu:24.04
lxc image list ubuntu:24.04 | grep -i container
lxc image list ubuntu:24.04 | grep -i virtual-machine
lxc image list images: | grep -i almalinux
lxc image list images: | grep -i almalinux/8 | grep -i x86_64 | grep -i container
lxc image list images: | grep -i almalinux/8 | grep -i x86_64 | grep -i virtual-machine
  • Menambahkan LXC Container
  • Gunakan image ubuntu:24.04 dengan nama container ubuntu-2404-ct
  • Gunakan image almalinux/8/amd64 dengan nama container almalinux-8-ct
lxc launch ubuntu:24.04 ubuntu-2404-ct
lxc launch images:almalinux/8/amd64 almalinux-8-ct
  • Cek lxc images
lxc image list
---<output>---
+-------+--------------+--------+---------------------------------------------+--------------+-----------+-----------+------------------------------+
| ALIAS | FINGERPRINT  | PUBLIC |                 DESCRIPTION                 | ARCHITECTURE |   TYPE    |   SIZE    |         UPLOAD DATE          |
+-------+--------------+--------+---------------------------------------------+--------------+-----------+-----------+------------------------------+
|       | 46e5040777b9 | no     | AlmaLinux 8 amd64 (20240518_0010)           | x86_64       | CONTAINER | 128.79MiB | May 18, 2024 at 5:06am (UTC) |
+-------+--------------+--------+---------------------------------------------+--------------+-----------+-----------+------------------------------+
|       | c9fba5728bfe | no     | ubuntu 24.04 LTS amd64 (release) (20240423) | x86_64       | CONTAINER | 238.37MiB | May 18, 2024 at 5:03am (UTC) |
+-------+--------------+--------+---------------------------------------------+--------------+-----------+-----------+------------------------------+
  • Set limit cpu dan memory lxc container
lxc config set ubuntu-2404-ct limits.cpu 1
lxc config set ubuntu-2404-ct limits.memory 512MB
lxc config set almalinux-8-ct limits.cpu 1
lxc config set almalinux-8-ct limits.memory 512MB
  • Cek status lxc
lxc list
---<output>---
+----------------+---------+-----------------------+------+-----------+-----------+
|      NAME      |  STATE  |         IPV4          | IPV6 |   TYPE    | SNAPSHOTS |
+----------------+---------+-----------------------+------+-----------+-----------+
| almalinux-8-ct | RUNNING | 192.168.10.69 (eth0)  |      | CONTAINER | 0         |
+----------------+---------+-----------------------+------+-----------+-----------+
| ubuntu-2404-ct | RUNNING | 192.168.10.220 (eth0) |      | CONTAINER | 0         |
+----------------+---------+-----------------------+------+-----------+-----------+

lxc list -c n,s,4,a,b,D,M,P,t,N,S
---<output>---
+----------------+---------+-----------------------+--------------+--------------+------------+---------------+----------+-----------+-----------+-----------+
|      NAME      |  STATE  |         IPV4          | ARCHITECTURE | STORAGE POOL | DISK USAGE | MEMORY USAGE% | PROFILES |   TYPE    | PROCESSES | SNAPSHOTS |
+----------------+---------+-----------------------+--------------+--------------+------------+---------------+----------+-----------+-----------+-----------+
| almalinux-8-ct | RUNNING | 192.168.10.69 (eth0)  | x86_64       | sys-ops.id   | 663.00MiB  | 21.6%         | default  | CONTAINER | 11        | 0         |
+----------------+---------+-----------------------+--------------+--------------+------------+---------------+----------+-----------+-----------+-----------+
| ubuntu-2404-ct | RUNNING | 192.168.10.220 (eth0) | x86_64       | sys-ops.id   | 918.74MiB  | 48.0%         | default  | CONTAINER | 24        | 0         |
+----------------+---------+-----------------------+--------------+--------------+------------+---------------+----------+-----------+-----------+-----------+

Menambahkan LXC Virtual Machine

  • Buat profile baru dengan nama vm
lxc profile create vm
  • Edit lxc profile vm
  • credential login: user: ubuntu ; password: ubuntu
lxc profile edit vm
---<isi file>---
config:
   user.user-data: |
     #cloud-config
     ssh_pwauth: yes
     
     users:
       - name: ubuntu
         passwd: "$6$iBF0eT1/6UPE2u$V66Rk2BMkR09pHTzW2F.4GHYp3Mb8eu81Sy9srZf5sVzHRNpHP99JhdXEVeN0nvjxXVmoA6lcVEhOOqWEd3Wm0"
         lock_passwd: false
         groups: lxd
         shell: /bin/bash
         sudo: ALL=(ALL) NOPASSWD:ALL
description: LXD profile for virtual machines
devices:
  config:
    source: cloud-init:config
    type: disk
name: vm
used_by:
  • Menambahkan LXC Virtual Machine
  • Gunakan image ubuntu:24.04 type vm dengan nama virtual machine ubuntu-2404-vm
  • Setting static IP address ubuntu-2404-vm dengan IP: 192.168.10.100
  • Set limit cpu dan memory lxc virtual machine
  • Start lxc vm
lxc init ubuntu:24.04 --vm ubuntu-2404-vm --profile default --profile vm
lxc config device override ubuntu-2404-vm eth0 ipv4.address=192.168.10.100
lxc config set ubuntu-2404-vm limits.cpu 2
lxc config set ubuntu-2404-vm limits.memory 1GB
lxc start ubuntu-2404-vm
  • Cek status lxc
lxc list
---<output>---
+----------------+---------+-------------------------+------+-----------------+-----------+
|      NAME      |  STATE  |          IPV4           | IPV6 |      TYPE       | SNAPSHOTS |
+----------------+---------+-------------------------+------+-----------------+-----------+
| almalinux-8-ct | RUNNING | 192.168.10.69 (eth0)    |      | CONTAINER       | 0         |
+----------------+---------+-------------------------+------+-----------------+-----------+
| ubuntu-2404-ct | RUNNING | 192.168.10.220 (eth0)   |      | CONTAINER       | 0         |
+----------------+---------+-------------------------+------+-----------------+-----------+
| ubuntu-2404-vm | RUNNING | 192.168.10.100 (enp5s0) |      | VIRTUAL-MACHINE | 0         |
+----------------+---------+-------------------------+------+-----------------+-----------+

lxc list -c n,s,4,a,b,D,M,P,t,N,S
---<output>---
lxc list -c n,s,4,a,b,D,M,P,t,N,S
+----------------+---------+-------------------------+--------------+--------------+------------+---------------+----------+-----------------+-----------+-----------+
|      NAME      |  STATE  |          IPV4           | ARCHITECTURE | STORAGE POOL | DISK USAGE | MEMORY USAGE% | PROFILES |      TYPE       | PROCESSES | SNAPSHOTS |
+----------------+---------+-------------------------+--------------+--------------+------------+---------------+----------+-----------------+-----------+-----------+
| almalinux-8-ct | RUNNING | 192.168.10.69 (eth0)    | x86_64       | sys-ops.id   | 663.00MiB  | 21.6%         | default  | CONTAINER       | 11        | 0         |
+----------------+---------+-------------------------+--------------+--------------+------------+---------------+----------+-----------------+-----------+-----------+
| ubuntu-2404-ct | RUNNING | 192.168.10.220 (eth0)   | x86_64       | sys-ops.id   | 918.74MiB  | 47.2%         | default  | CONTAINER       | 24        | 0         |
+----------------+---------+-------------------------+--------------+--------------+------------+---------------+----------+-----------------+-----------+-----------+
| ubuntu-2404-vm | RUNNING | 192.168.10.100 (enp5s0) | x86_64       | sys-ops.id   | 3.57GiB    | 38.3%         | default  | VIRTUAL-MACHINE | 18        | 0         |
|                |         |                         |              |              |            |               | vm       |                 |           |           |
+----------------+---------+-------------------------+--------------+--------------+------------+---------------+----------+-----------------+-----------+-----------+

Manajemen LXC

  • Masuk ke dalam bash lxc
root@Ubuntu-2404:~# lxc exec ubuntu-2404-ct bash
root@ubuntu-2404-ct:~# hostnamectl
 Static hostname: ubuntu-2404-ct
       Icon name: computer-container
         Chassis: container ☐
      Machine ID: 4909c019cd0f454783bac983a25b918a
         Boot ID: d8ff00ec988c4169ba28c35e421cdcb9
  Virtualization: lxc
Operating System: Ubuntu 24.04 LTS
          Kernel: Linux 6.8.0-31-generic
    Architecture: x86-64
root@ubuntu-2404-ct:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:4b:d8:95 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.10.220/24 metric 100 brd 192.168.10.255 scope global dynamic eth0
       valid_lft 2999sec preferred_lft 2999sec
    inet6 fe80::216:3eff:fe4b:d895/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu-2404-ct:~# exit
-----------------------------------------------------------------------------------------------------
root@Ubuntu-2404:~# lxc exec ubuntu-2404-vm bash
root@ubuntu-2404-vm:~# hostnamectl
 Static hostname: ubuntu-2404-vm
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 0984996f50db40c4b71f2b060f82ccb2
         Boot ID: c8de0dc4b22a45aba2cd5c7e46616f68
  Virtualization: kvm
Operating System: Ubuntu 24.04 LTS
          Kernel: Linux 6.8.0-31-generic
    Architecture: x86-64
 Hardware Vendor: QEMU
  Hardware Model: Standard PC _Q35 + ICH9, 2009_
Firmware Version: unknown
   Firmware Date: Wed 2022-02-02
    Firmware Age: 2y 3month 2w
root@ubuntu-2404-vm:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:16:3e:19:0f:d0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.100/24 metric 100 brd 192.168.10.255 scope global dynamic enp5s0
       valid_lft 3433sec preferred_lft 3433sec
    inet6 fe80::216:3eff:fe19:fd0/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu-2404-vm:~# exit
  • Manajemen lxc
### cek list container
lxc list

### stop container
lxc stop nama_container

### start container
lxc start nama_container

### restart container
lxc restart nama_container

### hapus container
lxc delete -f nama_container

### snapshot container
lxc snapshot nama_container nama_snapshot

### restore container snapshot
lxc restore nama_container nama_snapshot

### create image dari snapshot
lxc publish nama_container/nama_snapshot description="deskripsi images"

### set limit cpu container
lxc config set nama_container limits.cpu 2

### set limit memory container
lxc config set nama_container limits.memory 512MB

### upload file & folder ke dalam container
lxc file push -r nama_folder_host nama_container/nama_folder

### download file & folder dari dalam container 
lxc file pull -r nama_container/nama_folder . -r

### add device proxy / port forwarding
lxc config device add nama_container nama_device proxy connect=tcp:127.0.0.1:22 listen=tcp:0.0.0.0:22

### set static ip
lxc config device override nama_container eth0 ipv4.address=192.168.10.200

LXC Port Forwarding

  • Port forwarding lxc container port 8081 –> 80
lxc config device add ubuntu-2404-ct port80 proxy connect=tcp:127.0.0.1:80 listen=tcp:0.0.0.0:8081
  • Port forwarding lxc virtual machine port 8082 –> 80
  • Khusus untuk virtual machine, harus menggunakan static ip dan mengaktifkan nat mode
lxc config device add ubuntu-2404-vm port80 proxy connect=tcp:192.168.10.100:80 listen=tcp:10.10.1.16:8082 nat=true
  • Akses menggunakan IP node LXD

Install LXDWare (LXD Dashboard)

  • Buat lxc baru dengan ubuntu 22.04
lxc launch ubuntu:22.04 lxdware
lxc exec lxdware bash
  • Install Nginx, PHP dan SQLite pada lxc lxdware
apt update && apt install curl nano net-tools wget nginx php-fpm php-curl sqlite3 php-sqlite3 -y 
  • Download LXD dashboard v3.8.0, cek versi terbaru disini
wget https://github.com/lxdware/lxd-dashboard/archive/refs/tags/v3.8.0.tar.gz
tar -xzf v3.8.0.tar.gz
  • Copy file konfigurasi nginx dan file aplikasi lxd dashboard
cp -a lxd-dashboard-3.8.0/default /etc/nginx/sites-available/
cp -a lxd-dashboard-3.8.0/lxd-dashboard/ /var/www/html/
  • Buat directory lxdware untuk menyimpan data lxd dashboard
mkdir -p /var/lxdware/data/sqlite
mkdir -p /var/lxdware/data/lxd
mkdir -p /var/lxdware/backups
  • Edit permission directory lxdware dan html
chown -R www-data:www-data /var/lxdware/
chown -R www-data:www-data /var/www/html/
  • Ganti php7.4-fpm menjadi php8.1-fpm pada file: /etc/nginx/sites-available/default
  • Karena default versi php pada ubuntu 22.04 menggunakan versi 8.1
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html/lxd-dashboard;
        index index.php index.html;
        server_name _;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
        #include snippets/fastcgi-php.conf;
        #fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                #fastcgi_pass unix:/run/php/php7.4-fpm.sock;
                fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        include snippets/fastcgi-php.conf;
     }
}
  • Restart service nginx
systemctl restart nginx
exit
  • Konfigurasi port forwarding untuk port 8090 dari host lxd ke port 80 pada lxc container
lxc config device add lxdware port8090 proxy connect="tcp:127.0.0.1:80" listen="tcp:0.0.0.0:8090"
  • Akses LXD dashboard dengan url: http://ip_address_server_lxd:8090
  • Buat akun baru unruk akses ke dashboard lxdware
  • Buat file lxdware.crt , lalu copy LXD client sertificate ke dalam file tersebut
nano lxdware-cert.crt
  • Import LXD client sertificate ke dalam server LXD
lxc config trust add lxdware-cert.crt
lxc config set core.https_address [::]
  • Tambahkan server LXD host, dengan ip local server atau domain yang sudah di pointing ke server
  • Tampilan dashboard LXDWare

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.