如何为 Glance 配置 RBD 与 Swift 双后端并设置优先级?
解读
面试官问“双后端+优先级”,核心想验证三件事:
- 你是否真的在生产环境落地过多后端镜像存储;
- 是否理解 Glance 的store优先级调度逻辑(store_weight + store_capabilities);
- 能否把 Ceph RBD 与 Swift 的差异化特性(延迟、吞吐、EC、多AZ、成本)翻译成可运维的权重数字。
国内大厂私有云普遍用Ceph做主存储,Swift做冷数据归档,回答必须给出可落地的权重公式与灰度回滚方案,否则会被判“只背过文档”。
知识点
- Glance 后端模型:
- 每个后端对应一个store identifier(rbd:ceph、swift:s3api)。
- 调度器 glance_store 通过store_weight(int,越大越优先)与capability_filter(可用容量、延迟阈值)决定落盘位置。
- 配置入口:
/etc/glance/glance-api.conf的 [glance_store] 段,stores=rbd,swift 声明启用列表;- default_backend=rbd 仅影响未指定后端的上传,不决定优先级;
- 每个后端独立段落 [rbd]、[swift],内部可写 weight=N。
- 优先级生效顺序:
① 用户显式指定--store <identifier>;
② capability_filter 踢掉不可用后端;
③ 剩余后端按 weight 降序选择;
④ weight 相同则轮询。 - 国内踩坑点:
- Ceph 双活集群需保证.glance池在monmap里读写延迟<50 ms,否则capability_filter会踢掉RBD;
- Swift 使用S3API中间件时,identifier必须写
s3,否则glance_store不认; - 权重动态热更新需给glance-api做USR1信号重载,不能粗暴重启,否则长连接上传会5xx。
答案
步骤一:准备后端
- Ceph 侧创建专用 pool:
ceph osd pool create glance 128 128 replicated; ceph auth get-or-create client.glance mon 'profile rbd' osd 'profile rbd pool=glance' - Swift 侧创建容器:
openstack container create glance-cold --storage-policy ec104(国内常用EC 10+4降成本)
步骤二:修改 glance-api.conf
[glance_store]
stores = rbd,swift
default_backend = rbd
rbd_store_ceph_conf = /etc/ceph/ceph.conf
rbd_store_user = glance
rbd_store_pool = glance
rbd_store_chunk_size = 8
**rbd_weight = 100**
swift_store_auth_version = 3
swift_store_auth_address = https://swift.internal:5000/v3
swift_store_user = service:glance-swift
swift_store_key = $PASSWORD
swift_store_container = glance-cold
swift_store_region = RegionOne
swift_store_endpoint_type = internalURL
**swift_weight = 30**
步骤三:灰度验证
- 上传测试镜像并强制指定后端:
openstack image create --file cirros.qcow2 --store rbd cirros-on-rbd - 关闭RBD OSD 5台,模拟故障,再上传镜像,观察是否自动落到Swift;
- 通过
glance_store_capabilities接口查看实时权重:curl -s -H "X-Auth-Token: $TOKEN" http://glance-api:9292/v2/info/stores | jq '.stores[]|.weight'
步骤四:权重公式(生产经验)
weight_rbd = 100 + (100 - 平均延迟ms) * 2 – 已用容量%
weight_swift = 30 – 已用容量% / 2
每30秒由cron+ceph-exporter+swift-stat采集,写入glance-weight.conf,发USR1重载,实现动态优先级。
回滚方案:
若Swift出现EC重建风暴,立即把swift_weight手动置0,下发systemctl kill -s USR1 openstack-glance-api,30秒内新上传全部回到RBD,老镜像读流量仍由Swift承担,业务零中断。
拓展思考
- 多Region场景:
北京Region用SSD池,weight基线120;内蒙Region用SATA池,weight基线80。如何让用户就近读取?
答:在glance-cache-management里开启backend_location_metadata,把region_az写入镜像location,配合Nova的nova.scheduler.weights.all_weighers=io_ops_weight,affinity_weight,**glance_region_weight**,实现计算跟随镜像,减少跨Region 40%流量。 - 计费维度:
国内金融云要求按存储类型出账,Glance本身不记录backend维度用量。可写middleware拦截PUT/GET,把store_identifier写入Ceilometer的image.size额外字段,由cloudkitty按rbd-ssd、rbd-sata、swift-ec分别定价,精度±1 KB。 - 未来演进:
如果集群已全面容器化(Kolla-ansible),可把权重更新逻辑做成sidecar,通过ConfigMap+inotify同步到glance-api容器,无需USR1,实现GitOps级回滚。