(二)OpenStack ( Train版 ) Glance镜像服务 了解与部署

一、关于 Glance 镜像服务

        Glance 服务是OpenStack项目中负责镜像管理的模块,该服务使用户能够发现、创建、检索并管理虚拟机系统镜像。它提供了一个 REST  API,使用户能够查询虚拟机系统镜像的元数据(属性)并检索镜像文件。

        在早期版本的 OpenStack 中,Glance 只有管理镜像的功能,并不具备镜像存储功能。然而,随着发展,Glance 已经扩展为集上传、检索、管理和存储等多种功能的 OpenStack 核心服务。它接受 API 对镜像的请求,以及来自最终用户或 OpenStack 计算组件的元数据设定。使用者可以把通过镜像服务提供的虚拟机镜像存储在各种位置,包括普通的文件系统、Swift 对象存储、Amazon S3等。它还可以对虚拟机实例执行创建快照命令来创建新的镜像,或者备份虚拟机的状态。Glance 支持多种镜像格式,包括raw、vhd、vhdx、vmdk、vdi、iso、qcow2、aki、ami等。

二、Glance 服务的组成

      OpenStack Glance服务包含以下组件:

  • Glance-api:主要用于虚拟机镜像的构建、删除和存储读取请求的API调用;
  • Glance-Registry:主要进行镜像的注册,存储、处理和检索有关镜像的元数据(文件属性),元数据包括大小和类型等属性。(注册表是一项内部私有服务,供 OpenStack 镜像服务。不要向用户公开此服务。)
  • 数据库:存储图像元数据,可以根据自己的喜好选择数据库。大多数部署使用MySQL或SQLite。
  • 镜像文件的存储库:支持各种存储库类型,包括普通文件系统(挂载在Glance-API 控制节点上的任何文件系统)、对象存储、RADOS块设备、VMware数据存储和HTTP。请注意 有些存储库只支持 只读。
  • 元数据定义服务:一个通用的API,供供应商、管理员、服务和用户设置自己的自定义元数据。此元数据可以用于不同类型的资源,如镜像、工件、卷、特性和集合。定义包括新属性的键、描述、约束以及可以与其关联的资源类型。

三、Glance 服务的安装和配置

重要:

        为简单起见,此次将镜像服务配置为使用后端文件系统存储,它将镜像上传并存储在控制节点的文件系统目录中。在默认情况下,此目录为 :/var/lib/glance/images/。在继续操作之前,请确保控制节点的此目录中至少具有 10GB 的可用空间。

        本节介绍如何安装和配置 Glance 镜像服务,服务部署于控制节点上。为简单起见,本文配置将镜像存储在本地文件系统上(后续搭建 Swift 对象存储服务后,会介绍如何将镜像保存在 Swift 对象存储中)。

1、安装前提

        在安装和配置映像服务之前,必须先创建数据库、服务凭据和 API 端点。

①、创建数据库

  • 使用 root 用户访问数据库
# 命令解释: mysql  -u 用户名 -p密码
[root@syl-ct ~]# mysql -u root -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.20-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
  • 创建数据库:glance
MariaDB [(none)]> CREATE DATABASE glance;
Query OK, 1 row affected (0.000 sec)
  • 设定适当的 glance 数据库访问权限
# 给glance用户授权,让其可以用任何IP远程连接glance数据库并进行相应操作 
# 123为访问密码(可以设定为自己的密码)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost'  IDENTIFIED BYY '123';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%'  IDENTIFIED BY '123';  
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> 
  • 退出数据库
MariaDB [(none)]> exit
Bye
[root@syl-ct ~]# 

②、使用凭据以获得仅用于管理员 admin 对OpenStack CLI 命令的访问权限:

# 使用 Keystone 篇中创建的 admin-openrc 凭据文件
[root@syl-ct ~]# ls
admin-openrc     initial-setup-ks.cfg  syl-openrc  模板  图片  下载  桌面
anaconda-ks.cfg  original-ks.cfg       公共        视频  文档  音乐
[root@syl-ct ~]# . admin-openrc

③、创建服务凭据:

  • 创建用户:glance
# openstack user :用户管理命令,create :创建, --domain:指定用户所属域, --password 123:设置用户密码, glance:创建的用户名
[root@syl-ct ~]# openstack user create --domain default --password 123 glance 
+---------------------+----------------------------------+
| Field               | Value                            |
+---------------------+----------------------------------+
| domain_id           | default                          |
| enabled             | True                             |
| id                  | 001806849ff847b880411a425b41acfe |
| name                | glance                           |
| options             | {}                               |
| password_expires_at | None                             |
+---------------------+----------------------------------+
  • 将角色赋予给用户和项目:
