Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introduction

You

Table of Contents

Introduction

Panel

This article is useable, but not complete. What's missing,

  • Translation table of commands from LXC to LXD
  • Some more key basics of using LXD covered in LXC
  • Network discussion differences
  • Advanced Networking as a second article

In this version, you must finish the LXC and LXC with Advanced Networking before starting this article. 

...

  • LXD sits on top of LXC and uses a higher level set of commands
  • Install just LXD which add LXC Underlying Components (I found straight LXC underlying Components are different when installed directly)
  • REST API to orchestrates containers locally and remotely
  • Allows moving and copying between hosts
  • Takes advantage of advanced file systems (in particular ZFS)
  • Improves on security specifically running as unprivileged hence root inside container does not get root on host

...

Setup 

On your host system, either install LXC or install LXD. Though LXD setups an LXC subsystem, If found that the packages are different.

A key feature of virtualization technology is taking snapshots and cloning. With traditional file systems, this is expensive in terms of storage and speed. Next generation The next generation volume manager and file system ZFS solves many of theses problems and it is recommended by Ubuntu to install and use with LXD,

Code Block
languagebash
sudo apt-get install bridge-utils zfsutils-linux # must be using Ubuntu 16.04 or higher.

Next configure LXD,The other key feature to use with LXD is network bridging. By it's nature, the containers created by LXD exist in their own network. The network bridge utils allow you to expose your container to the rest of the network,

Code Block
languagebash
sudo apt-get install bridge-utils

Finally install LXD,

Code Block
languagebash
sudo apt-get install lxd

Initial Configuration

Before using LXD you need to to do an initial configuration.

The configuration is evolving and you will notice differences between versions of Ubuntu. Because the material is rather tough, we'll archive the older Ubuntu lxd init and keep the most current release here,

LXD Init for LTS

Code Block
languagebash
# show version of Ubuntu,
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.2 LTS
Release:    16.04
Codename:   xenial
 
# initialize LXD
sudo lxd init

For the most part you will be able to hit Enter and accept defaults. Some options you may go back and change but others will require lots of work so take your time.

I'll go over some of the more confusing options,

Code Block
languagebash
Name of the storage backend to use (dir or zfs) [default=zfs]: zfs
Create a new ZFS pool (yes/no) [default=yes]? yes
Name of the new ZFS pool [default=lxd]: lxd
Would you like to use an existing block device (yes/no) [default=no]? no
Size in GB of the new loop device (1GB minimum) [default=1518]: 15

LXD allows management over the network. This is useful in an environment with multiple host LXDs and you want the ability to centrally manage. In this article, I am choosing yes because we'll later use Virtual Machine Manager to show GUI management,

