Ceph异地数据同步之-Cephfs异地同步复制

发布于:2025-04-09 ⋅ 阅读:(72) ⋅ 点赞:(0)

#作者:闫乾苓

1.核心原理

Cephfs异地同步基于CephFS-mirror,其工作原理是基于CephFS的快照功能和cephfs-mirror工具的异步复制机制。它通过将本地CephFS文件系统的快照异步复制到远程CephFS文件系统,实现了数据的异地备份和容灾。同时,通过增量复制和并发同步等技术手段,提高了同步效率和可用性。

本章节后续具体部署、测试步骤均基于以下2个ceph集群进行:

ceph集群site-a(源集群):
节点hostname	IP
ceph01	192.168.61.11
ceph02	192.168.61.12
ceph03	192.168.61.13

ceph集群site-b(目标集群):
节点hostname	IP
ceph-a	192.168.61.31
ceph-b	192.168.61.32
ceph-c	192.168.61.33

2.部署步骤

(1).在源存储集群中,创建cephfs文件系统

登录到 Cephadm shell:
[root@ceph01 ~]# cephadm shell
创建cephfs文件系统cephfs_s,并创建子卷cephfs_s_subvol_1(因为子卷才能创建快照)
[ceph: root@ceph01 /]# ceph fs volume create cephfs_s
[ceph: root@ceph01 /]# ceph fs subvolume create cephfs_s cephfs_s_subvol_1 

(2).在源存储集群中,部署 CephFS 镜像守护进程:

[ceph: root@ceph01 /]# ceph orch apply cephfs-mirror ceph01
Scheduled cephfs-mirror update...

可选:部署多个 CephFS 镜像守护进程并实现高可用性

ceph orch apply cephfs-mirror --placement="3 host1 host2 host3"

(3).在目标存储集群中创建目标文件系统

登录到 Cephadm shell:
[root@ceph-a ~]# cephadm shell

[ceph: root@ceph-a /]# ceph fs volume create cephfs_bak
[ceph: root@ceph-a /]# ceph fs subvolume create cephfs_bak cephfs_bak_subvol_1 

(4).在目标存储集群中,为每个 CephFS 对等文件系统创建一个用户:

[ceph: root@ceph-a /]#  ceph fs authorize cephfs_bak client.mirror / rwps
[client.mirror]
        key = AQCue6BnZ+ZqKRAASrc1YhuWa7uJDhXEINu4Og==
        caps mds = "allow rwps fsname=cephfs_bak"
        caps mon = "allow r fsname=cephfs_bak"
        caps osd = "allow rw tag cephfs data=cephfs_bak"

(5).在源存储集群中,启用 CephFS 镜像模块:

[ceph: root@ceph01 /]# ceph mgr module enable mirroring
module 'mirroring' is already enabled

(6).在源存储集群中,在 Ceph 文件系统上启用镜像功能:

[ceph: root@ceph01 /]# ceph fs snapshot mirror enable cephfs_s
{}

可选:禁用快照镜像:

ceph fs snapshot mirror disable cephfs

(7).在目标节点上,启用 mirroring Ceph Manager 模块

[ceph: root@ceph-a /]# ceph mgr module enable mirroring

(8).在同一个目标节点上,创建 peer bootstrap

remote-site 是用户定义的字符串,用于识别目标存储集群。
在双引号之间复制令牌字符串,以便在下一步中使用。
[ceph: root@ceph-a /]# ceph fs snapshot mirror peer_bootstrap create cephfs_bak client.mirror remote-site
{"token": "eyJmc2lkIjogImJhZWRjNmMwLWUwNTgtMTFlZi1hMjNhLTAwMGMyOWNmODMyOSIsICJmaWxlc3lzdGVtIjogImNlcGhmc19iYWsiLCAidXNlciI6ICJjbGllbnQubWlycm9yIiwgInNpdGVfbmFtZSI6ICJyZW1vdGUtc2l0ZSIsICJrZXkiOiAiQVFDdWU2Qm5aK1pxS1JBQVNyYzFZaHVXYTd1SkRoWEVJTnU0T2c9PSIsICJtb25faG9zdCI6ICJbdjI6MTkyLjE2OC42MS4zMTozMzAwLzAsdjE6MTkyLjE2OC42MS4zMTo2Nzg5LzBdIFt2MjoxOTIuMTY4LjYxLjMyOjMzMDAvMCx2MToxOTIuMTY4LjYxLjMyOjY3ODkvMF0gW3YyOjE5Mi4xNjguNjEuMzM6MzMwMC8wLHYxOjE5Mi4xNjguNjEuMzM6Njc4OS8wXSJ9"}

