镜像服务
镜像服务 (glance) 允许用户发现、注册和获取虚拟机镜像。它提供了一个 REST API,允许您查询虚拟机镜像的 metadata 并获取一个现存的镜像。您可以将虚拟机镜像存储到各种位置,从简单的文件系统到对象存储系统—-例如 OpenStack 对象存储, 并通过镜像服务使用。
安装和配置
先决条件
安装和配置镜像服务之前,你必须创建创建一个数据库、服务凭证和API端点。
完成下面的步骤以创建数据库:
用数据库连接客户端以 root 用户连接到数据库服务器:
mysql -u root -p
创建 glance 数据库:
MariaDB [(none)]> CREATE DATABASE glance;
查询创建结果
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| glance |
| information_schema |
| keystone |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.003 sec)
对glance
数据库授予恰当的权限
MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' \
IDENTIFIED BY 'GLANCE_DBPASS';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' \
IDENTIFIED BY 'GLANCE_DBPASS';
获得 admin 凭证来获取只有管理员能执行的命令的访问权限
$ . admin-openrc
要创建服务证书,完成这些步骤:
创建 glance 用户:
$ openstack user create --domain default --password-prompt glance
User Password:
Repeat User Password:
+---------------------+----------------------------------+
| Field | Value |
+---------------------+----------------------------------+
| domain_id | default |
| enabled | True |
| id | 3f4e777c4062483ab8d9edd7dff829df |
| name | glance |
| options | {} |
| password_expires_at | None |
+---------------------+----------------------------------+
结果
[root@controller ~]# . admin-openrc
[root@controller ~]# openstack user create --domain default --password-prompt glance
User Password:
Repeat User Password:
+---------------------+----------------------------------+
| Field | Value |
+---------------------+----------------------------------+
| domain_id | default |
| enabled | True |
| id | a24c9f7cffd740afbd1a4388fecadc34 |
| 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 | 8c2c7f1b9b5049ea9e63757b5533e6d2 |
| name | glance |
| type | image |
+-------------+----------------------------------+
结果
[root@controller ~]# openstack service create --name glance \
> --description "OpenStack Image" image
+-------------+----------------------------------+
| Field | Value |
+-------------+----------------------------------+
| description | OpenStack Image |
| enabled | True |
| id | 824b05bbfbca4fe7abc376ad9b0b5ec4 |
| name | glance |
| type | image |
+-------------+----------------------------------+
[root@controller ~]# openstack service list
+----------------------------------+----------+----------+
| ID | Name | Type |
+----------------------------------+----------+----------+
| 125478add9754a8182936f04f2175c0e | keystone | identity |
| 824b05bbfbca4fe7abc376ad9b0b5ec4 | glance | image |
+----------------------------------+----------+----------+
创建镜像服务的 API 端点:
$ openstack endpoint create --region RegionOne \
image public http://controller:9292
+--------------+----------------------------------+
| Field | Value |
+--------------+----------------------------------+
| enabled | True |
| id | 340be3625e9b4239a6415d034e98aace |
| interface | public |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 8c2c7f1b9b5049ea9e63757b5533e6d2 |
| 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 | a6e4b153c2ae4c919eccfdbb7dceb5d2 |
| interface | internal |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 8c2c7f1b9b5049ea9e63757b5533e6d2 |
| 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 | 0c37ed58103f4300a84ff125a539032d |
| interface | admin |
| region | RegionOne |
| region_id | RegionOne |
| service_id | 8c2c7f1b9b5049ea9e63757b5533e6d2 |
| service_name | glance |
| service_type | image |
| url | http://controller:9292 |
+--------------+----------------------------------+
结果
[root@controller ~]# openstack endpoint list
+----------------------------------+-----------+--------------+--------------+---------+-----------+----------------------------+
| ID | Region | Service Name | Service Type | Enabled | Interface | URL |
+----------------------------------+-----------+--------------+--------------+---------+-----------+----------------------------+
| 270167f7978b458da1110a0e2db815ff | RegionOne | keystone | identity | True | admin | http://controller:5000/v3/ |
| 463b426aad584e9ea6d011cd0a6c527e | RegionOne | keystone | identity | True | internal | http://controller:5000/v3/ |
| 6051331a74b74ecd92fd9cf05bb61979 | RegionOne | glance | image | True | internal | http://controller:9292 |
| 90b589816dd14b77814928f67e42a601 | RegionOne | glance | image | True | public | http://controller:9292 |
| 99c92cfc2b27431a95167b4113f4a5b4 | RegionOne | keystone | identity | True | public | http://controller:5000/v3/ |
| d2f34737a14a4b55ad948f68b2fd5272 | RegionOne | glance | image | True | admin | http://controller:9292 |
+----------------------------------+-----------+--------------+--------------+---------+-----------+----------------------------+
安装并配置组件
安装软件包:
# yum install openstack-glance
编辑文件 /etc/glance/glance-api.conf 并完成如下动作:
在 [database] 部分,配置数据库访问:
[database]
# ...
connection = mysql+pymysql://glance:GLANCE_DBPASS@controller/glance
在 [keystone_authtoken] 和 [paste_deploy] 部分,配置认证服务访问:
[keystone_authtoken]
# ...
www_authenticate_uri = http://controller:5000
auth_url = http://controller:5000
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_store]
# ...
stores = file,http
default_store = file
filesystem_store_datadir = /var/lib/glance/images/
Populate the Image service database:
# su -s /bin/sh -c "glance-manage db_sync" glance
没有回显结果,实际有错误发生,后面会看到
完成安装
systemctl enable openstack-glance-api.service
systemctl start openstack-glance-api.service
验证操作
使用 CirrOS 对镜像服务进行验证,CirrOS是一个小型的Linux镜像可以用来帮助你进行 OpenStack部署测试。
获得 admin 凭证来获取只有管理员能执行的命令的访问权限:
$ . admin-openrc
下载源镜像:
$ wget http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img
使用 QCOW2 磁盘格式, bare 容器格式上传镜像到镜像服务并设置公共可见,这样所有的项目都可以访问它:
$ glance image-create --name "cirros" \
--file cirros-0.3.0-x86_64-disk.img \
--disk-format qcow2 --container-format bare \
--visibility public
+------------------+------------------------------------------------------+
| Field | Value |
+------------------+------------------------------------------------------+
| checksum | 133eae9fb1c98f45894a4e60d8736619 |
| container_format | bare |
| created_at | 2015-03-26T16:52:10Z |
| disk_format | qcow2 |
| file | /v2/images/cc5c6982-4910-471e-b864-1098015901b5/file |
| id | cc5c6982-4910-471e-b864-1098015901b5 |
| min_disk | 0 |
| min_ram | 0 |
| name | cirros |
| owner | ae7a98326b9c455588edd2656d723b9d |
| protected | False |
| schema | /v2/schemas/image |
| size | 13200896 |
| status | active |
| tags | |
| updated_at | 2015-03-26T16:52:10Z |
| virtual_size | None |
| visibility | public |
+------------------+------------------------------------------------------+
确认镜像的上传并验证属性:
$ glance image-list
+--------------------------------------+--------+--------+
| ID | Name | Status |
+--------------------------------------+--------+--------+
| 38047887-61a7-41ea-9b49-27987d5e8bb9 | cirros | active |
+--------------------------------------+--------+--------+
错误
[root@controller ~]# glance image-create --name "cirros" \
> --file cirros-0.3.0-x86_64-disk.img \
> --disk-format qcow2 --container-format bare \
> --visibility public
HTTP 500 Internal Server Error: The server has either erred or is incapable of performing the requested operation.
查看日志文件
[root@controller glance]# tail api.log
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi return wrapped(*args, **kwargs)
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi File "/usr/lib/python2.7/site-packages/oslo_db/sqlalchemy/engines.py", line 153, in create_engine
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi url = sqlalchemy.engine.url.make_url(sql_connection)
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi File "/usr/lib64/python2.7/site-packages/sqlalchemy/engine/url.py", line 225, in make_url
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi return _parse_rfc1738_args(name_or_url)
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi File "/usr/lib64/python2.7/site-packages/sqlalchemy/engine/url.py", line 287, in _parse_rfc1738_args
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi "Could not parse rfc1738 URL from string '%s'" % name
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi ArgumentError: Could not parse rfc1738 URL from string 'connection = mysql+pymysql://glance:zmd_9001@controller/glance'
2022-04-01 16:11:38.923 2077 ERROR glance.common.wsgi
2022-04-01 16:11:38.963 2077 INFO eventlet.wsgi.server [req-929e8218-fc79-40ea-ba84-c0739b52610f a6a881a6089843b9999b2a0a7397c5d7 faf1dd393fae450b8afc29ebc9d9b6d1 - default default] 192.168.2.11 - - [01/Apr/2022 16:11:38] "POST /v2/images HTTP/1.1" 500 454 0.054841
提示参数不对,翻看/etc/glance/glance-api.conf 中的对应部分,
有个配置没有写正确,多了个connection =,删除。
重新同步数据库。
[root@controller ~]# 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.
重启服务
[root@controller glance]# systemctl restart openstack-glance-api
重新上传
[root@controller ~]# glance image-create --name "cirros" \
> --file cirros-0.3.0-x86_64-disk.img \
> --disk-format qcow2 --container-format bare \
> --visibility public
+------------------+----------------------------------------------------------------------------------+
| Property | Value |
+------------------+----------------------------------------------------------------------------------+
| checksum | 50bdc35edb03a38d91b1b071afb20a3c |
| container_format | bare |
| created_at | 2022-04-01T08:25:34Z |
| disk_format | qcow2 |
| id | 59789cdd-76e9-48f4-a441-991bf9cf1396 |
| min_disk | 0 |
| min_ram | 0 |
| name | cirros |
| os_hash_algo | sha512 |
| os_hash_value | a9720c64e89624685770a63e9b959725f55fcb1b9fb9e5b31aed94a1c364c4ac47b256d74736562d |
| | 68dc4d6b31e7507eed765947001d726ea1c3e206e3135dba |
| os_hidden | False |
| owner | faf1dd393fae450b8afc29ebc9d9b6d1 |
| protected | False |
| size | 9761280 |
| status | active |
| tags | [] |
| updated_at | 2022-04-01T08:25:34Z |
| virtual_size | Not available |
| visibility | public |
+------------------+----------------------------------------------------------------------------------+
确认镜像的上传并验证属性:
$ glance image-list
+--------------------------------------+--------+--------+
| ID | Name | Status |
+--------------------------------------+--------+--------+
| 38047887-61a7-41ea-9b49-27987d5e8bb9 | cirros | active |
+--------------------------------------+--------+--------+
结果
[root@controller ~]# glance image-list
+--------------------------------------+--------+
| ID | Name |
+--------------------------------------+--------+
| 59789cdd-76e9-48f4-a441-991bf9cf1396 | cirros |
+--------------------------------------+--------+