debian搭建ceph记录(接入libvirt)

发布于:2025-05-25 ⋅ 阅读:(17) ⋅ 点赞:(0)

一. 搭建集群

1. 准备节点

我是在kvm环境下搭建的3台虚拟机

机器名 IP OS 数据磁盘
ceph1 192.168.22.131 Debian12 20GB*2
ceph2 192.168.22.132 Debian12 20GB*2
ceph3 192.168.22.133 Debian12 20GB*2

2. 安装基础包(所有节点都执行)

# 更换apt软件源
sed -i "s@deb.debian.org@mirrors.aliyun.com@g" /etc/apt/mirrors/debian*
apt update

# 添加aliyun的ceph软件源,url中的【squid】对应cepf版本19.2.2
apt install -y gnupg software-properties-common 
wget -q -O- 'https://mirrors.aliyun.com/ceph/keys/release.asc' | sudo apt-key add -
apt-add-repository 'deb https://mirrors.aliyun.com/ceph/debian-squid/ bookworm main'

# 安装包
apt install -y podman lvm2 ceph-common

3. 在节点1上初始化集群

# 安装cephadm 
apt install -y cephadm 

# 创建集群
cephadm bootstrap --mon-ip 192.168.22.131

# 配置公钥到其他节点
ssh-copy-id -f -i /etc/ceph/ceph.pub root@192.168.22.132
ssh-copy-id -f -i /etc/ceph/ceph.pub root@192.168.22.133

# 添加节点
ceph orch host add ceph2 192.168.22.132
ceph orch host add ceph3 192.168.22.133

# 查看节点情况
ceph orch host ls

# 添加_admin这个label后可以允许该节点运行ceph命令
ceph orch host label add ceph2 _admin
ceph orch host label add ceph3 _admin

# 设置 mon 节点
ceph orch apply mon 3
ceph orch apply mon ceph1,ceph2,ceph3 

# 查看 mon 详情
ceph mon dump

# 列出所有节点的存储设备
ceph orch device ls

# 所有可使用设备创建osd
ceph orch apply osd --all-available-devices

# 创建一个pool给rbd使用
ceph osd pool create rbd_pool
rbd pool init rbd_pool

# 创建用户rbd_user
ceph auth get-or-create client.rbd_user mon 'profile rbd' osd 'profile rbd pool=rbd_pool' mgr 'profile rbd pool=rbd_pool'
# 将输出内容追加到文件/etc/ceph/ceph.client.{ID}.keyring中

# 创建一个10GB的镜像
rbd create --size 10240 rbd_pool/image1

# 查看rbd_pool下的镜像
rbd -p rbd_pool ls

二. 客户端使用

1. 客户端连接集群

# 安装ceph-common
apt install -y ceph-common

# 将ceph集群中的下面两个文件拷贝到客户端同目录
# (可以通过配置不同ceph用户来控制访问权限,这里我配置了管理员admin的key)
/etc/ceph/ceph.conf
/etc/ceph/ceph.client.admin.keyring

# 查看集群状态,若返回集群信息则配置成功
ceph status

2. 挂载ceph块设备

# 将集群pool中的image映射到本地
rbd map rbd_pool/image1

# 执行lsblk命令,发现新增设备/dev/rbd0
lsblk

# 本地创建目录/data1
mkdir /data1

# 格式化rbd0
mkfs.ext4 /dev/rbd0

# 挂载到本地目录后,即可正常访问
mount /dev/rbd0 /data1

3. libvirt使用ceph块设备

准备另一台kvm机器,安装libvirt环境,下载qcow2镜像等,可参考我的另一篇文章
记录使用libvirt创建虚拟机、自定义qcow2镜像

把下载的qcow2文件转为ceph集群中的镜像(可以作为模板快速复制)

qemu-img convert -f qcow2 -O raw debian-12-genericcloud-amd64.qcow2 rbd:rbd_pool/vm1.qcow2

在rbd_pool中创建一个5GB的image作为数据盘

qemu-img create -f rbd rbd:rbd_pool/vm1-data 5G

生成访问ceph的key

# 创建用户rbd_user,限制只能访问rbd_pool
ceph auth get-or-create client.rbd_user mon 'profile rbd' osd 'profile rbd pool=rbd_pool' mgr 'profile rbd pool=rbd_pool'

# 生成secret文件
cat > secret.xml <<EOF
<secret ephemeral='no' private='no'>
        <usage type='ceph'>
                <name>client.rbd_user secret</name>
        </usage>
</secret>
EOF

# 定义secret, 返回【secret_uuid】
virsh secret-define --file secret.xml

# 把用户rbd_user的key写入文件
ceph auth get-key client.rbd_user | tee client.rbd_user.key

# 	设置secret的值
virsh secret-set-value --secret 【替换secret_uuid】 --base64 $(cat client.rbd_user.key) && rm client.rbd_user.key secret.xml

定义虚拟机的xml文件

<domain type='kvm'>
   <name>vm1</name>
   <memory unit='GiB'>1</memory>
   <vcpu>1</vcpu>
   <os>
       <type arch='x86_64' machine='pc-i440fx-2.9'>hvm</type>
       <boot dev='hd'/>
   </os>
   <features>
       <acpi/>
       <apic/>
       <pae/>
   </features>
   <devices>
       <disk type='file' device='cdrom'>
           <driver name='qemu' type='raw'/>
           <source file='/data/vm1/init.iso'/>
           <target dev='hda' bus='ide'/>
           <readonly/>
       </disk>
       <disk type='network' device='disk'>
           <source protocol='rbd' name='rbd_pool/os1'>
               <host name='192.168.22.131' port='6789'/>
               <host name='192.168.22.132' port='6789'/>
               <host name='192.168.22.133' port='6789'/>
           </source>
           <auth username='rbd_user'>
               <secret type='ceph' uuid='【替换secret_uuid】'/>
           </auth>
           <target dev='vda' bus='virtio'/>
       </disk>
       <disk type='network' device='disk'>
           <source protocol='rbd' name='rbd_pool/vm1-data'>
               <host name='192.168.22.131' port='6789'/>
               <host name='192.168.22.132' port='6789'/>
               <host name='192.168.22.133' port='6789'/>
           </source>
           <auth username='rbd_user'>
               <secret type='ceph' uuid='【替换secret_uuid】'/>
           </auth>
           <target dev='vdb' bus='virtio'/>
       </disk>
       <interface type='bridge'>
           <source bridge='virbr0'/>
           <model type='virtio'/>
       </interface>
       <console type='pty'>
           <target type='serial' port='0'/>
       </console>
   </devices>
</domain>

定义&启动&连接虚拟机

virsh define vm1.xml
virsh start vm1
virsh console vm1

https://www.cnblogs.com/gustabm/p/17663966.html
https://cloud-atlas.readthedocs.io/zh-cn/latest/ceph/rbd/compare_local_ssd_ceph_rbd.html


网站公告

今日签到

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