Exploring LXC Networking
Recently I’ve been finding myself in various conversations about Docker and Linux Containers (LXC). Most of the time the conversations eventually end up with one and the same question and that is whether we should run containers in production. Initially this post had a few paragraphs where I philosophised about readiness of the technology, but I’ve deleted those paragraphs now as the attention dedicated to containers in past year has been nothing short of amazing and more and more companies are running their Infrastructures in the containers or at least big parts of them. I’m sure this trend will continue to high degree in the future, so I’ve now removed these paragraphs and kept the technical content only.
One of the (many) things which were not entirely clear to me and to the people I speak with about the topic of containers almost on daily basis is how the networking can be done and configured when using LXC. Hopefully the first blog post on this topic is going to shed some more light on this matter and hopefully it will inspire further posts on various other topics related to the containers.
Setup
Whenever I’m looking for answers for my technical questions or when learning new technology I always prefer the “hands-on” blog posts, so that’s the approach I’ve decided to take when writing this post. You can follow it step by step and feel free to experiment as you work through the examples provided in each chapter.
All you need to follow the guide in this post is Vagrant. I used version 1.3.2, but for the purpose of this post any 1.2+ version will do (possibly even lower versions, but I’ve encountered some issues with older versions, so I don’t recommend them). In order to run Vagrant you need to put together a Vagrantfile. The one I used for this guide looks like this:
I decided to use Ubuntu Precise Pangolin version as that was the latest Ubuntu LTS version released at the time whe I wrote this post and that’s the OS which the companies I work for are running on their production servers.
As for the LXC tools, I initially ran all the examples in this blog post on daily LXC builds as back then when I was writing this post there was no stable LXC build per se and I thought it could be fun to test “on the edge” stuff. But on 20th of February 2014 Ubuntu guys released first stable LXC release 1.0. You can look up the stable LXC PPA on launchpad. I revisited the blog post and updated it accordingly so that people who visit this page later on could find some up to date information here. Anyways, place your Vagrantfile into some working directory, fire up
vagrant up
and off we go:
If you used the same Vagrantfile as myself then before the Virtual Machine (VM) boots you will be prompted by
vagrant
about which network interface on your workstation you want to bridge the Virtual Box one with. Pick any active interface on the workstation you are running the vagrant i.e. the one you’re using for networking (which is hopefully on private network). vagrant up
downloads the Vagrant box image (which is an OS image) if it’s not already present on your workstation, boots it and runs the commands specified in pkg_cmd
once the VM is up and running.
Once the whole set up has finished, you can run
vagrant ssh
in the same directory you have run vagrant up
in. This will log you on the newly created VM via ssh and you should see the LXC packages installed and ready to be used:
The actual version tags (such as the timestamp) of the packages above may differ for you as you’ll be installing the latest ones at the time you’ll be following this guide. As for the networking part of the setup, you should end up with something like below, though obviously IP and MAC addresses will be different on your workstation (for easier readability I’ve added extra new line after each interface output):
Quick explanation of the above:
- lo – loopback interfaces (more on this later)
- eth0 – default network interface created by Vagrant
- eth1 – private network interface which Vagrant created as instructed by our Vagrantfile configuration
:private_network
- eth2 – public network interface which Vagrant created as instructed by our Vagrantfile configuration
:public_network
- lxcbr0 – network bridge which was created automatically when the lxc tools have been installed
Installation of LXC tools hides a small subtlety you might want to be aware of and that is a creation of one iptables rule. The rule basically masquerades all the traffic leaving the containers which are bridged with
lxcbr0
, i.e. which are on 10.0.3.0/24
LAN so that you can reach “outside” world from inside these containers:
The above iptables rule is also created automatically on boot via
lxc-net
upstart job which you can easily verify by looking into particular upstart configuration file: /etc/init/lxc-net.conf
.LXC Network Types
As the official LXC Ubuntu manual pages mention there are 5 network virtualization types available to be used with LXC:
- empty
- veth
- macvlan
- vlan
- phys
There is one extra network type available on Trusty which is called “none”. Each of them works with different concepts and requires some knowledge of Linux networking. I hope the practical examples in this blog post will give you a better understanding of these networking types. Enough talking, let’s get party started !
Empty
Let’s create our first container to explore the empty network type. We will create a new container by running
lxc-create
command which can use ubuntu template script shipped with lxc-templates
package. LXC templates are essentially bash
scripts which make creation of the Linux containers supereasy. You can have a look at what’s “hiding” inside the ubuntu one (which is the one we will be using in this guide) in the following path: /usr/share/lxc/templates/lxc-ubuntu
. You can specify a particular Ubuntu version via -r
command line switch passed to the template script as you can see below:
The above command uses
debootstrap
package to download and install particular Ubuntu OS image into /var/lib/lxc
directory on the host machine. Apart from downloading the image, lxc-create
sets up container directory structure and performs few other tasks. Once the above command has successfully finished you should be able to see that the new image has been installed and is ready to be used:
Every container created using
lxc-create
utility has a configuration file in the particular container’s path (/var/lib/lxc/CONTAINERNAME/config
). The configuration file is then used by another utility called lxc-start
to actually create and start the container. We need to modify this file to use the empty network type as by default veth
network type is used. The new network related configuration looks like this:
Now that we have our networking configuration ready, let’s start the conatiner. Note the -dswitch below which starts the container in the background – if you don’t use this switch when starting the container you will see container start logs streaming in your standard output and you would get a container console once the container booted. After a bit of time (give it a short time to boot) let’s check if it’s running:
As you can see in the above output, the container is up and running, but it doesn’t have any IP address assigned. This is exactly what we should expect when we specified emptynetwork type as per documentation which describes it as follows: “empty network type creates only the loopback interface”. In fact, empty network type creates EMPTY NETWORK NAMESPACE in which the container runs. When you create an empty network namespace only the loopback device is created and that’s exactly what the documentation says. The newly created loopback interface is not visible on the host when running
sudo ip link list
as it’s in a different network namespace.
I will talk more about Linux Namespacing in another blog post, but if you are impatient and want to know more now, there is a small presentation of Network namespaces I gave short while ago or you can check a great blog post written by Jérôme Petazzoni which explains them very well. Short explanation is that the namespacing is a functionality implemented in Linux Kernel which allows for an isolation of various Kernel subsystems on the host.
You can verify that the new container is running on the host and has a new network namespace created by checking the contents of its
init
process’ proc
namespace entry. Make sure you are checking the container’s init process PID entry (2nd column on the 2nd line in the output below) and NOT the lxc-start
process one as lxc-start
is a parentprocess which forks the container’s init
process:
We can log on the container using
lxc-console
which attaches to one of the container’s ttys. user/password is ubuntu/ubuntu. We can now verify that the loopback device has been created correctly:
Inside the container just run simply
ip address list
:
We didn’t really have to even log on to the container to verify this. We could just simply switch into the container’s network namespace directly on the host, but let’s leave that for later. Alternatively, we could run
lxc-attach -- /sbin/ip address list
but due to some namespace implementation issues on the Kernel I’m running this guide on, lxc-attach
does not seem to work properly, hence I’m using lxc-console
. Stéphane Graber, one of the LXC project core developers recommends in the comments below to use the Kernel version >= 3.8 and lxc-attach
will work just fine.
Before we dive into discussion about how useful the “empty” network type in combination with container is I’m going to talk a bit about what loopback device actually is as a lot of people I come in touch somehow accept its existence but don’t really know even the basic implementation details.
Loopback device is a virtual network device implemented entirely in Kernel which the host uses to communicate with itself. Device (interface) is assigned a special non-routable IP address block
127.0.0.0/8
that is the IP addresses 127.0.0.1
through 127.255.255.254
can be used to communicate with the host itself. Linux distributions also “alias” one of these IP addresses in /etc/hosts
with entry called localhost
.
How does the actual communication work when we know that the assigned IP address block is non-routable? It works simply by creating a special entry in the host’s routing table which is routing all packets destined to
127.0.0.0/8
back to the loopback device lo
:
How is this useful? Mostly for diagnostics, troubleshooting, testing and when you don’t want your service to be exposed on any network. Imagine you want to test different Postgresqlconfigurations which bind to the localhost IP address on the same TCP port. Now, you can argue that you can copy the configuration files and run it on the different TCP port on the same host, but why all that “hassle”, when you can just simply run the Postgresql daemon in the container and avoid the “interference” with other Postgresql instances bound to the same IP address and port. Obviously, this is just one of many use cases. I’m sure you will be able to come up with many other. I’d like to hear about these in the comments below.
Veth
In order to explore the veth network type we will create a new container, but with different network settings. Let’s not waste any more time and fire up another
lxc-create
command:
As you must’ve noticed the container creation takes much shorter time than when we created our first container as
debootstrap
caches the previously downloaded image in the directory you can see in the output above and then just simply copies it over to the new container’s filesystem. Of course, if the second container was created with a different template than the first one, it would take longer as the whole new OS image would have to be downloaded from scratch.
Now, before we look into the container’s network configuration, let’s talk a little bit about what the veth network type actually is. LXC documentation says:
a peer network device is created with one side assigned to the container and the other side is attached to a bridge specified by the lxc.network.link
The peer device can be understood as a pair of fake Ethernet devices that act as a pipe, i.e. traffic sent via one interface comes out the other one. As these devices are Ethernet devices and not point to point devices you can handle broadcast traffic on these interfaces and use protocols other than IP – you are basically protocol independent on top of Ethernet. For the purpose of this guide we will only focus on the IP protocol though.
Armed with this knowledge, what we should expect when running the container with theveth network type enabled, is to have one network interface created on the host and the other one in the container, where the container interface will be “hidden” in the container’s network namespace. The host’s interface will then be bridged to the bridge created on the host if so configured.
Let’s proceed with the container’s network configuration modifications so that it looks as below:
As you’ve probably noticed,
lxc-create
default generated container configuration file is already set to use veth network type so you shouldn’t need to make any modifications to theveth01 container’s configuration if you followed this guide carefully. Now let’s start our new container!
Brilliant! The container is running and it has been assigned
10.0.3.118
IP address automatically. How is the IP address assigned ? In order to understand that we need to understand how is the container actually created. What is happening in terms of networking on the host is – in very simplified terms – the following:- A pair of veth devices is created on the host. Future container’s network device is then configured via DHCP server (it’s actually dnsmasq daemon) which is listening on the IP assigned to the LXC network bridge (in this case that’s
lxcbr0
). You can verify this by runningsudo netstat -ntlp|grep 53
. The bridge’s IP address will serve as the container’s default gateway as well as its nameserver - The host part of the veth device pair is bridged to the configured bridge as per container configuration – as I said in this case that is
lxcbr0
- “Slave” part of the pair is then “moved” to the container, renamed to
eth0
and finally configured in the container’s network namespace - Once the container’s
init
process is started it brings up particular network device interface in the container and we can start networking!
In other words, the above 4 steps serve to bridge the container’s network namespace with the host network namespace via VETH device pair, so you should be able to communicate with the container directly from the host. Let’s send it couple of pings from the host and see if that’s the case:
Awesome! Life is beautiful! But because we are curious we will log on to the veth01container and have a poke around and verify whether the theory we spoke earlier about have practical backing by checking the network configuration of the container. You can log on to the veth01 container either via ssh which runs on the container (ssh is installed by default when you create the container by following the steps in this guide) or you can log on via
lxc-console
as we did when we introduced empty networking type:
Container’s veth pair side looks as we expected.
eth0
has the correct IP assigned, and its network is configured to use the IP address of lxcbr0
bridge. Let’s check the Host’s side:
vethD9YPJ0 device has been created as the Host’s side device of the veth network pair (remember the other side of the pair is in the container).
Let’s see if the Host’s side veth device is bridged to the
lxcbr0
bridge:
BINGO! Looks like all is as we expected it to be and we can sleep well at night knowing that we learnt something new. Well at least I hope we did :–)
Now, let’s take this a bit further and explore Network Namespaces for a bit and try to simulate something similar to what is happening on the network level when the veth01container is created. We won’t dive into actual Kernel namespacing implementation, but we will play around with the userspace tools a bit.
The task we will work on now is to create a veth pair of devices, one of which will be in a different network namespace than the other one and then bridge them to the same bridge as the veth01 container is bridged to. In other words, let’s create a separate network stack in a separate Network Namespace from the Host one without all the “boiler plate” which comes with the creation of LXC container i.e. we won’t be creating any network namespaces except for the network one. Let’s pretend we are network experts and we need to perform some network activities and we don’t need full blown containers – just separate network stacks on the host.
One way of completing this task is simply to perform the following steps:
- create new network namespace
- create veth pair of network devices in the new namespace
- configure the “host isolated” device
- pass the other side of the pair back to the Host’s namespace
- bridge the Host’s veth pair device to the
lxcbr0
bridge
Simple! So let’s roll our sleves up and start working through the above plan. The result should be a pingable IP address in a separate network namespace. All the following steps are performed on the Host.
Let’s create a directory where the network namespaces are read from:
Let’s create a new network namespace and verify it was created (
ip
command has a special flags for listing network namespaces):
Awesome! We are set for some good Linux Network ride! We can check the box next to step (1) in the plan above.
Now Let’s switch to the newly created namespace, create a pair of veth devices and configure one of them to use
10.0.3.78/24
IP address:
As you can see, 3 separate network devices have been created.
lo
device which is a loopback device interface and which is created automatically by default for every newly created network namespace, and a pair of veth network devices. None of them has been assigned an IP address yet:
Let’s assign an IP address to
eth0
interface from the same IP range as lxcbr0
is on and bring it up to life:
Brilliant! We can now check the boxes next to steps (2) and (3). Let’s proceed with step (4) and move the
vethMYTEST
device to the Host’s namespace or in other words i.e. to the network namespace of the process whose PID = 1 which is the host’s init process. We can do that like so:
The
vethMYTEST
device should now be present in the Host’s network namespace so we should be able to bring it up on the Host, but first let’s exit the shell in the “mynamespace” network namespace (I know I’ve picked a horrible name for it) and then bring the device to life:
Until we bridge the
vethMYTEST
device with the lxcbr0
bridge, the “network isolated” IP address should not be accessible. We can verify that very easily:
Now let’s do some bridging! We have an awesome
brctl
utility for this so let’s use it:
Now that we can check the box next to step (5) we should be able to access the “mynamespace” isolated IP address. So let’s verify that claim:
Boom! We are done! Great job everyone!
Before we conclude this part of the post, I will list a few use cases veth interface can be used for:
- create virtual networks between containers – by linking them via different bridges on different networks
- provide a routed link for a container – for routing the packets to “outside” world with the help of iptables as mentioned earlier in this post
- emulate bridged networks – this could be handy for testing complicated network setups locally
- test pretty much almost any network topology
As always, I’m going to leave this to your imagination and creativity. Feel free to leave the suggestions in the comments. And now, let’s move on to the next network type available in containers – macvlan.
Macvlan
Another network type I will talk about in this guide is macvlan. But before we get to work, let’s get familiar with a little bit of theory first. macvlan – for easier understanding MAC VLAN – is a way to take a single network interface and create multiple virtual network interfaces with different MAC addresses asigned to them.
This is a “many-to-one” mapping. Linux VLANs which are capable to use single network interface and map it to multiple virtual networks provide “one-to-many” mapping – one network interface, many network VLANs in one trunk. MAC VLANs map multiple network interfaces (i.e. with different MAC addresses assigned) to one network interfaces. We can obviously combine Linux VLANs with MAC VLANs if we want to.
MAC VLAN allows each configured “slave” device to be in one of three modes:
- PRIVATE – the device never communicates with any other device on the “upper_dev” (ie “master” device) which means that all incoming packets on the “slave” virtual interface are dropped if their source MAC Address matches one of the MAC VLAN interfaces – i.e. the “slaves” can’t communicate with each other
- VEPA – Virtual Ethernet Port Aggregator is a MAC VLAN mode that aggregates virtual machine packets on the server before the resulting single stream is transmitted to the switch. When using VEPA we assume that the the adjacent bridge returns all frames where both source and destination are local to the macvlan port, i.e. the bridge is set up as a reflective relay. This mode of operation is also called “hairpin mode” and it must be supported by upstream switch, not the Linux kernel itself. As such, we forward all traffic out to the switch even if it is destined for us and we rely on the switch at the other end to send it back, so the isolation is done on the switch, not the Linux kernel.
- BRIDGE – provides the behavior of a simple bridge between different macvlan interfaces on the same port. Frames from one interface to another one get delivered directly and are not sent out externally which gives you some security guarantees for the inter-conainer communication. Think of this as a simplified switch which does not need to learn MAC addresses as it already knows them.
What do the above modes mean for LXC containers ? Let’s have a look
- PRIVATE mode disallows any communication between LXC containers
- VEPA mode isolates the containers from one another – UNLESS you have an upstream switch configured to work as reflective relay – in that case, you CAN address containers directly. On top of that, traffic destined to a different MAC VLAN on the same interface (i.e. on the same bridge) will travel through the physical (“master”) interface twice – once to leave (egress) the interface where it is switched and sent back and enters (ingresses) via the SAME interface, which means that this can affect available physical bandwidth and also restricts inter MAC VLAN traffic to the speed of the physical connection
- BRIDGE mode creates a special bridge (“pseudo” bridge – not the same bridge as a standard Linux bridge!) which allows the containers to talk to one another but isolates “pseudo-bridged” interfaces from the host
As VEPA requires a specially configured (reflective relay) switch to demonstrate its functionality and I could not find any way how to configure Linux bridge in reflective relay mode I will not deal with this mode in this guide. PRIVATE mode doesn’t provide much fun to play with, so I won’t be touching on this mode either. This leaves us with BRIDGE mode so let’s hurry up and have some fun.
Bridge mode
To demonstrate how this mode works, we will create 2 LXC containers, which we will bridge over manually created bridge using the LXC bridge network mode provided by macvlannetwork type. Let’s go ahead and create a new bridge on the Host – we don’t need to assign any IP address to the bridge as assigning an IP address to the bridge will not affect the results of this test if you read the above theory carefully:
Let’s create the containers now and configure them to use macvlan network type in bridgemacvlan mode. Let’s also assign each of them an IP address so they can communicate with each other:
We can now fire them up and start playing with them. As you can see below, both containers are now up and running and have been assigned IP addresses as per the configuration above:
If the theory discussed at the beginning of this subchapter is correct we should not be able to access any of the newly created containers from the host. So let’s go ahead and verify this by simple ping tests:
On the other hand, containers should be able to communicate with each other. So let’s go ahead and make a few tests from inside the containers.
Test performed on the first container:
Test performed on the second container:
Perfect! It looks like everything is working as expected! But, let’s test one more thing.
One of the obvious things I have not mentioned until now is that Linux containers can be configured with more than just ONE network interface. In fact you can have various network configurations in a single container each applied to different network interface. Armed with this knowledge we can create a container which will have:
- one VETH network interface linked to
lxcbr0
bridge – we can communicate with it directly from the host on which the container is running - one MAC VLAN network interface in bridge mode linked to
lxcbr1
so the container can communicate with macvlan01 and macvlan02 containers – remember these 2 containers are NOT accessible directly from the host (not accessible is via network – you can always access the containers from the host vialxc-console
indeed)
This will give us a “management” container acessible on the host via network through from which we can access the other two containers – think DMZ. So let’s get started and let’s create a new container which has these capabilities:
Network configuration should follow the above mentioned requirements – container should be accessible from the host and should have access to the MAC VLAN-ed containers:
Let’s start the container and verify that the requirements we set ourselves have been satisfied. The newly created container should have 2 IP addresses assigned on two separate network interfaces. Interface linked to
lxcbr0
should have an IP from 10.0.3.0/24
IP range and interface linked to lxcbr1
should have a manually assigned IP address as per our configuration above on the same LAN as containers macvlanbridge01 andmacvlanbridge02:
Perfect! Looks like the IP assignment has worked as expected! Let’s see if
10.0.3.251
is accessible from the host and let’s confirm that 10.0.5.5
is NOT:
Brilliant! Now, let’s ssh to the dmzmaster01 container, verify that the container has 2 separate network interfaces with particular IP addresses assigned and see if themacvlanbridge01 and macvlanbridge02 containers are accessible from it:
And the container-connectivity test:
With the last test we have concluded a walk through the LXC MAC VLAN networking mode by providing a practical example about how to use it to create a simple DMZ network. As with the other examples in this guide, possibilities are almost endless. We can, for example, create separate private Postgresql replication VLAN to avoid interferring with the application VLAN, we can have private VLAN for corosync intra cluster communication etc. I hope these examples will inspire your creativity and I’m looking forward to hearing the use cases you will come up with – leave them in the comments.
Before we close this subchapter on MAC VLAN it’s worth mentioning that there is a way how we could reach the MAC VLAN containers from the host and that is by creating a new macvlan network interface on the same bridge on which the macvlan containers were created earlier on. The new interface will operate in the same macvlan mode as the container ones i.e. in the bridge mode. Here is how we would go about it.
We will create new macvlan interfaces and link it to the same network interface as the macvlan containers are linked against:
Now let’s assign it an IP address from the same IP range as the macvlan containers are in and bring the interface up:
We should now be able to reach the macvlan containers FROM THE HOST. So let’s verify that claim:
Boom! All works as expected and with the last test we can conclude this subchapter by learning that we can either build DMZ container to reach macvlan containers or to simply create new macvlan interface link to the same master interface on the host as the containers are linked with.
Vlan
I’m not going to explain how Linux VLANs work as there is loads of really good materialavailable online already so I’ll jump straight into LXC documentation. LXC VLAN network type is described as an interface which is linked with the interface specified by
lxc.network.link
and which is then assigned to the container. The new interface “tags” the packets with VLAN tag specified via lxc.network.vlan.id
configuration directive. In other words, using VLAN network type allows you to create a regular VLAN interface in container’s network namespace.
If you wanted to create your own VLAN container which would be tagging ethernet packets with its VLAN id, your container configuration would look something like this:
You can now run our well known
lxc-start
command and create a new LXC container as per the above documentation. I’d like to talk about what is actually happening in the background. You could translate the network part of the container creation process into a concrete set of commands as follows.
First you create vlan interface on the host and link it against an existing host interface – I’ve picked
eth0
interface for our tests:
The above command has created a new interface which will be tagging the outgoing traffic with VLAN id of 20. You can verify that the above created interface is really a VLAN interface with the right VLAN id:
Now that the new interface exists it is going to be sent into particular container. So, let’s so it and send it to our
empty01
container and verify that we have succeeded afterwards:
Network interface no longer exists on the host. Let’s see if it was really moved into the said container. Let’s fire up
lxc-console
and check the links inside the container:
Boom! Our VLAN interface has indeed been moved to the
empty01
container as expected. The host link has now changed from original vlan20@eth0
into vlan20@if2
which essentially means that the vlan20
network interface is linked with host’s network interface whose index is 2
. Basically, when you create a new network interface on the host it is assigned an index and in this case eth0
has been assigned index of 2 on the host.
So what are VLAN containers useful for ? Their usefulness may mostly come into play when you start scaling your container infrastructure whilst keeping your network segmentation in place for various reasons such as security, banwidth etc. You can “hide” your container interface behind physical network interface on the host which would be plugged into physical switch. Obviously you can use them to simulate various VLAN setups.
One of the really interesting use case I could come up with would be that you would have a switch with let’s say 12 ports. Ports 1-11 would would be allocated for VLANs 1-11 and port 12 would be a trunk port. You’d plug your host interface into the trunk port and create 1-11 containers linked to the plugged port – each with a different VLAN id. Each container on this host would now have a physical inteface and could communicate on particular VLAN. Pretty cool! There is a bit of a catch – your trunk port can become a bandwidth bottlneck.
There is a subtle thing in the example I gave above – you can not have more than 1 container with the same VLAN id linked to the same physical network interface. This is logical given how VLANs work. If you want to have several containers on the same VLAN linked to one interface there is a solution for tht – it’s called bridging!
So what do you do if you want a setup like that ? You make a bridge, let’s call it
br10
for VLAN with id 10, you will then create a vlan interface vlan10
like so:
Then you will attach
vlan10
vlan device into br10
, and use the br10
bridge for the containers on VLAN with tag of 10. These containers will be created with network type vethand bridged against br10
. Simples!
This concludes the subchapter for vlan network type. If you have any other cool use cases I want to hear about them!
Phys
Finally, we have reached the last available network type in LXC configuration – phys. This network type is the least complicated to understand in my opinion. So what does LXC documentation say about the phys network type ?
an already existing interface specified by the lxc.network.link is assigned to the container
What this means in practice is – in very simplified terms – that you basically literally rip off the physical network card from the Virtual Box (Virtual Box is what
vagrant
uses to run virtual machines on which this guide is performed) or any other host you’re running this guide on and plug it into the container.
The interface “moves” from one network namespace to the new one, but should be accessible on the same network the original interface existed on i.e. you have basically isolated physical interface on the host (i.e. new network stack has been created in the container) in a similar way to what we were doing at the end of the veth subchapter when we pretended to be network experts.
Let’s get back to the Vagrantfile – the reason why I have specified
:public_network
config directive in it was so that Vagrant creates public network interface – “public” meaning the IP address assigned to this interface is on the same network as my laptop on which I’m running vagrant
commands.
By applying the above theory and moving the “public” interface into the container we should be able to simply access the container directly from our workstatsions or from ANY other host on the same network on which the public interface has been created. Let’s see if we can back our theory in practice! Let’s create a new container and configure it to use the physnetwork type.
Couple of points about the LXC network configuration before we proceed. MAKE SURE that
lxc.network.hwaddr
is the same as the original one created by vagrant – I have come across some ARP madness when I didn’t reuse the original MAC address as the new one was generated randomly by lxc-create
command. You can find the MAC address of the public interface by running sudo ip link list eth2
on the host:
Now that you know the MAC address (
link/ether
field in the output above) of the physical device, you can proceed with the container’s network configuration. Notice that you MUSTspecify the correct gateway – it’s the default gateway of the network on which the original public interface has been created. In my case simple, the public interface is on the same network as my laptop so simple check of Network Preferences gives me 192.168.1.254
.
Cool, we have all the tools, now we can start doing some work! Let’s create the container and modify the network configuration as below:
Now, let’s start the container and log on to it via
lxc-console
:
From the above, we can see that phys01 container has been assigned correct IP. If our theory is correct and if the physical network interface has been “ripped off” and “moved” inside the container we should no longer see in on the host. So let’s have a look if that’s the case:
Bingo! It looks like the interface no longer exists on the Host. Let’s have a look if all looks good inside the container. We need to check the routing and whether we can ping the default gateway. Let’s fire up our well known
lxc-console
command and check the network set up of the container:
Brilliant! Route to default gateway has been created and we can successfully ping it. That means that out packets should be routable across the whole
192.168.1.0/24
network! Which also means – in my case – that I should be able to:- ping my laptop from inside the container
- ping the container’s IP from my laptop
- given that the SSH is running in the container on publicly accessible interface which we have just moved into it and if access to the listening IP and ssh port is allowed, I should be able to ssh to it straight from my laptop
So let’s verify these claims. The IP address of my laptop is:
Let’s ping it from inside the container:
Perfect! Let’s try to ping the container from my laptop. As we know the container’s IP address is
192.168.1.135
Awesome! Now, I have installed ssh on the container and it’s listening on all container’s interfaces. Also there are no iptables rules present on the container so I should be able to ssh to it from anywhere on the same network:
Let’s try to SSH into the container directly from my laptop:
Excellent! With the last example we have now concluded our walk through phys network configuration! Let’s move to the “extra” one called in LXC configuration as none.
None
None networking type is a bit tricky and can get you in a “trouble” if you’re not careful. It certainly did get me into some interesting or rather unexpected situation whilst working on this subchapter until I learnt what is actually happening. In short, this networking type allows the container to share the host’s network namespace. This means that host’s network devices are usable in the container as the container is not running in its own private network namespace.
Let’s fire up a new container with network type set to
none
and have a poke around.
The network configuration should look something like this:
Let’s start the container and log on to it:
Boom! The above list shows exactly what we would expect to see for none01 container. All IP addresses assigned on the host are available inside the container. To be absolutely sure we can try to
lxc-console
into the container and make sure the output of lxc-ls
command above is correct:
Uhh, what is going on ? We are not getting a login prompt from the container like we would normally expect! Further investigation uncovers that
getty
didn’t get spawned. I was a bit clueless about why this is happening, so I turned back to Ubuntu LXC server guide for some answers.
It turned out that this problem is related to running Ubuntu container on Ubuntu host or rather more specifically upstart distribution container on upstart host. Go figure! Like I said the awesome Ubuntu server guide actually does mention this. So do read it carefully and don’t be surprised like myself.
Now, to get back to what is actually happening. Upstart uses an abstract unix socket for communication between processes and the
init
daemon. As it’s an abstract socket, even though the container doesn’t share the same filesystem as the host, the path still clashes so upstart in the container can’t bind it and anything running in the container will talk straight to the host instead. This has an effect where some things don’t get spawned like getty
in our case. What is worse shutting down the container will likely shutdown your machine, too. Yes, that’s correct – shutting down the container set to work with network type none can bring the whole host down. Just try to run lxc-stop -n none01
in the vagrant and you will see that your vagrant will halt.
So be careful when you decide to use this network type in your container setup. I could not think of any practical use cases for this so I turned to lxcontainers channel on IRC to ask the people if they’re using this networking type and how. Stéphane Graber who works for Ubuntu and is one of the LXC core developers mentioned that they are using none network type on the Ubuntu phone to have android (running in a container) be the one bringing up networking for the host.
If you have any interesting use cases, do let me know!
Conclusion
I hope that this blog post helped you to understand the LXC networking at least a little bit more and that it will also motivate you to explore the LXC world even further, including the technologies backed by it such as Docker. If this blog post got you interested in this topic, I would recommend you to check out pipework, which is an awesome tool to automate creation of extra interfaces for containers (for “raw” LXC as well as Docker containers) written by Docker guru Jérôme Petazzoni.
Linux containers have been here for a long time and they are here to stay – in one form or another. Possibilities they offer are endless. Recently they have been mostly spoken about as a synonym of future of software/application delivery. However, we should not stop just there!
Grumpy ops guy in me is seeing containers not only as application delivery tool but also as a missing piece of Infrastructure delivery. We can test our Chef cookbooks/Puppet modules or what have you, we can model networks just like this blog post showed – we can literally model the full stack infrastructure with unquestionable ease and speed on a small budget. Recently I have attended DevOps days London where John Willis gave a presentation about Software Defined Networking. LXC can be an awesome helping piece in what John is suggesting to be the next step in Infrastructure-as-a-code.
So, to sum up. Let’s not be afraid of containers, let’s embrace them, understand them and I’m confident this will pay off in long term. Hopefully this site will help us on the road to container love….
find wolf dildo,cheap sex toys,bulk sex dolls,sex chair,horse dildo,cheap sex toys,dog dildo,vibrators,wholesale sex toys check over here
ReplyDelete