Search code examples
linuxnetworkingrouteslxclxd

LXD public ip to container using routed


I'm trying to set a public ip to a container using the routed nictype in LXD, Essentially i inited a fresh container, ran lxc config device add c1 eth0 nic nictype=routed parent=eth0 ipv4.address=my.public.ip then started the container, it shows the correct ip in the IPV4 section for a split second, and running an lxc list again shows it dissapearing into a blank. So it IS* being set properly, at least to lxc, but a few seconds after startup it goes away.

My guess is there's maybe some DHCP style nonsense going on inside the container trying to get an ip from the host lxd machine? Any ideas are useful I don't have much knowledge with networking


Solution

  • For routed to work, you need to make some configuration in LXD and some configuration in the container. It is easier to create a LXD profile that contains both parts of the configuration.

    Here is an example LXD profile. The upper part is about the container configuration, and the below part is what LXD needs to know to configure routed for the container.

    config:
      user.network-config: |
        version: 2
        ethernets:
            eth0:
                addresses:
                - 192.168.1.200/32
                nameservers:
                    addresses:
                    - 8.8.8.8
                    search: []
                routes:
                -   to: 0.0.0.0/0
                    via: 169.254.0.1
                    on-link: true
    description: Default LXD profile
    devices:
      eth0:
        ipv4.address: 192.168.1.200
        nictype: routed
        parent: enp6s0
        type: nic
    name: routed_192.168.1.200
    used_by:
    

    To create a container with this profile, you would then run

    lxc launch ubuntu: mycontainer --profile default --profile routed_192.168.1.200
    

    References: https://blog.simos.info/how-to-get-lxd-containers-get-ip-from-the-lan-with-routed-network/