(9).在源存储集群中,从目标存储集群导入 bootstrap 令牌:

[ceph: root@ceph01 /]# ceph fs snapshot mirror peer_bootstrap import cephfs_s eyJmc2lkIjogImJhZWRjNmMwLWUwNTgtMTFlZi1hMjNhLTAwMGMyOWNmODMyOSIsICJmaWxlc3lzdGVtIjogImNlcGhmc19iYWsiLCAidXNlciI6ICJjbGllbnQubWlycm9yIiwgInNpdGVfbmFtZSI6ICJyZW1vdGUtc2l0ZSIsICJrZXkiOiAiQVFDdWU2Qm5aK1pxS1JBQVNyYzFZaHVXYTd1SkRoWEVJTnU0T2c9PSIsICJtb25faG9zdCI6ICJbdjI6MTkyLjE2OC42MS4zMTozMzAwLzAsdjE6MTkyLjE2OC42MS4zMTo2Nzg5LzBdIFt2MjoxOTIuMTY4LjYxLjMyOjMzMDAvMCx2MToxOTIuMTY4LjYxLjMyOjY3ODkvMF0gW3YyOjE5Mi4xNjguNjEuMzM6MzMwMC8wLHYxOjE5Mi4xNjguNjEuMzM6Njc4OS8wXSJ9
{}

(10).在源存储集群中,列出 CephFS 镜像对等点:

[ceph: root@ceph01 /]# ceph fs snapshot mirror peer_list cephfs_s|jq
{
  "827caf22-d004-481d-bf6c-4cfd3765e35a": {
    "client_name": "client.mirror",
    "site_name": "remote-site",
    "fs_name": "cephfs_bak"
  }
}

可选:删除快照对等点:

ceph fs snapshot mirror peer_remove FILE_SYSTEM_NAME PEER_UUID

(11).在源存储集群中,为快照镜像配置目录:

[ceph: root@ceph01 /]# ceph fs snapshot mirror add cephfs_s /volumes/_nogroup/cephfs_s_subvol_1
{}

可选:停止目录的快照镜像:

ceph fs snapshot mirror remove FILE_SYSTEM_NAME PATH

3.cephfs同步测试

准备1台客户端主机,用户挂载源集群和目的集群的文件系统并查看镜像测试
查看源集群admin的keyring

[ceph: root@ceph01 /]# grep key /etc/ceph/ceph.keyring 
        key = AQDsDJdnNm99KxAAzRBeGalZmX/8fgpcBiBCgA==

将源集群key写入到客户端主机配置文件

cat > /etc/ceph/ceph01.admin.secret <<EOF
AQDsDJdnNm99KxAAzRBeGalZmX/8fgpcBiBCgA==
EOF

查看目标集群admin的keyring

[ceph: root@ceph-a /]# grep key /etc/ceph/ceph.keyring 
        key = AQCHqZ1nT1eEBBAAdW6WvPygeF2MjU7olkkjrQ==

将目标集群key写入到客户端主机配置文件

cat > /etc/ceph/ceph-a.admin.secret <<EOF
AQCHqZ1nT1eEBBAAdW6WvPygeF2MjU7olkkjrQ==
EOF

在客户端主机创建两个用于挂载源,目标集群cephfs文件系统的目录

[root@cilent ~]# mkdir /mnt/{ceph01,ceph-a}
[root@cilent ~]# tree /mnt/
/mnt/
├── ceph01
└── ceph-a

