Shipping Containers

Reading Time: about 8 minutes.

Virtualization covers a range of related topics, all having to do with emulation of servers or other computer systems. It runs the gamut from fully virtualized hardware, such as KVM or VirtualBox, to lightweight containers like Docker. Somewhere in between those two is operating system-level virtualization. Currently, the most exciting OS-level virtualization for Linux is LXC, or Linux Containers.

Linux Containers (LXC) aim to create a virtualized container that offers an environment as close as possible to a fully virtualized machine. However, because it's a container, an LXC instance doesn't have the overhead of a separate kernel and full hardware emulation. This means that LXC instances can be deployed and managed quickly and efficiently.

LXD: the LXC Hypervisor

While LXC is great at what it does, its low-level command-line interface is not very user friendly. It requires a lot of specialized knowledge and complex configuration on top of that to use it effectively. Enter LXD. LXD is the newer, better way to interface with LXC. LXD provides a system-wide daemon, a new LXC command-line client, and an OpenStack Nova plugin. The daemon exports a REST API, which makes the entire LXD experience very powerful and extremely simple to use.

Since it uses LXC as its underlying layer, LXD is able to provide robust security with underprivileged containers and resource restrictions. Its new command-line client is simple to use and intuitive. From multiple containers on a laptop to thousands on a production server, LXD is highly scalable.

In this tutorial, we will deploy a CenturyLink Cloud Server and install LXD on it. We will then cover a few common LXD tasks and show you how to provision, deploy, and configure containers remotely.

Tools Used

CenturyLink Cloud Compute servers are high-performance cloud servers. These enterprise-grade virtual machines are easy to deploy and manage from the CenturyLink Cloud Console and via our powerful API. These servers are designed for high-performance workloads, with top-tier CPU, RAM, and 100% flash storage.

Before We Start

If you don’t have a CenturyLink Cloud account yet, head over to our website and activate an account. You’ll need it to access CenturyLink Cloud Servers.

Provisioning a Virtual Server

Before we get started with LXD, we need to provision and configure a new Cloud Server. Follow the instructions below:

  1. Log-in to the CenturyLink Cloud Control Portal.
  2. From the Navigation Menu, click Infrastructure > Servers.
  3. Select a region for your new server, then click create > server.
  4. In the "operating system" drop-down menu, select Ubuntu 14 | 64-bit.
  5. After you finish filling out the Create Server form, click create server.

Adding a Public IP to Your New Server

The next step is to add a public IP address to your server. Follow the directions below.

  1. From the Navigation Menu, click Infrastructure > Servers.
  2. Navigate to the region you selected when creating your server and click on the server name.
  3. Click more > add public ip.
  4. Check the box to open port SSH/SFTP (22).
  5. Click custom port... > single port.
  6. Select "TCP" in the dropdown menu and enter 8433 in the text box. This opens up port 8433 for LXD. Add Public IP
  7. Click add public ip address.

Installing LXD

Follow the steps below to install and configure LXD:

  1. Log-in to the CenturyLink Cloud Control Portal.
  2. From the Navigation Menu, click Infrastructure > Servers.
  3. Navigate to your server.
  4. Your server's public IP address and login credentials will be noted on the screen.
  5. From a shell on your local machine, connect to your new server with the following command. Replace "YOUR.VPS.IP" with your server's public IP address:

    ssh root@YOUR.VPS.IP
  6. From the shell prompt on your server, run the following commands to install LXD:

    apt-get update
    apt-get -t trusty-backports install lxd
  7. To configure LXD, run the following command:

    lxd init
  8. For Name of the storage backend to use, type "dir".

  9. For Would you like LXD to be available over the network, type "yes".
  10. For Address to bind LXD to, use the default value, "".
  11. For Port to bind LXD to, use the default value, "8443".
  12. Enter a new password for Trust password for new clients. Record this password. You will need it later.
  13. For Do you want to configure the LXD bridge, type "yes".
  14. The default values for the rest of the LXD bridge configuration should work just fine.
  15. LXD is now configured and running.

Importing and Launching Linux Container Images

Before we can deploy any LXC instances using LXD, we need to import images. LXD can import images via three different methods: remote LXD servers, the built-in image stores, and raw image files. We will import images for CentOS and Ubuntu using the built-in image stores. With this method, we can import and launch a new image with one command. Follow the directions below.

  1. To import and launch an Ubuntu container, run the following command:

    lxc launch ubuntu:14.04 ubuntu14
  2. To import and launch a CentOS container, run the following command:

    lxc launch images:centos/7/amd64 centos7
  3. To get a list of running images, run the following command:

    lxc list

    LXD Launch Containers

Using Linux Containers

The command-line client makes interacting with containers simple. For instance, to update all packages on your Ubuntu container, run the following shell command:

lxc exec ubuntu14 -- apt-get -y update

To execute an interactive shell on your CentOS container, run the following command:

lxc exec centos7 -- /bin/bash -l

You can see a list of available options with lxc help.

Accessing Linux Containers Remotely

If you have a Linux workstation, you can manipulate containers in a remote LXD daemon. Follow these steps on your local Linux machine to connect the remote LXD instance. Replace YOUR.VPS.IP with the public IP address of your LXD server. You will also need the password you entered in Step 12 of Installing LXD.

  1. Install the LXD command-line client. In Ubuntu 16.04, this can be done with the following command:

    sudo apt install -y lxd-client
  2. Add your remote LXD server to the client configuration with the following command:

    lxc remote add ctl YOUR.VPS.IP
  3. At the Admin password for ctl: prompt, type the password from Step 12 of Installing LXD.

You can now manipulate containers running on your virtual server's LXD instance by prefixing their image names with ctl:. For example, to execute an interactive shell on the Ubuntu container you previously created, run the following command:

lxc exec ctl:centos7 -- /bin/bash -l

Observação: For non-Linux workstations that need to access LXD instances remotely, check out the list of API clients for LXD.

Next Steps

You will see even more performance with a CenturyLink Cloud Bare Metal Server. Bare Metal servers are fully integrated into CenturyLink Cloud, allowing you to use LXD to create a powerful, container-based public cloud with full scaling and provisioning support.

LXD also allows you to create your own images. The containers can be useful for provisioning and deploying development environments. Check out our article on moving from cloud-based development to on-premises installation for ideas on how hosted virtual servers and on-premises deployment can be used to create a hybrid cloud solution.

Sign-up for our Developer-focused newsletter CODE. Designed hands-on by developers, for developers. Keep up to date on topics of interest: tutorials, tips and tricks, and community building events.

We’re a different kind of cloud provider – let us show you why.