# openstack role:角色管理命令,add:添加,--project service:指定项目,-user glance:指定用户,admin:角色名
# 此条命令没有输出回显
[root@syl-ct ~]# openstack role add --project service --user glance admin

  • 创建 glance 服务:
# openstack service:服务管理命令,--name glance:服务名称,--description "OpenStack Image":服务描述,image:服务类型
[root@syl-ct ~]# openstack service create --name glance --description "OpenStack Image" image 
+-------------+----------------------------------+
| Field       | Value                            |
+-------------+----------------------------------+
| description | OpenStack Image                  |
| enabled     | True                             |
| id          | 00bf11c828034ce7bc46405a18a19098 |
| name        | glance                           |
| type        | image                            |
+-------------+----------------------------------+

④、创建镜像服务 API 端点:

注意:

        此处的URL中域名可用主机名,也可以使用IP地址

        创建出错可以使用:openstack endpoint list 命令进行查看,将多余/错误的端点使用:openstack endpoint delete ID  进行删除

[root@syl-ct ~]# openstack endpoint create --region RegionOne image public http://syl-ct:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | ebe1384db7c94424be85d656df1096a6 |
| interface    | public                           |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | 00bf11c828034ce7bc46405a18a19098 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://syl-ct:9292               |
+--------------+----------------------------------+
[root@syl-ct ~]# openstack endpoint create --region RegionOne image internal  http://syl-ct:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | fe025377a0f341fba7c017c50ed157da |
| interface    | internal                         |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | 00bf11c828034ce7bc46405a18a19098 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://syl-ct:9292               |
+--------------+----------------------------------+
[root@syl-ct ~]# openstack endpoint create --region RegionOne image admin  http://syl-ct:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | 867c5e6fcf854ecab435e18ce6a41d5d |
| interface    | admin                            |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | 00bf11c828034ce7bc46405a18a19098 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://syl-ct:9292               |
+--------------+----------------------------------+

2、安装和配置组件

注意:

        默认配置文件因发行版不同而异。您可能需要添加这些段落和选项,而不是修改现有的节和选项。此外,配置片段中的省略号表示您应该保留的潜在默认配置选项。。。。

①、安装软件包:

[root@syl-ct ~]# yum install openstack-glance -y

②、编辑配置文件:/etc/glance/glance-api.conf

  • 配置数据库访问:[database]
[database]
connection = mysql+pymysql://glance:123@syl-ct/glance
# 格式为:mysql+pymysql://用户名:密码@主机名/数据库名
# 此处的用户名和密码为配置 glance 数据库时设置的用户名和密码
  • 配置身份服务访问:[keystone_authtoken] 和 [paste_deploy]

注意:注释掉或删除 [keystone_authtoken] 部分中的任何其他选项。

[keystone_authtoken]
# ...
www_authenticate_uri = http://syl-ct:5000
auth_url = http://syl-ct:5000
memcached_servers = syl-ct:11211
auth_type = password
project_domain_name = Default
user_domain_name = Default
project_name = service
username = glance
password = 123

#此处的用户密码即为前面创建 glance 用户时所用用户名和密码
[paste_deploy]
# ...
flavor = keystone
  • 配置镜像文件的存储:[glance_store]
[glance_store]
# ...
stores = file,http
default_store = file
filesystem_store_datadir = /var/lib/glance/image/
# 此处的路径即为 glance 服务的镜像存放目录

③、填充 Glance 服务数据库:

[root@syl-ct ~]# su -s /bin/sh -c "glance-manage db_sync" glance
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
...
Database is synced successfully.
[root@syl-ct ~]# 

3、完成安装

①、启动 Glance 服务同时设置为开机自启

[root@syl-ct ~]# systemctl enable openstack-glance-api.service
Created symlink from /etc/systemd/system/multi-user.target.wants/openstack-glance-api.service to /usr/lib/systemd/system/openstack-glance-api.service.
[root@syl-ct ~]# systemctl start openstack-glance-api.service

②、验证 Glance 服务

  • 查看9292端口情况,如果存在,则表示 Glance 服务启动成功:
# 此处的(# ...) :指其他不相关的端口信息
[root@syl-ct ~]# netstat -tnlup
# ...
tcp        0      0 0.0.0.0:9292            0.0.0.0:*               LISTEN      910/python2     
# ...
  • 下载 cirros 镜像(cirros是一个很小的Linux系统),验证 Glance 服务的功能是否正常

