Ceph系列第五期:Ceph 对象存储(RADOS Gateway)精讲
本期目标
- 理解对象存储的核心概念及与块存储、文件存储的区别
- 掌握 RADOS Gateway(RGW)的架构与核心组件(realm, zonegroup, zone)
- 学会部署 RGW 服务并配置多站点复制
- 熟练使用 S3 和 Swift API 访问对象存储
- 管理 RGW 用户、子用户及访问密钥
- 配置用户、桶和全局配额
前置要求:已有一个健康的 Ceph 集群(参考第二期),并了解基本的 Cephx 认证(第三期)。
1. 对象存储简介
1.1 什么是对象存储?
对象存储将数据存储为对象,每个对象包含三个部分:
- Key(键):对象的唯一标识(类似文件名)
- Data(数据):文件的实际内容
- Metadata(元数据):描述对象的属性(如 Content-Type、自定义键值对)
与块存储和文件存储的对比:
| 特点 | 块存储(RBD) | 文件存储(CephFS) | 对象存储(RGW) |
|---|---|---|---|
| 数据组织 | 固定大小的块 | 层级目录树 | 扁平桶+对象 |
| 访问协议 | SCSI、iSCSI | NFS、POSIX | HTTP(S) (S3/Swift) |
| 适合场景 | 数据库、虚拟机 | 共享文件、HPC | 海量非结构化数据、备份、静态网站 |
| 扩展性 | 受限于单个卷大小 | 可扩展,有元数据瓶颈 | 几乎无限扩展 |
1.2 对象存储的典型特征
- 扁平命名空间 :没有嵌套目录,对象直接存放在**桶(Bucket)或容器(Container)**中
- 通过 RESTful API 访问:使用 HTTP/HTTPS 协议(GET/PUT/DELETE)
- 高扩展性:轻松扩展到 PB/EB 级别
- 元数据丰富:可自定义系统元数据和用户元数据
1.3 Ceph 对象存储支持的 API
- Amazon S3 兼容:大多数子集兼容,使用"桶"和"对象"
- OpenStack Swift 兼容:使用"容器"和"对象"
2. RADOS Gateway 架构
2.1 核心组件
text
HTTP 客户端(S3/Swift)
│
▼
┌───────────────────┐
│ RadosGW 守护进程 │ ← Beast HTTP 前端(端口 80/8080)
└─────────┬─────────┘
│ librados
▼
┌───────────────────┐
│ RADOS │
└───────────────────┘
- RADOS Gateway(radosgw):基于 librados 构建的 HTTP 服务,负责处理 S3/Swift API 请求,并将对象数据存储到底层 RADOS 池中。
- 多站点支持:可在多个 Ceph 集群间异步复制数据,实现跨地域灾备。
2.2 多站点概念(Realm / Zonegroup / Zone)
为了实现数据同步和命名空间隔离,Ceph 引入了三级逻辑结构:
| 概念 | 说明 | 类比 |
|---|---|---|
| Realm(域) | 全局唯一的命名空间,包含一个或多个 Zonegroup。 | 一个独立的存储系统 |
| Zonegroup(区域组) | 由一个或多个 Zone 组成,数据在 Zonegroup 内复制。 | 数据中心集群 |
| Zone(区域) | 一个具体的 Ceph 集群,包含一组 RGW 实例。 | 物理集群 |
| Period(时期) | 某一时刻 Realm/Zonegroup/Zone 配置的快照。 | 配置版本 |
| Epoch(时期号) | Period 内的配置变更序号。 | 版本号 |
- 主 Zone:在 Zonegroup 中只有一个主 Zone,负责处理所有元数据操作(创建/删除用户、桶等)。其他为次要 Zone。
- 主 Zonegroup:在 Realm 中只有一个主 Zonegroup。
同步方向:主 Zone 的元数据和数据会复制到次要 Zone;次要 Zone 的数据也会复制回主 Zone(在双向模式下)。
2.3 RGW 使用的 RADOS 池
一个典型的 RGW 部署会创建多个池(以 default 区域为例):
| 池名 | 用途 |
|---|---|
default.rgw.control |
控制信息 |
default.rgw.meta |
用户、桶等元数据 |
default.rgw.log |
日志 |
default.rgw.buckets.index |
桶索引(存储桶内对象列表) |
default.rgw.buckets.data |
实际对象数据 |
生产环境建议根据负载使用不同性能的存储类(如 SSD 存索引,HDD 存数据)。
3. 部署 RGW 单站点
3.1 创建 Realm、Zonegroup 和 Zone
使用 radosgw-admin 命令行工具配置。
bash
# 1. 创建 realm,并设置为默认
radosgw-admin realm create --rgw-realm=webapp --default
# 2. 创建 zonegroup,设置为 master 和默认
radosgw-admin zonegroup create --rgw-realm=webapp --rgw-zonegroup=video --master --default
# 3. 创建 zone,设置为 master 和默认
radosgw-admin zone create --rgw-realm=webapp --rgw-zonegroup=video --rgw-zone=storage1 --master --default
# 4. 提交配置(生成新的 period)
radosgw-admin period update --rgw-realm=webapp --commit
3.2 部署 RGW 服务(使用 cephadm)
bash
# 在 ceph1~ceph3 上部署 3 个 rgw 实例,端口 8080,关联上面创建的 realm 和 zone
ceph orch apply rgw webapp \
--placement="3 ceph1.whisky.cloud ceph2.whisky.cloud ceph3.whisky.cloud" \
--realm=webapp --zone=storage1 --port=8080
3.3 验证 RGW 服务
bash
# 查看服务状态
ceph orch ls rgw
# 查看具体 daemon
ceph orch ps --daemon-type rgw
# 测试 HTTP 访问
curl http://ceph1.whisky.cloud:8080
正常返回 XML 内容(<ListAllMyBucketsResult>)表示 RGW 已就绪。
3.4 使用服务规格文件(YAML)部署
bash
cat > rgw_service.yaml << EOF
service_type: rgw
service_id: webapp
placement:
count: 3
hosts:
- ceph1.whisky.cloud
- ceph2.whisky.cloud
- ceph3.whisky.cloud
spec:
rgw_frontend_port: 8080
EOF
ceph orch apply -i rgw_service.yaml
3.5 删除 RGW 服务
bash
# 删除服务(不会删除池中的数据)
ceph orch rm rgw.webapp
4. 管理 RGW 用户
对象存储有自己的用户数据库,独立于 Cephx 系统。通过 radosgw-admin 管理。
4.1 创建用户
bash
# 创建 S3 用户(自动生成 access_key 和 secret_key)
radosgw-admin user create --uid=s3user --display-name="S3 User"
# 创建时指定 access_key 和 secret_key
radosgw-admin user create --uid=operator --display-name="S3 Operator" \
--email=operator@example.com --access-key=12345 --secret-key=67890
4.2 查看和列出用户
bash
radosgw-admin user list
radosgw-admin user info --uid=s3user
4.3 重新生成密钥
bash
# 重新生成 secret_key
radosgw-admin key create --uid=s3user --gen-secret
# 为现有用户添加第二个 access_key
radosgw-admin key create --uid=s3user --gen-access-key
4.4 删除访问密钥
bash
radosgw-admin key rm --uid=s3user --access-key=<KEY>
4.5 启用/禁用用户
bash
radosgw-admin user suspend --uid=s3user # 禁用
radosgw-admin user enable --uid=s3user # 启用
4.6 修改用户信息
bash
radosgw-admin user modify --uid=s3user --display-name="New Name" --email=new@example.com
4.7 删除用户(同时删除其所有数据)
bash
radosgw-admin user rm --uid=s3user --purge-data
5. 使用 S3 API 访问
5.1 安装 AWS CLI 客户端
在客户端节点(client)上安装 awscli(Python 方式):
bash
# 配置 pip 国内源(可选)
mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
# 安装
pip3 install awscli
5.2 配置 AWS 凭据
bash
aws configure
# 输入 Access Key ID: 12345
# 输入 Secret Access Key: 67890
# Default region name: 直接回车
# Default output format: 直接回车
配置文件保存在 ~/.aws/credentials。也可以使用 profile 方式:
bash
aws configure --profile=ceph
5.3 基本 S3 操作(需指定 endpoint)
bash
# 创建桶
aws --endpoint=http://ceph1.whisky.cloud:8080 s3 mb s3://mybucket
# 列出桶
aws --endpoint=http://ceph1.whisky.cloud:8080 s3 ls
# 上传对象
echo "Hello World" > test.txt
aws --endpoint=http://ceph1.whisky.cloud:8080 s3 cp test.txt s3://mybucket/
# 上传并设置 ACL 为公共读
aws --endpoint=http://ceph1.whisky.cloud:8080 s3 cp test.txt s3://mybucket/ --acl=public-read
# 列出桶内对象
aws --endpoint=http://ceph1.whisky.cloud:8080 s3 ls s3://mybucket/
# 下载对象
aws --endpoint=http://ceph1.whisky.cloud:8080 s3 cp s3://mybucket/test.txt ./test_download.txt
# 删除对象
aws --endpoint=http://ceph1.whisky.cloud:8080 s3 rm s3://mybucket/test.txt
# 删除桶(必须为空)
aws --endpoint=http://ceph1.whisky.cloud:8080 s3 rb s3://mybucket
使用 wget 下载公共对象:
bash
# 对象被设置为 public-read 后可直接通过 HTTP 访问
wget http://ceph1.whisky.cloud:8080/mybucket/test.txt
6. 使用 Swift API 访问
6.1 创建 Swift 子用户
Swift 使用"租户:用户"模型,映射到 RGW 的"主用户:子用户"。
bash
# 创建一个主用户 swift
radosgw-admin user create --uid=swift --display-name="Swift User"
# 创建子用户 swift:swift_rgw,并分配 full 权限
radosgw-admin subuser create --uid=swift --subuser=swift:swift_rgw --access=full
# 生成 Swift 密钥(自动生成 secret_key)
radosgw-admin key create --subuser=swift:swift_rgw --key-type=swift --gen-secret
# 查看生成的 secret_key
radosgw-admin user info --uid=swift
记录下 swift_keys[0].secret_key。
6.2 安装 Swift 客户端
bash
pip3 install python-swiftclient
6.3 配置环境变量
bash
export ST_AUTH=http://ceph1.whisky.cloud:8080/auth/1.0
export ST_USER=swift:swift_rgw
export ST_KEY=<上面记录的 secret_key>
6.4 基本 Swift 操作
bash
# 查看容器(桶)列表
swift list
# 创建容器(相当于 S3 的 bucket)
swift post mycontainer
# 上传对象
echo "Swift content" > swift.txt
swift upload mycontainer swift.txt
# 列出容器内对象
swift list mycontainer
# 下载对象
swift download mycontainer swift.txt
# 删除对象
swift delete mycontainer swift.txt
# 删除容器(必须为空)
swift delete mycontainer
7. 配额管理
支持用户级别、桶级别和全局配额,限制最大对象数或最大字节数。
7.1 用户配额
bash
# 启用用户配额(以 uid=s3user 为例)
radosgw-admin quota enable --quota-scope=user --uid=s3user
# 设置最大对象数为 1024
radosgw-admin quota set --quota-scope=user --uid=s3user --max-objects=1024
# 设置最大容量为 10GB
radosgw-admin quota set --quota-scope=user --uid=s3user --max-size=10G
# 查看用户配额
radosgw-admin user info --uid=s3user | grep -A 10 user_quota
# 禁用配额(设置 max-objects=-1 或 max-size=-1)
radosgw-admin quota set --quota-scope=user --uid=s3user --max-objects=-1
radosgw-admin quota disable --quota-scope=user --uid=s3user
7.2 桶配额
bash
# 启用桶配额
radosgw-admin quota enable --quota-scope=bucket --bucket=mybucket
# 设置桶最大对象数
radosgw-admin quota set --quota-scope=bucket --bucket=mybucket --max-objects=100
# 设置桶最大容量
radosgw-admin quota set --quota-scope=bucket --bucket=mybucket --max-size=1G
# 查看桶配额
radosgw-admin bucket stats --bucket=mybucket
# 禁用桶配额
radosgw-admin quota disable --quota-scope=bucket --bucket=mybucket
7.3 全局配额
作用于所有用户或所有桶(可被具体配额覆盖)。
bash
# 设置所有用户的默认配额
radosgw-admin global quota set --quota-scope=user --max-objects=10000 --max-size=100G
# 启用全局用户配额
radosgw-admin global quota enable --quota-scope=user
# 设置所有桶的默认配额
radosgw-admin global quota set --quota-scope=bucket --max-objects=500 --max-size=10G
radosgw-admin global quota enable --quota-scope=bucket
# 提交配置(重要!)
radosgw-admin period update --commit
# 查看全局配额
radosgw-admin global quota get --quota-scope=user
修改全局配额后必须执行
radosgw-admin period update --commit并重启 RGW 进程。
7.4 查看使用情况
bash
# 查看特定用户使用量
radosgw-admin user stats --uid=s3user
# 查看所有用户汇总
radosgw-admin usage show --show-log-entries=false
# 查看指定日期范围
radosgw-admin usage show --uid=s3user --start-date=2025-01-01 --end-date=2025-12-31
8. 多站点同步配置(基础)
本部分简述如何配置两个集群(主集群 prod,备集群 backup)之间的单向数据同步。更复杂的双向或多区域配置可参考官方文档。
8.1 架构规划
- 主集群(ceph1~3):realm=webapp,zonegroup=video,zone=storage1(主 zone)
- 备集群(ceph4~6):realm=webapp,zonegroup=video,zone=storage2(次要 zone)
两个集群需网络互通,且已分别部署好 Ceph 集群和 RGW(备集群暂不创建本地 realm)。
8.2 主集群配置
bash
# 已部署好 RGW(参考第 3 节),确认运行正常
# 创建同步系统用户(专门用于跨集群同步)
radosgw-admin user create --uid=sync-user --display-name="Sync User" \
--access-key=syncaccess --secret=syncsecret --system
8.3 备集群配置
bash
# 从主集群拉取 realm 配置
radosgw-admin realm pull --url=http://ceph1.whisky.cloud:8080 \
--access-key=syncaccess --secret=syncsecret
# 拉取 period
radosgw-admin period pull --url=http://ceph1.whisky.cloud:8080 \
--access-key=syncaccess --secret=syncsecret
# 创建本地 zone(storage2),并指定 endpoints 为备集群的 RGW 地址
radosgw-admin zone create --rgw-zonegroup=video --rgw-zone=storage2 \
--endpoints="http://ceph4.whisky.cloud:8080,http://ceph5.whisky.cloud:8080,http://ceph6.whisky.cloud:8080" \
--access-key=syncaccess --secret=syncsecret
# 更新 period
radosgw-admin period update --commit
# 在备集群部署 RGW
ceph orch apply rgw webapp --placement="3 ceph4.whisky.cloud ceph5.whisky.cloud ceph6.whisky.cloud" \
--realm=webapp --zone=storage2 --port=8080
8.4 验证同步状态
bash
# 在备集群查看同步状态
radosgw-admin sync status
# 在主集群创建桶并上传对象,稍后查看备集群是否同步
9. 常用命令速查表
| 操作 | 命令 |
|---|---|
| 创建 realm | radosgw-admin realm create --rgw-realm=<name> --default |
| 创建 zonegroup | radosgw-admin zonegroup create --rgw-realm=<realm> --rgw-zonegroup=<name> --master --default |
| 创建 zone | radosgw-admin zone create --rgw-realm=<realm> --rgw-zonegroup=<zonegroup> --rgw-zone=<name> --master --default |
| 更新 period | radosgw-admin period update --commit |
| 部署 RGW | ceph orch apply rgw <svc_id> --placement=<placement> --realm=<realm> --zone=<zone> --port=<port> |
| 创建 S3 用户 | radosgw-admin user create --uid=<uid> --display-name=<name> |
| 创建子用户(Swift) | radosgw-admin subuser create --uid=<uid> --subuser=<uid:sub> --access=full |
| 生成 Swift 密钥 | radosgw-admin key create --subuser=<uid:sub> --key-type=swift --gen-secret |
| 启用用户配额 | radosgw-admin quota enable --quota-scope=user --uid=<uid> |
| 设置用户配额 | radosgw-admin quota set --quota-scope=user --uid=<uid> --max-objects=<num> --max-size=<bytes> |
| 查看用户信息 | radosgw-admin user info --uid=<uid> |
| 删除用户及数据 | radosgw-admin user rm --uid=<uid> --purge-data |
| 拉取 realm(备集群) | radosgw-admin realm pull --url=<master_rgw_url> --access-key=<key> --secret=<secret> |
| 查看同步状态 | radosgw-admin sync status |
10. 本期小结
- Ceph 对象存储通过 RGW 守护进程提供兼容 S3 和 Swift 的 RESTful API。
- 核心逻辑结构:Realm(域) → Zonegroup(区域组) → Zone(区域),用于多站点复制和命名空间隔离。
- 使用
radosgw-admin管理用户、桶、配额和同步配置。 - S3 客户端(awscli)和 Swift 客户端(python-swiftclient)均可访问,需指定 endpoint 和认证信息。
- 配额支持用户级、桶级和全局级,可限制对象数量或存储容量,修改后需执行
period update。 - 多站点同步通过系统用户进行身份验证,备集群需要从主集群拉取 realm 和 period。
下一期预告:Ceph 文件系统(CephFS)精讲,包括 MDS 管理、多文件系统、内核与 FUSE 客户端挂载、快照及跨集群 Mirror。