[Linux] Cara Install Linux Container (LXD) di Ubuntu
Sebelumnya saya sudah menulis mengenai Linux Container (LXC, dibaca lex‐cee), kali ini masih membahas topik yang sama tentang container yaitu LXD (dibaca lex‐dee). Canonical menyebut LXD sebagai “The Linux container hypervisor. Fast, dense and secure container management for Ubuntu.”
Proyek LXD dimulai oleh Canonical dan Ubuntu dengan kontribusi dari perusahaan dan individu. LXD dibuat dari 3 komponen yaitu:
1. system-wide daemon (lxd)
2. command line client (lxc)
3. OpenStack Nova plugin (nova-compute-lxd)
LXD tidak ditulis ulang dari LXC, melainkan LXD dibangun di atas LXC untuk memberikan user experience yang lebih baik. LXD menggunakan LXC melalui liblxc dan Go untuk membuat dan mengelola container.
LXD tidak lagi mengenal template seperti di LXC. Untuk membuat container LXD menggunakan image. Proses membuat container di LXD juga tidak seperti LXC yang sangat mirip dengan instalasi distribusi Linux. Ukuran file image LXD container juga lebih kecil.
Instalasi LXD Container
Install LXD di Ubuntu 15.04 atau terbaru
1 | apt-get install lxd |
Install LXD di Ubuntu 14.04 LTS
1 2 3 | sudo add-apt-repository ppa:ubuntu-lxc/lxd-stable sudo apt-get update sudo apt-get install lxd |
Konfigurasi awal
1 2 | newgrp lxd sudo lxd init |
Image
Untuk membuat container, kita membutuhkan image.
LXD image repository images.linuxcontainers.org
Ada 3 default remote images
1. ubuntu:, stable Ubuntu images
2. ubuntu-daily:, daily Ubuntu images
3. images:, distribusi Linux lainnya
Menampilkan image dari image repository
1 | lxc image list images: |
Contoh hasil yang ditampilkan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | alpine/3.1 (3 more) | 37b4d0040f43 | yes | Alpine 3.1 amd64 (20160925_17:50) | x86_64 | 2.32MB | Sep 25, 2016 at 12:00am (UTC) | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | alpine/3.1/armhf (1 more) | c33310613e2f | yes | Alpine 3.1 armhf (20160925_17:50) | armv7l | 1.55MB | Sep 25, 2016 at 12:00am (UTC) | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | alpine/3.1/i386 (1 more) | ed9b395eb229 | yes | Alpine 3.1 i386 (20160925_17:50) | i686 | 2.26MB | Sep 25, 2016 at 12:00am (UTC) | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | alpine/3.2 (3 more) | e556d344c70a | yes | Alpine 3.2 amd64 (20160925_17:50) | x86_64 | 2.61MB | Sep 25, 2016 at 12:00am (UTC) | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | alpine/3.2/armhf (1 more) | 23328d273a0c | yes | Alpine 3.2 armhf (20160925_17:50) | armv7l | 1.54MB | Sep 25, 2016 at 12:00am (UTC) | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | alpine/3.2/i386 (1 more) | baf389f7009e | yes | Alpine 3.2 i386 (20160925_17:50) | i686 | 2.44MB | Sep 25, 2016 at 12:00am (UTC) | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | alpine/3.3 (3 more) | 0aaff45ce743 | yes | Alpine 3.3 amd64 (20160925_17:50) | x86_64 | 2.61MB | Sep 25, 2016 at 12:00am (UTC) | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | alpine/3.3/armhf (1 more) | 9c8783f482af | yes | Alpine 3.3 armhf (20160925_17:50) | armv7l | 1.54MB | Sep 25, 2016 at 12:00am (UTC) | +---------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ |
Contoh pembuatan container dari image repository
1 2 3 | lxc launch ubuntu:14.04 my-ubuntu lxc launch ubuntu-daily:16.04 my-ubuntu-dev lxc launch images:centos/6/amd64 my-centos |
Menampilkan images yang ada di lokal
1 | lxc image list |
Contoh hasil yang ditampilkan
1 2 3 4 5 6 7 8 9 10 11 12 13 | +--------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE | +--------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | xenial | 767a2bafeaef | no | ubuntu 16.04 LTS amd64 (release) (20160922) | x86_64 | 176.93MB | Sep 24, 2016 at 5:44am (UTC) | +--------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | | 6d18d536dc5b | no | Centos 6 amd64 (20160925_02:16) | x86_64 | 65.77MB | Sep 25, 2016 at 2:09pm (UTC) | +--------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | | 0bdcd79f6d7f | no | Centos 7 amd64 (20160925_02:16) | x86_64 | 62.92MB | Sep 25, 2016 at 2:11pm (UTC) | +--------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | | d00ecfc8ad7d | no | Debian jessie amd64 (20160924_22:42) | x86_64 | 104.12MB | Sep 25, 2016 at 2:07pm (UTC) | +--------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | | 2e88fb1b356a | no | Debian wheezy amd64 (20160924_22:42) | x86_64 | 98.57MB | Sep 25, 2016 at 10:38pm (UTC) | +--------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ |
Membuat dan menghapus alias dari sebuah image. Dengan alias, membuat container tidak perlu lagi menulis Fingerprint dari image cukup menulis alias saja.
1 2 3 | lxc image alias create ubuntu1604 767a2bafeaef lxc image alias delete xenial lxc image list |
Contoh hasil yang ditampilkan
1 2 3 4 5 6 7 8 9 10 11 12 13 | +------------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE | +------------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | ubuntu1604 | 767a2bafeaef | no | ubuntu 16.04 LTS amd64 (release) (20160922) | x86_64 | 176.93MB | Sep 24, 2016 at 5:44am (UTC) | +------------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | | 6d18d536dc5b | no | Centos 6 amd64 (20160925_02:16) | x86_64 | 65.77MB | Sep 25, 2016 at 2:09pm (UTC) | +------------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | | 0bdcd79f6d7f | no | Centos 7 amd64 (20160925_02:16) | x86_64 | 62.92MB | Sep 25, 2016 at 2:11pm (UTC) | +------------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | | d00ecfc8ad7d | no | Debian jessie amd64 (20160924_22:42) | x86_64 | 104.12MB | Sep 25, 2016 at 2:07pm (UTC) | +------------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ | | 2e88fb1b356a | no | Debian wheezy amd64 (20160924_22:42) | x86_64 | 98.57MB | Sep 25, 2016 at 10:38pm (UTC) | +------------+--------------+--------+---------------------------------------------+--------+----------+-------------------------------+ |
Container
Menampilkan container yang sudah ada
1 | lxc list |
Contoh hasil yang ditampilkan
1 2 3 4 5 | +-------------+---------+------+------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +-------------+---------+------+------+------------+-----------+ | my-ubuntu | STOPPED | | | PERSISTENT | 0 | +-------------+---------+------+------+------------+-----------+ |
Menjalankan dan menghentikan container
1 2 | lxc start my-ubuntu lxc stop my-ubuntu |
Masuk ke shell container
1 | lxc exec my-ubuntu -- /bin/bash |
Membuat container baru dari image lokal, jadi tidak harus lagi mengunduh dari image repository
1 | lxc launch 0bdcd79f6d7f centos7-python |
Membuat container baru dari image ubuntu dengan alias ubuntu1604
1 | lxc launch ubuntu1604 ubuntu-lemp |
Menampilkan semua container
1 | lxc list |
Contoh hasil yang ditampilkan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | centos-lxd | STOPPED | | | PERSISTENT | 0 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | centos7-lxd | STOPPED | | | PERSISTENT | 0 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | centos7-python | RUNNING | 10.74.99.247 (eth0) | fd84:94fd:c809:b2c6:216:3eff:fe31:e915 (eth0) | PERSISTENT | 0 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | debian7-lxd | STOPPED | | | PERSISTENT | 0 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | debian8-lxd | STOPPED | | | PERSISTENT | 0 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | ubuntu-lamp | STOPPED | | | PERSISTENT | 0 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | ubuntu-lemp | STOPPED | | | PERSISTENT | 0 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ | ubuntu-lxd | STOPPED | | | PERSISTENT | 0 | +----------------+---------+---------------------+-----------------------------------------------+------------+-----------+ |
Menjalankan container secara otomatis
1 | lxc config set container_name boot.autostart 1 |
Snapshot
Snapshot adalah checkpoint backup dari container, jadi kita bisa mengembalikan container seperti pada saat melakukan snapshot.
Cara membuat snapshot dari sebuah container
1 | lxc snapshot namacontainer namasnpshot |
Mengecek jumlah snapshot
1 2 3 4 5 6 | lxc list +-------------+---------+------+------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +-------------+---------+------+------+------------+-----------+ | ubuntu-lamp | STOPPED | | | PERSISTENT | 2 | +-------------+---------+------+------+------------+-----------+ |
Pada kolom snapshots terlihat ada 2 snapshot.
Untuk melihat waktu pengambilan snapshot, dapat mengetik perintah info container
1 2 3 4 5 6 7 8 9 10 11 | lxc info ubuntu-lamp Name: ubuntu-lamp Remote: unix:/var/lib/lxd/unix.socket Architecture: x86_64 Created: 2016/11/19 10:28 UTC Status: Stopped Type: persistent Profiles: default Snapshots: snap1 (taken at 2016/11/24 08:32 UTC) (stateless) snap2 (taken at 2016/11/24 08:39 UTC) (stateless) |
Merestore snapshot
1 | lxc restore namacontainer namasnapshot |
Menghapus snapshot
1 | lxc delete namacontainer/namasnapshot |
Container menjadi image
Container yang sudah diset dapat dijadikan image, agar nantinya jika ingin membuat container yang sama tidak perlu lagi melakukan konfigurasi dari awal, cukup membuat container dari image tersebut.
1 | lxc publish namacontainer --alias namaaliascontainer |
Export dan Import image
Export image untuk menyimpan image ke dalam file compress tar.xz
1 | lxc image export aliasimage . |
atau
1 | lxc image export fingerprintimage . |
Import image untuk memasukkan image dari file compress tar.xz hasil export
1 | lxc image import namafileimage.tar.xz |
Jaringan
Konfigurasi ulang jaringan
1 | sudo dpkg-reconfigure -p medium lxd |
Konfigurasi jaringan container menggunakan interface fisik host os
lxc profile device set default interfaceContainer parent interfaceHost
1 2 | lxc profile device set default eth0 parent eth0 lxc profile device set default eth0 nictype macvlan |
selamat mencoba 🙂
referensi:
ubuntu.com
linuxcontainers.org
roots.io
tycho.ws
bitsandslices.wordpress.com