OpenStackGlance组件全面培训教材

admin 2026-01-27 14:38:49 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文档是OpenStackGlance镜像服务的全面培训教材,涵盖基础入门、中级进阶和高级运维三个层次。内容包括Glance核心功能与架构解析、多种存储后端配置(Filesystem/Swift/Ceph)、镜像格式转换、性能优化、高可用部署、故障排查及安全加固等完整知识体系,提供了大量CLI操作命令和配置示例,适合从初学者到高级工程师的系统性学习。 综合评分: 75 文章分类: 云安全,安全建设,安全运营


cover_image

OpenStack Glance 组件全面培训教材

原创

刘军军 刘军军

运维星火燎原

2026年1月27日 00:08 河北

第一部分:Glance 基础入门(适合初学者)

1.1 Glance 概述

什么是Glance?

Glance是OpenStack的镜像服务,负责虚拟机镜像的存储、管理和发现。它提供了RESTful API来查询、注册、检索虚拟机镜像。

Glance 的核心功能

  • 镜像存储管理:存储和管理虚拟机镜像文件
  • 镜像元数据管理:维护镜像的属性信息
  • 镜像格式转换:支持多种镜像格式转换
  • 镜像共享机制:支持跨项目/租户镜像共享
  • 快照管理:从运行实例创建镜像

Glance 的重要性

# Glance是虚拟机创建的基础
Nova创建VM → 从Glance获取镜像 → 启动虚拟机

# 没有Glance,无法创建自定义虚拟机
只能使用预置的有限镜像

1.2 Glance 基本概念

核心概念解析

# 镜像 (Image)
- 虚拟机的磁盘模板文件
- 包含操作系统和预装软件

# 镜像格式 (Image Format)
- RAW: 原始磁盘格式,性能最好
- QCOW2: QEMU格式,支持快照和压缩
- VHD: Hyper-V格式
- VMDK: VMware格式
- ISO: 光盘镜像格式

# 镜像状态 (Image Status)
- queued: 已排队,等待上传
- saving: 正在上传
- active: 可用状态
- killed: 上传失败
- deleted: 已删除(可恢复)
- pending_delete: 等待删除

# 镜像属性 (Image Properties)
- disk_format: 磁盘格式
- container_format: 容器格式(通常为bare)
- min_disk: 最小磁盘要求
- min_ram: 最小内存要求
- architecture: CPU架构
- os_distro: 操作系统发行版

镜像生命周期

queued → saving → active → (deleted) → pending_delete → 永久删除

1.3 快速开始:Glance 基本操作

环境准备

# 安装OpenStack客户端
pip install python-openstackclient python-glanceclient

# 配置认证信息
export OS_AUTH_URL=http://controller:5000/v3
export OS_USERNAME=admin
export OS_PASSWORD=admin_pass
export OS_PROJECT_NAME=admin
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_DOMAIN_NAME=Default

基本操作演示

# 1. 查看当前镜像列表
openstack image list

# 2. 查看镜像详情
openstack image show <image-id>

#&nbsp;3. 查看Glance服务状态
openstack image service list

#&nbsp;4. 查看可用存储后端
openstack store list

上传第一个镜像

#&nbsp;1. 下载测试镜像(以CirrOS为例)
wget http://download.cirros-cloud.net/0.5.2/cirros-0.5.2-x86_64-disk.img

#&nbsp;2. 上传镜像到Glance
openstack image create \
&nbsp; --file cirros-0.5.2-x86_64-disk.img \
&nbsp; --disk-format qcow2 \
&nbsp; --container-format bare \
&nbsp; --public \
&nbsp; --property os_distro=cirros \
&nbsp; --property architecture=x86_64 \
&nbsp; cirros-0.5.2

#&nbsp;3. 查看上传进度
openstack image show cirros-0.5.2

#&nbsp;4. 验证镜像可用性
openstack image list --name cirros-0.5.2

镜像管理操作

#&nbsp;1. 设置镜像属性
openstack image set \
&nbsp; --min-disk 5 \
&nbsp; --min-ram 512 \
&nbsp; --property hw_disk_bus=scsi \
&nbsp; cirros-0.5.2

#&nbsp;2. 去激活镜像(禁用)
openstack image deactivate cirros-0.5.2

#&nbsp;3. 重新激活镜像
openstack image reactivate cirros-0.5.2

#&nbsp;4. 删除镜像
openstack image delete cirros-0.5.2

#&nbsp;5. 恢复已删除镜像(在删除保护期内)
openstack image restore <image-id>

镜像共享和导出

#&nbsp;1. 共享镜像给其他项目
openstack image&nbsp;add&nbsp;project \
<image-id>&nbsp;\
<project-id>

#&nbsp;2. 取消共享
openstack image&nbsp;remove&nbsp;project \
<image-id>&nbsp;\
<project-id>

