LXD 虚拟化平台(2):镜像管理

一、前言

LXD 虚拟化平台(1):简介 初步介绍了 LXD 虚拟化平台的基本概念与初始化安装,接下来的会详细讲解 LXD 平台的各项功能。

本文聚焦在 LXD 镜像管理管理功能上。

二、镜像管理

和容器镜像类似,LXD 也有镜像管理的功能。但 LXD 本身没有镜像仓库的机制,每一个 LXD Server 本身就提供镜像仓库的功能,可以通过访问 LXD 服务端的 HTTPS 端口(上一章提到的 lxd init 时是否允许 LXD 通过 HTTPS 提供 API 接口)来查询和上传、下载镜像。

image.png

2.1 镜像源

在 LXD 平台中,镜像源就是远端服务器,管理远端服务器的命令是 remote 指令,remote 指令的说明如下:

参数 使用说明 案例说明
add 添加远端的 LXD 服务器(如果 LXD 服务器设置验证,需要提供验证信息才允许访问) lxc remote add remote_server https://192.168.1.1:8443
get-default 当前默认使用的远端服务器,默认是 local(本地) lxc remote get-default
list 查询当前远端服务器列表 lxc remote list
remove 移除远端服务器 lxc remote remove remote_server
rename 修改远端服务器的名称 lxc remote rename remote_server remote_server_new_name
set-url 修改远端服务器的地址 lxc remote set-url remote_server https://192.168.1.1:8443
switch 切换默认的远端服务器 lxc remote switch remote_server

LXD 初始化完毕后,已经内置好了一些官方镜像源,能覆盖大部分的 Linux 发行版本,内置的远端服务器列表信息如下:

远端名称 远端说明
local 本地服务器,代指 LXD Server 本身,也是默认的 LXD remote
images LXD 社区镜像源,由 LXD 团队维护,可以下载大部分的开源 Linux 发行版镜像
ubuntu 和 ubuntu-daily Ubuntu 官方镜像源

因此,当需要创建虚拟机时,一般先查询一下远端服务器可用的镜像信息,例如查询远端服务器 images 的可用镜像列表:

root@easyops:~# lxc image list images:
+------------------------------------------+--------------+--------+----------------------------------------------+--------------+-----------------+-----------+-------------------------------+
|                  ALIAS                   | FINGERPRINT  | PUBLIC |                 DESCRIPTION                  | ARCHITECTURE |      TYPE       |   SIZE    |          UPLOAD DATE          |
+------------------------------------------+--------------+--------+----------------------------------------------+--------------+-----------------+-----------+-------------------------------+
| almalinux/8 (3 more)                     | 27376a416044 | yes    | Almalinux 8 amd64 (20230611_02:25)           | x86_64       | CONTAINER       | 128.47MB  | Jun 11, 2023 at 12:00am (UTC) |
+------------------------------------------+--------------+--------+----------------------------------------------+--------------+-----------------+-----------+-------------------------------+
| almalinux/8 (3 more)                     | bba99c11b237 | yes    | Almalinux 8 amd64 (20230611_02:25)           | x86_64       | VIRTUAL-MACHINE | 722.59MB  | Jun 11, 2023 at 12:00am (UTC) |
+------------------------------------------+--------------+--------+----------------------------------------------+--------------+-----------------+-----------+-------------------------------+
| almalinux/8/arm64 (1 more)               | 66161cf07508 | yes    | Almalinux 8 arm64 (20230611_01:14)           | aarch64      | CONTAINER       | 124.99MB  | Jun 11, 2023 at 12:00am (UTC) |
+------------------------------------------+--------------+--------+----------------------------------------------+--------------+-----------------+-----------+-------------------------------+
| ......
+------------------------------------------+--------------+--------+----------------------------------------------+--------------+-----------------+-----------+-------------------------------+
社区维护的镜像列表,可查询:https://images.linuxcontainers.org/

创建虚拟机的指令如下:

# lxc launch [远端地址]:[远端镜像] [虚拟机名称] [其他参数]
lxc launch images:almalinux/8 my-test-container -c limits.cpu=1 -c limits.memory=64MiB

该指令执行后,LXD Daemon 会向远端服务器发送镜像下载请求,下载到本地后解压该镜像并且创建虚拟机。

launch 指令将会在后续的实例管理章节中详细讲解,在此不展开。

2.2 制作与发布镜像

在企业生产使用的场景下,需要对当前企业使用的基础镜像制作打包和发布使用,LXD 制作镜像的基本流程如下:

image.png

  1. (可选)使用 distrobuilder 来创建镜像;
  1. 使用镜像创建虚拟机实例,或者对已有的虚拟机创建快照;
  1. 使用虚拟机或者虚拟机快照发布镜像。

2.2.1 (可选)使用 distrobuilder 制作镜像

首先,需要初始化一个 LXD 环境来制作镜像,初始化 LXD 环境已在以往的章节中提及,这里不展开描述(LXD 虚拟化平台(1):简介)。

然后下载 distrobuilder 工具包,地址是 https://linuxcontainers.org/distrobuilder/downloads/,也可以执行以下的指令安装:

go get -v -x github.com/lxc/distrobuilder/distrobuilder

