Swift服務的基本使用

huhy發表於2022-05-06

swift概述

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個儲存片。

相關文章