#&nbsp;3. 导出镜像到本地文件
openstack image save \
&nbsp; --file&nbsp;exported-image.img \
&nbsp;&nbsp;<image-id>

第二部分:Glance 中级进阶(适合有一定基础)

2.1 Glance 架构深度解析

服务架构

# Glance 核心组件
glance-api &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# API服务,接收REST请求
glance-registry &nbsp; &nbsp;&nbsp;# 元数据服务(已弃用,v2+版本集成到api)
glance-glare &nbsp; &nbsp; &nbsp; &nbsp;# 元数据API(实验性)

# 多版本API支持
- v1: 传统API(已弃用)
- v2: 当前主要版本
- v3: 最新版本(元数据增强)

# 工作流程
客户端 → glance-api → 存储后端 → 数据库(元数据)

存储后端架构

# 支持的存储后端
-&nbsp;Filesystem: 本地文件系统(简单)
-&nbsp;Swift: OpenStack对象存储(分布式)
-&nbsp;Ceph: 分布式存储(高性能)
-&nbsp;AWS S3: 亚马逊对象存储
-&nbsp;HTTP: 远程HTTP存储
-&nbsp;RBD: Ceph RBD块设备

# 多存储后端配置
-&nbsp;可以配置多个存储后端
-&nbsp;支持存储策略和迁移

配置文件结构

# 主要配置文件
/etc/glance/glance-api.conf
/etc/glance/glance-registry.conf &nbsp;# v1版本使用
/etc/glance/glance-cache.conf &nbsp; &nbsp;&nbsp;# 缓存配置

# 配置文件分段
[DEFAULT] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 通用配置
[database] &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;# 数据库配置
[glance_store] &nbsp; &nbsp;&nbsp;# 存储后端配置
[image_format] &nbsp; &nbsp;&nbsp;# 镜像格式配置
[keystone_authtoken]&nbsp;# 认证配置
[paste_deploy] &nbsp; &nbsp;&nbsp;# WSGI配置

2.2 存储后端详解

文件系统后端配置

# /etc/glance/glance-api.conf
[DEFAULT]
show_image_direct_url&nbsp;=&nbsp;true

[glance_store]
stores&nbsp;= file,http
default_store&nbsp;= file
filesystem_store_datadir&nbsp;= /var/lib/glance/images/

Swift 对象存储配置

# Swift存储后端配置
[glance_store]
stores&nbsp;= swift
default_store&nbsp;= swift

[swift]
store_container&nbsp;= glance
swift_store_config_file&nbsp;= /etc/glance/glance-swift.conf
large_object_size&nbsp;=&nbsp;5120
large_object_chunk_size&nbsp;=&nbsp;200
swift_store_create_container_on_put&nbsp;=&nbsp;true

Ceph RBD 配置

# Ceph RBD存储配置
[glance_store]
stores&nbsp;= rbd
default_store&nbsp;= rbd

[rbd]
rbd_store_pool&nbsp;= images
rbd_store_user&nbsp;= glance
rbd_store_ceph_conf&nbsp;= /etc/ceph/ceph.conf
rbd_store_chunk_size&nbsp;=&nbsp;8

多存储后端配置

# 配置多个存储后端
[glance_store]
stores&nbsp;= file, swift, rbd
default_store&nbsp;= file

# 存储策略(基于镜像属性)
[glance_store]
store_type_preference&nbsp;= rbd,swift,file

2.3 镜像格式和转换

支持的镜像格式

# 磁盘格式 (disk_format)
-&nbsp;raw: 原始格式,性能最佳
-&nbsp;qcow2: QEMU格式,支持压缩和快照
-&nbsp;vhd: Hyper-V虚拟硬盘
-&nbsp;vmdk: VMware虚拟磁盘
-&nbsp;iso: 光盘镜像
-&nbsp;aki/ari/ami: Amazon镜像格式

# 容器格式 (container_format)
-&nbsp;bare: 无容器,直接使用磁盘镜像
-&nbsp;ovf: Open Virtualization Format
-&nbsp;aki/ari/ami: Amazon容器格式

镜像转换配置

# 镜像转换设置
[image_format]
disk_formats&nbsp;= raw,qcow2,vhd,vmdk,iso
container_formats&nbsp;= bare,ovf,aki,ari,ami

# QCOW2优化配置
[image_format]
qcow2_allowed_compression_types&nbsp;= zlib,zstd

自动格式转换

#&nbsp;上传时自动转换格式
openstack image create \
&nbsp; --file ubuntu.iso \
&nbsp; --disk-format qcow2 \
&nbsp; --container-format bare \
&nbsp; ubuntu-converted

#&nbsp;查看转换后格式
openstack image show ubuntu-converted | grep disk_format

2.4 高级镜像管理

镜像元数据管理