Code Block
languagebash
Would you like LXD to be available over the network (yes/no) [default=no]? noyes
WouldAddress youto likebind staleLXD cached images to be(not updated automatically (yes/noincluding port) [default=yesall]?: yes
WouldPort youto likebind LXD to create a new network bridge [default=8443]: 
Trust password for new clients: 
Again: 

LXD init will configure a bridge. In order to say yes to this you must have the bridge utils installed. In this article we covered that so say yes

Code Block
languagebash
Do you want to configure the LXD bridge (yes/no) [default=yes]? yes
What should the new bridge be called [default=lxdbr0]? lxdbr0
What IPv4 subnet should be used (CIDR notation, "auto" or "none") [default=auto]? auto
What IPv6 subnet should be used (CIDR notation, "auto" or "none") [default=auto]? auto

 You will be prompted with a "text ui" interface. Use your arrow keys to pick the buttons.

Introduction,

Panel

Conifiguring lxd

Containers need a bridge to connect them together and to the host for outside network connectivity. Choosing this option will let you configure the default LXD bridge to your liking. If you would rather not have LXD do this for you, then you will be asked whether you want to use an existing bridge or just do everything manually. Would you like to setup a network bridge for LXD containers now? 

Yes

The default bridge name is lxdbr0. It's just a name but suggest you leave as default. To get to the button, use the down arrow key,

Panel

Conifiguring lxd

A valid network interface name (e.g. lxdbr0).

Bridge interface name: lxdbr0

Ok

Go with IPv4 unless you have some special need to use IPv6,

Panel

This is needed to provide IPv4 connectivity for your containers. Do you want to setup an IPv4 subnet?


Yes

You will be assigned a random subnet. For the purposes of this article, think of a subnet as a way of naming your postal code. It's the difference between Canada and US postal code. When given time I'll provide more description and write or link to a good article on subnets,

Panel

A random subnet was selected for you. This subnet was selected for your convenience and the next questions have been pre-answered accordingly. Please make sure this subnet isn't already in use somewhere on your network, if it is, change it to one which isn't. If you later notice network connectivity issues, re-configure lxd and pick a different subnet.

Yes

IPv4 address. This prompt is not very clear. Going to go back and check shortly. It is either reserving this IP address or asking you for a sample to determine the subnet.

Panel

A valid IPv4 address. (e.g. 10.0.8.1): 10.139.135.1

Ok

A valid CIDR mask. (e.g. 24),

Panel

IPv4 CIDR mask:: 24

Ok

Initial IP Address Range does not matter unless you want to use a lot of static IP addresses. I need to think about this a bit more... the idealism behind cloud is not require static, but reality (at least for now), I still find static useful and in some application designs (stateful apps, older PKI's... ect...) absolutely necessary.

Panel

The first address to be handed out over DHCP (e.g. 10.0.8.2)

First DHCP address: 10.139.135.2

Ok

Pick default for the last address,

Panel

The last address to be handed out over DHCP (e.g. 10.0.8.254)

Last DHCP address: 10.139.135.254

Ok

The DHCP leas number should be default unless you went and changed the range. If so you should adjust to match. Yeah I know the numbers do not add up (254 - 2 = 252) but I believe there is some fancy math for that... look up and link to my LXC articles.

Panel

 The maximum number of DHCP leases that can be obtained. (e.g. 250)

Max number of DHCP clients: 250

Ok

For almost all networks this will be Yes. Tin google or to talk to Dickson or Andrew though at some point to put explanations here.

Panel

This is needed unless you are using a routed IPv4 subnet.

Do you want to NAT the IPv4 traffic? 

Yes

Unless you really need IPv6 say no,

Panel

This is needed to provide IPv6 connectivity for your containers.

Do you want to setup an IPv6 subnet?   

No

After this you will be taken out of the Text UI. The warning message is due to network bridge setup,

Code Block
languagebash
Warning: Stopping lxd.service, but it can still be activated by:
  lxd.socket
LXD has been successfully configured.

You no longer need root, but users must be added to the lxd group to run the lxc commands.I hate doing this in Linux, but let's reboot to make sure the lxd service is ok,

Code Block
languagebash
sudo reboot

Add Users to the lxd Group

By default, users cannot yet use LXD until you add them to the reserved group. In this example, I'm adding my account,

Code Block
languagebash
sudo usermod -a -G lxd usernametin.pham

Reminder that if you use your own account, you need to log out then log back in.

Verify LXD is Working

Basic test to verify lxd is working and it will also generate your random client certificate (used by LXD to secure calls) on your account,

Code Block
languagebash
lxc image list
+-------+-------------+--------+-------------+------+------+-------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------+-------------+--------+-------------+------+------+-------------+

At this point we have no LXD images.

Basic LXD Commands

To view your bridge information.

Code Block
languagebash
lxc profile edit default

Notice the following,

Code Block
languagebash
config: {}
description: Default LXD profile
devices:
  eth0:
    nictype: bridged
    parent: lxdbr0
    type: nic
name: default

What we see here is that the containers will be handed IPs from the LXD bridge and exist within the LXD network.

Warning
Tin is refining the 2nd pass of this article ... I am currently at this point.

Images

...

Cached Images in Image Store

...

Image Server LIstPurposeComment
ubuntu:Ubuntu stable images.We'll be working with this one.
ubuntu-daily:

images:All Linux distributionsdistriibutions.For example you can load a RedHat Linux alternative distrobution.

...

Code Block
languagebash
lxc image list ubuntu: | less
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+
|       ALIAS        | FINGERPRINT  | PUBLIC |                   DESCRIPTION                   |  ARCH   |   SIZE   |          UPLOAD DATE          |
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+
| p (5 more)         | 6041c5e200b6 | yes    | ubuntu 12.04 LTS amd64 (release) (20161205)     | x86_64  | 156.47MB | Dec 5, 2016 at 12:00am (UTC)  |
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+
| p/armhf (2 more)   | c19b1fff3336 | yes    | ubuntu 12.04 LTS armhf (release) (20161205)     | armv7l  | 135.19MB | Dec 5, 2016 at 12:00am (UTC)  |
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+
| p/i386 (2 more)    | ce5c6821eebb | yes    | ubuntu 12.04 LTS i386 (release) (20161205)      | i686    | 139.28MB | Dec 5, 2016 at 12:00am (UTC)  |
+--------------------+--------------+--------+-------------------------------------------------+---------+----------+-------------------------------+
...
# Reduce the list to your machine architecture, in my case and most it will be amd64
lxc image list ubuntu: amd64

The output table from list is not very clear. I don't understand what to reference to use What you reference when using the "launch" command . Will put explanation here once figured out. Here is what I can tellis the main part of the ALIAS name ignoring anything in brackets. For example to install and launch other distrobusions based off of the images: list which has all Linux distributions,

Example "launch" Command ReferenceColumnNote
ubuntu:16.04
This will download from the Ubuntu image server grabbing the latest 16.04 matching your machines architecture, win in my case amd64.
ubuntu:6041c5e200b6FINGERPRINTWill specifically reference the specific image in the list.

images:centos/7


Go against the "images" image server, download the latest cantos version 7 matching your machines architecture.
images:centos/6/amd64
In this example, specifies Specifies the latest version 6 and specifies the architecture to use.

At this point LXD is all setup and ready to use.

...

Also, let's look at the running container. The commands are only slightly different than using straight LXC

Code Block
languagebash
lxc list
+-------------+---------+----------------------+------+------------+-----------+
|    NAME     |  STATE  |         IPV4         | IPV6 |    TYPE    | SNAPSHOTS |
+-------------+---------+----------------------+------+------------+-----------+
| container01 | RUNNING | 10.94.217.171 (eth0) |      | PERSISTENT | 0         |
+-------------+---------+----------------------+------+------------+-----------+

# Inspect the container,
lxc info container01

Name: container01
Remote: unix:/var/lib/lxd/unix.socket
Architecture: x86_64
Created: 2016/12/22 03:37 UTC
Status: Running
Type: persistent
Profiles: default
Pid: 19681
Ips:
  eth0:	inet	10.94.217.171	vethDXUYOE
  eth0:	inet6	fe80::216:3eff:fe07:69ea	vethDXUYOE
  lo:	inet	127.0.0.1
  lo:	inet6	::1
Resources:
  Processes: 26
  Disk usage:
    root: 144.32MB
  Memory usage:
    Memory (current): 23.24MB
    Memory (peak): 45.28MB
# ...

...

There are two key methods. Clone semi-live using snapshots and shutting down first.

Note

Investigate if cloning between different lxd versions is supported and to what extent.


Shutdown Approach

Log into myhost02

...

Code Block
languagebash
lxc list
+-------+---------+------+------+------------+-----------+
| NAME  |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+-------+---------+------+------+------------+-----------+
| web01 | STOPPED |      |      | PERSISTENT | 0         |
+-------+---------+------+------+------------+-----------+

...

Semi-Live Approach

...

Taking Snapshots

Snapshots are a great way to backup a container before upgrading or changing any files that will break a container.

Code Block
languagebash
lxc snapshot container01 snapshotname01

Using lxc list will show that your container now has a snapshot.

Code Block
languagebash
lxc list
+-------------+---------+------+------+------------+-----------+
|    NAME     |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+-------------+---------+------+------+------------+-----------+
| container01 | STOPPED |      |      | PERSISTENT | 1         |
+-------------+---------+------+------+------------+-----------+

The snapshot name can be viewed using the lxc info command

Code Block
languagebash
lxc info container01
Name: container01
Remote: https://192.168.0.109:8443
Architecture: x86_64
...
Snapshots:
  snapshotname01 (taken at 2017/02/02 15:33 UTC) (stateless)

To restore a snapshot

Code Block
languagebash
lxc restore container01 snapshotname01

Renaming a snapshot

Code Block
languagebash
lxc move container01/snapshotname01 container01/snaptestname01
lxc info container01
...
Snapshots:
  snaptestname01 (taken at 2017/02/02 15:33 UTC) (stateless)

Creating a container from a snapshot

Code Block
languagebash
lxc copy container01/snaptestname01 container02

Deleting a snapshot

Code Block
languagebash
lxc delete container01/snaptestname01


Moving Containers

With copy, the new container is identical in every way except no snapshots and volatile keys (ie MAC address and hostnames) will be regenerated.

...

  1. Security
  2. Advanced Networking
    1. Tunnelling ect... as covered in LXC with Advanced Networking
    2. Joining LXD host networks together
  3. Capping Resource Utilization
  4. Sparse Containers like Solaris...
  5. Permissions Inside and Outside of Containers

File Transfer with Containers

Files can now be transferred from host to containers or container to container with the new lxc push and pull commands.

Code Block
languagebash
lxc file push sourcefile container/path/to/directory/
lxc file pull container/path/to/file directory

Reconfigure

If you need to reconfigure the bridge you can by using this command.

Code Block
languagebash
sudo dpkg-reconfigure -p medium lxd

Reference

Initializing LXD  - https://insights.ubuntu.com/2016/03/16/lxd-2-0-installing-and-configuring-lxd-212/

...

Live Container Migrations - https://bobcares.com/blog/lxc-live-migration-to-minimize-business-downtime/

Mounting a local folder - https://tribaal.io/nicer-mounting-home-in-lxd.html