
OpenStack管理摘要本文全面介绍了OpenStack镜像管理组件Glance的核心概念与实践应用。首先从公有云镜像概念入手对比了公共镜像、私有镜像、共享镜像和市场镜像的区别。接着详细解析了Glance在OpenStack架构中的作用、组件架构演变从V1/V2 API到Stein版本后的架构调整以及镜像、实例、规格三者的关系。文章深入讲解了Glance支持的多种磁盘格式如qcow2、raw、vmdk等和镜像状态机queued、saving、active、deactivated等状态。通过实际实验演示了镜像上传流程包括文件格式检查、Web界面操作、数据库验证等步骤。最后探讨了Glance镜像制作方法、使用Swift作为后端存储的配置方案以及命令行上传大文件镜像的技巧。全文结合大量截图和代码示例为OpenStack管理员提供了完整的Glance镜像管理指南。OpenStack镜像管理-glance公有云(华为云)镜像概念解释说明镜像是一个包含了操作系统及必要配置的弹性云服务器模板使用镜像可以创建弹性云服务器。公共镜像公共镜像是云平台提供的常见的标准操作系统镜像所有用户可见包括操作系统以及预装的公共应用。公共镜像具有高度稳定性皆为正版授权您也可以根据实际需求自助配置应用环境或相关软件。私有镜像基于云服务器或外部镜像文件等方式创建的个人镜像仅用户自己可见。包含操作系统、预装的公共应用以及用户的私有应用。选择私有镜像创建云服务器可以节省您重复配置云服务器的时间。更多信息请参见怎样制作私有镜像共享镜像您可以接受云平台其他用户共享的私有镜像作为自己的镜像进行使用。更多信息请参见怎样共享镜像市场镜像提供预装操作系统、应用环境和各类软件的优质第三方镜像。无需配置可一键部署满足建站、应用开发、可视化管理等个性化需求。最主要的作用节约时间Glance在OpenStack中的作用Glance架构Glance架构•在Newton版本及之前版本中Glance支持REST API V1和V2。•在V2 API版本中Glance-Registry的内容被整合进了Glance-API。如果Glance-API接收到与镜像元数据有关的请求则会直接操作数据库不需要再通过Glance-Registry。•在V1 API版本中Glance-Registry与Glance-API一样也是一个WSGI Server但是Glance-Registry处理的是与镜像元数据相关的RESTful请求。Glance-API在接收到用户的RESTful请求后如果该请求与元数据相关则将其转发给Glance-Registry。需要注意的是Glance-Registry提供的REST API是给Glance-API使用的 不对OpenStack外部用户暴露。•在Newton版本中V1已经过时并从Stein版本开始Glance-Registry被废弃由Glance-API代替然后通过Store模块的接口实现对各种不同后台存储系统的支持包括Glance架构图中的Amazon S3、Cinder/Swift、Ceph、Sheepdog等存储后端。查看glalnce-api配置文件观察与database通信的部分glance的配置文件在/etc/glance目录下#查看配置文件不做修改[rootcontroller glance]# vim /etc/glance/glance-api.conf1619connectionmysqlpymysql://glance:cb89fe4fee034ad6192.168.108.10/glance连接数据库查看现象[rootcontroller ~]# mysql #连接数据库 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 841 Server version: 10.3.28-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)] show databases; #查看所有数据库 -------------------- | Database | -------------------- | aodh | | cinder | | glance | | gnocchi | | heat | | information_schema | | keystone | | mysql | | neutron | | nova | | nova_api | | nova_cell0 | | performance_schema | | placement | | test | -------------------- 15 rows in set (0.014 sec) MariaDB [(none)] use glance; #切换到指定的glance数据库 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [glance] show tables; #查看所有当前数据库中的所有表 ---------------------------------- | Tables_in_glance | ---------------------------------- | alembic_version | | image_locations | | image_members | | image_properties | | image_tags | | images | | metadef_namespace_resource_types | | metadef_namespaces | | metadef_objects | | metadef_properties | | metadef_resource_types | | metadef_tags | | migrate_version | | task_info | | tasks | ---------------------------------- 15 rows in set (0.000 sec)镜像image、实例instance、规格flavor三者区别镜像、实例和规格的关系用户可以从同一个镜像启动任意数量的实例每个启动的实例都是基于镜像的一个副本实例上的任何修改都不会影响到镜像启动实例时必须指定一个规格实例按照规格使用资源Glance磁盘格式磁盘格式描述vhdXenMicrosoftVirtualBox等使用的常见磁盘格式vhdxvhd格式的增强版本支持更大的磁盘容量和其他功能vmdkVMware常见的磁盘格式vdiVirtualBox和QEMU支持的磁盘格式iso光盘例如CDROM的存档格式ploogVirtuozzo支持和使用的磁盘格式用于运行OS Containersqcow2QEMU支持的磁盘格式支持动态扩展和写时复制akiAmazon Kernel ImageariAmazon Ramdisk ImageamiAmazon Machine Imageraw一种非结构化的磁盘镜像格式将镜像添加到Glance时必须指定虚拟机镜像的磁盘格式和容器格式Glance状态机Glance中有两种状态机镜像状态和任务状态镜像状态描述queued已在glance-registry中保留镜像标识符但镜像数据未上传镜像大小未初始化saving镜像的原始数据正在上传到Glance中uploading对镜像调用了import>Glance状态机转换图•queued没有上传image数据只有db中的元数据。•saving正在上传image data当注册一个镜像使用POST /images并且当前携带了一个x-image-meta-location头这个镜像将不会进入saving状态(镜像的数据已经是可以获得的不能重传)。•active当镜像数据上传完毕镜像就可以被使用了(可获得的)此时处于active状态。•deactivated表示任何非管理员用户都无权访问镜像数据禁止下载镜像也禁止像镜像导出和镜像克隆之类的操作请求镜像数据的操作。•killed表示上传过程中发生错误并且镜像是不可读的。•deletedglance已经保存了该镜像的数据但是该镜像不再可用处于该状态的镜像将在不久后被自动删除。•pending_delete 与deleted相似glance还没有清除镜像数据只是处于该状态的镜像不可恢复。上传镜像实验此次实验上传两个镜像cirros-0.5.2-x86_64-disk.imgTinyCore-12.0.iso直接先将这2个文件拖放到controller节点的/root目录。观察两个文件磁盘格式[rootcontroller ~]# lsanaconda-ks.cfg answers.txt cirros-0.5.2-x86_64-disk.img keystonerc_admin TinyCore-12.0.iso[rootcontroller ~]#[rootcontroller ~]# qemu-img info cirros-0.5.2-x86_64-disk.imgimage: cirros-0.5.2-x86_64-disk.imgfileformat: qcow2#磁盘格式qcow2virtual size:112MiB(117440512bytes)disk size:15.5MiB cluster_size:65536Format specific information: compat:1.1compression type: zlib lazy refcounts:falserefcount bits:16corrupt:falseextended l2:false[rootcontroller ~]#[rootcontroller ~]# qemu-img info TinyCore-12.0.isoimage: TinyCore-12.0.isofileformat: raw virtual size:20MiB(20971520bytes)disk size:20MiB[rootcontroller ~]#点击上图右上角问号可以看到帮助上传成功点击镜像cirros-0.5.2上传后ID为e846b42b-5024-479b-9f79-1ae181bfa14e镜像上传到哪里去了[rootcontroller ~]# find / -name e846b42b-5024-479b-9f79-1ae181bfa14e/var/lib/glance/images/e846b42b-5024-479b-9f79-1ae181bfa14e去数据库中查看现象[rootcontroller ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 7012 Server version: 10.3.28-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)] show databases; -------------------- | Database | -------------------- | aodh | | cinder | | glance | | gnocchi | | heat | | information_schema | | keystone | | mysql | | neutron | | nova | | nova_api | | nova_cell0 | | performance_schema | | placement | | test | -------------------- 15 rows in set (0.000 sec) MariaDB [(none)] use glance; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [glance] show tables; ---------------------------------- | Tables_in_glance | ---------------------------------- | alembic_version | | image_locations | | image_members | | image_properties | | image_tags | | images | | metadef_namespace_resource_types | | metadef_namespaces | | metadef_objects | | metadef_properties | | metadef_resource_types | | metadef_tags | | migrate_version | | task_info | | tasks | ---------------------------------- 15 rows in set (0.000 sec) MariaDB [glance] select id,value from image_locations; ------------------------------------------------------------------------ | id | value | ------------------------------------------------------------------------ | 1 | file:///var/lib/glance/images/e846b42b-5024-479b-9f79-1ae181bfa14e | | 2 | file:///var/lib/glance/images/bb8a4940-3bde-428c-83b4-9c98eb430721 | ------------------------------------------------------------------------ 2 rows in set (0.000 sec)Glance镜像制作最简单的Glance镜像制作方法是下载系统供应商官方发布的OpenStack镜像文件。大多数镜像预安装了cloud-init包支持SSH密钥对登录和用户数据注入功能。•镜像的具体下载链接请参考OpenStack社区网站▫https://docs.openstack.org/image-guide/obtain-images.html。使用swift作为Glance后端存储#将/etc/glance/glance-api.conf中的#号开头的行(注释行)和空行过滤掉显示出配置行[rootcontroller ~]# grep -Ev ^# /etc/glance/glance-api.conf | grep -Ev ^$[DEFAULT]bind_host0.0.0.0bind_port9292workers4image_cache_dir/var/lib/glance/image-cachedebugFalselog_file/var/log/glance/api.loglog_dir/var/log/glancetransport_urlrabbit://guest:guest192.168.108.10:5672/[cinder][cors][database]connectionmysqlpymysql://glance:cb89fe4fee034ad6192.168.108.10/glance[file][glance.store.http.store][glance.store.rbd.store][glance.store.s3.store][glance.store.swift.store][glance.store.vmware_datastore.store][glance_store]storesfile,http,swiftdefault_storefilefilesystem_store_datadir/var/lib/glance/images/[image_format][keystone_authtoken]www_authenticate_urihttp://192.168.108.10:5000/v3auth_typepasswordauth_urlhttp://192.168.108.10:5000usernameglancepasswordhuaweiuser_domain_nameDefaultproject_nameservicesproject_domain_nameDefault[oslo_concurrency][oslo_messaging_amqp][oslo_messaging_kafka][oslo_messaging_notifications]drivermessagingv2topicsnotifications[oslo_messaging_rabbit]sslFalsedefault_notification_exchangeglance[oslo_middleware][oslo_policy]policy_file/etc/glance/policy.json[paste_deploy]flavorkeystone[profiler][store_type_location_strategy][task][taskflow_executor][wsgi]#下面为查看swift密码方式[rootcontroller ~]# vim answers.txt1137CONFIG_SWIFT_HASHf8715a7c2bf640b9#这个密码配置到上面的配置文件[rootcontroller ~]# vim /etc/glance/glance-api.conf3111default_storeswift3982swift_store_regionRegionOne4090swift_store_containergqdglance4118swift_store_large_object_size51204160swift_store_create_container_on_puttrue4182swift_store_multi_tenanttrue4230swift_store_admin_tenantsservices4391swift_store_auth_addresshttp://192.168.108.10:5000/v34399swift_store_userswift4408swift_store_keyf8715a7c2bf640b9#这里的swift密码是安装的时候决定的随机产生的查看answers.txt[rootcontroller ~(keystone_admin)]# systemctl restart openstack-glance* #重启glance进程上传镜像测试效果查看具体ID6a5a50d9-be54-4151-bd2f-4b1c08efdb18点击对象存储、容器数据库中观察现象[rootcontroller ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 23720 Server version: 10.3.28-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)] show databases; -------------------- | Database | -------------------- | aodh | | cinder | | glance | | gnocchi | | heat | | information_schema | | keystone | | mysql | | neutron | | nova | | nova_api | | nova_cell0 | | performance_schema | | placement | | test | -------------------- 15 rows in set (0.000 sec) MariaDB [(none)] use glance; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [glance] show tables; ---------------------------------- | Tables_in_glance | ---------------------------------- | alembic_version | | image_locations | | image_members | | image_properties | | image_tags | | images | | metadef_namespace_resource_types | | metadef_namespaces | | metadef_objects | | metadef_properties | | metadef_resource_types | | metadef_tags | | migrate_version | | task_info | | tasks | ---------------------------------- 15 rows in set (0.000 sec) MariaDB [glance] select id,value from image_locations; ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | id | value | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 1 | file:///var/lib/glance/images/e846b42b-5024-479b-9f79-1ae181bfa14e | | 2 | file:///var/lib/glance/images/bb8a4940-3bde-428c-83b4-9c98eb430721 | | 3 | swifthttp://192.168.108.10:8080/v1/AUTH_03e49a46342d48ab9607b4919925e42c/gqdglance_6a5a50d9-be54-4151-bd2f-4b1c08efdb18/6a5a50d9-be54-4151-bd2f-4b1c08efdb18 | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3 rows in set (0.000 sec)命令行上传大文件镜像-带进度条WEB界面上传文件没有进度条只会转圈命令行上传大文件[rootcontroller ~(keystone_admin)]# glance image-create --name image_centos --file CentOS-7-x86_64-Minimal-1810.iso --disk-format raw --container bare --progress现象如下有进度条建议openstack上传大文件使用命令行web界面可能上传失败