客户端主机安装ceph-common

[root@cilent ~]# yum install ceph-common

分别挂载源、目标文件系统挂载到创建的目录

[root@cilent ~]# mount -t ceph 192.168.61.11:6789:/ /mnt/ceph01 -o name=admin,secretfile=/etc/ceph/ceph01.admin.secret 
[root@cilent ~]# mount -t ceph 192.168.61.31:6789:/ /mnt/ceph-a -o name=admin,secretfile=/etc/ceph/ceph-a.admin.secret 
[root@cilent ~]# df -h |grep ceph
192.168.61.11:6789:/      475G     0  475G    0% /mnt/ceph01
192.168.61.31:6789:/      475G     0  475G    0% /mnt/ceph-a

查看源、目标集群的镜像同步:

[root@cilent ~]# tree /mnt/
/mnt/
├── ceph01
│   └── volumes
│       ├── _:cephfs_s_subvol_1.meta
│       └── _nogroup
│           └── cephfs_s_subvol_1
│               └── 316d3b1c-b767-425e-81ec-6ed5cf0fe7e1
└── ceph-a
    └── volumes
        └── _nogroup
            └── cephfs_s_subvol_1

9 directories, 1 file

在源集群文件系统子卷创建2个测试文件

[root@cilent ~]# cd /mnt/ceph01/volumes/_nogroup/cephfs_s_subvol_1/316d3b1c-b767-425e-81ec-6ed5cf0fe7e1
[root@cilent 316d3b1c-b767-425e-81ec-6ed5cf0fe7e1]# dd if=/dev/zero of=test100M-1 bs=1M count=100
[root@cilent 316d3b1c-b767-425e-81ec-6ed5cf0fe7e1]# dd if=/dev/zero of=test100M-2 bs=1M count=100
[root@cilent 316d3b1c-b767-425e-81ec-6ed5cf0fe7e1]# ll
总用量 204800
-rw-r--r--. 1 root root 104857600  2月  3 16:47 test100M-1
-rw-r--r--. 1 root root 104857600  2月  3 16:47 test100M-2

cephfs快照默认1小时创建1个,创建快照后,将快照同步到目标集群,刚在源集群创建了2个测试文件,因未到1小时,快照为创建,所有测试文件未同步。

[root@cilent ~]# tree /mnt/
/mnt/
├── ceph01
│   └── volumes
│       ├── _:cephfs_s_subvol_1.meta
│       └── _nogroup
│           └── cephfs_s_subvol_1
│               └── 316d3b1c-b767-425e-81ec-6ed5cf0fe7e1
│                   ├── test100M-1
│                   └── test100M-2
└── ceph-a
└── volumes
└── _nogroup
└── cephfs_s_subvol_1
9 directories, 3 files

可以等1小时后自动触发镜像同步,也可以手动创建快照,手动同步以便检验镜像同步效果。
在源集群为文件系统ceph-s手动创建快照

[ceph: root@ceph01 /]# ceph fs subvolume snapshot create cephfs_s cephfs_s_subvol_1 snapshot_1

在客户端再此查看测试文件是否已经同步到目标集群

[root@cilent ~]# tree /mnt/
/mnt/
├── ceph01
│   └── volumes
│       ├── _:cephfs_s_subvol_1.meta
│       └── _nogroup
│           └── cephfs_s_subvol_1
│               └── 316d3b1c-b767-425e-81ec-6ed5cf0fe7e1
│                   ├── test100M-1
│                   └── test100M-2
└── ceph-a
    └── volumes
        └── _nogroup
            └── cephfs_s_subvol_1
                └── 316d3b1c-b767-425e-81ec-6ed5cf0fe7e1
                    ├── test100M-1
                    └── test100M-2

10 directories, 5 files

4.查看cephfs文件同步状态

检查 cephfs-mirror守护进程状态

