目录

  • Glance 镜像服务
  • 1.镜像
  • 2.镜像服务的功能
  • 3.镜像的 API 版本
  • 4.镜像格式
  • 4.1虚拟机镜像文件磁盘格式
  • 4.2 镜像文件容器格式
  • 5.镜像状态
  • 5.1 镜像从上传到可识别的几个状态:
  • 5.2 镜像在上载完成后的状态
  • 6.镜像访问权限
  • 7.工作流程
  • 一、创建数据库实例和数据库用户
  • 二、创建用户、修改配置文件
  • 1.创建glance用户
  • 2.创建镜像服务API端点
  • 3. 安装 openstack-glance 软件包
  • 3.1 添加glance-api.conf配置
  • 3.2 修改glance-registry.conf 配置文件
  • 4.初始化数据库
  • 5.开启glance服务
  • 6.赋予openstack-glance-api.service服务对存储设备的可写权限
  • 7.镜像导入
  • 8.查看镜像


Glance 镜像服务

1.镜像

镜像通常指的是一系列文件或一个磁盘驱动的精确副本,将特定的一系列文件按照一定的格式制作成独立的文件,以方便用户的下载和使用。简单来说就是一系列资源/服务的集合,也可以作为模板创建多个同样的独立的副本

2.镜像服务的功能

镜像服务主要是用来灌流镜像,让用户能够发现、获取和保存镜像,主要功能如下:

  • 查询和获取镜像的元数据和镜像本身(元数据:镜像的概要信息和描述信息)
  • 注册和上传虚拟机镜像,包括镜像的创建、.上传、 下载和管理
  • 维护镜像信息,包括元数据和镜像本身。
  • 支持多种方式存储镜像,包括普通的文件系统、Swift、 Amazon S3等
  • 对虚拟机实例执行创建快照命令来创建新的镜像,或者备份虚拟机的状态

3.镜像的 API 版本

Glance提供的RESTful API有两个版本:V1,V2:

  • v1只提供基本的镜像和成员操作功能,包括镜像创建、删除、下载、列表、详细信息查询、 更新,以及镜像租户成员的创建、删除和列表。
  • v2除了支持v1的所有功能外,主要增加了镜像位置的添加、删除、修改,元数据和名称空间操作,以及镜像标记操作。

4.镜像格式

4.1虚拟机镜像文件磁盘格式

磁盘格式

用途

raw

无结构的磁盘格式

vhd

该格式通用于VMware、Xen、 VirtualBox以及 其他虚拟机管理程序

vhdx

vhd格式的增强版本,支持更大的磁盘尺寸

vmdk

一种比较通 用的虚拟机磁盘格式

vdi

由VirtualBox虛拟机监控程序和QEMU仿真器支持的磁盘格式

iso

用于光盘(CD- ROM)数据内容的档案格式

ploop

由Virtuozzo支持,用于运行OS容器的磁盘格式

qcow2

由QEMU仿真支持,可动态扩展,支持写时复制(Copy on Write)的磁盘格式

aki

在Glance中存储的Amazon内核格式

ar

在Glance中存储的Amazon虚拟内存盘(Ramdisk)格式

ami

在Glance中存储的Amazon机器格式

4.2 镜像文件容器格式

bare

没有容器或元数据 “信封” 的镜像,原始的资源集合,所以不存在兼容性问题,不确定选择哪种容器模式时,就在指定为bare最安全。

Docker

在glance中存储的容器文件系统的dockerd的tar档案。能够隔离磁盘存储的数据、元数据。

ovf

开放虚拟化格式

ova

在Glance中存储的开放虚拟化设备格式

aki

在Glance中存储的Amazon内核格式

ari

在Glance中存储的Amazon虚拟内存盘(Ramdisk) 格式

5.镜像状态

5.1 镜像从上传到可识别的几个状态:

queued

这是一种初始化状态, 镜像文件刚被创建,在Glance数据库只有其元数据,镜像数据还没有上传至数据库中

saving

是镜像的原始数据在上传到数据库中的一种过渡状态,表示正在上传镜像

uploading

指已进行导入数据提交调用,可以给服务识别和调用的状态

importing

指已经完成导入调用,服务已经识别,可调用,但是镜像还未准备好给虚拟机提供服务

5.2 镜像在上载完成后的状态

active

表示当镜像数据成功上传,可使用

deactivated

只对管理员开放权限,任何非管理员用户都无权访问镜像数据,禁止下载镜像,也禁止镜像导出和镜像克隆之类的操作

klled

表示镜像上传过程中发生错误,镜像不可读

deleted

镜像将在不久后被自动删除,该镜像不可再用,但是目前Glance仍然保留该镜像的相关信息和原始数据,删除后可恢复

