swift概述

Swift 最初是由Rackspace公司开发的高可用分布式对象存储服务,并于2010年贡献给OpenStack开源社区作为其最初的核心子项目之一,为其Nova子项目提供虚机镜像存储服务。Swift构筑在比较便宜的标准硬件存储基础设施之上,无需采用 RAID(磁盘冗余阵列),通过在软件层面引入一致性散列技术和数据冗余性,牺牲一定程度的数据一致性来达到高可用性和可伸缩性,支持多租户模式、容器和对象读写操作,适合解决互联网的应用场景下非结构化数据存储问题。Swift在OpenStack系统中不依赖于任何服务,可以独立部署为其他系统提供分布式对象存储服务。而在OpenStack的应用中,其Proxy Server往往由Keystone节点兼任,由Keystone来完成服务访问的安全认证。

Swift是业务提供时,使用普通的服务器来构建冗余的、可扩展的分布式对象存储集群,存储容量可达PB级。通过统一REST API进行友好访问,不仅易于扩展,且无中心数据库,避免单点故障或单点性能瓶颈。Swift主要通过Account、Container和Object三个表单结构来完成存储对象的存储、查询、获取和上传等功能,通过数据存储的多副本机制实现数据的高可用。

[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

创建容器

[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> ...]
[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                                     |
+--------------+---------------------------------------+

创建对象

[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> ...]
[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
[root@controller ~]# openstack object create swift-test test/object-test
+------------------+------------+----------------------------------+
| object           | container  | etag                             |
+------------------+------------+----------------------------------+
| test/object-test | swift-test | d41d8cd98f00b204e9800998ecf8427e |
+------------------+------------+----------------------------------+

查看对象

[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>
[root@controller ~]# openstack object list swift-test
+------------------+
| Name             |
+------------------+
| 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                      |
+----------------+---------------------------------------+

下载对象

[root@controller ~]# openstack help object save
usage: openstack object save [-h] [--file <filename>] <container> <object>

Save object locally
[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

删除对象

[root@controller opt]# openstack help object delete
usage: openstack object delete [-h] <container> <object> [<object> ...]
[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]#

删除容器

[root@controller opt]# openstack help container delete
usage: openstack container delete [-h] [--recursive]
                                  <container> [<container> ...]
[root@controller opt]# openstack container delete swift-test
Conflict (HTTP 409) (Request-ID: tx8c3de39f832f49ac9d655-00601bba0a)
[root@controller opt]# openstack container delete --recursive swift-test
[root@controller opt]# openstack container list

分片存储案例

[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
[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 ~]#
[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个存储片。

05-06 14:22