Expert Coders

Expert Coders

State-Of-The-Art Software Development

"The software you built has made mud logging less stressful, enjoyable and flat out easy!" -Customer

Description

Mike Cunningham
Owner

LXD Containers on Ubuntu Servers: Employing macvlan for Local IP Addressing

LXD (pronounced "lex-dee") has rapidly become a go-to solution for virtualization on Linux. It offers a lightweight and streamlined alternative to full-fledged virtual machines. One of the most compelling advantages of LXD is its ability to run system containers that look and behave like a traditional VM but with the overhead and flexibility of a container. In this blog post, we'll delve deep into using LXD containers on Ubuntu servers, and the secret sauce that makes it all come together – macvlan – to give your containers a dedicated IP address.

Why macvlan?

For those unfamiliar, macvlan is a Linux network driver. It allows a physical NIC (Network Interface Card) to be represented as multiple virtual NICs. In simpler terms, it's a way to assign multiple MAC addresses (and hence multiple IP addresses) to a single physical interface, each corresponding to a different virtual interface.

This becomes especially useful for LXD containers. By utilizing macvlan, you can give each container its IP address, making it appear as a standalone entity on the network. For example, if you're running a PostgreSQL server inside an LXD container, it would be advantageous to have it accessible directly via its dedicated IP address.

Setting up LXD on Ubuntu

Before we dive into the macvlan setup, let's ensure our LXD environment is ready.

    1. Installation:

      bash
  • sudo apt update sudo apt install lxd lxd-client
  • Initialize LXD:

    bash
  • lxc profile create macvlan lxc profile device add macvlan eth0 nic nictype=macvlan parent=YOUR_NIC_NAME

    Replace YOUR_NIC_NAME with the name of your primary network interface (e.g., ens160).

  • Launch a container with the macvlan profile:

    bash
  • sudo apt update sudo apt install postgresql postgresql-contrib
  • Adjust the listening addresses: Edit the postgresql.conf file and set listen_addresses to '*' to ensure PostgreSQL listens on all available addresses.

  • Modify pg_hba.conf: Make sure you allow connections from your desired hosts.

  • Restart PostgreSQL:

    bash
  1. sudo lxd init

    Follow the prompts, and you'll have LXD configured in no time.

Configuring macvlan for LXD

Now, let's set up macvlan for our containers.

  1. Define the macvlan profile:

    bash
  1. lxc launch ubuntu:20.04 mycontainer -p default -p macvlan
  2. Verify IP Address: Inside your container, check its IP address using ip a. You should notice it has an IP address from your local network.

Running a PostgreSQL Server in the Container

With your container now having a dedicated IP, setting up a PostgreSQL server becomes straightforward.

  1. Install PostgreSQL:

    bash
  1. sudo service postgresql restart

Your PostgreSQL server should now be accessible directly using the dedicated IP address of the container.

Conclusion

LXD containers on Ubuntu servers provide an exciting blend of VM-like functionality with the agility and low overhead of containers. By employing macvlan, you can push the envelope further by offering dedicated IP addresses to each of your containers. This can be particularly beneficial when running services like PostgreSQL, ensuring easy accessibility and clear network segmentation.

Whether you're a software developer specializing in tailored solutions, like those available at expertcoders.net, or just someone keen on exploring the vast universe of Linux virtualization, LXD and macvlan promise a journey worth embarking on.