pending_ delete

与deleted相似, Glance还没有清除镜像数据,但处于该状态的镜像不可恢复

6.镜像访问权限

public公共的

可以被所有的项目使用

private私有的

只有被镜像所有者所在的项目使用

shared共享的

一个非共有的镜像,可以共享给其他项目,通过项目成员(member-*)操作来实现的

protected(受保护的)

这种镜像不能被删除

7.工作流程

  • 首先是对客户端的安全认证流程:openstack的操作都需要经过keystone进行身份认证,并授权,glance也不例外,授权成功再去请求glance服务,glance服务接收到外部请求后,会去keystone进行认证,此请求是否已授权,认证通过后,才会将请求传到后端处理。
  • glance domain controller 是API和后端功能模块的中间件,相当于调度器,作用是将外部服务分发到下面的各个功能层去处理。在调度时,遵循调度算法,首先有一个预选,排除不符合要求的节点,再进行优选,通过打分机制,对都能够处理此功能的节点进行打分,考虑它们当前的负荷,处理能力和速度,选出最优的一个。对于一些有污点的节点,调度器是直接跳过他们的,如果其余可用节点负担都太大,无法处理外部请求,会有一个容忍机制,由运维人员控制,让调度器接受污点,对污点再进行优选。

一、创建数据库实例和数据库用户

[root@ct ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
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)]> CREATE DATABASE glance;   //创建数据库
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' IDENTIFIED BY 'GLANCE_DBPASS';               //用户提权,glance库的所有权限
Query OK, 0 rows affected (0.001 sec)

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

MariaDB [(none)]> flush privileges;          //刷新权限表
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> show grants for glance;        查看glance用户的权限
+-------------------------------------------------------------------------------------------------------+
| Grants for glance@%                                                                                   |
+-------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'glance'@'%' IDENTIFIED BY PASSWORD '*C0CE56F2C0C7234791F36D89700B02691C1CAB8E' |
| GRANT ALL PRIVILEGES ON `glance`.* TO 'glance'@'%'                                                    |
+-------------------------------------------------------------------------------------------------------+
2 rows in set (0.000 sec)

二、创建用户、修改配置文件

1.创建glance用户

[root@ct ~]#  openstack user create --domain default --password GLANCE_PASS glance   //创建glance用户
+---------------------+----------------------------------+
| Field               | Value                            |
+---------------------+----------------------------------+
| domain_id           | default                          |
| enabled             | True                             |
| id                  | a9d9bf07994441e9be0e9e93af2a6186 |
| name                | glance                           |
| options             | {}                               |
| password_expires_at | None                             |
+---------------------+----------------------------------+
[root@ct ~]# openstack role add --project service --user glance admin    //将glance用户添加到service项目中,并且针对这个项目拥有admin权限
[root@ct ~]# openstack service create --name glance --description "OpenStack Image" image  //创建一个service服务,service名称为glance,类型为image
+-------------+----------------------------------+
| Field       | Value                            |
+-------------+----------------------------------+
| description | OpenStack Image                  |
| enabled     | True                             |
| id          | d92ce1778a7840f394fd18a8711c0f39 |
| name        | glance                           |
| type        | image                            |
+-------------+----------------------------------+
[root@ct ~]# openstack service list           //查看项目
+----------------------------------+----------+----------+
| ID                               | Name     | Type     |
+----------------------------------+----------+----------+
| 7681affb926e43848166b7a4092857f0 | keystone | identity |
| d92ce1778a7840f394fd18a8711c0f39 | glance   | image    |
+----------------------------------+----------+----------+
[root@ct ~]#

2.创建镜像服务API端点

[root@ct ~]# openstack endpoint create --region RegionOne image public http://ct:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | 855e2d86542f4069943527e5c0bb1966 |
| interface    | public                           |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | d92ce1778a7840f394fd18a8711c0f39 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://ct:9292                   |
+--------------+----------------------------------+
[root@ct ~]# openstack endpoint create --region RegionOne image internal http://ct:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | 20850176456e414685f3351d545ada37 |
| interface    | internal                         |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | d92ce1778a7840f394fd18a8711c0f39 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://ct:9292                   |
+--------------+----------------------------------+
[root@ct ~]# openstack endpoint create --region RegionOne image admin http://ct:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | 620b8b7a695646b0803121873fbbb85b |
| interface    | admin                            |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | d92ce1778a7840f394fd18a8711c0f39 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://ct:9292                   |
+--------------+----------------------------------+
[root@ct ~]#

3. 安装 openstack-glance 软件包

[root@ct ~]# yum -y install openstack-glance
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * centos-ceph-nautilus: mirrors.cn99.com
 * centos-nfs-ganesha28: mirrors.aliyun.com
 * centos-openstack-train: mirrors.huaweicloud.com
 * centos-qemu-ev: mirrors.aliyun.com
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com
软件包 1:openstack-glance-19.0.4-1.el7.noarch 已安装并且是最新版本
无须任何处理
[root@ct ~]#

3.1 添加glance-api.conf配置

[root@ct ~]# cp -a /etc/glance/glance-api.conf{,.bak}   //备份
[root@ct ~]# grep -Ev '^$|#' /etc/glance/glance-api.conf.bak > /etc/glance/glance-api.conf       //过滤注释信息
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf database connection mysql+pymysql://glance:GLANCE_DBPASS@ct/glance
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_url http://ct:5000
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf keystone_authtoken memcached_servers ct:11211
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_type password
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf keystone_authtoken project_domain_name Default
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf keystone_authtoken user_domain_name Default
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf keystone_authtoken project_name service
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf keystone_authtoken username glance
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf keystone_authtoken password GLANCE_PASS
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf paste_deploy flavor keystone
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf glance_store stores file,http
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf glance_store default_store file
[root@ct ~]# openstack-config --set /etc/glance/glance-api.conf glance_store filesystem_store_datadir /var/lib/glance/images/
[root@ct ~]# cat /etc/glance/glance-api.conf
[DEFAULT]
[cinder]
[cors]
[database]
connection = mysql+pymysql://glance:GLANCE_DBPASS@ct/glance
[file]
[glance.store.http.store]
[glance.store.rbd.store]
[glance.store.sheepdog.store]
[glance.store.swift.store]
[glance.store.vmware_datastore.store]
[glance_store]
stores = file,http
default_store = file
filesystem_store_datadir = /var/lib/glance/images/
[image_format]
[keystone_authtoken]
www_authenticate_uri = http://ct:5000
auth_url = http://ct:5000
memcached_servers = ct:11211
auth_type = password
project_domain_name = Default
user_domain_name = Default
project_name = service
username = glance
password = GLANCE_PASS
[oslo_concurrency]
[oslo_messaging_amqp]
[oslo_messaging_kafka]
[oslo_messaging_notifications]
[oslo_messaging_rabbit]
[oslo_middleware]
[oslo_policy]
[paste_deploy]
flavor = keystone
[profiler]
[store_type_location_strategy]
[task]
[taskflow_executor]
[root@ct ~]#

3.2 修改glance-registry.conf 配置文件

[root@ct ~]# cp -a /etc/glance/glance-registry.conf{,.bak}   //备份
[root@ct ~]# grep -Ev '^$|#' /etc/glance/glance-registry.conf.bak > /etc/glance/glance-registry.conf   //过滤注释信息
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf database connection  mysql+pymysql://glance:GLANCE_DBPASS@t/glance
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken www_authenticate_uri   http://ct:5000
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_url  http://ct:5000
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken memcached_servers  ct:11211
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_type  password
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken project_domain_name  Default
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken user_domain_name  Default
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken project_name  service
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken username  glance
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken password  GLANCE_PASS
[root@ct ~]# openstack-config --set /etc/glance/glance-registry.conf paste_deploy flavor  keystone
[root@ct ~]# cat /etc/glance/glance-registry.conf
[DEFAULT]
[database]
connection = mysql+pymysql://glance:GLANCE_DBPASS@t/glance
[keystone_authtoken]
www_authenticate_uri = http://ct:5000
auth_url = http://ct:5000
memcached_servers = ct:11211
auth_type = password
project_domain_name = Default
user_domain_name = Default
project_name = service
username = glance
password = GLANCE_PASS
[oslo_messaging_amqp]
[oslo_messaging_kafka]
[oslo_messaging_notifications]
[oslo_messaging_rabbit]
[oslo_policy]
[paste_deploy]
flavor = keystone
[profiler]
[root@ct ~]#

4.初始化数据库

[root@ct ~]# su -s /bin/sh -c "glance-manage db_sync" glance
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
/usr/lib/python2.7/site-packages/pymysql/cursors.py:170: Warning: (1280, u"Name 'alembic_version_pkc' ignored for PRIMARY key.")
  result = self._query(query)
