[ad_1]
让我们学习在 LXC 上安装 Install Ubuntu。 容器 是我们可以使用的最有用和最有用的工具之一。 在 Linux 上,我们有 LXC 容器,它们非常易于设置和使用。 它们允许我们在不需要管理程序的情况下在单个主机上使用多个操作系统。 在本模块中,我们将学习如何使用一些非常简单的步骤在 LXC 容器中设置 Ubuntu Server 映像!
在 LXC 上安装 Ubuntu 的步骤
让我们直接进入在 LXC 上获取 Ubuntu 映像的步骤。
1. 先决条件
在我们开始之前,使用 apt 命令更新和升级您的系统:
$ sudo apt update && sudo apt upgrade -y
然后使用以下命令安装所需的软件包:
$ sudo apt install lxc lxd zfsutils-linux
但是,在某些发行版的情况下,您可能需要从快照存储安装 LXD 包:
$ sudo snap install lxd
现在我们有了所有必需的依赖项,我们可以继续我们的设置!
2. 将用户添加到 LXC 组
要允许您的用户在本地访问 LXD 守护程序,它必须是 lxd
团体。 您可以通过键入以下内容来检查您当前的用户是否在上述组中:
$ id uid=1000(mint) gid=1000(mint) groups=1000(mint),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),130(sambashare)
您可以将所需的用户添加到 lxd
使用 adduser 命令分组:
$ sudo adduser <USER> lxd
要使更改生效,您可能需要重新启动计算机。 最后,我们可以验证我们的用户是否已添加到组中:
$ id uid=1000(mint) gid=1000(mint) groups=1000(mint),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),130(sambashare),999(lxd)
现在我们可以在本地访问 lxd 守护进程,我们可以继续设置我们的容器!
3. 设置 LXD
要设置 LXD 守护进程,我们需要输入:
$ lxd init
这应该会导致一系列用于配置守护程序的问题。 现在,只需按 Enter 键并使用所有默认值即可。
$ lxd init Would you like to use LXD clustering? (yes/no) [default=no]: Do you want to configure a new storage pool? (yes/no) [default=yes]: Name of the new storage pool [default=default]: Name of the storage backend to use (btrfs, dir, lvm, zfs) [default=zfs]: Create a new ZFS pool? (yes/no) [default=yes]: Would you like to use an existing block device? (yes/no) [default=no]: Size in GB of the new loop device (1GB minimum) [default=15GB]: Would you like to connect to a MAAS server? (yes/no) [default=no]: Would you like to create a new local network bridge? (yes/no) [default=yes]: What should the new bridge be called? [default=lxdbr0]: What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: Would you like LXD to be available over the network? (yes/no) [default=no]: Would you like stale cached images to be updated automatically? (yes/no) [default=yes] Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:
现在我们可以继续在我们的容器中安装 Ubuntu 服务器镜像。
4. 在 LXC(Linux 容器)上安装 Ubuntu
接下来,我们需要列出我们可以使用的所有可用的 Ubuntu 镜像:
$ lxc image list images:ubuntu +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/14.04 (7 more) | 206eb037c726 | yes | Ubuntu trusty amd64 (20210426_07:42) | x86_64 | 75.58MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/14.04/arm64 (3 more) | 8cfec06e99b9 | yes | Ubuntu trusty arm64 (20210426_08:17) | aarch64 | 69.67MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/14.04/armhf (3 more) | 070778ecdb5c | yes | Ubuntu trusty armhf (20210426_08:13) | armv7l | 70.33MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/14.04/i386 (3 more) | 713d5412e372 | yes | Ubuntu trusty i386 (20210426_07:42) | i686 | 73.77MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/14.04/ppc64el (3 more) | 1ca3bb8e252c | yes | Ubuntu trusty ppc64el (20210426_07:42) | ppc64le | 74.40MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04 (7 more) | 98ab2293381b | yes | Ubuntu xenial amd64 (20210426_07:42) | x86_64 | 83.31MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/arm64 (3 more) | f0929ebc6a36 | yes | Ubuntu xenial arm64 (20210426_08:31) | aarch64 | 75.57MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/armhf (3 more) | 8fad9647d7fa | yes | Ubuntu xenial armhf (20210426_08:03) | armv7l | 76.40MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/cloud (3 more) | 305d675ede12 | yes | Ubuntu xenial amd64 (20210426_07:42) | x86_64 | 102.54MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/cloud/arm64 (1 more) | 7dfb2383b7df | yes | Ubuntu xenial arm64 (20210426_08:03) | aarch64 | 94.06MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/cloud/armhf (1 more) | 5901500f1774 | yes | Ubuntu xenial armhf (20210426_07:59) | armv7l | 94.84MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/cloud/i386 (1 more) | f82b6db381b3 | yes | Ubuntu xenial i386 (20210426_07:42) | i686 | 102.69MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/cloud/ppc64el (1 more) | cb3e1afc0ba5 | yes | Ubuntu xenial ppc64el (20210426_07:59) | ppc64le | 100.74MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/cloud/s390x (1 more) | 85e0a8b3f5da | yes | Ubuntu xenial s390x (20210426_07:50) | s390x | 77.78MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/i386 (3 more) | e96f698aa6cd | yes | Ubuntu xenial i386 (20210426_07:42) | i686 | 83.12MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/ppc64el (3 more) | 62c2b64162de | yes | Ubuntu xenial ppc64el (20210426_07:59) | ppc64le | 81.54MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/16.04/s390x (3 more) | 9f47976f2914 | yes | Ubuntu xenial s390x (20210426_07:42) | s390x | 59.53MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04 (7 more) | 8b4eeb802ff5 | yes | Ubuntu bionic amd64 (20210426_07:42) | x86_64 | 98.57MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/arm64 (3 more) | 2705ccf08122 | yes | Ubuntu bionic arm64 (20210426_07:42) | aarch64 | 89.77MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/armhf (3 more) | cf8e11bdb0cc | yes | Ubuntu bionic armhf (20210426_08:25) | armv7l | 89.74MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/cloud (3 more) | bcc0425bfe04 | yes | Ubuntu bionic amd64 (20210426_07:42) | x86_64 | 109.45MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/cloud/arm64 (1 more) | c150378cd2f0 | yes | Ubuntu bionic arm64 (20210426_08:32) | aarch64 | 100.08MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/cloud/armhf (1 more) | 9931b6542e8d | yes | Ubuntu bionic armhf (20210426_08:31) | armv7l | 99.86MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/cloud/i386 (1 more) | 5c73a72e09a9 | yes | Ubuntu bionic i386 (20210426_07:42) | i686 | 108.58MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/cloud/ppc64el (1 more) | 4fdb34bb3397 | yes | Ubuntu bionic ppc64el (20210426_07:42) | ppc64le | 110.07MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/cloud/s390x (1 more) | 7d126a22814c | yes | Ubuntu bionic s390x (20210426_07:50) | s390x | 81.22MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/i386 (3 more) | d51c526ad6c2 | yes | Ubuntu bionic i386 (20210426_07:42) | i686 | 97.75MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/ppc64el (3 more) | 9879ee7c311b | yes | Ubuntu bionic ppc64el (20210426_07:42) | ppc64le | 98.71MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/18.04/s390x (3 more) | da9ff3db9362 | yes | Ubuntu bionic s390x (20210426_07:42) | s390x | 71.03MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10 (7 more) | 1839820c186a | yes | Ubuntu groovy amd64 (20210426_07:42) | x86_64 | 103.77MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/arm64 (3 more) | 16fb45a4596d | yes | Ubuntu groovy arm64 (20210426_08:15) | aarch64 | 99.16MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/armhf (3 more) | 3fe35efa8a56 | yes | Ubuntu groovy armhf (20210426_08:16) | armv7l | 96.61MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/cloud (3 more) | 42deda3e1f71 | yes | Ubuntu groovy amd64 (20210426_07:42) | x86_64 | 120.26MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/cloud/arm64 (1 more) | e69a15d157d9 | yes | Ubuntu groovy arm64 (20210426_08:31) | aarch64 | 115.12MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/cloud/armhf (1 more) | 53a84b64afc5 | yes | Ubuntu groovy armhf (20210426_08:17) | armv7l | 112.31MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/cloud/ppc64el (1 more) | fe884af581b2 | yes | Ubuntu groovy ppc64el (20210426_07:42) | ppc64le | 125.11MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/cloud/s390x (1 more) | efb0ebb7e4a1 | yes | Ubuntu groovy s390x (20210426_07:42) | s390x | 98.27MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/ppc64el (3 more) | 159366ee16d2 | yes | Ubuntu groovy ppc64el (20210426_07:42) | ppc64le | 107.61MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/20.10/s390x (3 more) | c305f9be891f | yes | Ubuntu groovy s390x (20210426_07:42) | s390x | 82.56MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04 (7 more) | 2aebf4148b54 | yes | Ubuntu hirsute amd64 (20210426_07:42) | x86_64 | 104.73MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/arm64 (3 more) | 7f63017f49d9 | yes | Ubuntu hirsute arm64 (20210426_07:42) | aarch64 | 100.38MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/armhf (3 more) | 8330c0dabd40 | yes | Ubuntu hirsute armhf (20210426_07:42) | armv7l | 98.07MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/cloud (3 more) | 77fcbd9cbe96 | yes | Ubuntu hirsute amd64 (20210426_07:42) | x86_64 | 121.15MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/cloud/arm64 (1 more) | 6c0a8af1d2a1 | yes | Ubuntu hirsute arm64 (20210426_07:42) | aarch64 | 116.20MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/cloud/armhf (1 more) | 502474d0068d | yes | Ubuntu hirsute armhf (20210426_07:42) | armv7l | 113.34MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/cloud/ppc64el (1 more) | ed2437bcda33 | yes | Ubuntu hirsute ppc64el (20210426_07:42) | ppc64le | 126.67MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/cloud/s390x (1 more) | 875f23f2b967 | yes | Ubuntu hirsute s390x (20210426_07:42) | s390x | 101.16MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/ppc64el (3 more) | c10a5e4bfe1a | yes | Ubuntu hirsute ppc64el (20210426_07:42) | ppc64le | 109.09MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/21.04/s390x (3 more) | 0ad7c6db31c1 | yes | Ubuntu hirsute s390x (20210426_07:42) | s390x | 84.82MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal (7 more) | 5c5eda29629e | yes | Ubuntu focal amd64 (20210426_07:42) | x86_64 | 100.98MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/arm64 (3 more) | 2b2c0998cadd | yes | Ubuntu focal arm64 (20210426_07:42) | aarch64 | 95.98MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/armhf (3 more) | a8c4a14b77cd | yes | Ubuntu focal armhf (20210426_07:42) | armv7l | 93.48MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/cloud (3 more) | d79f9ba499ea | yes | Ubuntu focal amd64 (20210426_07:42) | x86_64 | 116.07MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/cloud/arm64 (1 more) | 7a404ae6363c | yes | Ubuntu focal arm64 (20210426_08:31) | aarch64 | 110.62MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/cloud/armhf (1 more) | 677570e0f311 | yes | Ubuntu focal armhf (20210426_08:02) | armv7l | 107.86MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/cloud/ppc64el (1 more) | a4bde8caa245 | yes | Ubuntu focal ppc64el (20210426_07:57) | ppc64le | 120.54MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/cloud/s390x (1 more) | f914fb7160ed | yes | Ubuntu focal s390x (20210426_07:42) | s390x | 92.95MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/ppc64el (3 more) | f31de423d1dd | yes | Ubuntu focal ppc64el (20210426_07:42) | ppc64le | 104.61MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+ | ubuntu/focal/s390x (3 more) | 8a8131af401d | yes | Ubuntu focal s390x (20210426_07:42) | s390x | 78.75MB | Apr 26, 2021 at 12:00am (UTC) | +-------------------------------------+--------------+--------+-----------------------------------------+---------+----------+-------------------------------+
这为我们提供了大量可供选择的图像列表。 然而,我们要安装的是 ubuntu/20.10/cloud
我们可以用:
$ lxc launch images:ubuntu/20.10/cloud ucloud
在这里,ucloud 是我们创建的容器的名称。 下载图像后,您可以使用以下命令列出它:
$ lxc ls +--------+---------+---------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +--------+---------+---------------------+-----------------------------------------------+------------+-----------+ | ucloud | RUNNING | 10.120.27.98 (eth0) | fd42:7f00:50c2:7015:216:3eff:fee8:1392 (eth0) | PERSISTENT | 0 | +--------+---------+---------------------+-----------------------------------------------+------------+-----------+
这样我们就有了一个正在运行的 Ubuntu 云容器!
5. 在我们的容器中执行命令
现在我们有一个正在运行的容器,我们可以配置它并在其上运行命令! 以 root 身份运行命令的语法是:
$ lxc exec [CONTAINER NAME] -- [CMD]
例如,我们现在可以使用以下命令更新我们的 Ubuntu 云映像:
$ lxc exec ucloud -- apt update
您可以使用以下命令将根 shell 放入容器中:
$ lxc exec ucloud -- /bin/bash
结论
容器在现代场景中非常有用。 您可以使用它们来托管私人或公共服务器或进行测试。 无论哪种方式,它们对许多不同的东西都非常有用,并且在某些方面比虚拟机更有效。 您可以阅读有关 LXC 容器的更多信息 这里 !
[ad_2]