分布式存储,用一个软件把多台服务器集合到一起当一个存储
红帽的软件,红帽收购的
ceph头足类软体动物,头下面就是脚
NFS:文件系统存储,本地没有多硬盘。提供共享文件夹,可以直接mount挂载
U盘:块存储
-
- 监视器:Ceph Monitor(ceph-mon)维护集群状态图,包括监视器图、管理器图、OSD图、MDS图和CRUSH图。这些映射是Ceph守护进程相互协调所需的关键集群状态。监视器还负责管理守护程序和客户端之间的身份验证。为了冗余和高可用性,通常至少需要三台Monitor。大一点的服务器需要五台,一定是单数部署,投票超一半就可以定
- 管理器:Ceph Manager(ceph-mgr)负责跟踪ceph集群的运行时指标和当前状态,包括存储利用率、当前性能指标和系统负载。Ceph Manager守护进程还托管基于python的模块来管理和公开Ceph集群信息,包括基于web的Ceph仪表板和REST API。高可用性通常需要至少两台Manager。主备部署,高可用
- Ceph OSD(几块硬盘都有几个OSD,每个服务器上都有):ceph-osd存储数据,处理数据复制、恢复、重新平衡,并通过检查其他Ceph OSD守护进程的心跳来为Ceph监视器和管理器提供一些监视信息。为了实现冗余和高可用性,通常至少需要三个Ceph OSD。默认三副本存储。
- MDS:ceph-mds代表Ceph文件系统存储元数据(即,Ceph块设备和Ceph对象存储不使用MDS)。Ceph元数据服务器允许POSIX文件系统用户执行基本命令(如ls、find等)。)而不会给Ceph存储集群带来巨大的负担。部署两个
- RGW:对象存储网关Rados Gateway,是一个提供对象存储功能的组件,可以通过RESTful接口向外部应用程序提供可扩展和高可用的存储服务。(RGW)
-
Ceph将数据作为对象存储在逻辑存储池中。使用CRUSH算法,Ceph计算哪个归置组(PG)应该包含该对象,以及哪个OSD应该存储该归置组。CRUSH算法支持Ceph存储集群动态扩展、重新平衡和恢复。
-
部署ceph集群需要python3、podman或docker、时间服务(如chrony)、lvm2
摩尔定律
基础环境搭建
在pubserver上配置ansible环境
[root@pubserver ~]# mkdir ceph
[root@pubserver ~]# cd ceph
[root@pubserver ceph]# vim ansible.cfg
[defaults]
inventory = inventory
host_key_checking = false
[root@pubserver ceph]# vim inventory
[ceph] # 定义ceph组
ceph1 ansible_host=192.168.88.11
ceph2 ansible_host=192.168.88.12
ceph3 ansible_host=192.168.88.13
[clients] # 定义客户端组
client1 ansible_host=192.168.88.10
[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=a
[root@pubserver ceph]# mkdir files/
[root@pubserver ceph]# vim files/local88.repo
[BaseOS]
name = BaseOS
baseurl = ftp://192.168.88.240/dvd/BaseOS
enabled = 1
gpgcheck = 0
[AppStream]
name = AppStream
baseurl = ftp://192.168.88.240/dvd/AppStream
enabled = 1
gpgcheck = 0
[rpms]
name = rpms
baseurl = ftp://192.168.88.240/rpms
enabled = 1
gpgcheck = 0
# 配置yum
[root@pubserver ceph]# vim 01-upload-repo.yml
---
- name: config repos.d
hosts: all
tasks:
- name: delete repos.d
file:
path: /etc/yum.repos.d
state: absent
- name: create repos.d
file:
path: /etc/yum.repos.d
state: directory
mode: '0755'
- name: upload local88
copy:
src: files/local88.repo
dest: /etc/yum.repos.d/
[root@pubserver ceph]# ansible-playbook 01-upload-repo.yml
配置名称解析
# 配置三台主机实现名称解析,解析的名字务必与主机实际名字一致
[root@pubserver ceph]# vim 02-modify-hosts.yml
---
- name: add names
hosts: ceph
tasks:
- name: add block
blockinfile: # 类似于lineinfile模块,可在目标文件中加多行
path: /etc/hosts
block: |
192.168.88.11 ceph1
192.168.88.12 ceph2
192.168.88.13 ceph3
192.168.88.240 quay.io
[root@pubserver ceph]# ansible-playbook 02-modify-hosts.yml
# 查看结果,以ceph1为例
[root@ceph1 ~]# tail -6 /etc/hosts
# BEGIN ANSIBLE MANAGED BLOCK
192.168.88.11 ceph1
192.168.88.12 ceph2
192.168.88.13 ceph3
192.168.88.240 quay.io
# END ANSIBLE MANAGED BLOCK
-
配置pubserver为NTP服务器
-
# 1. 查看pubserver自己的时区,如果时区不正确需要改正 [root@pubserver ~]# timedatectl [root@pubserver ~]# timedatectl set-timezone Asia/Shanghai # 2. 查看时间,如果时间不正确,需要调整时间 [root@pubserver ~]# date [root@pubserver ~]# date -s "年-月-日 时:分:秒" # 3. 配置chronyd服务 [root@pubserver ~]# yum install -y chrony [root@pubserver ~]# vim /etc/chrony.conf # 打开23、26行的注释 ...略... 24 # Allow NTP client access from local network. 25 allow 192.168.0.0/16 # 为192.168开头的客户端提供时间服务 26 27 # Serve time even if not synchronized to a time source. 28 local stratum 10 # 即使自己没有时间源,也为客户端提供时间服务 ...略... [root@pubserver ~]# systemctl enable chronyd --now [root@pubserver ~]# ss -ulnp | grep :123 # ntp使用udp 123端口
-
配置ceph1-ceph3使用pubserver提供的时间服务
[root@pubserver ceph]# vim 03-config-ntp.yml
---
- name: config ntp
hosts: ceph
tasks:
- name: install chrony # 安装chrony
yum:
name: chrony
state: present
- name: modify config # 替换以pool开头的行
lineinfile:
path: /etc/chrony.conf
regexp: '^pool'
line: "pool 192.168.88.240 iburst"
notify: restart ntp # 如果该任务的状态是CHANGED,则执行restart ntp任务
handlers:
- name: restart ntp # 只有notify通知时,才执行重启任务
service:
name: chronyd
state: restarted
enabled: yes
[root@pubserver ceph]# ansible-playbook 03-config-ntp.yml
# 以ceph1为例,查看结果
[root@ceph1 ~]# chronyc sources -v
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.88.240 10 6 37 4 -8731ns[-6313us] +/- 7118us
-
准备容器仓库服务器
-
# 1. 将真机docker-distribution-2.6.2-2.git48294d9.el7.x86_64.rpm拷贝到pubserver的/root目录并安装 [root@pubserver ~]# yum install -y docker-distribution-2.6.2-2.git48294d9.el7.x86_64.rpm # 2. 启动服务 [root@pubserver ~]# systemctl enable docker-distribution --now
-
安装软件包,并导入镜像
-
# 1. 在ceph集群节点上安装软件包 [root@pubserver ceph]# vim 04-install-ceph.yml --- - name: install pkg hosts: ceph tasks: - name: install pkg # 安装软件包 yum: name: python39,podman,lvm2 state: present [root@pubserver ceph]# ansible-playbook 04-install-ceph.yml # 2. 将真机/linux-soft/s2/zzg/ceph_soft/ceph-server目录拷贝到ceph各节点,并导入镜像 [root@ceph1 ~]# cd ceph-server/ [root@ceph1 ceph-server]# for c in *.tar > do > podman load -i $c > done [root@ceph2 ~]# cd ceph_soft/ [root@ceph2 ceph-server]# for c in *.tar > do > podman load -i $c > done [root@ceph3 ~]# cd ceph_soft/ [root@ceph3 ceph-server]# for c in *.tar > do > podman load -i $c > done # 3. 查看执行结果 [root@ceph1 ceph-server]# podman images REPOSITORY TAG IMAGE ID CREATED SIZE quay.io/ceph/ceph v17 cc65afd6173a 7 weeks ago 1.4 GB quay.io/ceph/ceph-grafana 8.3.5 dad864ee21e9 8 months ago 571 MB quay.io/prometheus/prometheus v2.33.4 514e6a882f6e 9 months ago 205 MB quay.io/prometheus/node-exporter v1.3.1 1dbe0e931976 12 months ago 22.3 MB quay.io/prometheus/alertmanager v0.23.0 ba2b418f427c 15 months ago 58.9 MB # 4. 配置ceph1-ceph3使用pubserver作为仓库服务器 [root@pubserver ceph]# vim 05-config-registry.yml --- - name: config registry hosts: ceph tasks: - name: modify config blockinfile: path: /etc/containers/registries.conf block: | [[registry]] location = "quay.io:5000" # 指定服务器地址 insecure = true # 允许使用http协议 [root@pubserver ceph]# ansible-playbook 05-config-registry.yml # 5. 以ceph1为例,查看执行结果 [root@ceph1 ceph_soft]# tail -5 /etc/containers/registries.conf # BEGIN ANSIBLE MANAGED BLOCK [[registry]] location = "quay.io:5000" insecure = true # END ANSIBLE MANAGED BLOCK # 5. 修改镜像名称,以便可以将其推送到自建镜像服务器 [root@ceph1 ceph-server]# podman tag quay.io/ceph/ceph:v17 quay.io:5000/ceph/ceph:v17 [root@ceph1 ceph-server]# podman tag quay.io/ceph/ceph-grafana:8.3.5 quay.io:5000/ceph/ceph-grafana:8.3.5 [root@ceph1 ceph-server]# podman tag quay.io/prometheus/prometheus:v2.33.4 quay.io:5000/prometheus/prometheus:v2.33.4 [root@ceph1 ceph-server]# podman tag quay.io/prometheus/node-exporter:v1.3.1 quay.io:5000/prometheus/node-exporter:v1.3.1 [root@ceph1 ceph-server]# podman tag quay.io/prometheus/alertmanager:v0.23.0 quay.io:5000/prometheus/alertmanager:v0.23.0 # 6. 将镜像推送到镜像服务器,以便其他节点可以通过服务器下载镜像 [root@ceph1 ceph-server]# podman push quay.io:5000/ceph/ceph:v17 [root@ceph1 ceph-server]# podman push quay.io:5000/ceph/ceph-grafana:8.3.5 [root@ceph1 ceph-server]# podman push quay.io:5000/prometheus/prometheus:v2.33.4 [root@ceph1 ceph-server]# podman push quay.io:5000/prometheus/node-exporter:v1.3.1 [root@ceph1 ceph-server]# podman push quay.io:5000/prometheus/alertmanager:v0.23.0
安装ceph
- 所有Ceph集群都需要至少一个监视器Monitor,以及至少与存储在集群上的对象副本一样多的OSD。引导初始监视器是部署Ceph存储集群的第一步。
- Monitor部署还为整个集群设置了重要的标准,例如池的副本数量、每个OSD的放置组数量、心跳间隔、是否需要身份验证等。这些值中的大部分是默认设置的。
- 创建集群
# 1. 在ceph1上初始化集ceph集群。 # 集群初始化完成后,将自动生成ssh免密密钥,存放在/etc/ceph/目录下 [root@ceph1 ceph-server]# ./cephadm bootstrap --mon-ip 192.168.88.11 --initial-dashboard-password=123456 --dashboard-password-noupdate # 2. ceph将会以容器化的方式部署,查看生成了6个容器。 [root@ceph1 ceph-server]# podman ps # 3. 拷贝密钥文件至其他节点 [root@ceph1 ceph-server]# ssh-copy-id -f -i /etc/ceph/ceph.pub ceph2 [root@ceph1 ceph-server]# ssh-copy-id -f -i /etc/ceph/ceph.pub ceph3 # 4. 进入管理容器,查看ceph状态 [root@ceph1 ceph-server]# ./cephadm shell # 进入管理容器 [ceph: root@ceph1 /]# ceph -s # 查看ceph状态 cluster: id: 1ddfccf2-77b4-11ed-8941-000c2953b002 health: HEALTH_WARN OSD count 0 < osd_pool_default_size 3 services: mon: 1 daemons, quorum ceph1 (age 11m) mgr: ceph1.vnoivz(active, since 10m) osd: 0 osds: 0 up, 0 in data: pools: 0 pools, 0 pgs objects: 0 objects, 0 B usage: 0 B used, 0 B / 0 B avail pgs: # 5. 查看相关容器状态,显示所有容器均已启动 [ceph: root@ceph1 /]# ceph orch ls NAME PORTS RUNNING REFRESHED AGE PLACEMENT alertmanager ?:9093,9094 1/1 91s ago 3m count:1 crash 1/3 91s ago 4m * grafana ?:3000 1/1 91s ago 4m count:1 mgr 1/2 91s ago 4m count:2 mon 1/5 91s ago 4m count:5 node-exporter ?:9100 1/3 91s ago 4m * prometheus ?:9095 1/1 91s ago 4m count:1 # 6. 查看集群中现有主机 [ceph: root@ceph1 /]# ceph orch host ls HOST ADDR LABELS STATUS ceph1 192.168.88.11 _admin 1 hosts in cluster # 7. 向集群中添加其他主机 [ceph: root@ceph1 /]# ceph orch host add ceph2 192.168.88.12 [ceph: root@ceph1 /]# ceph orch host add ceph3 192.168.88.13 # 注:删除错误的主机命令为:ceph orch host rm 主机名 --force # 8. 查看集群中主机 [ceph: root@ceph1 /]# ceph orch host ls HOST ADDR LABELS STATUS ceph1 192.168.88.11 _admin ceph2 192.168.88.12 ceph3 192.168.88.13 3 hosts in cluster # 9. 扩容MON节点。一共有3台MON,位于ceph1-ceph3 [ceph: root@ceph1 /]# ceph orch apply mon --placement="3 ceph1 ceph2 ceph3" #3 后面给三台服务器中选择 # 10. 查看mon状态 [ceph: root@ceph1 /]# ceph -s cluster: id: a4b69ab4-79dd-11ed-ae7b-000c2953b002 health: HEALTH_WARN OSD count 0 < osd_pool_default_size 3 services: mon: 3 daemons, quorum ceph1,ceph3,ceph2 (age 2m) mgr: ceph1.gmqorm(active, since 15m), standbys: ceph3.giqaph osd: 0 osds: 0 up, 0 in data: pools: 0 pools, 0 pgs objects: 0 objects, 0 B usage: 0 B used, 0 B / 0 B avail pgs: [ceph: root@ceph1 /]# ceph mon stat e3: 3 mons at {ceph1=[v2:192.168.88.11:3300/0,v1:192.168.88.11:6789/0],ceph2=[v2:192.168.88.12:3300/0,v1:192.168.88.12:6789/0],ceph3=[v2:192.168.88.13:3300/0,v1:192.168.88.13:6789/0]}, election epoch 14, leader 0 ceph1, quorum 0,1,2 ceph1,ceph3,ceph2 # 11. ceph2和ceph3上也将会出现相关容器 [root@ceph2 ~]# podman ps [root@ceph3 ~]# podman ps
ceph命令:
-
cephadm shell 进入管理容器
-
ceph -s 查看容器状态
-
ceph orch(ochestration)ls 列出服务 编排
-
ceph orch host ls
-
podman rm -f 容器名 删除容器
-
添加OSD硬盘
-
[ceph: root@ceph1 /]# ceph orch daemon add osd ceph1:/dev/vdb [ceph: root@ceph1 /]# ceph orch daemon add osd ceph1:/dev/vdc [ceph: root@ceph1 /]# ceph orch daemon add osd ceph1:/dev/vdd [ceph: root@ceph1 /]# ceph orch daemon add osd ceph2:/dev/vdb [ceph: root@ceph1 /]# ceph orch daemon add osd ceph2:/dev/vdc [ceph: root@ceph1 /]# ceph orch daemon add osd ceph2:/dev/vdd [ceph: root@ceph1 /]# ceph orch daemon add osd ceph3:/dev/vdb [ceph: root@ceph1 /]# ceph orch daemon add osd ceph3:/dev/vdc [ceph: root@ceph1 /]# ceph orch daemon add osd ceph3:/dev/vdd # 2. 在节点上查询容器信息,将会发现又有新的osd容器出现 [root@ceph1 ~]# podman ps # 3. 此时ceph的状态将会是HEALTH_OK,ceph集群搭建完成。 [ceph: root@ceph1 /]# ceph -s cluster: id: a4b69ab4-79dd-11ed-ae7b-000c2953b002 health: HEALTH_OK services: mon: 3 daemons, quorum ceph1,ceph3,ceph2 (age 2m) mgr: ceph1.gmqorm(active, since 2h), standbys: ceph3.giqaph osd: 9 osds: 9 up (since 35s), 9 in (since 59s) data: pools: 1 pools, 1 pgs objects: 2 objects, 449 KiB usage: 186 MiB used, 180 GiB / 180 GiB avail pgs: 1 active+clean
故障排除:
查看服务状态:
[ceph: root@ceph1 /]# ceph orch ps
如果有error(比如node-exporter.ceph2),则把相应的服务删除:
[ceph: root@ceph1 /]# ceph orch daemon rm node-expoter.ceph2
然后重新配置:
[ceph: root@ceph1 /]# ceph orch daemon reconfig node-exporter.ceph2
# 或
[ceph: root@ceph1 /]# ceph orch daemon redeploy node-exporter.ceph2
如果是mgr这样的服务出故障,删除后,部署的命令是:
[ceph: root@ceph1 /]# ceph orch daemon reconfig mgr ceph2
# 或
[ceph: root@ceph1 /]# ceph orch daemon redeploy mgr ceph2
实现块存储
块存储基础
块设备存取数据时,可以一次存取很多。字符设备只能是字符流
[root@ceph1 ~]# ll /dev/sda
brw-rw---- 1 root disk 8, 0 Dec 12 13:15 /dev/sda
# b表示block,块设备
[root@ceph1 ~]# ll /dev/tty
crw-rw-rw- 1 root tty 5, 0 Dec 12 13:31 /dev/tty
# c表示character,字符设备
块存储,就是可以提供像硬盘一样的设备。使用块存储的节点,第一次连接块设备,需要对块设备进行分区、格式化,然后挂载使用。
ceph中的块设备叫做rbd,是rados block device的简写,表示ceph的块设备。rados是Reliable, Autonomic Distributed Object Store的简写,意思是“可靠、自主的分布式对象存储”。
Ceph块设备采用精简配置,可调整大小,并将数据存储在多个OSD上。
ceph提供存储时,需要使用存储池。为了给客户端提供存储资源,需要创建名为存储池的容器。存储池类似于逻辑卷管理中的卷组。卷组中包含很多硬盘和分区;存储池中包含各节点上的硬盘。
查看基础存储池信息
# 查看存储池。默认有一个名为.mgr的存储池,编号为1
[ceph: root@ceph1 /]# ceph osd lspools
1 .mgr
# 查看存储详细使用情况
[ceph: root@ceph1 /]# ceph df
--- RAW STORAGE ---
CLASS SIZE AVAIL USED RAW USED %RAW USED
hdd 180 GiB 180 GiB 187 MiB 187 MiB 0.10
TOTAL 180 GiB 180 GiB 187 MiB 187 MiB 0.10
--- POOLS ---
POOL ID PGS STORED OBJECTS USED %USED MAX AVAIL
.mgr 1 1 449 KiB 2 449 KiB 0 57 GiB
# 查看.mgr存储池的副本数量
[ceph: root@ceph1 /]# ceph osd pool get .mgr size
size: 3
- 在ceph中,操作块存储的命令也是rbd。
- 创建存储池。如果不指定操作哪一个存储池,rbd命令将会操作名为rbd的存储池。该存储池不存在,需要自己创建。
# 不指定存储池名字执行查看操作。提示名为rbd的存储池不存在 [ceph: root@ceph1 /]# rbd ls rbd: error opening default pool 'rbd' Ensure that the default pool has been created or specify an alternate pool name. rbd: listing images failed: (2) No such file or directory
存储池
- 创建存储池是需要指定存储池中PG的数量。
- Placement Group简称PG,可翻译为归置组。
- PG只是一个组而已,用于把存储的对象分组管理。
- 将数据放入集群时,对象被映射到pg,而这些pg被映射到OSD。这减少了我们需要跟踪的每个对象的元数据的数量以及我们需要运行的进程的数量。
- 创建并使用存储池
-
# 1. 创建名为rbd的存储池 [ceph: root@ceph1 /]# ceph osd pool create rbd 100 pool 'rbd' created # 2. 设置rbd存储池的应用类型是rbd。还可以是rgw或cephfs # 语法:ceph osd pool application enable <pool-name> <app-name> [ceph: root@ceph1 /]# ceph osd pool application enable rbd rbd # 3. 查看 [ceph: root@ceph1 /]# ceph osd pool ls .mgr rbd [ceph: root@ceph1 /]# ceph df --- RAW STORAGE --- CLASS SIZE AVAIL USED RAW USED %RAW USED hdd 180 GiB 180 GiB 191 MiB 191 MiB 0.10 TOTAL 180 GiB 180 GiB 191 MiB 191 MiB 0.10 --- POOLS --- POOL ID PGS STORED OBJECTS USED %USED MAX AVAIL .mgr 1 1 897 KiB 2 2.6 MiB 0 57 GiB rbd 2 99 0 B 0 0 B 0 57 GiB # 4. 执行命令。不指定存储池,默认操作名为rbd的存储池。 [ceph: root@ceph1 /]# rbd ls # 无输出内容,也不会报错
镜像
- 在存储池中划分空间提供给客户端作为硬盘使用。
- 划分出来的空间,术语叫做镜像。
# 1. 查看rbd存储池中有哪些镜像
[ceph: root@ceph1 /]# rbd ls
# 2. 创建名为img1的镜像,大小10GB
[ceph: root@ceph1 /]# rbd create img1 --size 10G
# 3. 查看存储池中有哪些镜像
[ceph: root@ceph1 /]# rbd ls
img1
# 4. 查看镜像详情
[ceph: root@ceph1 /]# rbd info img1
rbd image 'img1':
size 10 GiB in 2560 objects
...略...
# 5. 扩容。容量只是承诺大小,并不会立即分配全部空间,所以值可以超过总容量。
[ceph: root@ceph1 /]# rbd resize img1 --size 200G
Resizing image: 100% complete...done.
[ceph: root@ceph1 /]# rbd info img1
rbd image 'img1':
size 200 GiB in 51200 objects
...略...
# 6. 删除镜像
[ceph: root@ceph1 /]# rbd rm img1
Removing image: 100% complete...done.
ceph客户端
-
客户端使用ceph块存储需要解决的问题:
- 怎么用?装软件
- ceph集群在哪?通过配置文件说明集群地址
- 权限。keyring文件
# 1. 拷贝/linux-soft/s2/zzg/ceph_soft/cephclient-rpm/目录内所有rpm包到pubserver的/var/ftp/rpms目录
# 2. 更新yum仓库
[root@pubserver ~]# createrepo /var/ftp/rpms/
# 3. 安装ceph客户端软件
[root@client1 ~]# yum install -y ceph-common
# 4. 将ceph1上的配置文件和密钥keyring文件拷贝给客户端
[root@ceph1 ceph_soft]# scp /etc/ceph/ceph.client.admin.keyring /etc/ceph/ceph.conf 192.168.88.10:/etc/ceph/
# 5. 在客户端验证是否可以操作ceph
[root@client1 ~]# rbd create img1 --size 10G
[root@client1 ~]# rbd ls
img1
[root@client1 ~]# rbd info img1
rbd image 'img1':
size 10 GiB in 2560 objects
...略...
# 6. 将ceph镜像映射为本地硬盘
[root@client1 ~]# rbd map img1
/dev/rbd0 # rbd为固定名称,0是编号
# 7. 查看
[root@client1 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
└─sda1 8:1 0 60G 0 part /
sr0 11:0 1 10.5G 0 rom
rbd0 253:0 0 10G 0 disk # rbd0来自于ceph镜像
[root@client1 ~]# rbd showmapped # 镜像img1映射为了本地硬盘rbd0
id pool namespace image snap device
0 rbd img1 - /dev/rbd0
# 8. 应用
[root@client1 ~]# mkdir /data
[root@client1 ~]# mkfs.xfs /dev/rbd0
[root@client1 ~]# mount /dev/rbd0 /data/
[root@client1 ~]# df -h /data/
Filesystem Size Used Avail Use% Mounted on
/dev/rbd0 10G 105M 9.9G 2% /data
[root@client1 ~]# cp /etc/hosts /data/
[root@client1 ~]# ls /data/
hosts
删除
# 查看img1的状态
[root@client1 ~]# rbd status img1
# 按以下步骤删除img1
[root@client1 ~]# umount /dev/rbd0
[root@client1 ~]# rbd unmap img1
[root@client1 ~]# rbd rm img1
Removing image: 100% complete...done.
快照
- 快照可以保存某一时间点时的状态数据
- 快照是映像在特定时间点的只读逻辑副本
- 希望回到以前的一个状态,可以恢复快照
- 使用镜像、快照综合示例
# 1. 在rbd存储池中创建10GB的镜像,名为img1
[root@client1 ~]# rbd --help # 查看子命令
[root@client1 ~]# rbd help create # 查看子命令create的帮助
[root@client1 ~]# rbd create img1 --size 10G
[root@client1 ~]# rbd list
img1
[root@client1 ~]# rbd info img1
rbd image 'img1':
size 10 GiB in 2560 objects
order 22 (4 MiB objects)
snapshot_count: 0
id: fa91208bfdaf
block_name_prefix: rbd_data.fa91208bfdaf
format: 2
features: layering, exclusive-lock, object-map, fast-diff, deep-flatten
op_features:
flags:
create_timestamp: Sat Dec 17 10:44:17 2022
access_timestamp: Sat Dec 17 10:44:17 2022
modify_timestamp: Sat Dec 17 10:44:17 2022
# 2. 在客户端使用镜像img1,将其挂载到/mnt
[root@client1 ~]# rbd list
img1
[root@client1 ~]# rbd map img1
/dev/rbd0
[root@client1 ~]# mkfs.xfs /dev/rbd0
[root@client1 ~]# mount /dev/rbd0 /mnt/
[root@client1 ~]# rbd showmapped
id pool namespace image snap device
0 rbd img1 - /dev/rbd0
[root@client1 ~]# df -h /mnt/
Filesystem Size Used Avail Use% Mounted on
/dev/rbd0 10G 105M 9.9G 2% /mnt
# 3. 向/mnt中写入数据
[root@client1 ~]# cp /etc/hosts /mnt/
[root@client1 ~]# cp /etc/passwd /mnt/
[root@client1 ~]# ls /mnt/
hosts passwd
# 4. 创建img1的快照,名为img1-sn1
[root@client1 ~]# rbd snap create img1 --snap img1-sn1
Creating snap: 100% complete...done.
[root@client1 ~]# rbd snap ls img1
SNAPID NAME SIZE PROTECTED TIMESTAMP
4 img1-sn1 10 GiB Sat Dec 17 10:46:07 2022
# 5. 删除/mnt/中的数据
[root@client1 ~]# rm -f /mnt/*
# 6. 通过快照还原数据
[root@client1 ~]# umount /mnt/
[root@client1 ~]# rbd unmap /dev/rbd0
[root@client1 ~]# rbd help snap rollback # 查看子命令帮助
# 回滚img1到快照img1-sn1
[root@client1 ~]# rbd snap rollback img1 --snap img1-sn1
# 重新挂载
[root@client1 ~]# rbd map img1
/dev/rbd0
[root@client1 ~]# mount /dev/rbd0 /mnt/
[root@client1 ~]# ls /mnt/ # 数据还原完成
hosts passwd
- 保护快照,防止删除
[root@client1 ~]# rbd help snap protect
# 保护镜像img1的快照img1-sn1
[root@client1 ~]# rbd snap protect img1 --snap img1-sn1
[root@client1 ~]# rbd snap rm img1 --snap img1-sn1 # 不能删
- 删除操作
# 1. 取消对快照的保护
[root@client1 ~]# rbd snap unprotect img1 --snap img1-sn1
# 2. 删除快照
[root@client1 ~]# rbd snap rm img1 --snap img1-sn1
# 3. 卸载块设备
[root@client1 ~]# umount /dev/rbd0
# 4. 取消映射
[root@client1 ~]# rbd unmap img1
# 5. 删除镜像
[root@client1 ~]# rbd rm img1
快照克隆
- 不能将一个镜像同时挂载到多个节点,如果这样操作,将会损坏数据
- 如果希望不同的节点,拥有完全相同的数据盘,可以使用克隆技术
- 克隆是基于快照的,不能直接对镜像克隆
- 快照必须是受保护的快照,才能克隆
- 克隆流程
- 给多个客户端生成数据相同的数据盘
# 1. 创建名为img2的镜像,大小10GB
[root@client1 ~]# rbd create img2 --size 10G
# 2. 向镜像中写入数据
[root@client1 ~]# rbd map img2
/dev/rbd0
[root@client1 ~]# mkfs.xfs /dev/rbd0
[root@client1 ~]# mount /dev/rbd0 /mnt/
[root@client1 ~]# for i in {1..20}
> do
> echo "Hello World $i" > /mnt/file$i.txt
> done
[root@client1 ~]# ls /mnt/
file10.txt file15.txt file1.txt file5.txt
file11.txt file16.txt file20.txt file6.txt
file12.txt file17.txt file2.txt file7.txt
file13.txt file18.txt file3.txt file8.txt
file14.txt file19.txt file4.txt file9.txt
# 3. 卸载镜像
[root@client1 ~]# umount /mnt/
[root@client1 ~]# rbd unmap img2
# 4. 为img2创建名为img2-sn1快照
[root@client1 ~]# rbd snap create img2 --snap img2-sn1
# 5. 保护img2-sn1快照
[root@client1 ~]# rbd snap protect img2 --snap img2-sn1
# 6. 通过受保护的快照img2-sn1创建克隆镜像
[root@client1 ~]# rbd clone img2 --snap img2-sn1 img2-sn1-1
[root@client1 ~]# rbd clone img2 --snap img2-sn1 img2-sn1-2
# 7. 查看创建出来的、克隆的镜像
[root@client1 ~]# rbd ls
img2
img2-sn1-1
img2-sn1-2
# 8. 不同的客户端挂载不同的克隆镜像,看到的是相同的数据
[root@client1 ~]# rbd map img2-sn1-1
/dev/rbd0
[root@client1 ~]# mkdir /data
[root@client1 ~]# mount /dev/rbd0 /data
[root@client1 ~]# ls /data
file10.txt file15.txt file1.txt file5.txt
file11.txt file16.txt file20.txt file6.txt
file12.txt file17.txt file2.txt file7.txt
file13.txt file18.txt file3.txt file8.txt
file14.txt file19.txt file4.txt file9.txt
[root@ceph1 ~]# yum install -y ceph-common
[root@ceph1 ~]# rbd map img2-sn1-2
/dev/rbd0
[root@ceph1 ~]# mkdir /data
[root@ceph1 ~]# mount /dev/rbd0 /data/
[root@ceph1 ~]# ls /data/
file10.txt file15.txt file1.txt file5.txt
file11.txt file16.txt file20.txt file6.txt
file12.txt file17.txt file2.txt file7.txt
file13.txt file18.txt file3.txt file8.txt
file14.txt file19.txt file4.txt file9.txt
- 查询镜像和快照
# 查看快照信息
[root@client1 ~]# rbd info img2 --snap img2-sn1
rbd image 'img2':
size 10 GiB in 2560 objects
order 22 (4 MiB objects)
snapshot_count: 1
id: d46eed84bb61
block_name_prefix: rbd_data.d46eed84bb61
format: 2
features: layering, exclusive-lock, object-map, fast-diff, deep-flatten
op_features:
flags:
create_timestamp: Sat Dec 17 10:58:05 2022
access_timestamp: Sat Dec 17 10:58:05 2022
modify_timestamp: Sat Dec 17 10:58:05 2022
protected: True # 受保护
# 查看克隆的快照
[root@client1 ~]# rbd info img2-sn1-2
rbd image 'img2-sn1-2':
size 10 GiB in 2560 objects
order 22 (4 MiB objects)
snapshot_count: 0
id: d48fe3d6559e
block_name_prefix: rbd_data.d48fe3d6559e
format: 2
features: layering, exclusive-lock, object-map, fast-diff, deep-flatten
op_features:
flags:
create_timestamp: Sat Dec 17 10:59:53 2022
access_timestamp: Sat Dec 17 10:59:53 2022
modify_timestamp: Sat Dec 17 10:59:53 2022
parent: rbd/img2@img2-sn1 # 父对象是rbd池中img2镜像的img2-sn1快照
overlap: 10 GiB
-
合并父子镜像
- img2-sn1-2是基于img2的快照克隆来的,不能独立使用。
- 如果父镜像删除了,子镜像也无法使用。
- 将父镜像内容合并到子镜像中,子镜像就可以独立使用了。
# 把img2的数据合并到子镜像img2-sn1-2中
[root@client1 ~]# rbd flatten img2-sn1-2
# 查看状态,它就没有父镜像了
[root@client1 ~]# rbd info img2-sn1-2
rbd image 'img2-sn1-2':
size 10 GiB in 2560 objects
order 22 (4 MiB objects)
snapshot_count: 0
id: d48fe3d6559e
block_name_prefix: rbd_data.d48fe3d6559e
format: 2
features: layering, exclusive-lock, object-map, fast-diff, deep-flatten
op_features:
flags:
create_timestamp: Sat Dec 17 10:59:53 2022
access_timestamp: Sat Dec 17 10:59:53 2022
modify_timestamp: Sat Dec 17 10:59:53 2022
# 删除父镜像,如果镜像正在被使用,则先取消
[root@client1 ~]# umount /data/
[root@client1 ~]# rbd unmap img2-sn1-1
# 1. 删除镜像img2-sn1-1
[root@client1 ~]# rbd rm img2-sn1-1
# 2. 取消img2-sn1的保护
[root@client1 ~]# rbd snap unprotect img2 --snap img2-sn1
# 3. 删除img2-sn1快照
[root@client1 ~]# rbd snap rm img2 --snap img2-sn1
# 4. 删除img2
[root@client1 ~]# rbd rm img2
# 因为img2-sn1-2已经是独立的镜像了,所以它还可以使用
# ceph1上的镜像没有受到影响
[root@ceph1 ~]# cat /data/file1.txt
Hello World 1
开机自动挂载
# 1. 准备镜像
[root@client1 ~]# rbd create img1 --size 10G
[root@client1 ~]# rbd map img1
/dev/rbd0
[root@client1 ~]# mkfs.xfs /dev/rbd0
# 2. 设置开机自动挂载
[root@client1 ~]# vim /etc/ceph/rbdmap # 指定要挂载的镜像及用户名、密钥
rbd/img1 id=admin,keyring=/etc/ceph/ceph.client.admin.keyring
[root@client1 ~]# vim /etc/fstab # 追加
/dev/rbd/rbd/img1 /data xfs noauto 0 0
# noauto的意思是,等rbdmap服务启动后,再执行挂载
# 3. 启动rbdmap服务
[root@client1 ~]# systemctl enable rbdmap --now
# 4. reboot后查看结果
[root@client1 ~]# df -h /data/
Filesystem Size Used Avail Use% Mounted on
/dev/rbd0 10G 105M 9.9G 2% /data
ceph文件系统
- 文件系统:相当于是组织数据存储的方式。
- 格式化时,就是在为存储创建文件系统。
- Linux对ceph有很好的支持,可以把ceph文件系统直接挂载到本地。
- 要想实现文件系统的数据存储方式,需要有MDS组件
使用MDS
-
元数据就是描述数据的属性。如属主、属组、权限等。
-
ceph文件系统中,数据和元数据是分开存储的
-
新建存储池
- 归置组PG:存储池包含PG。PG是一个容器,用于存储数据。
- 为了管理方便,将数量众多的数据放到不同的PG中管理,而不是直接把所有的数据扁平化存放。
- 通常一个存储池中创建100个PG。
-
创建ceph文件系统
# 1. 新建一个名为data1的存储池,目的是存储数据,有100个PG [root@client1 ~]# ceph osd pool create data01 100 # 2. 新建一个名为metadata1的存储池,目的是存储元数据 [root@client1 ~]# ceph osd pool create metadata01 100 # 3. 创建名为myfs1的cephfs,数据保存到data1中,元数据保存到metadata1中 [root@client1 ~]# ceph fs new myfs01 metadata01 data01 # 4. 查看存储池 [root@client1 ~]# ceph osd lspools 1 .mgr 2 rbd 3 data01 4 metadata01 [root@client1 ~]# ceph df --- RAW STORAGE --- CLASS SIZE AVAIL USED RAW USED %RAW USED hdd 180 GiB 180 GiB 206 MiB 206 MiB 0.11 TOTAL 180 GiB 180 GiB 206 MiB 206 MiB 0.11 --- POOLS --- POOL ID PGS STORED OBJECTS USED %USED MAX AVAIL .mgr 1 1 449 KiB 2 1.3 MiB 0 57 GiB rbd 2 32 7.1 MiB 43 22 MiB 0.01 57 GiB data01 3 94 0 B 0 0 B 0 57 GiB metadata01 4 94 0 B 0 0 B 0 57 GiB # 5. 查看文件系统 [root@client1 ~]# ceph fs ls name: myfs01, metadata pool: metadata01, data pools: [data01 ] # 6. 启动MDS服务 [root@client1 ~]# ceph orch apply mds myfs01 --placement="2 ceph1 ceph2" # 7. 查看部署结果 [root@client1 ~]# ceph -s cluster: id: a4b69ab4-79dd-11ed-ae7b-000c2953b002 health: HEALTH_OK services: mon: 3 daemons, quorum ceph1,ceph3,ceph2 (age 92m) mgr: ceph1.gmqorm(active, since 92m), standbys: ceph3.giqaph mds: 1/1 daemons up, 1 standby # mds服务信息 osd: 9 osds: 9 up (since 92m), 9 in (since 4d) ...略...
- 客户端使用cephfs
# 挂载文件系统需要密码。查看密码 [root@client1 ~]# cat /etc/ceph/ceph.client.admin.keyring [client.admin] key = AQBmhINh1IZjHBAAvgk8m/FhyLiH4DCCrnrdPQ== # -t 指定文件系统类型。-o是选项,提供用户名和密码 [root@client1 ~]# mkdir /mydata [root@client1 ~]# mount.ceph 192.168.88.13:/ /mydata -o name=admin,secret=AQC5u5ZjnTA1ERAAruLAI8F1W1nyOgxZSx0UXw== [root@client1 ~]# df -h /mydata/ Filesystem Size Used Avail Use% Mounted on 192.168.88.13:/ 57G 0 57G 0% /mydata
对象存储
配置服务器端
- 需要专门的客户端访问
- 键值对存储方式
- 对象存储需要rgw组件
- 安装部署
# 1. 在ceph1/ceph2上部署rgw服务,名为myrgw [root@client1 ~]# ceph orch apply rgw myrgw --placement="2 ceph1 ceph2" --port 8080 [root@client1 ~]# ceph -s cluster: id: a4b69ab4-79dd-11ed-ae7b-000c2953b002 health: HEALTH_OK services: mon: 3 daemons, quorum ceph1,ceph3,ceph2 (age 101m) mgr: ceph1.gmqorm(active, since 6h), standbys: ceph3.giqaph mds: 1/1 daemons up, 1 standby osd: 9 osds: 9 up (since 6h), 9 in (since 5d); 1 remapped pgs rgw: 2 daemons active (2 hosts, 1 zones) # rgw信息 ...略...
配置客户端
- ceph对象存储提供了一个与亚马逊S3(Amazon Simple Storage Service)兼容的接口
- 在S3中,对象被存储在一个称作桶(bucket)的器皿中。这就好像是本地文件存储在目录中一样。
# 1. 安装amazon S3 cli工具(客户端工具) [root@client1 ~]# yum install -y awscli # 2. 在ceph中创建一个用户 [root@client1 ~]# radosgw-admin user create --uid=testuser --display-name="Test User" --email=test@tedu.cn --access-key=12345 --secret=67890 # 3. 初始化客户端 [root@client1 ~]# aws configure --profile=ceph AWS Access Key ID [None]: 12345 AWS Secret Access Key [None]: 67890 Default region name [None]: # 回车 Default output format [None]: # 回车 # 4. 创建名为testbucket的bucket,用于存储数据 [root@client1 ~]# vim /etc/hosts # 添加以下内容 192.168.88.11 ceph1 192.168.88.12 ceph2 192.168.88.13 ceph3 [root@client1 ~]# aws --profile=ceph --endpoint=http://ceph1:8080 s3 mb s3://testbucket # 5. 上传文件 [root@client1 ~]# aws --profile=ceph --endpoint=http://ceph1:8080 --acl=public-read-write s3 cp /etc/hosts s3://testbucket/hosts.txt # 6. 查看bucket中的数据 [root@client1 ~]# aws --profile=ceph --endpoint=http://ceph1:8080 s3 ls s3://testbucket 2022-12-17 17:05:58 241 hosts.txt # 7. 下载数据 [root@client1 ~]# wget -O zhuji http://ceph1:8080/testbucket/hosts.txt
访问Dashborad
- 通过浏览器访问
https://192.168.88.11:8443
,用户名为admin,密码是安装时指定的123456。