3

I am trying to set a static IP address in an lxd container. The container is made with:

nonroot $ lxc launch images:centos/7/amd64 my_centos

in a pretty bogstandard lxd (just the default apt-get install lxd; service lxd start) environment. Now I want to give that thing a static IP 10.0.3.50 (say).

After some googling (why this isn't clear from the documentation on the site isn't clear to me yet), I found that the profile information, which I have to hack to get that done, was in a sqlite database. Okay:

lxc profile copy default my_centos
lxc profile edit my_centos
...
name: my_centos 
  config: {} 
  description: "" 
  devices:   
    eth0:
      name: eth0
      nictype: bridged
      parent: lxcbr0
      type: nic

and it won't let me change it to:

name: my_centos
config: 
  raw.lxc: 
    lxc.network.type = veth
    lxc.network.name = eth0
    lxc.network.link = lxcbr0
    lxc.network.ipv4 = 10.0.3.50/24
    lxc.network.ipv4.gateway = 10.0.3.1
    lxc.network.flags = up

description: ""
devices:
  eth0:
    name: eth0
    nictype: bridged
    parent: lxcbr0
    type: nic

The error is: Config parsing error: Only interface-specific ipv4/ipv6 lxc.network keys are allowed

This probably has to do with the nictype and the type of eth0, but removing the device has the same effect.

Can someone enlighten me on the right way to do this ?

This is kinda related with LXD containers and networking with static IP, but I want to set this up with lxd (so, with profiles instead of config files).

2 Answers 2

3

I had a similar problem where I wanted containers with static IPs and wanted them to be visible on my local network.

Haven't figured out how to do it via lxd profiles. But below are the steps to create a single container with a static IP. Then you can write a script that clones the container and simply changes its IP in the /etc/network/interfaces file thus in effect giving you the ability to create containers with static IPs.

Here are the steps:

Create a bridge on the host machine.

edit : /etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
#auto eth0
#iface eth0 inet dhcp

auto br0
iface br0 inet static
    address 172.16.115.208
    broadcast 172.16.119.255
    netmask 255.255.248.0
    gateway 172.16.112.1
    bridge_ports eth0
    bridge_fd 9
    bridge_hello 2
    bridge_maxage 12
    bridge_stp off

Replace IP addresses with your values.

Note that "bridge_ports" adds your eth0 to the bridge.

Restart networking:

sudo /etc/init.d/networking restart

Bring up the new bridge:

sudo ifup br0

At this point you should ensure whether you still have network connectivity via the newly created bridge.

Edit your lxc profile and set parent as the newly created bridge.

lxc profile edit default<or your container's profile name>

-       parent: lxcbr0
+       parent: br0

Access the container and set static IP address.

lxc exec my-container-name /bin/bash
sudo vim /etc/network/interfaces

    #auto eth0
    #iface eth0 inet dhcp

    auto eth0
    iface eth0 inet static
    address 172.16.115.252
    broadcast 172.16.119.255
    netmask 255.255.248.0
    gateway 172.16.112.1

Again, change ip addresses accordingly. Restart your container and check whether it has been assigned the new IP.

lxc info my-container-name

Your container now has a static IP and is visible on the network. You can now write a script that clones it and changes the IP value in the interfaces file to get multiple containers with different static IPs.

1
  • I've done the same with the default bridge which is created on "lxd init", just excluded some IPs from the DHCP range and set up a static IP address in container's /etc/network/interfaces.d/eth0 - seems to work.
    – Zmey
    Commented Jul 23, 2016 at 11:03
2

You can make lxd-bridge assign static ip addresses by adding an external dnsmasq configuration file.

Open up /etc/default/lxd-bridge and you'll find a spot for adding it.

LXD_CONFILE=""

Insert a file name there. Something like lxd_dnsmasq.conf. You can name it anything you wish.

Then create the file and add an entry like this for each container.

dhcp-host=containername,ipaddress

Here's a link to detailed instructions for setting this up: lxd static ip addresses

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .