映像服务使用户能够发现、注册和检索虚拟机映像。它提供了一个 REST API,使你能够查询虚拟机映像元数据并检索实际映像。您可以将通过映像服务提供的虚拟机映像存储在各种位置,从简单的文件系统到对象存储系统(如 OpenStack 对象存储)。
OpenStack镜像服务是IaaS的核心服务
安装和配置
这个部分描述如何在控制节点上安装和配置镜像服务,即 glance。简单来说,这个配置将镜像保存在本地文件系统中。
安装和配置镜像服务之前,你必须创建创建一个数据库、服务凭证和API端点。
完成下面的步骤以创建数据库:
用数据库连接客户端以 root
用户连接到数据库服务器:
mysql -u root -p
创建 glance
数据库:
CREATE DATABASE glance;
对glance
数据库授予恰当的权限:
GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' IDENTIFIED BY 'GLANCE_DBPASS';
GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' IDENTIFIED BY 'GLANCE_DBPASS';
用一个合适的密码替换 GLANCE_DBPASS
。
退出数据库客户端。
获得 admin
凭证来获取只有管理员能执行的命令的访问权限:
. admin-openrc
要创建服务证书,完成这些步骤:
- 创建
glance
用户:
openstack user create --domain default --password GLANCE_PASS glance
+---------------------+----------------------------------+
| Field | Value |
+---------------------+----------------------------------+
| domain_id | default |
| enabled | True |
| id | 3de80fd2909c4a99a18ca5c9dd19fc18 |
| name | glance |
| options | {} |
| password_expires_at | None |
+---------------------+----------------------------------+
- 添加
admin
角色到glance
用户和service
项目上。
openstack role add --project service --user glance admin
这个命令执行后没有输出。
- 创建
glance
服务实体:
openstack service create --name glance --description "OpenStack Image" image
+-------------+----------------------------------+
| Field | Value |
+-------------+----------------------------------+
| description | OpenStack Image |
| enabled | True |
| id | 525b21252e1e46aab075ded987b14fe2 |
| name | glance |
| type | image |
+-------------+----------------------------------+
创建镜像服务的 API 端点:
openstack endpoint create --region RegionOne image public http://controller:9292
+--------------+----------------------------------+
| Field | Value |
+--------------+----------------------------------+
| enabled | True |
| id | 894fbde74b9a4d678eb1232233103edf |
| interface | public |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 525b21252e1e46aab075ded987b14fe2 |
| service_name | glance |
| service_type | image |
| url | http://controller:9292 |
+--------------+----------------------------------+
openstack endpoint create --region RegionOne image internal http://controller:9292
+--------------+----------------------------------+
| Field | Value |
+--------------+----------------------------------+
| enabled | True |
| id | 0f005a4ac2ba4654b3304c848a1f782f |
| interface | internal |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 525b21252e1e46aab075ded987b14fe2 |
| service_name | glance |
| service_type | image |
| url | http://controller:9292 |
+--------------+----------------------------------+
openstack endpoint create --region RegionOne image admin http://controller:9292
+--------------+----------------------------------+
| Field | Value |
+--------------+----------------------------------+
| enabled | True |
| id | 102accc773644bdc83f0a51da5426357 |
| interface | admin |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 525b21252e1e46aab075ded987b14fe2 |
| service_name | glance |
| service_type | image |
| url | http://controller:9292 |
+--------------+----------------------------------+
配置组件
默认配置文件在各发行版本中可能不同。你可能需要添加这些部分,选项而不是修改已经存在的部分和选项。另外,在配置片段中的省略号(...
)表示默认的配置选项你应该保留。
安装软件包:
yum install -y openstack-glance
编辑文件 /etc/glance/glance-api.conf
并完成如下动作:
vim /etc/glance/glance-api.conf
在 [database]
部分,配置数据库访问:
[database]
# ...
connection = mysql+pymysql://glance:GLANCE_DBPASS@controller/glance
将GLANCE_DBPASS
替换为你为镜像服务选择的密码。
在 [keystone_authtoken]
和 [paste_deploy]
部分,配置认证服务访问:
[keystone_authtoken]
# ...
auth_uri = http://controller:5000
auth_url = http://controller:35357
memcached_servers = controller:11211
auth_type = password
project_domain_name = default
user_domain_name = default
project_name = service
username = glance
password = GLANCE_PASS
[paste_deploy]
# ...
flavor = keystone
将GLANCE_PASS
替换为你为认证服务中你为glance
用户选择的密码。
在 [glance_store]
部分,配置本地文件系统存储和镜像文件位置:
[glance_store]
# ...
stores = file,http
default_store = file
filesystem_store_datadir = /var/lib/glance/images/
编辑文件 /etc/glance/glance-registry.conf
并完成如下动作:
vim /etc/glance/glance-registry.conf
在 [database]
部分,配置数据库访问:
[database]
# ...
connection = mysql+pymysql://glance:GLANCE_DBPASS@controller/glance
将GLANCE_DBPASS
替换为你为镜像服务选择的密码。
在 [keystone_authtoken]
和 [paste_deploy]
部分,配置认证服务访问:
[keystone_authtoken]
# ...
auth_uri = http://controller:5000
auth_url = http://controller:35357
memcached_servers = controller:11211
auth_type = password
project_domain_name = default
user_domain_name = default
project_name = service
username = glance
password = GLANCE_PASS
[paste_deploy]
# ...
flavor = keystone
将GLANCE_PASS
替换为你为认证服务中你为glance
用户选择的密码。
写入镜像服务数据库:
su -s /bin/sh -c "glance-manage db_sync" glance
完成安装
启动镜像服务并将其配置为随机启动:
systemctl enable openstack-glance-api.service openstack-glance-registry.service;systemctl start openstack-glance-api.service openstack-glance-registry.service
验证操作
CirrOS是一个小型的Linux镜像可以用来帮助你进行 OpenStack部署测试。
在控制节点上执行这些命令。
获得 admin
凭证来获取只有管理员能执行的命令的访问权限:
. admin-openrc
下载源镜像:
wget https://cdn.thtown.cn/OpenStack/image/cirros-0.4.0-x86_64-disk.img
使用 QCOW2磁盘格式, bare 容器格式上传镜像到镜像服务并设置公共可见,这样所有的项目都可以访问它:
openstack image create "cirros" \
--file cirros-0.4.0-x86_64-disk.img \
--disk-format qcow2 --container-format bare \
--public
+------------------+------------------------------------------------------+
| Field | Value |
+------------------+------------------------------------------------------+
| checksum | 443b7623e27ecf03dc9e01ee93f67afe |
| container_format | bare |
| created_at | 2022-04-27T07:34:07Z |
| disk_format | qcow2 |
| file | /v2/images/003509cc-345a-41e6-b7a0-0866e228bdff/file |
| id | 003509cc-345a-41e6-b7a0-0866e228bdff |
| min_disk | 0 |
| min_ram | 0 |
| name | cirros |
| owner | ce0e09a6236a49cd83b0818f89399885 |
| protected | False |
| schema | /v2/schemas/image |
| size | 12716032 |
| status | active |
| tags | |
| updated_at | 2022-04-27T07:34:08Z |
| virtual_size | None |
| visibility | public |
+------------------+------------------------------------------------------+
OpenStack 是动态生成 ID 的,因此您看到的输出会与示例中的命令行输出不相同。
确认镜像的上传并验证属性:
openstack image list
+--------------------------------------+--------+--------+
| ID | Name | Status |
+--------------------------------------+--------+--------+
| 003509cc-345a-41e6-b7a0-0866e228bdff | cirros | active |
+--------------------------------------+--------+--------+