Static IP for container #2083

Closed
kirgene opened this Issue Jun 6, 2016 · 14 comments

Comments

Projects
None yet
7 participants

kirgene commented Jun 6, 2016

So how can I set static IP for containers?

Owner

stgraber commented Jun 6, 2016

The recommendation is that you configure the distribution in the container to have a static IP.

On Ubuntu/Debian, that's done through /etc/network/interfaces

LXC does support setting those for you from outside the container, but most distributions will then fail to boot or will just unset that preset data.

If you're using an environment which does support pre-configured network interfaces at boot time, you can set something like:

raw.lxc: |-
  lxc.network.0.ipv4 = 10.0.1.2/24
  lxc.network.0.ipv4.gateway = 10.0.1.1

The fact that this is only available through "raw.lxc" is a clear indication that while we let you pass that through, it's not supported and may very well break.

Owner

stgraber commented Jun 6, 2016

Upcoming work on network bridge configuration inside LXD itself will likely come with the ability to configure specific DHCP leases which we'd then be able to support.

@stgraber stgraber closed this Jun 7, 2016

@stgraber much appreciated! I really love lxd, while I don't use static ips in prod, stage. On my local machine for dev of chef cookbooks they come in very handy!!!

I appreciate you allowing raw lxc.network again!

With version 2.3 is this now possible with LXD?

Previously I was extending the dnsmasq configuration and setting "dhcp-host" and "dhcp-option" config values to provide static IP addresses and default gateways to LXD hosts using their hostnames.

After the 2.3 upgrade, this no longer working, I think maybe it is because "--dhcp-hostsfile" is specified as an arg to dnsmasq, not sure if this was always the case. the "/var/lib/lxd/networks/lxdbr0/dnsmasq.hosts" is read only too.

What is the recommended way, still the above?

Owner

stgraber commented Oct 4, 2016

You can just set ipv4.address and/or ipv6.address in your container's device entry which will then generate a dhcp-host line for you.

Owner

stgraber commented Oct 4, 2016

lxc network attach lxdbr0 c1 eth0
lxc config device set c1 eth0 ipv4.address 10.x.x.123

@stgraber thanks again for adding support for this ^^^ I know its been in a little while, but it was really much appreciated!

Thanks @stgraber, that worked :)

Not sure if it is always required but I had to execute all of the below:

lxc network attach lxdbr0 c1 eth0
lxc config device set c1 eth0 name eth0
lxc config device set c1 eth0 host_name veth000001
lxc config device set c1 eth0 ipv4.address 10.38.15.11

Without host_name set, I got "LXC doesn't now about this device and the host_name property isn't set, can't find host side veth name" when trying to set ipv4.address

Without name set the container kept getting an eth1 without an ipv4 address, ipv6 only.

Is it possible to also set:<tag> on the dhcp-hostsfile entry? It would also be great to be able to use dhcp-option to configure extra options using raw.dnsmasq option on the network.

My use case here, is that 1 container is a transparent proxy (needs static IP). Other hosts will have their default router set to the transparent proxy's IP. I had this setup with the previous networking model as it all lived in the LXD_CONFILE, trying to see how it can be done with LXD 2.3

Owner

stgraber commented Oct 5, 2016

Hmm, that's odd, host_name shouldn't be needed for ipv4.address. It sounds like this may have regressed a bit when I introduced the mac_filtering feature...

LXD doesn't do any filtering on raw.dnsmasq so I'm not sure why dnsmasq is ignoring your dhcp-option in there. It may be because LXD is always generating one for DNS management when dns.mode is managed.

If that's the problem, then doing: lxc network set lxdbr0 dns.mode dynamic
Combined with not having ipv4.address set for the container, should cause LXD to drop its dhcp-host entry and hopefully have dnsmasq respect yours.

FYI, I can confirm that this works for me:
echo -e "lxc.network.0.ipv4 = 10.100.0.10/24\nlxc.network.0.ipv4.gateway = 10.100.0.1\n" | lxc config set my-container raw.lxc -

I do publish a container for static purposes, after I run:
lxc exec my-container -- sed -i 's/dhcp/manual/g' /etc/network/interfaces.d/eth0.cfg

Otherwise I get a dhcp address and static ip

wtayyeb commented Oct 5, 2016

I have done it with new lxc network command:

create /path/to/host-to-ip-file.conf with following synax (dnsmasq syntax)

c1,10.100.0.10
c2,10.100.0.20

then run

 lxc network set lxdbr0 set raw.dnsmasq hostsfile=/path/to/host-to-ip-file.conf
 lxc restart c1 c2

it will restart dnsmasq and load the hostsfile and set new ips to c1, c2

each time the above hostsfile changed one must restart the lxd service or fake edit the network configs to changes take effect. (also need to restart the container)

Owner

stgraber commented Oct 6, 2016

Interesting, good to know!

I am trying LXD new to build Linux based infrastructure container. when i setup it with init command, i let it use the default private subnet 10.140.216.1 it took since I dont want to create any issues with existing network. I created a container and setup static IP from valid internal network of the actual host, but since lxdbr0 shows 10.140.216.1 its not bridging the connection to the actual container.

How do I change this to match with host network? at the sametime without impacting any existing systems on the subnet/VLAN.

Actual host is on: 10.250.120.0/24
I assigned 10.250.120.201 to the container statically by editing the /etc/network/interfaces file.

#ifconfig -a

lxdbro0 Link encap:Ethernet HWaddr XX:XX:XX:XX:XX:XX
inet addr:10.140.216.1 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:570 (570.0 B)

#lxc list
+--------+---------+---------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------+---------+---------------------+------+------------+-----------+
| l-db01 | RUNNING | 10.250.120.201 (eth0) | | PERSISTENT | 0 |
+--------+---------+---------------------+------+------------+-----------+

Looks like the newer releases of LXD (I'm using 2.11) make this fairly trivial using cloud-init. Caveats are that you need a cloud-init based image (like the ones at https://cloud-images.ubuntu.com/releases). Full documentation is here https://github.com/lxc/lxd/blob/master/doc/cloud-init.md

  1. Create the network config

    vi network-config.yml
    version: 1
    config:
      - type: physical
        name: eth0
        subnets:
          - type: static
            ipv4: true
            address: 10.10.10.10
            netmask: 255.255.255.0
            gateway: 10.10.10.1
            control: auto
      - type: nameserver
        address: 8.8.8.8
    
  2. Launch the container with the static network config

    lxc launch ubuntu:16.04 test3 --config=user.network-config="$(cat network-config.yml)"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment