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
- Configure the container on our Ubuntu Desktop,
- Create a container,
- Install a web Server,
- Test this web server and
- 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
- sudo apt update
- sudo apt upgrade
InstalllxdPackage: run ubuntu container on windows
- 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
- 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:
- $ sudo lxd init
- Name of the storage backend to use(diror zfs): zfs
- Create a new ZFS pool (yes/no)?yes
- Name of the new ZFS pool: lxd-pool
- Would you like to use an existing block device (yes/no)?no
- Sizein GB of the new loop device (1GB minimum):30
- Would you like LXD to be available over the network (yes/no)?no
- Do you want to configure the LXD bridge (yes/no)?yes
- >You will be asked about the network bridge configuration.Accept all defaults andcontinue.
- Warning:Stopping lxd.service, but it can still be activated by:
- lxd.socket
- LXD has been successfully configured.
- $ _
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.
- lxc list
List all installed containers. Obviously, this list is empty now, but it indicates that our installation is okay.
- 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.
- lxc image list ubuntu:
List the remote images that can be downloaded and started. The Ubuntu image is displayed.
- 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:
- $ lxc launch ubuntu:x c1
- Creating c1
- Starting c1
- $
We uselaunchAction, and then select the imageubuntu:x(xXenial/16.04 image ).c1Container name.
Let's take a look at the first installed container,
- $ lxc list
- +---------|---------|----------------------|------|------------|-----------+
- | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
- +---------|---------|----------------------|------|------------|-----------+
- | c1 | RUNNING |10.173.82.158(eth0)|| PERSISTENT |0|
- +---------|---------|----------------------|------|------------|-----------+
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.
- $ lxc exec c1 --uptime
- 11:47:25 up 2 min,0users,load average:0.07,0.05,0.04
- $ _
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.-.
- $ 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.
- $ lxc exec c1 bash
- root@c1:~# apt update
- Ign http://archive.ubuntu.com trusty InRelease
- Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
- Get:2 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
- ...
- Hit http://archive.ubuntu.com trusty/universe Translation-en
- Fetched11.2 MB in9s(1228 kB/s)
- Readingpackage lists...Done
- root@c1:~# apt upgrade
- Readingpackage lists...Done
- Building dependency tree
- ...
- Processing triggers forman-db (2.6.7.1-1ubuntu1)...
- Setting up dpkg (1.17.5ubuntu5.7)...
- root@c1:~# _
We use nginx for web servers. Nginx is cooler than Apache web server in some aspects.
- root@c1:~# apt install nginx
- Readingpackage lists...Done
- Building dependency tree
- ...
- Setting up nginx-core (1.4.6-1ubuntu3.5)...
- Setting up nginx (1.4.6-1ubuntu3.5)...
- Processing triggers for libc-bin (2.19-0ubuntu6.9)...
- 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.
- root@c1:~#cd/var/www/html/
- root@c1:/var/www/html#ls-l
- total 2
- -rw-r--r--1 root root 612Jun2512:15 index.nginx-debian.html
- 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.
- $ lxc list
- +---------+---------+----------------------+------+------------+-----------+
- | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
- +---------+---------+----------------------+------+------------+-----------+
- | c1 | RUNNING |10.173.82.169(eth0)|| PERSISTENT |0|
- +---------+---------+----------------------+------+------------+-----------+
- $ lxc stop c1
- $ lxc delete c1
- $ lxc list
- +---------+---------+----------------------+------+------------+-----------+
- | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
- +---------+---------+----------------------+------+------------+-----------+
- +---------+---------+----------------------+------+------------+-----------+
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