# 自定义元数据属性
openstack image&nbsp;set&nbsp;\
&nbsp; --property hw_cpu_sockets=2 \
&nbsp; --property hw_cpu_cores=4 \
&nbsp; --property hw_vif_model=virtio \
&nbsp; --property os_require_quiesce=true&nbsp;\
&nbsp; custom-image

# 查看所有元数据
openstack image show <image-id> -f json | jq&nbsp;'.properties'

# 基于元数据过滤镜像
openstack image list --property os_distro=ubuntu

镜像标签管理

# 添加标签
openstack image&nbsp;add&nbsp;tag&nbsp;\
&nbsp;&nbsp;<image-id>&nbsp;\
&nbsp; production

# 移除标签
openstack image&nbsp;remove&nbsp;tag&nbsp;\
&nbsp;&nbsp;<image-id>&nbsp;\
&nbsp; production

# 按标签过滤
openstack image&nbsp;list&nbsp;--tag&nbsp;production

镜像导入/导出

#&nbsp;从URL导入镜像
openstack image create \
&nbsp; --disk-format qcow2 \
&nbsp; --container-format bare \
&nbsp; --location https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
&nbsp; ubuntu-20.04

#&nbsp;导出镜像数据
openstack image save \
&nbsp; --file ubuntu-backup.img \
&nbsp; ubuntu-20.04

#&nbsp;批量导出元数据
openstack image list -f json > all-images-metadata.json

第三部分:Glance 高级运维(适合高级工程师)

3.1 Glance 性能优化

配置优化

# /etc/glance/glance-api.conf 优化
[DEFAULT]
# Worker进程数
workers&nbsp;=&nbsp;8
api_workers&nbsp;=&nbsp;8

# 数据库连接池
max_pool_size&nbsp;=&nbsp;30
max_overflow&nbsp;=&nbsp;10
pool_timeout&nbsp;=&nbsp;30

# 消息队列优化
rpc_conn_pool_size&nbsp;=&nbsp;30
rpc_response_timeout&nbsp;=&nbsp;60

# 缓存配置
image_cache_dir&nbsp;= /var/lib/glance/image-cache/
image_cache_stall_time&nbsp;=&nbsp;86400
image_cache_max_size&nbsp;=&nbsp;10737418240

存储后端优化

# 文件系统存储优化
[glance_store]
filesystem_store_datadir&nbsp;= /var/lib/glance/images
filesystem_store_file_perm&nbsp;=&nbsp;0o640

# 使用XFS文件系统(适合大文件)
# mkfs.xfs /dev/sdb1
# mount -o noatime,nodiratime /dev/sdb1 /var/lib/glance/images

# Swift存储优化
[swift]
swift_store_threads&nbsp;=&nbsp;8
swift_upload_buffer_size&nbsp;=&nbsp;65536

镜像缓存优化

# 镜像缓存配置
[image_cache]
enabled&nbsp;=&nbsp;true
driver&nbsp;= sqlite
sqlite_db&nbsp;= /var/lib/glance/image-cache/metadata.db

# 缓存清理策略
[image_cache]
clean_interval&nbsp;=&nbsp;3600
stall_time&nbsp;=&nbsp;86400

3.2 高可用部署

多节点高可用架构

# 控制节点 (3节点集群)
Controller1:&nbsp;glance-api&nbsp;(Active)
Controller2:&nbsp;glance-api&nbsp;(Standby)
Controller3:&nbsp;glance-api&nbsp;(Standby)

# 共享存储配置
-&nbsp;使用CephRBD或Swift作为后端存储
-&nbsp;确保所有节点可以访问同一存储

# 负载均衡配置
frontendglance-api
&nbsp; &nbsp;&nbsp;bind&nbsp;*:9292
&nbsp; &nbsp;&nbsp;modehttp
&nbsp; &nbsp;&nbsp;default_backendglance-api-backend

backendglance-api-backend
&nbsp; &nbsp;&nbsp;balanceroundrobin
&nbsp; &nbsp;&nbsp;servercontroller1&nbsp;10.0.0.11:9292check
&nbsp; &nbsp;&nbsp;servercontroller2&nbsp;10.0.0.12:9292check
&nbsp; &nbsp;&nbsp;servercontroller3&nbsp;10.0.0.13:9292&nbsp;check

数据库高可用

# MySQL Galera Cluster配置
[database]
connection&nbsp;= mysql+pymysql://glance:glance_pass@controller1,controller2,controller3/glance?charset=utf8

# 连接参数优化
max_retries&nbsp;=&nbsp;3
retry_interval&nbsp;=&nbsp;1
connection_debug&nbsp;=&nbsp;0

分布式存储配置

#&nbsp;Ceph RBD高可用配置
[rbd]
rbd_store_pool = images
rbd_store_user = glance
rbd_store_ceph_conf = /etc/ceph/ceph.conf
rbd_store_chunk_size = 8

