CRUSH映射图及自定义规则实战篇

一.PG与OSD映射调整

1.ceph集群运行图类型及算法

ceph集群中由mon服务器维护的5种运行图:
    monitor map:
        监视器运行图。
    OSD map:
        OSD运行图
    PG map:
        PG运行图。
    Crush map:
        crush全称为:Controllers Replication Under Scalable Hashing
        表示可控的,可复制的,可伸缩的一致性hash算法。
        crush 运行图,当新建存储池时会基于 OSD map创建出新的PG组合列表用于存储数据。

    MDS map:
        cephFS metadata运行图。

crush算法针对目的节点的选择:
	目前有5种算法来实现节点的选择,包括Uniform,List,Tree,Straw,Straw2。

早期版本使用的是ceph项目的发起者发明的Straw,目前已经发展社区优化的Straw2版本。

straw抽签算法:
	抽签是指挑取一个最长的签,而这个签值就是OSD的权重,当创建存储池的时候会向PG分配OSD。
	straw算法会遍历当前可用的OSD并优先使用中签的OSD,以让权重高的OSD被分配较多的PG以存储更多的数据。

2.PG与OSD映射调整概述

默认情况下,crush算法自行对创建的pool中的PG分配OSD,但是可以手动基于权重设置crush算法分配数据的倾向性。

比如1T磁盘权重是1,2T的权重为2,为了避免需要做这样权重的修改,推荐使用相同大小的设备。

生产环境中,我们可以基于WEIGHT和REWEIGHT实现数据均衡的效果。

3.查看osd使用状态

[root@ceph141 ~]# ceph osd df  # 注意观察"%USE"和"PGS"的区别
ID CLASS WEIGHT  REWEIGHT SIZE    RAW USE DATA    OMAP   META     AVAIL   %USE VAR  PGS STATUS 
 0   hdd 0.19530  1.00000 200 GiB 1.2 GiB 252 MiB    0 B    1 GiB 199 GiB 0.62 1.66 103     up 
 1   hdd 0.19530  1.00000 200 GiB 1.2 GiB 178 MiB    0 B    1 GiB 199 GiB 0.59 1.56  83     up 
 2   hdd 0.48830  1.00000 500 GiB 1.3 GiB 312 MiB    0 B    1 GiB 499 GiB 0.26 0.69 188     up 
 3   hdd 0.48830  1.00000 500 GiB 1.5 GiB 556 MiB    0 B    1 GiB 498 GiB 0.31 0.82 218     up 
 4   hdd 0.19530  1.00000 200 GiB 1.2 GiB 158 MiB  9 KiB 1024 MiB 199 GiB 0.58 1.53  90     up 
 5   hdd 0.19530  1.00000 200 GiB 1.2 GiB 211 MiB    0 B    1 GiB 199 GiB 0.60 1.60  96     up 
 6   hdd 0.48830  1.00000 500 GiB 1.5 GiB 467 MiB  8 KiB 1024 MiB 499 GiB 0.29 0.77 201     up 
 7   hdd 0.48830  1.00000 500 GiB 1.5 GiB 462 MiB  4 KiB 1024 MiB 499 GiB 0.29 0.77 205     up 
 8   hdd 0.19530  1.00000 200 GiB 1.2 GiB 184 MiB    0 B    1 GiB 199 GiB 0.59 1.57 100     up 
 9   hdd 0.19530  1.00000 200 GiB 1.2 GiB 246 MiB    0 B    1 GiB 199 GiB 0.62 1.65  83     up 
10   hdd 0.48830  1.00000 500 GiB 1.5 GiB 511 MiB    0 B    1 GiB 498 GiB 0.30 0.80 205     up 
11   hdd 0.48830  1.00000 500 GiB 1.3 GiB 358 MiB    0 B    1 GiB 499 GiB 0.27 0.72 204     up 
                    TOTAL 4.1 TiB  16 GiB 3.8 GiB 21 KiB   12 GiB 4.1 TiB 0.38                 
MIN/MAX VAR: 0.69/1.66  STDDEV: 0.17
[root@ceph141 ~]# 

4.修改WEIGHT并验证

