Experience the LXD container on Ubuntu

Source: Internet
Author: User

Experience the LXD container on Ubuntu

ubuntu plugin container 

The main character of this article is the container, a structure similar to a virtual machine but more lightweight. You can easily create a bunch of containers in your Ubuntu Desktop System!

A virtual opportunity virtualizes the entire computer and allows you to install the client operating system. In contrast, containers reuse the Linux kernel of the host, but simply include the selected root file system (that is, the runtime environment ). The Linux kernel has many functions to split running Linux containers from our hosts (that is, our Ubuntu Desktop ).

Linux requires some manual operations to directly manage them. Fortunately, LXD (pronounced Lex-deeh) is a service for us to manage Linux containers.

We will see how: docker create ubuntu 16.04 container

  1. Configure the container on our Ubuntu Desktop,
  2. Create a container,
  3. Install a web Server,
  4. Test this web server and
  5. Clean up everything.

 Docker ubuntu container

Set Ubuntu container

If you are installing Ubuntu 16.04, you do not need to do anything. You only need to install some additional packages listed below. If you have installed Ubuntu 14.04.x or Ubuntu 15.10, perform some operations according to the LXD 2.0 series (II): installation and configuration, and then return.

Make sure that the package list has been updated: docker ubuntu container run

  1. sudo apt update
  2. sudo apt upgrade

InstalllxdPackage: run ubuntu container on windows 

  1. sudo apt install lxd

If you have installed Ubuntu 16.04, you can store your container files in the format of ZFS file system. Linux kernel in Ubuntu 16.04 contains the necessary kernel modules that support ZFS. To enable LXD to use ZFS for storage, we only need to install the ZFS toolkit. Without ZFS, containers are stored as separate files in the host file system. With ZFS, We have features such as copy at write time, which can make the task faster.

Installzfsutils-linuxPackage (if you have installed Ubuntu 16.04.x ): ubuntu container image

  1. sudo apt install zfsutils-linux

After LXD is installed, the package installation script will add youlxdGroup. This group of members does not need to passsudoYou can directly use LXD to manage containers. According to Linux's habits, You need to log out of the desktop session before logging on to the application.lxd. (If you are a master, you can also executenewgrp lxd).

Before getting started, LXD needs to initialize storage and network parameters.

Run the following command:

  1. $ sudo lxd init
  2. Name of the storage backend to use(diror zfs): zfs
  3. Create a new ZFS pool (yes/no)?yes
  4. Name of the new ZFS pool: lxd-pool
  5. Would you like to use an existing block device (yes/no)?no
  6. Sizein GB of the new loop device (1GB minimum):30
  7. Would you like LXD to be available over the network (yes/no)?no
  8. Do you want to configure the LXD bridge (yes/no)?yes
  9. >You will be asked about the network bridge configuration.Accept all defaults andcontinue.
  10. Warning:Stopping lxd.service, but it can still be activated by:
  11. lxd.socket
  12. LXD has been successfully configured.
  13. $ _