[ceph: root@ceph01 /]# ceph fs snapshot mirror daemon status|jq
[
  {
    "daemon_id": 54168,
    "filesystems": [
      {
        "filesystem_id": 1,
        "name": "cephfs_s",
        "directory_count": 1,
        "peers": [
          {
            "uuid": "827caf22-d004-481d-bf6c-4cfd3765e35a",
            "remote": {
              "client_name": "client.mirror",
              "cluster_name": "remote-site",
              "fs_name": "cephfs_bak"
            },
            "stats": {
              "failure_count": 0,
              "recovery_count": 0
            }
          }
        ]
      }
    ]
  }
]

如需更多信息,请使用 admin 套接字接口,如下所述。
在运行 CephFS 镜像守护进程的节点上查找 Ceph 文件系统 ID:
本例中的 Ceph 文件系统 ID 是 cephfs_s@1

[ceph: root@ceph01 /]# ceph --admin-daemon /var/run/ceph/ceph-client.cephfs-mirror.ceph01.lieebi.7.94310288324944.asok help|grep @
    "fs mirror peer status cephfs_s@1 827caf22-d004-481d-bf6c-4cfd3765e35a": "get peer mirror status",
    "fs mirror status cephfs_s@1": "get filesystem mirror status",

查看镜像状态

827caf22-d004-481d-bf6c-4cfd3765e35a 这是唯一的对等 UUID
[ceph: root@ceph01 /]# ceph --admin-daemon /var/run/ceph/ceph-client.cephfs-mirror.ceph01.lieebi.7.94310288324944.asok fs mirror status cephfs_s@1                                     
{
    "rados_inst": "192.168.61.11:0/877331395",
    "peers": {
        "827caf22-d004-481d-bf6c-4cfd3765e35a": {
            "remote": {
                "client_name": "client.mirror",
                "cluster_name": "remote-site",
                "fs_name": "cephfs_bak"
            }
        }
    },
    "snap_dirs": {
        "dir_count": 1
    }
}

查看对等状态:

[ceph: root@ceph01 /]# ceph --admin-daemon /var/run/ceph/ceph-client.cephfs-mirror.ceph01.lieebi.7.94310288324944.asok fs mirror peer status cephfs_s@1 827caf22-d004-481d-bf6c-4cfd3765e35a
{
    "/volumes/_nogroup/cephfs_s_subvol_1": {
        "state": "idle",
        "last_synced_snap": {
            "id": 4,
            "name": "snapshot_3",
            "sync_duration": 1.645025513,
            "sync_time_stamp": "5004.206685s"
        },
        "snaps_synced": 3,
        "snaps_deleted": 0,
        "snaps_renamed": 0
    }
}

state 可以是以下三个值之一:

  • idle 表示目录当前没有同步。
  • syncing 意味着目录当前正在同步。
  • failed 表示目录连续失败已达到上限。
    默认连续故障数为 10,默认重试间隔为 60 秒。

显示 cephfs-mirror 守护进程映射到的目录:

[ceph: root@ceph01 /]# ceph fs snapshot mirror dirmap cephfs_s /volumes/_nogroup/cephfs_s_subvol_1
{
    "instance_id": "54261",
    "last_shuffled": 1738571008.0191686,
    "state": "mapped"
}

5.优化cephfs文件系统同步的时间间隔

在源集群ceph01节点编写在cephadm shell中创建快照的脚本,后续可通过配置crontab定时任务,定时执行此脚本,从而缩短cephfs文件系统镜像同步的时间间隔(可规避cephfs快照默认最短间隔1小时的配置)

#!/bin/bash
CEPH_FSID="0978cb18-dc68-11ef-b0d7-000c29460ffd"
CEPH_CONFIG_PATH="/var/lib/ceph/0978cb18-dc68-11ef-b0d7-000c29460ffd/mon.ceph01/config"

cephadm shell \
  --fsid ${CEPH_FSID} \
  -c ${CEPH_CONFIG_PATH} \
  ceph fs subvolume snapshot create cephfs_s cephfs_s_subvol_1 snapshot_$(date +%Y%m%d_%H%M%S)

网站公告

今日签到

点亮在社区的每一天
去签到