How did I use LXD to manage dev environment?

How did I use LXD to manage dev environment?

I've used LXD for a while when I worked for Canonical, it's really cool thing to create the isolated linux development environments.

What is LXD?

The main feature of LXD is that it provides the virtual machines similar functions by using and managing the linux containers. it's much lighter and faster than the traditional virtual machines.

It's image based with a bunch of pre-made images.

You can find more details from [official site] (https://linuxcontainers.org/lxd/introduction/)

Installtion

Ubuntu:

apt install lxd lxd-client

or

snap install lxd

Mac:

brew install lxc

Create the ZFS pool

What is ZFS

ZFS(Zettabyte File System) can provide the intelligent performance and data integrity when given a pool of disks. ZFS has some outstanding features:

  1. ZFS is 128-bit, so it's super scalable. So the following data storage units, in theory it can store up to 2 ** (128 - 80) Yib data.

Data storage units measurement

1Kib = 2 ** 10 (Kilobyte)
1Mib = 2 ** 20 (Megabyte)
1Gib = 2 ** 30 (Gigabyte)
1Tib = 2 ** 40 (Tebibyte)
1Pib = 2 ** 50 (Pebibye)
1Eib = 2 ** 60 (Exbibyte)
1Zib = 2 ** 70 (Zettabyte)
1Yib = 2 ** 80 (Yobibyte)

Since ZFS uses a storage pool, it means the storage can be dynamically added to ore removed from the pools without interrupting services.

  1. Unsurpassed data reliability
    By transparently checking the checksums in parent block pointers, the data reliablity is ensured. it includes the silent corruption detection and automatic data repair.

  2. Provide the compression to reduce the disk space and IO bandwidth

See more details from here

Create the ZFS pool

On ubuntu, install the package:

$ sudo apt install zfsutils-linux 

then using the zpool to creat the storage pool:

  1. A partion/disk based zpool

    $ sudo zpool create lxd-pool /dev/mapper/vg_ubuntu-lv_zfs
    $ sudo zpool status
    pool: lxd
    state: ONLINE
    scan: none requested
    config:

     NAME                STATE     READ WRITE CKSUM
     lxd                 ONLINE       0     0     0
       vg_ubuntu-lv_zfs  ONLINE       0     0     0
    
  2. A partion/disk based zpool

    $ dd if=/dev/zero of=/home/user/image-file-name bs=1M count=2048
    $ sudo zpool create lxd-pool /home/user/image-file-name
    $ sudo zpool status
    pool: lxd-pool
    state: ONLINE
    scan: none requested
    config:

         NAME                          STATE     READ WRITE CKSUM
         lxd-pool                      ONLINE       0     0     0
           /home/user/image-file-name  ONLINE       0     0     0
    

For more ZFS operations see here

Create the lxc instance

Now you can create your instance simply run:

$ lxc launch ubuntu:18.04 dev-env

First time it takes some time to download the ubuntu 18.04 images.

see the lxc container list:

$ lxc list

access the container shell:

$ lxc exec dev-env -- /bin/bash

push a file to container:

lxc file push hosts/file dev-env/tmp/

stop the container:

$ lxc stop dev-env

start the container:

$ lxc start dev-env

Now you can see it's dramatically quickly start and stop the containers.

finally you can delete the container:

$ lxc delete dev-env

Be sure the container is stopped before you delete the container

Create the Development Profile

When we use the LXC instance as our development environment, we don want to share the project folder or home folder from the host machine, we can define the disk, network policy from the profile:

First we create a new profile:

$ lxc profile create my-dev-profile

$ lxc profile edit my-dev-profile

then replace the following snippet in the editor:

config:
  security.privileged: "true"
  user.user-data: |
    users:
      name: user_name
      groups: sudo
      shell: /bin/bash
description: dev profile for lxd
devices:
  .home:
    path: /home/user_name
    source: /home/user_name
    type: disk
  eth0:
    name: eth0
    nictype: bridged
    parent: lxdbr0
    type: nic
  root:
    path: /
    pool: lxd-pool  # Remember to use the above new created pool name here
    type: disk

A useful script to auto create the LXC instance

Restore the LXC containers

sudo zpool create -m /dev/sda13 lxd
sudo zpool attach lxd /dev/sda13
sudo zpool attach /dev/sda13 lxd

sudo zpool reopen lxd
sudo zpool create lxd /dev/sda13
sudo zpool create lxd /dev/sda13 -f

Restart the network

Dispatch the LXC instance ports to local machine