#&nbsp;多monitor配置
rbd_store_ceph_conf = /etc/ceph/ceph.conf
#&nbsp;ceph.conf内容:
[global]
mon host = controller1,controller2,controller3

3.3 故障排查和调试

服务状态检查

#&nbsp;检查Glance服务状态
systemctl status glance-api
journalctl -u glance-api -f

#&nbsp;检查数据库连接
mysql -u glance -p -e "SELECT COUNT(*) FROM images;"

#&nbsp;检查存储后端
openstack store list
openstack store info <store-name>

#&nbsp;检查镜像缓存
ls -la /var/lib/glance/image-cache/

日志分析

# 关键日志文件
/var/log/glance/api.log
/var/log/glance/registry.log&nbsp; # v1版本
/var/log/glance/glance-cache.log

# 日志级别配置
[DEFAULT]
debug&nbsp;= true
verbose&nbsp;= true
log_file = /var/log/glance/api.log

# 日志分析技巧
grep"ERROR"&nbsp;/var/log/glance/api.log
grep"Image upload"&nbsp;/var/log/glance/api.log
grep"Storage backend"&nbsp;/var/log/glance/api.log

# 详细调试
[wsgi]
debug&nbsp;= true

常见故障排查

# 1. 镜像上传失败
-&nbsp;检查存储空间是否充足
-&nbsp;检查存储后端配置
-&nbsp;检查文件权限

# 2. 镜像下载失败
-&nbsp;检查网络连接
-&nbsp;检查存储后端可访问性
-&nbsp;检查镜像状态是否为active

# 3. 元数据操作失败
-&nbsp;检查数据库连接
-&nbsp;检查数据库权限

# 4. 认证失败
-&nbsp;检查Keystone服务
-&nbsp;检查认证配置

3.4 安全加固

API安全配置

# SSL/TLS加密
[ssl]
enable&nbsp;=&nbsp;true
certfile&nbsp;= /etc/glance/ssl/cert.pem
keyfile&nbsp;= /etc/glance/ssl/key.pem
ca_certs&nbsp;= /etc/glance/ssl/ca.pem

# CORS安全配置
[cors]
allowed_origin&nbsp;= https://dashboard.example.com
allow_credentials&nbsp;=&nbsp;true
max_age&nbsp;=&nbsp;3600

# 请求限制
[DEFAULT]
max_request_body_size&nbsp;=&nbsp;1073741824&nbsp; #&nbsp;1GB
client_socket_timeout&nbsp;=&nbsp;900

存储安全

#&nbsp;文件权限设置
chown glance:glance /var/lib/glance/images/
chmod 750 /var/lib/glance/images/

#&nbsp;存储后端访问控制
#&nbsp;Swift: 使用临时URL签名
#&nbsp;Ceph: 使用CephX认证
#&nbsp;S3: 使用IAM策略

镜像安全扫描

# 集成安全扫描工具
[task]
task_executor&nbsp;= taskflow
taskflow_executor&nbsp;= serial

# 自定义任务流程(安全扫描)
[taskflow_executor]
engine_mode&nbsp;= serial

# 使用ClamAV扫描镜像
# 集成第三方安全扫描工具

3.5 监控和审计

监控指标

# 关键性能指标
-glance.image.upload.count: 镜像上传数量
-glance.image.download.count: 镜像下载数量
-glance.image.size.bytes: 镜像总大小
-glance.api.request.duration:&nbsp;API请求时间

# 存储指标
-glance.store.usage.bytes: 存储使用量
-glance.cache.hit.ratio: 缓存命中率
-glance.task.completed.count: 任务完成数量

审计日志

# 启用详细审计
[audit]
middleware =&nbsp;true
audit_map_file = /etc/glance/audit_map.conf

# 审计日志配置
[audit]
audit_log_file = /var/log/glance/audit.log
audit_log_format = json

# 关键审计事件
- 镜像上传/下载
- 镜像元数据变更
- 存储后端操作
- 管理员操作

集成监控系统

# Prometheus监控
- 使用glance-exporter暴露指标
- 配置告警规则

# ELK日志分析
- 收集Glance日志
- 分析镜像使用模式
- 检测异常访问

# 自定义监控脚本
#!/bin/bash
# 检查Glance服务健康
response=$(curl -s -o /dev/null -w&nbsp;"%{http_code}"&nbsp;http://controller:9292/v2/images)
if&nbsp;[&nbsp;"$response"&nbsp;-ne 200 ];&nbsp;then
&nbsp; &nbsp;&nbsp;echo"Glance service is down"
&nbsp; &nbsp;&nbsp;exit&nbsp;1
fi

# 检查存储

免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:运维星火燎原 刘军军 刘军军《OpenStack Glance 组件全面培训教材》

评论:0   参与:  0