We build a file system in a (separate) file instead of a block device (that is, a partition) as a ZFS pool, so we do not need to perform additional partitioning operations. In this example, I specify the size of 30 GB. The space is taken from the root (/In the file system. This file is/var/lib/lxd/zfs.img.

All right! The initial configuration is complete. If you have any questions or want to learn more, please read the https://www.stgraber.org/2016/03/15/lxd-2-0-installing-and-configuring-lxd-212.

 

Create the first container

All LXD management operations can be performed throughlxcCommand. We givelxcManage containers with different parameters.

  1. lxc list

List all installed containers. Obviously, this list is empty now, but it indicates that our installation is okay.

  1. lxc image list

List (cached) images that can be used to start a container. Obviously, this list is empty, but it also indicates that our installation is correct.

  1. lxc image list ubuntu:

List the remote images that can be downloaded and started. The Ubuntu image is displayed.

  1. lxc image list images:

List the images of various releases that can be used to start containers (cached. This will list the images of various releases, such as Alpine, Debian, Gentoo, OpenSUSE, and Fedora.

Let's start a Ubuntu 16.04 container calledc1:

  1. $ lxc launch ubuntu:x c1
  2. Creating c1
  3. Starting c1
  4. $

We uselaunchAction, and then select the imageubuntu:x(xXenial/16.04 image ).c1Container name.

Let's take a look at the first installed container,

  1. $ lxc list
  2. +---------|---------|----------------------|------|------------|-----------+
  3. | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
  4. +---------|---------|----------------------|------|------------|-----------+
  5. | c1 | RUNNING |10.173.82.158(eth0)|| PERSISTENT |0|
  6. +---------|---------|----------------------|------|------------|-----------+

Our first container c1 is running, and it also has its own IP address (which can be accessed locally ). We can start using it!

 

Install web Server

We can run commands in the container. The operation for running the command isexec.

  1. $ lxc exec c1 --uptime
  2. 11:47:25 up 2 min,0users,load average:0.07,0.05,0.04
  3. $ _

InexecThen, specify the container and enter the command to run in the container. The container runs for only 2 minutes. This is a newly released container :-).

In the command line--It is related to the shell parameter processing process. If our command does not have any parameters, it can be omitted completely.-.

  1. $ lxc exec c1 --df-h

This is a must-Because our command uses Parameters-h. If this parameter is omitted-.

Then we run the shell in the container to update the package list.

  1. $ lxc exec c1 bash
  2. root@c1:~# apt update
  3. Ign http://archive.ubuntu.com trusty InRelease
  4. Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
  5. Get:2 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
  6. ...
  7. Hit http://archive.ubuntu.com trusty/universe Translation-en
  8. Fetched11.2 MB in9s(1228 kB/s)
  9. Readingpackage lists...Done
  10. root@c1:~# apt upgrade
  11. Readingpackage lists...Done
  12. Building dependency tree
  13. ...
  14. Processing triggers forman-db (2.6.7.1-1ubuntu1)...
  15. Setting up dpkg (1.17.5ubuntu5.7)...
  16. root@c1:~# _

We use nginx for web servers. Nginx is cooler than Apache web server in some aspects.

  1. root@c1:~# apt install nginx
  2. Readingpackage lists...Done
  3. Building dependency tree
  4. ...
  5. Setting up nginx-core (1.4.6-1ubuntu3.5)...
  6. Setting up nginx (1.4.6-1ubuntu3.5)...
  7. Processing triggers for libc-bin (2.19-0ubuntu6.9)...
  8. root@c1:~# _

Let's use a browser to access this web server. Remember that the IP address is 10.173.82.158, so you need to enter this IP address in your browser.


Lxd-nginx

Let's make some minor changes to the page text. Return to the container and enter the default HTML page Directory.

  1. root@c1:~#cd/var/www/html/
  2. root@c1:/var/www/html#ls-l
  3. total 2
  4. -rw-r--r--1 root root 612Jun2512:15 index.nginx-debian.html
  5. root@c1:/var/www/html#

Use nano to edit the file and save the file:


Lxd-nginx-nano

Then, click the page to see,


Lxd-nginx-modified

 

Clear

Let's clear the container, that is, delete it. When needed, we can easily create a new container.

  1. $ lxc list
  2. +---------+---------+----------------------+------+------------+-----------+
  3. | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
  4. +---------+---------+----------------------+------+------------+-----------+
  5. | c1 | RUNNING |10.173.82.169(eth0)|| PERSISTENT |0|
  6. +---------+---------+----------------------+------+------------+-----------+
  7. $ lxc stop c1
  8. $ lxc delete c1
  9. $ lxc list
  10. +---------+---------+----------------------+------+------------+-----------+
  11. | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
  12. +---------+---------+----------------------+------+------------+-----------+
  13. +---------+---------+----------------------+------+------------+-----------+

We stopped (shut down) the container and deleted it.

This article ends now. There are many ways to use containers. This is only the first step to configure Ubuntu and try to use containers.

Via: https://blog.simos.info/trying-out-lxd-containers-on-our-ubuntu/

Author: Simos Xenitellis Translator: lujun9972 proofreaders: wxy

This article was originally compiled by LCTT and launched with the honor of Linux in China



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.