cirros 镜像地址: https://download.cirros-cloud.net/0.6.2/cirros-0.6.2-x86_64-disk.img

# 安装 wget 下载软件,如已安装则可跳过此步
[root@syl-ct ~]# yum install wget -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.bfsu.edu.cn
 * centos-ceph-nautilus: mirrors.bfsu.edu.cn
 * centos-nfs-ganesha28: mirrors.bfsu.edu.cn
 * centos-openstack-train: mirrors.bfsu.edu.cn
 * centos-qemu-ev: mirrors.bfsu.edu.cn
 * extras: mirrors.bfsu.edu.cn
 * updates: mirrors.bfsu.edu.cn
软件包 wget-1.14-18.el7_6.1.x86_64 已安装并且是最新版本
无须任何处理

# 下载 cirros 镜像 (也可以使用其他工具下载,然后上传到控制节点的root目录下)
[root@syl-ct ~]# wget https://download.cirros-cloud.net/0.6.2/cirros-0.6.2-x86_64-disk.img
--2024-03-25 04:46:46--  https://download.cirros-cloud.net/0.6.2/cirros-0.6.2-x86_64-disk.img
正在解析主机 download.cirros-cloud.net (download.cirros-cloud.net)... 69.163.176.183, 2607:f298:6:a014::c3e:9bd6
...
正在保存至: “cirros-0.6.2-x86_64-disk.img”
100%[==============================================================================>] 21,430,272  21.2KB/s 用时 13m 47s==
2024-03-25 05:00:36 (25.3 KB/s) - 已保存 “cirros-0.6.2-x86_64-disk.img” [21430272/21430272])

[root@syl-ct ~]# ls
admin-openrc     cirros-0.6.2-x86_64-disk.img  syl-openrc 

# 使用管理员凭据,上传 cirros 镜像到 Glance 服务中
[root@syl-ct ~]# . admin-openrc 
[root@syl-ct ~]# openstack image create --file /root/cirros-0.6.2-x86_64-disk.img --disk-format qcow2 --container-format bare --public cirros
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field            | Value                                                                                                                                                                                      |
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| checksum         | c8fc807773e5354afe61636071771906                                                                                                                                                           |
| container_format | bare                                                                                                                                                                                       |
| created_at       | 2024-03-24T21:07:18Z                                                                                                                                                                       |
| disk_format      | qcow2                                                                                                                                                                                      |
| file             | /v2/images/9ed262c8-46d9-4440-b183-20a500254e33/file                                                                                                                                       |
| id               | 9ed262c8-46d9-4440-b183-20a500254e33                                                                                                                                                       |
| min_disk         | 0                                                                                                                                                                                          |
| min_ram          | 0                                                                                                                                                                                          |
| name             | cirros                                                                                                                                                                                     |
| owner            | d9132d41e61b49138d7528631164fe80                                                                                                                                                           |
| properties       | os_hash_algo='sha512', os_hash_value='1103b92ce8ad966e41235a4de260deb791ff571670c0342666c8582fbb9caefe6af07ebb11d34f44f8414b609b29c1bdf1d72ffa6faa39c88e8721d09847952b', os_hidden='False' |
| protected        | False                                                                                                                                                                                      |
| schema           | /v2/schemas/image                                                                                                                                                                          |
| size             | 21430272                                                                                                                                                                                   |
| status           | active                                                                                                                                                                                     |
| tags             |                                                                                                                                                                                            |
| updated_at       | 2024-03-24T21:07:19Z                                                                                                                                                                       |
| virtual_size     | None                                                                                                                                                                                       |
| visibility       | public                                                                                                                                                                                     |
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

  • 查看Glance中的镜像
# 使用哪个用户的凭据,则查看到该用户所属的镜像
# 此处上传与查看都是使用 admin 用户的凭据

[root@syl-ct ~]# openstack image list
+--------------------------------------+--------+--------+
| ID                                   | Name   | Status |
+--------------------------------------+--------+--------+
| 9ed262c8-46d9-4440-b183-20a500254e33 | cirros | active |
+--------------------------------------+--------+--------+
  • 查看镜像的物理文件

注意:

        此处物理文件所在位置为 三、2、② 的 “配置镜像文件的存储“ 的中所设置的路径,本文路径为:/var/lib/glance/image/

[root@syl-ct ~]# ll /var/lib/glance/image/
总用量 20928
-rw-r-----. 1 glance glance 21430272 3月  25 05:07 9ed262c8-46d9-4440-b183-20a500254e33

四、下一篇:Placement 放置服务的部署

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值