HMK's blog

保持思考|00后|等待


  • Home
  • Archive
  • Tags
  •   

© 2026 Hekang

Theme Typography by Makito

Proudly published with Hexo

Ceph-RBD

Posted at 2026-08-17 Storage 

一、Ceph RBD 使用详解

1.1 RBD

Ceph 可以同时提供 RADOSGW(对象存储网关)、RBD(块存储)、Ceph FS(文件系统存储),RBD即 RADOS Block Device 的简称,RBD 块存储是常用的存储类型之一,RBD 块设备类似磁盘可以被挂载,RBD 块设备具有快照、多副本、克隆和一致性等特性,数据以条带化的方式存储在 Ceph 集群的多个 OSD 中。

条带化技术就是一种自动的将 I/O 的负载均衡到多个物理磁盘上的技术,条带化技术就是将一块连续的数据分成很多小部分并把他们分别存储到不同磁盘上去。这就能使多个进程同时访问数据的多个不同部分而不会造成磁盘冲突,而且在需要对这种数据进行顺序访问的时候可以获得最大程度上的 I/O 并行能力,从而获得非常好的性能。

1.2:创建存储池

1
2
3
4
5
6
7
8
# ceph osd pool create rbd1 32 32
ceph osd pools

# 存储池启用rbd
ceph osd pool application enbale rbd1 rbd

# 初始化rbd
rbd pool init -p rbd1

删除存储池

1
2
3
4
5
6
7
8
9
# 删除存储池
root@ceph-node1:/data/ceph# ceph osd pool rm myssdpool myssdpool --yes-i-really-really-mean-it
Error EPERM: pool deletion is disabled; you must first set the mon_allow_pool_delete config option to true before you can destroy a pool

# 设置mon 可以删除 删除完复原
root@ceph-node1:/data/ceph# ceph tell mon.* injectargs --mon_allow_pool_delete=true
# 删除 存储池
root@ceph-node1:/data/ceph# ceph osd pool rm myssdpool myssdpool --yes-i-really-really-mean-it
pool 'myssdpool' removed

1.3 创建 img 镜像

rbd 存储池并不能直接用于块设备,而是需要事先在其中按需创建映像(image),并把映像文件作为块设备使用。rbd 命令可用于创建、查看及删除块设备相在的映像(image),以及克隆映像、创建快照、将映像回滚到快照和查看快照等管理操作。例如,下面的命令能够在指定的 RBD 即 rbd1 创建一个名为 myimg1 的映像:

1.3.1 命令格式

1
rbd help create 

1.3.2 创建镜像

1
2
3
4
5
6
7
8
# 创建镜像
rbd create data-img1 --size 3G --pool rbd1 --image-format 2 --image-feature layering

# 验证镜像
rbd ls --pool rbd1

# 列出镜像信息
rbd ls --pool rbd1 ls

1.3.3 查看镜像详细信息

1
2
rbd --image data-img1 --pool rbd1 info

以json格式显示镜像信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@ceph-node1:/data/ceph# rbd ls -p myrbd1 -l --format json --pretty-format
[
{
"image": "myimg1",
"id": "401a185ec2d1",
"size": 5368709120,
"format": 2,
"lock_type": "exclusive"
},
{
"image": "myimg2",
"id": "40204ea9c7e8",
"size": 3221225472,
"format": 2
}
]

1.3.4 镜像的其他特性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
rbd help feature enable
usage: rbd feature enable [--pool <pool>] [--image <image>]
[--journal-splay-width <journal-splay-width>]
[--journal-object-size <journal-object-size>]
[--journal-pool <journal-pool>]
<image-spec> <features> [<features> ...]
Enable the specified image feature.
Positional arguments
<image-spec>
image specification
(example: [<pool-name>/]<image-name>)
<features>
image features
[exclusive-lock, object-map, fast-diff, journaling]
Optional arguments-p [--pool ] arg
pool name--image arg
image name--journal-splay-width arg number of active journal objects--journal-object-size arg size of journal objects [4K <= size <= 64M]--journal-pool arg
pool for journal objects