[root@ceph141 ~]# ceph osd crush reweight osd.2 1.5  # 修改权重后会触发数据的重新分配。
reweighted item id 2 name 'osd.2' to 1.5 in crush map
[root@ceph141 ~]# 
[root@ceph141 ~]# ceph -s
  cluster:
    id:     a0f5dd38-4b16-48a7-9618-556f924b4c5d
    health: HEALTH_WARN
            Degraded data redundancy: 215/1728 objects degraded (12.442%), 62 pgs degraded

  services:
    mon: 3 daemons, quorum ceph141,ceph142,ceph143 (age 29h)
    mgr: ceph141(active, since 5h), standbys: ceph143, ceph142
    mds: oldboyedu-linux-cephfs:2 {0=ceph142=up:active,1=ceph143=up:active} 1 up:standby
    osd: 12 osds: 12 up (since 13h), 12 in (since 13h); 18 remapped pgs
    rgw: 3 daemons active (ceph141, ceph142, ceph143)

  task status:

  data:
    pools:   12 pools, 592 pgs
    objects: 576 objects, 1.2 GiB
    usage:   16 GiB used, 4.1 TiB / 4.1 TiB avail
    pgs:     215/1728 objects degraded (12.442%)
             52/1728 objects misplaced (3.009%)
             523 active+clean
             46  active+recovery_wait+degraded
             16  active+recovery_wait+undersized+degraded+remapped
             3   active+recovery_wait+remapped
             2   active+recovery_wait
             1   active+recovering+undersized+remapped
             1   active+recovering

  io:
    recovery: 4.0 MiB/s, 0 objects/s