INFO  [alembic.runtime.migration] Running upgrade  -> liberty, liberty initial
INFO  [alembic.runtime.migration] Running upgrade liberty -> mitaka01, add index on created_at and updated_at columns of 'images' table
INFO  [alembic.runtime.migration] Running upgrade mitaka01 -> mitaka02, update metadef os_nova_server
INFO  [alembic.runtime.migration] Running upgrade mitaka02 -> ocata_expand01, add visibility to images
INFO  [alembic.runtime.migration] Running upgrade ocata_expand01 -> pike_expand01, empty expand for symmetry with pike_contract01
INFO  [alembic.runtime.migration] Running upgrade pike_expand01 -> queens_expand01
INFO  [alembic.runtime.migration] Running upgrade queens_expand01 -> rocky_expand01, add os_hidden column to images table
INFO  [alembic.runtime.migration] Running upgrade rocky_expand01 -> rocky_expand02, add os_hash_algo and os_hash_value columns to images table
INFO  [alembic.runtime.migration] Running upgrade rocky_expand02 -> train_expand01, empty expand for symmetry with train_contract01
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
Upgraded database to: train_expand01, current revision(s): train_expand01
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
Database migration is up to date. No migration needed.
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade mitaka02 -> ocata_contract01, remove is_public from images
INFO  [alembic.runtime.migration] Running upgrade ocata_contract01 -> pike_contract01, drop glare artifacts tables
INFO  [alembic.runtime.migration] Running upgrade pike_contract01 -> queens_contract01
INFO  [alembic.runtime.migration] Running upgrade queens_contract01 -> rocky_contract01
INFO  [alembic.runtime.migration] Running upgrade rocky_contract01 -> rocky_contract02
INFO  [alembic.runtime.migration] Running upgrade rocky_contract02 -> train_contract01
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
Upgraded database to: train_contract01, current revision(s): train_contract01
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
Database is synced successfully

5.开启glance服务

[root@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@ct ~]#  systemctl start openstack-glance-api.service
[root@ct ~]# netstat -antp | grep 9292
tcp        0      0 0.0.0.0:9292            0.0.0.0:*               LISTEN      84418/python2       
[root@ct ~]#

6.赋予openstack-glance-api.service服务对存储设备的可写权限

[root@ct ~]#  chown -hR glance:glance /var/lib/glance/   //只对符号连接/软链接的文件修改
[root@ct ~]# ll /var/lib/glance/
总用量 0
drwxr-x--- 2 glance glance 6 8月  24 18:18 images

7.镜像导入

[root@ct ~]# ls
anaconda-ks.cfg  cirros-0.3.5-x86_64-disk.img
[root@ct ~]# openstack image create --file cirros-0.3.5-x86_64-disk.img --disk-format qcow2 --container-format bare --public cirros
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field            | Value                                                                                                                                                                                      |
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| checksum         | f8ab98ff5e73ebab884d80c9dc9c7290                                                                                                                                                           |
| container_format | bare                                                                                                                                                                                       |
| created_at       | 2021-08-24T10:23:15Z                                                                                                                                                                       |
| disk_format      | qcow2                                                                                                                                                                                      |
| file             | /v2/images/e8390729-8e4a-4a4a-9ccb-9c1f6bc4a3c2/file                                                                                                                                       |
| id               | e8390729-8e4a-4a4a-9ccb-9c1f6bc4a3c2                                                                                                                                                       |
| min_disk         | 0                                                                                                                                                                                          |
| min_ram          | 0                                                                                                                                                                                          |
| name             | cirros                                                                                                                                                                                     |
| owner            | cdf2831522724a8b91c8b7e57bd84516                                                                                                                                                           |
| properties       | os_hash_algo='sha512', os_hash_value='f0fd1b50420dce4ca382ccfbb528eef3a38bbeff00b54e95e3876b9bafe7ed2d6f919ca35d9046d437c6d2d8698b1174a335fbd66035bb3edc525d2cdb187232', os_hidden='False' |
| protected        | False                                                                                                                                                                                      |
| schema           | /v2/schemas/image                                                                                                                                                                          |
| size             | 13267968                                                                                                                                                                                   |
| status           | active                                                                                                                                                                                     |
| tags             |                                                                                                                                                                                            |
| updated_at       | 2021-08-24T10:23:15Z                                                                                                                                                                       |
| virtual_size     | None                                                                                                                                                                                       |
| visibility       | public                                                                                                                                                                                     |
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
[root@ct ~]#

8.查看镜像

[root@ct ~]# openstack image list
+--------------------------------------+--------+--------+
| ID                                   | Name   | Status |
+--------------------------------------+--------+--------+
| e8390729-8e4a-4a4a-9ccb-9c1f6bc4a3c2 | cirros | active |
+--------------------------------------+--------+--------+
[root@ct ~]# glance image-list 
+--------------------------------------+--------+
| ID                                   | Name   |
+--------------------------------------+--------+
| e8390729-8e4a-4a4a-9ccb-9c1f6bc4a3c2 | cirros |
+--------------------------------------+--------+
[root@ct ~]#