Swift 最初是由Rackspace公司开发的高可用分布式对象存储服务,并于2010年贡献给OpenStack开源社区作为其最初的核心子项目之一,为其Nova子项目提供虚机镜像存储服务。Swift构筑在比较便宜的标准硬件存储基础设施之上,无需采用 RAID(磁盘冗余阵列),通过在软件层面引入一致性散列技术和数据冗余性,牺牲一定程度的数据一致性来达到高可用性和可伸缩性,支持多租户模式、容器和对象读写操作,适合解决互联网的应用场景下非结构化数据存储问题。Swift在OpenStack系统中不依赖于任何服务,可以独立部署为其他系统提供分布式对象存储服务。而在OpenStack的应用中,其Proxy Server往往由Keystone节点兼任,由Keystone来完成服务访问的安全认证。
Swift是业务提供时,使用普通的服务器来构建冗余的、可扩展的分布式对象存储集群,存储容量可达PB级。通过统一REST API进行友好访问,不仅易于扩展,且无中心数据库,避免单点故障或单点性能瓶颈。Swift主要通过Account、Container和Object三个表单结构来完成存储对象的存储、查询、获取和上传等功能,通过数据存储的多副本机制实现数据的高可用。
在openstack平台中使用命令“swift stat”查看对象存储服务状态
[root@controller ~]# swift stat
Account: AUTH_bf36698d9afd45c4b46c1933469a9101
Containers: 0
Objects: 0
Bytes: 0
X-Put-Timestamp: 1651842033.53425
X-Timestamp: 1651842033.53425
X-Trans-Id: tx82d7ac28d8ea43fb92949-0062751bf0
Content-Type: text/plain; charset=utf-8
X-Openstack-Request-Id: tx82d7ac28d8ea43fb92949-0062751bf0
创建容器
通过“openstack container create”命令创建容器,命令格式如下所示
[root@controller ~]# openstack help container create
usage: openstack container create [-h] [-f {csv,json,table,value,yaml}]
[-c COLUMN] [--max-width <integer>]
[--fit-width] [--print-empty] [--noindent]
[--quote {all,minimal,none,nonnumeric}]
[--sort-column SORT_COLUMN]
<container-name> [<container-name> ...]
使用命令创建容器,名称为“swift-test"
[root@controller ~]# openstack container create swift-test
+---------------------------------------+------------+------------------------------------+
| account | container | x-trans-id |
+---------------------------------------+------------+------------------------------------+
| AUTH_bf36698d9afd45c4b46c1933469a9101 | swift-test | tx6e69e142b8fa4ce69e3f7-0062751c5c |
+---------------------------------------+------------+------------------------------------+
查看容器
使用命令查询容器列表信息
[root@controller ~]# openstack container list
+------------+
| Name |
+------------+
| swift-test |
+------------+
[root@controller ~]# openstack container show swift-test
+--------------+---------------------------------------+
| Field | Value |
+--------------+---------------------------------------+
| account | AUTH_bf36698d9afd45c4b46c1933469a9101 |
| bytes_used | 0 |
| container | swift-test |
| object_count | 0 |
+--------------+---------------------------------------+
创建对象
创建完容器后,可以创建对象,通过使用命令“openstack object create”
[root@controller ~]# openstack help object create
usage: openstack object create [-h] [-f {csv,json,table,value,yaml}]
[-c COLUMN] [--max-width <integer>]
[--fit-width] [--print-empty] [--noindent]
[--quote {all,minimal,none,nonnumeric}]
[--sort-column SORT_COLUMN] [--name <name>]
<container> <filename> [<filename> ...]
在使用命令创建对象前,需要将上传后的目录结构在本地创建。在本地创建名为“test”的目录“/root/test”,将/root/object-test文件复制至“/root/test”目录中
[root@controller ~]# ls
CentOS-7-x86_64-DVD-1804.iso chinaskills_cloud_iaas2.6.iso object-test test
[root@controller ~]# cd test/
[root@controller test]# ls
object-test
创建对象的过程也是向容器中上传文件,使用命令创建“test/object-test对象
[root@controller ~]# openstack object create swift-test test/object-test
+------------------+------------+----------------------------------+
| object | container | etag |
+------------------+------------+----------------------------------+
| test/object-test | swift-test | d41d8cd98f00b204e9800998ecf8427e |
+------------------+------------+----------------------------------+
查看对象
创建完对象后,通过命令查看容器中对象信息,使用命令“openstack object list”查看对象信息
[root@controller ~]# openstack help object list
usage: openstack object list [-h] [-f {csv,json,table,value,yaml}] [-c COLUMN]
[--max-width <integer>] [--fit-width]
[--print-empty] [--noindent]
[--quote {all,minimal,none,nonnumeric}]
[--sort-column SORT_COLUMN] [--prefix <prefix>]
[--delimiter <delimiter>] [--marker <marker>]
[--end-marker <end-marker>]
[--limit <num-objects>] [--long] [--all]
<container>
使用命令查看容器“swift-test”中所有对象信息
[root@controller ~]# openstack object list swift-test
+------------------+
| Name |
+------------------+
| test/object-test |
+------------------+
通过查询命令可以看出,在通过命令上传对象时,本地路径即为容器内对象路径。使用命令“openstack object show”查询“swift-test”容器中“test/object-test”对象详细信息
[root@controller ~]# openstack object show swift-test test/object-test
+----------------+---------------------------------------+
| Field | Value |
+----------------+---------------------------------------+
| account | AUTH_bf36698d9afd45c4b46c1933469a9101 |
| container | swift-test |
| content-length | 0 |
| content-type | application/octet-stream |
| etag | d41d8cd98f00b204e9800998ecf8427e |
| last-modified | Fri, 06 May 2022 14:03:10 GMT |
| object | test/object-test |
+----------------+---------------------------------------+
下载对象
存储在容器中的对象,可以在需要使用时,通过“openstack object save”命令进行下载至本地
[root@controller ~]# openstack help object save
usage: openstack object save [-h] [--file <filename>] <container> <object>
Save object locally
使用命令将“swift-test”容器中“test/object-test”对象下载至本地/opt/目录下
[root@controller ~]# cd /opt/
[root@controller opt]# openstack object save swift-test test/object-test
[root@controller opt]# ls
centos iaas test
[root@controller opt]# cd test/
[root@controller test]# ls
object-test
删除对象
使用“openstack object delete”命令删除容器内的对象
[root@controller opt]# openstack help object delete
usage: openstack object delete [-h] <container> <object> [<object> ...]
使用删除对象命令将“swift-test”容器内“test/object-test”删除,查看“swift-test”容器中对象列表信息
[root@controller test]# openstack object list swift-test
+------------------+
| Name |
+------------------+
| test/object-test |
+------------------+
[root@controller test]# openstack object delete swift-test test/object-test
[root@controller test]# openstack object list swift-test
[root@controller test]#
删除容器
使用“openstack container delete”命令删除容器
[root@controller opt]# openstack help container delete
usage: openstack container delete [-h] [--recursive]
<container> [<container> ...]
使用删除容器命令将“swift-test”容器删除,如果容器内存有对象,无法直接删除
[root@controller opt]# openstack container delete swift-test
Conflict (HTTP 409) (Request-ID: tx8c3de39f832f49ac9d655-00601bba0a)
需要添加“--recursive”参数将容器内部对象一起删除,才可以删除“swift-test”容器,查看容器列表信息
[root@controller opt]# openstack container delete --recursive swift-test
[root@controller opt]# openstack container list
分片存储案例
使用命令创建一个容器test并查看容器的状态信息
[root@controller ~]# swift post test
[root@controller ~]# swift stat test
Account: AUTH_bf36698d9afd45c4b46c1933469a9101
Container: test
Objects: 0
Bytes: 0
Read ACL:
Write ACL:
Sync To:
Sync Key:
Accept-Ranges: bytes
X-Storage-Policy: Policy-0
Last-Modified: Fri, 06 May 2022 14:18:35 GMT
X-Timestamp: 1651846714.52385
X-Trans-Id: txd3afaaa151f24632ba5a5-0062752e43
Content-Type: application/json; charset=utf-8
X-Openstack-Request-Id: txd3afaaa151f24632ba5a5-0062752e43
上传镜像并分片存储
将提供的镜像CentOS-7-x86_64-DVD-1804.iso上传至controller节点的/root目录下,并使用命令上传至test容器中,进行分片存储,每个片段的大小为10M
[root@controller ~]# ls
CentOS-7-x86_64-DVD-1804.iso chinaskills_cloud_iaas2.6.iso object-test test
[root@controller ~]# swift upload test -S 10000000 CentOS-7-x86_64-DVD-1804.iso
CentOS-7-x86_64-DVD-1804.iso segment 3
CentOS-7-x86_64-DVD-1804.iso segment 1
CentOS-7-x86_64-DVD-1804.iso segment 5
CentOS-7-x86_64-DVD-1804.iso segment 6
CentOS-7-x86_64-DVD-1804.iso segment 9
...........
CentOS-7-x86_64-DVD-1804.iso segment 447
CentOS-7-x86_64-DVD-1804.iso segment 443
CentOS-7-x86_64-DVD-1804.iso segment 444
CentOS-7-x86_64-DVD-1804.iso segment 436
CentOS-7-x86_64-DVD-1804.iso segment 446
CentOS-7-x86_64-DVD-1804.iso
[root@controller ~]#
查看cirros镜像的存储路径
[root@controller ~]# swift stat test CentOS-7-x86_64-DVD-1804.iso
Account: AUTH_bf36698d9afd45c4b46c1933469a9101
Container: test
Object: CentOS-7-x86_64-DVD-1804.iso
Content Type: application/octet-stream
Content Length: 4470079488
Last Modified: Fri, 06 May 2022 14:25:57 GMT
ETag: "8c8ee739c7e27a2a1f4d27838e8544ee"
Manifest: test_segments/CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/
Meta Mtime: 1644959184.000000
Accept-Ranges: bytes
X-Timestamp: 1651847156.29073
X-Trans-Id: tx5e336396a0164476ad69c-006275307e
X-Openstack-Request-Id: tx5e336396a0164476ad69c-006275307e
查看存储路径中的数据片
[root@controller ~]# swift list test_segments
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000000
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000001
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000002
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000003
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000004
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000005
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000006
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000007
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000008
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000009
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000010
...........
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000442
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000443
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000444
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000445
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000446
CentOS-7-x86_64-DVD-1804.iso/1644959184.000000/4470079488/10000000/00000447
可以看到,centos镜像在上传至swfit对象存储中被分片存储了,单个存储片的大小为10M,因为该镜像大小为4450M左右,所有分成了447个存储片。