[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd df   # 注意观察"%USE"和"PGS"的区别
ID CLASS WEIGHT  REWEIGHT SIZE    RAW USE DATA    OMAP   META     AVAIL   %USE VAR  PGS STATUS 
 0   hdd 0.19530  1.00000 200 GiB 1.1 GiB 126 MiB    0 B    1 GiB 199 GiB 0.56 1.49  62     up 
 1   hdd 0.19530  1.00000 200 GiB 1.1 GiB  90 MiB    0 B    1 GiB 199 GiB 0.54 1.44  51     up 
 2   hdd 1.50000  1.00000 500 GiB 1.7 GiB 748 MiB    0 B    1 GiB 498 GiB 0.35 0.92 351     up 
 3   hdd 0.48830  1.00000 500 GiB 1.3 GiB 353 MiB    0 B    1 GiB 499 GiB 0.27 0.71 128     up 
 4   hdd 0.19530  1.00000 200 GiB 1.2 GiB 166 MiB  9 KiB 1024 MiB 199 GiB 0.58 1.54  93     up 
 5   hdd 0.19530  1.00000 200 GiB 1.2 GiB 225 MiB    0 B    1 GiB 199 GiB 0.61 1.62  96     up 
 6   hdd 0.48830  1.00000 500 GiB 1.5 GiB 508 MiB  8 KiB 1024 MiB 498 GiB 0.30 0.79 202     up 
 7   hdd 0.48830  1.00000 500 GiB 1.4 GiB 418 MiB  4 KiB 1024 MiB 499 GiB 0.28 0.75 201     up 
 8   hdd 0.19530  1.00000 200 GiB 1.2 GiB 169 MiB    0 B    1 GiB 199 GiB 0.58 1.54 103     up 
 9   hdd 0.19530  1.00000 200 GiB 1.3 GiB 258 MiB    0 B    1 GiB 199 GiB 0.63 1.66  87     up 
10   hdd 0.48830  1.00000 500 GiB 1.5 GiB 473 MiB    0 B    1 GiB 499 GiB 0.29 0.77 203     up 
11   hdd 0.48830  1.00000 500 GiB 1.4 GiB 418 MiB    0 B    1 GiB 499 GiB 0.28 0.75 199     up 
                    TOTAL 4.1 TiB  16 GiB 3.9 GiB 21 KiB   12 GiB 4.1 TiB 0.38                 
MIN/MAX VAR: 0.71/1.66  STDDEV: 0.16
[root@ceph141 ~]# 

5.修改REWEIGHT并验证

OSD的REWEIGHT的值默认为1,值可以调整,范围在0~1之间,值越低PG越小。

如果调整了任何一个OSD的REWEIGHT值,那么OSD的PG会立即和其他OSD进行重新平衡,即数据的重新分配。

用于某个OSD的PG相对较多需要降低其PG数量的场景。


实操案例:
[root@ceph141 ~]# ceph osd reweight 9 0.8
reweighted osd.9 to 0.8 (cccc)
[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd df  # 注意观察"%USE"和"PGS"的区别
ID CLASS WEIGHT  REWEIGHT SIZE    RAW USE DATA    OMAP   META     AVAIL   %USE VAR  PGS STATUS 
 0   hdd 0.19530  1.00000 200 GiB 1.1 GiB 126 MiB    0 B    1 GiB 199 GiB 0.56 1.49  62     up 
 1   hdd 0.19530  1.00000 200 GiB 1.1 GiB  90 MiB    0 B    1 GiB 199 GiB 0.54 1.44  51     up 
 2   hdd 1.50000  1.00000 500 GiB 1.7 GiB 748 MiB    0 B    1 GiB 498 GiB 0.35 0.92 351     up 
 3   hdd 0.48830  1.00000 500 GiB 1.3 GiB 353 MiB    0 B    1 GiB 499 GiB 0.27 0.71 128     up 
 4   hdd 0.19530  1.00000 200 GiB 1.2 GiB 166 MiB  9 KiB 1024 MiB 199 GiB 0.58 1.54  93     up 
 5   hdd 0.19530  1.00000 200 GiB 1.2 GiB 225 MiB    0 B    1 GiB 199 GiB 0.61 1.62  96     up 
 6   hdd 0.48830  1.00000 500 GiB 1.5 GiB 508 MiB  8 KiB 1024 MiB 498 GiB 0.30 0.79 202     up 
 7   hdd 0.48830  1.00000 500 GiB 1.4 GiB 418 MiB  4 KiB 1024 MiB 499 GiB 0.28 0.75 201     up 
 8   hdd 0.19530  1.00000 200 GiB 1.2 GiB 169 MiB    0 B    1 GiB 199 GiB 0.58 1.54 103     up 
 9   hdd 0.19530  1.00000 200 GiB 1.3 GiB 258 MiB    0 B    1 GiB 199 GiB 0.63 1.66  87     up 
10   hdd 0.48830  1.00000 500 GiB 1.5 GiB 473 MiB    0 B    1 GiB 499 GiB 0.29 0.77 203     up 
11   hdd 0.48830  1.00000 500 GiB 1.4 GiB 418 MiB    0 B    1 GiB 499 GiB 0.28 0.75 199     up 
                    TOTAL 4.1 TiB  16 GiB 3.9 GiB 21 KiB   12 GiB 4.1 TiB 0.38                 
MIN/MAX VAR: 0.71/1.66  STDDEV: 0.16
[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd reweight 9 0.8
reweighted osd.9 to 0.8 (cccc)
[root@ceph141 ~]# 
[root@ceph141 ~]# ceph -s
  cluster:
    id:     a0f5dd38-4b16-48a7-9618-556f924b4c5d
    health: HEALTH_OK

  services:
    mon: 3 daemons, quorum ceph141,ceph142,ceph143 (age 29h)
    mgr: ceph141(active, since 5h), standbys: ceph143, ceph142
    mds: oldboyedu-linux-cephfs:2 {0=ceph142=up:active,1=ceph143=up:active} 1 up:standby
    osd: 12 osds: 12 up (since 13h), 12 in (since 13h)
    rgw: 3 daemons active (ceph141, ceph142, ceph143)

  task status:

  data:
    pools:   12 pools, 592 pgs
    objects: 576 objects, 1.2 GiB
    usage:   16 GiB used, 4.1 TiB / 4.1 TiB avail
    pgs:     592 active+clean

  io:
    recovery: 2.6 MiB/s, 2 objects/s

[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd df  # 注意观察"%USE"和"PGS"的区别
ID CLASS WEIGHT  REWEIGHT SIZE    RAW USE DATA    OMAP   META     AVAIL   %USE VAR  PGS STATUS 
 0   hdd 0.19530  1.00000 200 GiB 1.1 GiB 127 MiB    0 B    1 GiB 199 GiB 0.56 1.49  62     up 
 1   hdd 0.19530  1.00000 200 GiB 1.1 GiB  91 MiB    0 B    1 GiB 199 GiB 0.54 1.44  51     up 
 2   hdd 1.50000  1.00000 500 GiB 1.7 GiB 749 MiB    0 B    1 GiB 498 GiB 0.35 0.92 351     up 
 3   hdd 0.48830  1.00000 500 GiB 1.3 GiB 354 MiB    0 B    1 GiB 499 GiB 0.27 0.71 128     up 
 4   hdd 0.19530  1.00000 200 GiB 1.2 GiB 167 MiB  9 KiB 1024 MiB 199 GiB 0.58 1.54  93     up 
 5   hdd 0.19530  1.00000 200 GiB 1.2 GiB 226 MiB    0 B    1 GiB 199 GiB 0.61 1.62  96     up 
 6   hdd 0.48830  1.00000 500 GiB 1.5 GiB 509 MiB  8 KiB 1024 MiB 498 GiB 0.30 0.79 202     up 
 7   hdd 0.48830  1.00000 500 GiB 1.4 GiB 418 MiB  4 KiB 1024 MiB 499 GiB 0.28 0.75 201     up 
 8   hdd 0.19530  1.00000 200 GiB 1.2 GiB 182 MiB    0 B    1 GiB 199 GiB 0.59 1.56 108     up 
 9   hdd 0.19530  0.79999 200 GiB 1.2 GiB 203 MiB    0 B    1 GiB 199 GiB 0.60 1.59  65     up 
10   hdd 0.48830  1.00000 500 GiB 1.5 GiB 485 MiB    0 B    1 GiB 499 GiB 0.29 0.78 211     up 
11   hdd 0.48830  1.00000 500 GiB 1.4 GiB 450 MiB    0 B    1 GiB 499 GiB 0.29 0.76 208     up 
                    TOTAL 4.1 TiB  16 GiB 3.9 GiB 21 KiB   12 GiB 4.1 TiB 0.38                 
MIN/MAX VAR: 0.71/1.62  STDDEV: 0.16
[root@ceph141 ~]# 

二.CRUSH运行图修改案例

1.管理CRUSH运行图思路

通过工具将ceph的crush运行图导出并进行编辑,然后导入。

2.导出crush运行图

导出crush运行图为二进制格式,无法通过文本编辑器直接打开。

使用crushtool工具转换crush运行图为文本格式后才能通过vim等文本工具进行编辑。
	
[root@ceph141 ~]# mkdir -pv /yinzhengjie/data/ceph
[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd getcrushmap -o /yinzhengjie/data/ceph/v1.crushmap
39
[root@ceph141 ~]# 
[root@ceph141 ~]# ll -h /yinzhengjie/data/ceph/v1.crushmap
-rw-r--r-- 1 root root 1.2K Nov 14 23:19 /yinzhengjie/data/ceph/v1.crushmap
[root@ceph141 ~]# 
[root@ceph141 ~]# file /yinzhengjie/data/ceph/v1.crushmap
/yinzhengjie/data/ceph/v1.crushmap: MS Windows icon resource - 16 icons, 1-colors
[root@ceph141 ~]# 

3.将运行图转换为文本

导出的运行图不能直接编辑,需要转换为文本格式在进行查看与编辑。

[root@ceph141 ~]# file /yinzhengjie/data/ceph/v1.crushmap
/yinzhengjie/data/ceph/v1.crushmap: MS Windows icon resource - 16 icons, 1-colors
[root@ceph141 ~]# 
[root@ceph141 ~]# crushtool -d /yinzhengjie/data/ceph/v1.crushmap > /yinzhengjie/data/ceph/v1.txt
[root@ceph141 ~]# 
[root@ceph141 ~]# file /yinzhengjie/data/ceph/v1.txt
/yinzhengjie/data/ceph/v1.txt: ASCII text
[root@ceph141 ~]# 
[root@ceph141 ~]# wc -l /yinzhengjie/data/ceph/v1.txt
95 /yinzhengjie/data/ceph/v1.txt
[root@ceph141 ~]# 
[root@ceph141 ~]# cat /yinzhengjie/data/ceph/v1.txt
# begin crush map  # 可调整的crush map参数

tunable choose_local_tries 0
tunable choose_local_fallback_tries 0
tunable choose_total_tries 50
tunable chooseleaf_descend_once 1
tunable chooseleaf_vary_r 1
tunable chooseleaf_stable 1
tunable straw_calc_version 1
tunable allowed_bucket_algs 54

# devices  # 当前的OSD设备列表

device 0 osd.0 class hdd
device 1 osd.1 class hdd
device 2 osd.2 class hdd
device 3 osd.3 class hdd
device 4 osd.4 class hdd
device 5 osd.5 class hdd
device 6 osd.6 class hdd
device 7 osd.7 class hdd
device 8 osd.8 class hdd
device 9 osd.9 class hdd
device 10 osd.10 class hdd
device 11 osd.11 class hdd

# types  # 当前支持的bucket类型

type 0 osd  # OSD守护进程,对应到一个磁盘设备
type 1 host  # 一个主机
type 2 chassis  # 刀片服务器的机箱
type 3 rack  # 包含若干个服务器的机柜/机架
type 4 row  # 包含若干个服务器的机柜的一排机柜(一行机柜)
type 5 pdu  # 机柜的介入电源插座
type 6 pod  # 一个机房中若干个小房间
type 7 room  # 包含若干机柜的房间,一个数据中心有好多这样的房间组成
type 8 datacenter  # 一个数据中心或IDC
type 9 zone  # 一个区域,比如AWS宁夏中卫数据中心。
type 10 region   # 全部区域
type 11 root  # bucket分层的最顶部,根。

# buckets

host ceph141 {  # 类型host 名称为ceph141的节点信息
	id -3		# do not change unnecessarily  # Ceph生成的OSD ID,非必要不要改。
	id -4 class hdd		# do not change unnecessarily
	# weight 2.379
	alg straw2  # crush算法,管理OSD角色。
	hash 0	# rjenkins1  # 使用哪个hash算法,0表示选择rjenkins1这种hash算法。
	item osd.0 weight 0.195  # OSD0权重比例,crush会自动根据磁盘空间计算,不同的磁盘空间的权重不一样。
	item osd.1 weight 0.195
	item osd.2 weight 1.500
	item osd.3 weight 0.488
}
host ceph142 {
	id -5		# do not change unnecessarily
	id -6 class hdd		# do not change unnecessarily
	# weight 1.367
	alg straw2
	hash 0	# rjenkins1
	item osd.4 weight 0.195
	item osd.5 weight 0.195
	item osd.6 weight 0.488
	item osd.7 weight 0.488
}
host ceph143 {
	id -7		# do not change unnecessarily
	id -8 class hdd		# do not change unnecessarily
	# weight 1.367
	alg straw2
	hash 0	# rjenkins1
	item osd.8 weight 0.195
	item osd.9 weight 0.195
	item osd.10 weight 0.488
	item osd.11 weight 0.488
}
root default {  # 根的配置
	id -1		# do not change unnecessarily
	id -2 class hdd		# do not change unnecessarily
	# weight 5.113
	alg straw2
	hash 0	# rjenkins1
	item ceph141 weight 2.379
	item ceph142 weight 1.367
	item ceph143 weight 1.367
}

# rules

rule replicated_rule {  # 副本池的默认配置
	id 0
	type replicated
	min_size 1  # 默认的最小副本是1。
	max_size 10  # 默认的最大副本为10。
	step take default  # 基于"root default"定义的主机分配OSD
	step chooseleaf firstn 0 type host  # 选择主机,故障域类型为主机
	step emit  # 弹出配置即返回给客户端。
}

# end crush map

[root@ceph141 ~]# 

4.编辑文本

[root@ceph141 ~]# vim /yinzhengjie/data/ceph/v1.txt
...
rule replicated_rule {
		...
		# 此处我就对这里进行配置修改测试哈!
        max_size 5
}

5.将文本转换为crush格式

[root@ceph141 ~]# crushtool -c /yinzhengjie/data/ceph/v1.txt -o /yinzhengjie/data/ceph/v2.crushmap
[root@ceph141 ~]# 
[root@ceph141 ~]# file /yinzhengjie/data/ceph/v2.crushmap
/yinzhengjie/data/ceph/v2.crushmap: MS Windows icon resource - 8 icons, 1-colors
[root@ceph141 ~]# 

6.导入新的crush

导入的运行图会立即覆盖原有的运行图并立即生效。

[root@ceph141 ~]# ceph osd crush rule dump
[
    {
        "rule_id": 0,
        "rule_name": "replicated_rule",  # 我们只需要观察定义的副本池配置
        "ruleset": 0,
        "type": 1,
        "min_size": 1,
        "max_size": 10,  # 注意,默认的crush运行图的参数是10哈!
        "steps": [
            {
                "op": "take",
                "item": -1,
                "item_name": "default"
            },
            {
                "op": "chooseleaf_firstn",
                "num": 0,
                "type": "host"
            },
            {
                "op": "emit"
            }
        ]
    }
]

[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd setcrushmap -i /yinzhengjie/data/ceph/v2.crushmap  # 应用咱们修改的配置
40
[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd crush rule dump
[
    {
        "rule_id": 0,
        "rule_name": "replicated_rule",
        "ruleset": 0,
        "type": 1,
        "min_size": 1,
        "max_size": 5,  # 注意观察,这里的配置就生效啦!
        "steps": [
            {
                "op": "take",
                "item": -1,
                "item_name": "default"
            },
            {
                "op": "chooseleaf_firstn",
                "num": 0,
                "type": "host"
            },
            {
                "op": "emit"
            }
        ]
    }
]

[root@ceph141 ~]# 

三.CRUSH数据分类管理-将数据分配SSD和HDD案例

1.crush数据分类管理概述

ceph crush算法分配PG的时候可以将PG分配到不同主机的OSD上,以实现以主机为单位的高可用,这也是默认机制。

这种默认机制无法保证不同PG位于不同机柜或者机房的主机,如果要实现基于机柜或者更高级的IDC等方式的数据高可用。

而且也不能实现A项目的数据在SSD,B项目的数据在机械盘,如果想要实现此功能需要导出crush运行图并手动编辑,之后再导入并覆盖原有的crush运行图。

2.导出crush运行图

[root@ceph141 ~]# ceph osd getcrushmap -o /yinzhengjie/data/ceph/v3.crushmap 
40
[root@ceph141 ~]# 

3.将运行图转换为文本

[root@ceph141 ~]# file /yinzhengjie/data/ceph/v3.crushmap
/yinzhengjie/data/ceph/v3.crushmap: MS Windows icon resource - 8 icons, 1-colors
[root@ceph141 ~]# 
[root@ceph141 ~]# crushtool -d /yinzhengjie/data/ceph/v3.crushmap > /yinzhengjie/data/ceph/v3.txt
[root@ceph141 ~]# 
[root@ceph141 ~]# file /yinzhengjie/data/ceph/v3.txt
/yinzhengjie/data/ceph/v3.txt: ASCII text
[root@ceph141 ~]# 

4.添加自定义配置

[root@ceph141 ~]# cat /yinzhengjie/data/ceph/v3.txt
...

# buckets

host ceph141 {
	id -3		# do not change unnecessarily
	id -4 class hdd		# do not change unnecessarily
	# weight 2.378
	alg straw2
	hash 0	# rjenkins1
	# 注意哟,我将osd.0和osd.1的设备并没有添加到我的这个主机,因为我将其用于yinzhengjie-ceph141-ssd自定义的主机啦!
	# item osd.0 weight 0.195
	# item osd.1 weight 0.195
	item osd.2 weight 1.500
	item osd.3 weight 0.488
}



# 在适当位置添加如下配置即可,这个配置根据咱们自己的主机来写即可。

host yinzhengjie-ceph141-ssd {
    # 住两个id字段不要和上面的冲突即可,我就将ceph141的ID后面加了个0,哈哈哈~偷个懒!懒得箱编号啦!
	id -30		# do not change unnecessarily
	id -40 class hdd		# do not change unnecessarily
	# weight 2.378
	alg straw2
	hash 0	# rjenkins1
	item osd.0 weight 0.195
	item osd.1 weight 0.195
	# 注意哟,我将osd.2和osd.3的设备并没有添加到我的这个主机,因为我将其用于ceph141默认的主机啦!
	# item osd.2 weight 1.500
	# item osd.3 weight 0.488
}

host yinzhengjie-ceph142-ssd {
	id -50		# do not change unnecessarily
	id -60 class hdd		# do not change unnecessarily
	# weight 1.366
	alg straw2
	hash 0	# rjenkins1
	item osd.4 weight 0.195
	item osd.5 weight 0.195
	# item osd.6 weight 0.488
	# item osd.7 weight 0.488
}

host yinzhengjie-ceph143-ssd {
	id -70		# do not change unnecessarily
	id -80 class hdd		# do not change unnecessarily
	# weight 1.366
	alg straw2
	hash 0	# rjenkins1
	item osd.8 weight 0.195
	item osd.9 weight 0.195
	# item osd.10 weight 0.488
	# item osd.11 weight 0.488
}

root yinzhengjie-ssd {
    # 注意哟,复制的时候所有的id都不能重复哟!
	id -10		# do not change unnecessarily
	id -20 class hdd		# do not change unnecessarily
	# weight 5.113
	alg straw2
	hash 0	# rjenkins1
	item yinzhengjie-ceph141-ssd weight 0.488
	item yinzhengjie-ceph142-ssd weight 0.488
	item yinzhengjie-ceph143-ssd weight 0.488
}

# 注意,这个规则的名称也不要重复哈!

rule yinzhengjie_ceph_cluster_rule {
	id 10
	type replicated
	min_size 1
	max_size 3
	step take yinzhengjie-ssd
	step chooseleaf firstn 0 type host
	step emit
}


5.转换crush二进制格式

[root@ceph141 ~]# crushtool -c /yinzhengjie/data/ceph/v3.txt -o /yinzhengjie/data/ceph/v4.crushmap
[root@ceph141 ~]# 
[root@ceph141 ~]# file /yinzhengjie/data/ceph/v4.crushmap
/yinzhengjie/data/ceph/v4.crushmap: MS Windows icon resource
[root@ceph141 ~]# 

6.导入新的crush运行图,观察osd是否被分组啦!

[root@ceph141 ~]# ceph osd tree
ID CLASS WEIGHT  TYPE NAME        STATUS REWEIGHT PRI-AFF 
-1       5.11298 root default                             
-3       2.37900     host ceph141                         
 0   hdd 0.19499         osd.0        up  1.00000 1.00000 
 1   hdd 0.19499         osd.1        up  1.00000 1.00000 
 2   hdd 1.50000         osd.2        up  1.00000 1.00000 
 3   hdd 0.48799         osd.3        up  1.00000 1.00000 
-5       1.36699     host ceph142                         
 4   hdd 0.19499         osd.4        up  1.00000 1.00000 
 5   hdd 0.19499         osd.5        up  1.00000 1.00000 
 6   hdd 0.48799         osd.6        up  1.00000 1.00000 
 7   hdd 0.48799         osd.7        up  1.00000 1.00000 
-7       1.36699     host ceph143                         
 8   hdd 0.19499         osd.8        up  1.00000 1.00000 
 9   hdd 0.19499         osd.9        up  0.79999 1.00000 
10   hdd 0.48799         osd.10       up  1.00000 1.00000 
11   hdd 0.48799         osd.11       up  1.00000 1.00000 
[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd setcrushmap -i /yinzhengjie/data/ceph/v4.crushmap
41
[root@ceph141 ~]# 
[root@ceph141 ~]# ceph osd tree
ID  CLASS WEIGHT  TYPE NAME                        STATUS REWEIGHT PRI-AFF 
-10       1.46397 root yinzhengjie-ssd                                     
-30       0.48799     host yinzhengjie-ceph141-ssd                         
  0   hdd 0.19499         osd.0                        up  1.00000 1.00000 
  1   hdd 0.19499         osd.1                        up  1.00000 1.00000 
-50       0.48799     host yinzhengjie-ceph142-ssd                         
  4   hdd 0.19499         osd.4                        up  1.00000 1.00000 
  5   hdd 0.19499         osd.5                        up  1.00000 1.00000 
-70       0.48799     host yinzhengjie-ceph143-ssd                         
  8   hdd 0.19499         osd.8                        up  1.00000 1.00000 
  9   hdd 0.19499         osd.9                        up  0.79999 1.00000 
 -1       5.11298 root default                                             
 -3       2.37900     host ceph141                                         
  2   hdd 1.50000         osd.2                        up  1.00000 1.00000 
  3   hdd 0.48799         osd.3                        up  1.00000 1.00000 
 -5       1.36699     host ceph142                                         
  6   hdd 0.48799         osd.6                        up  1.00000 1.00000 
  7   hdd 0.48799         osd.7                        up  1.00000 1.00000 
 -7       1.36699     host ceph143                                         
 10   hdd 0.48799         osd.10                       up  1.00000 1.00000 
 11   hdd 0.48799         osd.11                       up  1.00000 1.00000 
[root@ceph141 ~]# 

7.验证crush运行图是否生效

[root@ceph141 ~]# ceph osd crush rule dump
[
    {
        "rule_id": 0,
        "rule_name": "replicated_rule",
        "ruleset": 0,
        "type": 1,
        "min_size": 1,
        "max_size": 5,
        "steps": [
            {
                "op": "take",
                "item": -1,
                "item_name": "default"
            },
            {
                "op": "chooseleaf_firstn",
                "num": 0,
                "type": "host"
            },
            {
                "op": "emit"
            }
        ]
    },
    {
        "rule_id": 10,
        "rule_name": "yinzhengjie_ceph_cluster_rule",  # 注意,咱们定义的规则生效啦!
        "ruleset": 10,
        "type": 1,
        "min_size": 1,
        "max_size": 3,
        "steps": [
            {
                "op": "take",
                "item": -10,
                "item_name": "yinzhengjie-ssd"
            },
            {
                "op": "chooseleaf_firstn",
                "num": 0,
                "type": "host"
            },
            {
                "op": "emit"
            }
        ]
    }
]

[root@ceph141 ~]# 

8.测试创建存储池

[root@ceph141 ~]# ceph osd pool create yinzhengjie-ssdpool 32 32
pool 'yinzhengjie-ssdpool' created
[root@ceph141 ~]# 

9.验证pgp状态

[root@ceph141 ~]# ceph pg ls-by-pool yinzhengjie-ssdpool | awk '{print$1,$2,$15}'
PG OBJECTS ACTING
15.0 0 [11,3,6]p11
15.1 0 [10,3,7]p10
15.2 0 [2,7,11]p2
15.3 0 [11,3,7]p11
15.4 0 [10,2,7]p10
15.5 0 [6,10,3]p6
15.6 0 [2,10,6]p2
15.7 0 [3,11,7]p3
15.8 0 [2,6,11]p2
15.9 0 [6,2,10]p6
15.a 0 [2,6,10]p2
15.b 0 [2,7,10]p2
15.c 0 [10,2,7]p10
15.d 0 [11,3,7]p11
15.e 0 [2,10,7]p2
15.f 0 [6,10,2]p6
15.10 0 [11,7,2]p11
15.11 0 [2,6,10]p2
15.12 0 [3,7,10]p3
15.13 0 [2,6,10]p2
15.14 0 [6,2,11]p6
15.15 0 [7,10,3]p7
15.16 0 [2,10,6]p2
15.17 0 [7,2,10]p7
15.18 0 [3,10,6]p3
15.19 0 [6,10,3]p6
15.1a 0 [11,2,7]p11
15.1b 0 [7,11,2]p7
15.1c 0 [3,7,11]p3
15.1d 0 [7,11,2]p7
15.1e 0 [11,7,2]p11
15.1f 0 [11,6,2]p11

* NOTE: afterwards
  [root@ceph141 ~]# 
  [root@ceph141 ~]# ceph osd tree
  ID  CLASS WEIGHT  TYPE NAME                        STATUS REWEIGHT PRI-AFF 
  -10       1.46397 root yinzhengjie-ssd                                     
  -30       0.48799     host yinzhengjie-ceph141-ssd                         
  0   hdd 0.19499         osd.0                        up  1.00000 1.00000 
  1   hdd 0.19499         osd.1                        up  1.00000 1.00000 
  -50       0.48799     host yinzhengjie-ceph142-ssd                         
  4   hdd 0.19499         osd.4                        up  1.00000 1.00000 
  5   hdd 0.19499         osd.5                        up  1.00000 1.00000 
  -70       0.48799     host yinzhengjie-ceph143-ssd                         
  8   hdd 0.19499         osd.8                        up  1.00000 1.00000 
  9   hdd 0.19499         osd.9                        up  0.79999 1.00000 
   -1       5.11298 root default                                             
   -3       2.37900     host ceph141                                         
  2   hdd 1.50000         osd.2                        up  1.00000 1.00000 
  3   hdd 0.48799         osd.3                        up  1.00000 1.00000 
   -5       1.36699     host ceph142                                         
  6   hdd 0.48799         osd.6                        up  1.00000 1.00000 
  7   hdd 0.48799         osd.7                        up  1.00000 1.00000 
   -7       1.36699     host ceph143                                         
   10   hdd 0.48799         osd.10                       up  1.00000 1.00000 
   11   hdd 0.48799         osd.11                       up  1.00000 1.00000 
  [root@ceph141 ~]# 


如上结果所示,分到的pg对应的OSD编号有2,3,6,7,10,11,走的时候默认的规则对应的OSD编号。
对应的pg没有并没有落到0,1,4,5,8,9对应的分区编号。


如何让其调度到我们定义的规则的OSD上呢? 需要再进一步研究,现在已经是凌晨了1店多了.... 先睡了。
posted @ 2023-11-15 01:14  尹正杰  阅读(28)  评论(0编辑  收藏  举报