#特性简介
layering: 支持镜像分层快照特性,用于快照及写时复制,可以对image创建快照并保护,然后从快照克隆出新的image出来,父子image之间采用COW技术,共享对象数据。
striping: 支持条带化 v2,类似raid0,只不过在ceph环境中的数据被分散到不同的对象中,可改善顺序读写场景较多情况下的性能。
exclusive-lock: 支持独占锁,限制一个镜像只能被一个客户端使用。
object-map: 支持对象映射(依赖exclusive-lock),加速数据导入导出及已用空间统计等,此特性开启的时候,会记录image所有对象的一个位图,用以标记对象是否真的存在,在一些场景下可以加速io。
fast-diff: 快速计算镜像与快照数据差异对比(依赖 object-map)。
deep-flatten: 支持快照扁平化操作,用于快照管理时解决快照依赖关系等。
journaling: 修改数据是否记录日志,该特性可以通过记录日志并通过日志恢复数据(依赖独占锁),开启此特性会增加系统磁盘IO使用。
jewel 默认开启的特性包括:layering/exlcusivelock/object map/fast diff/deep flatten

1.3.5 镜像特性的启用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# rbd feature enable exclusive-lock --pool rbd1 --image data-img1
# rbd feature enable object-map --pool rbd1 --image data-img1
# rbd feature enable fast-diff --pool rbd1 --image data-img1

#验证镜像特性:
$ rbd--image data-img1--pool rbd-data1 info
rbd image 'data-img1':
size 3 GiB in 768 objects
order 22 (4 MiB objects)
id: d45b6b8b4567
block_name_prefix: rbd_data.d45b6b8b4567
format: 2
features: layering, exclusive-lock, object-map, fast-diff
op_features:
flags: object map invalid, fast diff invalid

1.3.6 镜像特性的禁用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#禁用指定存储池中指定镜像的特性:
$ rbd feature disable fast-diff--pool rbd1--image data-img1
#验证镜像特性:
$ rbd--image data-img1--pool rbd1 info
rbd image 'data-img1':
size 3 GiB in 768 objects
order 22 (4 MiB objects)
id: d45b6b8b4567
block_name_prefix: rbd_data.d45b6b8b4567
format: 2
features: layering, exclusive-lock, object-map #少了一个 fast-diff 特性
op_features:
flags: object map invalid

1.4 配置客户端使用RBD

客户端挂载RBD,并分别使用admin及普通用户挂载RBD并验证使用。

客户端要想挂载使用cephRBD,需要安装ceph客户端组件ceph-common

客户端使用admin 账户挂载并使用RBD

同步 admin 账户认证文件:

1
scp ceph.conf ceph.client.admin.keyring root@10.10.0.232:/etc/ceph/

客户端映射映像

1
2
[root@ceph-client ~]# rbd help map #映射 rbd 命令
[root@ceph-client ~]# rbd -p rbd1 map data-img1

在客户端格式化rbd并挂载

1
2
3
[root@ceph-client ~]# mkfs.xfs /dev/rbd0
[root@ceph-client ~]# mkdir /data /data1-p
[root@ceph-client ~]# mount /dev/rbd0 /data

1.5 客户端使用普通账户挂载并使用RBD

1.5.1 创建普通账户并授权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#创建普通账户
root@ceph-node1:~# ceph auth add client.hmk mon 'allow r' osd 'allow rwx pool=myrbd1'
added key for client.hmk

#验证用户信息
[cephadmin@ceph-deploy ceph-cluster]$ ceph auth get client.hmk
[client.hmk]
key = AQA2b4Jqhi6MARAAS5jRA3uqB8U61mtkXDWxew==
caps mon = "allow r"
caps osd = "allow rwx pool=myrbd1"

root@ceph-node1:~# cat ceph.client.hmk.keyring
[client.hmk]
key = AQA2b4Jqhi6MARAAS5jRA3uqB8U61mtkXDWxew==
caps mon = "allow r"
caps osd = "allow rwx pool=myrbd1"
# 同步普通用户认证文件:
root@ceph-node1:~# scp ceph.client.hmk.keyring 10.10.0.233:/etc/ceph/
root@10.10.0.233's password:
ceph.client.hmk.keyring

1.5.2 客户端验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@client:~# ceph --user hmk -s
cluster:
id: 32e044ec-8fb1-11f1-a636-000c29b8ae8b
health: HEALTH_OK

services:
mon: 3 daemons, quorum ceph-node1,ceph-node2,ceph-node3 (age 16m) [leader: ceph-node1]
mgr: ceph-node1.axywbf(active, since 15m), standbys: ceph-node2.zuwijg
mds: 1/1 daemons up, 1 standby
osd: 6 osds: 6 up (since 15m), 6 in (since 11d)
rgw: 2 daemons active (2 hosts, 1 zones)

data:
volumes: 1/1 healthy
pools: 8 pools, 289 pgs
objects: 577 objects, 1.0 GiB
usage: 3.3 GiB used, 117 GiB / 120 GiB avail
pgs: 289 active+clean

io:
client: 170 B/s rd, 0 op/s rd, 0 op/s wr

1.5.2.1 映射rbd

使用普通账用户权限映射rbd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
root@client:~# rbd --id hmk -p myrbd1 map myimg2
/dev/rbd0
root@client:/# mkfs.ext4 /dev/rbd0
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done
Creating filesystem with 786432 4k blocks and 196608 inodes
Filesystem UUID: a2c8751e-0409-4eb8-82e1-3fb917bfb8e3
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

root@client:/# mount /dev/rbd0 /data/
root@client:/# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 2G 0 part /boot
└─sda3 8:3 0 38G 0 part
└─ubuntu--vg-ubuntu--lv 252:0 0 38G 0 lvm /
sr0 11:0 1 1024M 0 rom
rbd0 251:0 0 3G 0 disk /data
root@client:/# rbd ls -p myrbd1 -l
NAME SIZE PARENT FMT PROT LOCK
myimg1 5 GiB 2 excl
myimg2 3 GiB 2


root@client:/# lsmod | grep ceph
libceph 548864 1 rbd
libcrc32c 12288 3 btrfs,raid456,libceph
root@client:/# modinfo libceph
filename: /lib/modules/6.8.0-137-generic/kernel/net/ceph/libceph.ko.zst
license: GPL
description: Ceph core library
author: Patience Warnick <patience@newdream.net>
author: Yehuda Sadeh <yehuda@hq.newdream.net>
author: Sage Weil <sage@newdream.net>
srcversion: CF5B6D95355BD434E63A3B2
depends: libcrc32c
retpoline: Y
intree: Y
name: libceph

1.5.3 rbd 映像空间拉伸

可以扩展空间不建议缩小空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
root@client:~# rbd ls -p myrbd1 -l
NAME SIZE PARENT FMT PROT LOCK
myimg1 5 GiB 2 excl
myimg2 3 GiB 2
root@client:~# rbd help resize
usage: rbd resize [--pool <pool>] [--namespace <namespace>]
[--image <image>] --size <size> [--allow-shrink]
[--no-progress] [--encryption-format <encryption-format>]
[--encryption-passphrase-file <encryption-passphrase-file>]
<image-spec>

Resize (expand or shrink) image.

Positional arguments
<image-spec> image specification
(example:
[<pool-name>/[<namespace>/]]<image-name>)

Optional arguments
-p [ --pool ] arg pool name
--namespace arg namespace name
--image arg image name
-s [ --size ] arg image size (in M/G/T) [default: M]
--allow-shrink permit shrinking
--no-progress disable progress output
--encryption-format arg encryption format (luks, luks1, luks2)
[default: luks]
--encryption-passphrase-file arg path to file containing passphrase for
unlocking the image

# 拉伸rbd 映像

root@client:~# rbd resize -p myrbd1 --image myimg2 --size 5G
Resizing image: 100% complete...done.


root@client:~# fdisk -l /dev/rbd0
Disk /dev/rbd0: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 65536 bytes / 65536 bytes


root@client:~# resize2fs /dev/rbd0 //ext4
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/rbd0 is mounted on /data; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/rbd0 is now 1310720 (4k) blocks long.

root@client:~# xfs_growfs /data/ //xfs

1.5.4 卸载rbd映像

1
2
root@client:~# umount /data/
root@client:~# rbd --id hmk -p myrbd1 unmap myimg1

1.5.5 删除rbd映像

映像删除后数据也会被删除而且是无法恢复,因此在执行删除操作的时候要谨慎

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@client:~# rbd help rm
usage: rbd rm [--pool <pool>] [--namespace <namespace>] [--image <image>]
[--no-progress]
<image-spec>

Delete an image.

Positional arguments
<image-spec> image specification
(example: [<pool-name>/[<namespace>/]]<image-name>)

Optional arguments
-p [ --pool ] arg pool name
--namespace arg namespace name
--image arg image name
--no-progress disable progress output

root@client:/etc/ceph# rbd -p myrbd1 rm --image myimg1
Removing image: 100% complete...done.

1.5.6 rbd映像像回收站机制

删除的镜像数据无法恢复,但是还有另外一种方法可以先把镜像移动到回收站,后期确认删除的时候再从回收站删除即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 查看映像状态
root@client:/etc/ceph# rbd status --pool myrbd1 --image myimg2
Watchers: none
# 将映像移动到回收站
root@client:/etc/ceph# rbd trash move -p myrbd1 --image myimg2
# 查看回收站映像
root@client:/etc/ceph# rbd trash list -p myrbd1
40204ea9c7e8 myimg2
# 从回收站删除映像
root@client:~# rbd trash remove -p myrbd1 40204ea9c7e8
Removing image: 100% complete...done.

# 还原映像
root@client:~# rbd trash list -p myrbd1
1e60e73819cbd myimg1
root@client:~# rbd trash restore -p myrbd1 --image myimg1 --image-id 1e60e73819cbd
root@client:~# rbd ls -p myrbd1
myimg1

1.5.7 映像快照

1
2
3
4
5
6
7
8
9
10
11
rbd 
snap create (snap add) #创建快照
snap limit clear #清除镜像的快照数量限制
snap limit set #设置一个镜像的快照上限
snap list (snap ls) #列出快照
snap protect #保护快照被删除
snap purge #删除所有未保护的快照
snap remove (snap rm) #删除一个快照
snap rename #重命名快照
snap rollback (snap revert) #还原快照
snap unprotect #允许一个快照被删除(取消快照保护)
1
2
3
4
5
6
7
8
# 创建快照
root@client:~# rbd snap create -p myrbd1 --image myimg1 --snap myimg1-snap20260817
Creating snap: 100% complete...done.
# 验证快照
root@client:~# rbd snap list -p myrbd1 --image myimg1
SNAPID NAME SIZE PROTECTED TIMESTAMP
3 myimg1-snap20260817 3 GiB Mon Aug 17 03:15:10 2026

1.5.8 删除数据还原快照

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#客户端删除数据
[root@ceph-client2 ~]# rm -rf /data/passwd
#验证数据
[root@ceph-client2 ~]# ll /data/
total 1544
drwx------ 2 root root 16384 Dec 15 11:17 lost+found
-rw------- 1 root root 1563590 Dec 15 14:27 messages
#卸载 rbd
[root@ceph-client2 ~]# umount /data
[root@ceph-client2 ~]# rbd unmap /dev/rbd0

#回滚快照
root@client:~# rbd snap rollback -p myrbd1 --image myimg1 --snap myimg1-snap20260817
Rolling back to snapshot: 100% complete...done.

1.5.9 删除快照

1
2
3
4
5
6
7
# 删除指定快照
root@client:~# rbd snap remove -p myrbd1 --image myimg1 --snap myimg1-snap20260817
Removing snap: 100% complete...done.
# 验证
root@client:~# rbd ls -p myrbd1
myimg1
root@client:~# rbd snap list -p myrbd1 --image myimg1

快照数量限制

1
2
3
4
5
# 设置与修改快照数量限制

root@client:~# rbd snap limit set -p myrbd1 --image myimg1 --limit 30
root@client:~# rbd snap limit set -p myrbd1 --image myimg1 --limit 15
root@client:~# rbd snap limit clear -p myrbd1 --image myimg1

Share 

 Next post: CephX 认证机制 

© 2026 Hekang

Theme Typography by Makito

Proudly published with Hexo