接下来,本文以 CentOS-8 镜像制作过程作为基本的示例。

a). 设置 rpm 命令操作的根目录

mkdir /tmp/centos8/my_rootfs 
rpm --root /tmp/centos8/my_rootfs/ --initdb

b). 下载并解压 CentOS-8 的 release 包

wget http://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/x86_64/os/Packages/centos-linux-release-8.5-1.2111.el8.noarch.rpm

rpm -ivh --nodeps --root /tmp/centos8/my_rootfs/ --package ./centos-linux-release-8.5-1.2111.el8.noarch.rpm

c). 配置镜像的 YUM 源

mkdir /tmp/centos8/my_rootfs/etc/yum.repos.d

wget -O /tmp/centos8/my_rootfs/etc/yum.repos.d/Centos-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

d). 通过YUM安装必需的软件包

# 安装yum以及其他软件包
yum --installroot=/tmp/centos8/my_rootfs/ \
install systemd
yum --installroot=/tmp/centos8/my_rootfs/ \
install `cat rpm.list|xargs echo`

# 注意:这里的 rpm.list 是从一个 lxd centos8 stream 官方镜像生成的系统中获取的,需要执行 rpm -qa 转为需要的包名:
cat centos8-stream.rpm.list |sed -r 's/(.*)-(.*)-(.*).(x86_64|noarch)/\1/' > rpm.list

e). 执行镜像文件的打包操作

tar -cvzf rootfs.tar.gz -C /tmp/centos8/my_rootfs . 

f). 制作 metadata.tar.gz
编辑文件 metadata.yaml,添加以下的内容:

architecture: x86_64
creation_date: 1646190387 # `date +%s`
properties:
  architecture: x86_64
  description: Centos 8.5 x86_64
  os: centos
  release: 8
  name: centos8_base
  variant: default
templates:
  /etc/hosts:
    when:
    - create
    - copy
    create_only: false
    template: hosts.tpl
    properties: {}
  /etc/sysconfig/network:
    when:
    - create
    - copy
    create_only: false
    template: network.lxd.tpl
    properties: {}
  /etc/sysconfig/network-scripts/ifcfg-eth0:
    when:
    - create
    - copy
    create_only: false
    template: ifcfg-eth0.lxd.tpl
    properties: {}

编辑文件 templates/hosts.tpl,添加以下内容:

127.0.1.1       {{ container.name }}
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

编辑文件 templates/ifcfg-eth0.lxd.tpl,添加以下内容:

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HOSTNAME={{ container.name }}
NM_CONTROLLED=no
TYPE=Ethernet
DHCP_HOSTNAME={{ container.name }}

编辑文件 templates/network.lxd.tpl,添加以下内容:

NETWORKING=yes
HOSTNAME={{ container.name }}

最后把文件都打包为 metadata.tar.gz:

tar -cvzf metadata.tar.gz metadata.yaml templates

g). 导入镜像文件和 metadata 文件

lxc image import metadata.tar.gz rootfs.tar.gz --alias centos8

# 查看镜像确认导入结果
lxc image ls

h). 创建实例验证镜像的可用性

# 使用镜像创建虚拟机实例
lxc launch centos8 test-1

# 进入虚拟机配置网络,验证网络的连通性
lxc exec test-1 -- /bin/bash

# 配置网卡文件 /etc/sysconfig/network-scripts/ifcfg-eth0,添加以下的内容
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
HOSTNAME=c1
NM_CONTROLLED=no
TYPE=Ethernet
IPADDR=172.30.0.25
GATEWAY=172.30.0.1
NETMASK=255.255.255.0
DNS1=114.114.114.114

# 重启网络服务
/etc/init.d/network restart

# 查看eth0网卡IP是否显示正常
ifconfig

2.2.2 使用 lxc publish 发布镜像

使用 distrobuilder 制作镜像后,可以通过 lxc publish 来发布镜像。当然,如果觉得使用 distrobuilder 制作镜像比较麻烦,也可以跳过该步骤,使用 lxc publish 把当前实例或者实例的快照制作为镜像并且进行发布。

publish 指令的基本方法如下:

Description:
  Publish instances as images

Usage:
  lxc publish [:][/] [:] [flags] [key=value...]


Flags:
      --alias              New alias to define at target
      --compression none   Compression algorithm to use (none for uncompressed)
      --expire             Image expiration date (format: rfc3339)
  -f, --force              Stop the instance if currently running
      --public             Make the image public

提供以下的例子作为使用的参考:

# 案例1:把本地虚拟机 test-container 制作名为 test-image 的本地镜像
lxc publish local:test-container local:test-image

# 案例2:把本地虚拟机 test-container 制作名为 test-image 的镜像,并且上传到远端服务器 test-remote-server
lxc publish local:test-container test-remote-server:test-image

# 案例3:把本地虚拟机 test-container 的快照 test-01 制作名为 test-image 的本地镜像
lxc publish local:test-container/test-01 local:test-image

比较常用的做法就是通过 images、ubuntu 等官方社区源,创建好虚拟机,然后启动虚拟机进行企业的本地化裁剪后,通过 lxc publish 发布为内部镜像进行推广使用。

你可能感兴趣的:(LXD 虚拟化平台(2):镜像管理)