sumarsono.com
Take it with a grain of salt


Bikin lxc container dapet IP address dari router

Posted on

Secara default, lxc (dalam hal ini aku pakai lxd) container akan pakai profile bernama default. Di profile default tersebut, didefinisikan bahwa container akan pakai network mode bridge dengan parent lxdbr0. Oleh sebab itu, container tidak akan mendapatkan ip dari router.

Supaya container mendapat ip dari router, maka kita harus ganti nictype bridge menjadi macvlan. Kemudian ganti parent menjadi eth fisik yang dalam hal ini adalah enp2s0 (kita bisa dapetin dengan ip link)

Ringkasan langkah-langkah yang ku tempuh:

lxc profile list # lihat daftar profile lxc
lxc profile copy default macvlanprofile # copy profile default
ip link # cek nama perangkat fisik
lxc profile device set macvlanprofile eth0 nictype macvlan # ubah nictype menjadi macvlan
lxc profile device set macvlanprofile eth0 parent enp2s0 # ubah parent menjadi perangkat fisik
lxc profile show macvlanprofile # lihat hasil edit
lxc launch -p macvlanprofile ubuntu:16.04 net1 # tes bikin container pakai profile macvlanprofile
lxc list # cek list container, disini tertera bahwa ip container dapet dari router
lxc exec net1 ip route # untuk memastikan ip container

Biar lebih jelas,

$ ip a show enp2s0
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether f4:4d:30:a7:57:7c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.28/24 brd 192.168.1.255 scope global noprefixroute enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::10bc:5396:d876:5572/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

$ lxc list
+---------+---------+---------------------+------------------------------------------------+------------+-----------+
|  NAME   |  STATE  |        IPV4         |                      IPV6                      |    TYPE    | SNAPSHOTS |
+---------+---------+---------------------+------------------------------------------------+------------+-----------+
| centos7 | STOPPED |                     |                                                | PERSISTENT | 0         |
+---------+---------+---------------------+------------------------------------------------+------------+-----------+
| gitea   | RUNNING | 10.70.3.189 (eth0)  | fd42:a719:f701:92e0:c513:3a0a:d11f:c264 (eth0) | PERSISTENT | 0         |
+---------+---------+---------------------+------------------------------------------------+------------+-----------+
| net1    | RUNNING | 192.168.1.86 (eth0) |                                                | PERSISTENT | 0         |
+---------+---------+---------------------+------------------------------------------------+------------+-----------+

container gitea pakai mode bridge vs container net1 pakai mode macvlan.

Sekarang, perangkat dalam jaringan bisa komunikasi langsung dengan container yang kita buat. Cool!

Tapi coba ping dari host ke container. Host tidak bisa komunikasi dengan container yang pakai macvlan. Solusinya, bikin macvlan di host.

Agar macvlan yang dibuat di host tidak menggangu route enp2s0, maka aku bikin macvlan0 mode static tanpa gateway:

sudo vim /etc/netctl/macvlan0

Contoh konfigurasi macvlan0 static

Description='Virtual LAN with a static MAC on interface eth0 using DHCP'
Interface=macvlan0
Connection=macvlan
BindsToInterfaces=enp2s0
Mode="bridge"
MACAddress="12:34:56:78:9a:bc"
#IP=dhcp
IP=static
Address=('192.168.1.88/24')
sudo netctl restart macvlan0

Dari sini, host sudah bisa komunikasi sama si container. Route utama juga tidak terganggu.

Cool!

Referensi: