您好,登錄后才能下訂單哦!
OpenStack Object Storage swift操作和實踐
之前已經介紹了OpenStack Object Storage (swift)的相關概念,接下來介紹一些swift的操作。
【環境搭建】
為了方便在初期學習swift功能的時候我們可以不考慮身份驗證的部分,所以我們要做一點處理:
在安裝之前我們可以修改源碼中一個配置文件proxy-server.conf,如圖:
將tempauth去掉
同時屏蔽掉下面兩行
因為在swift中功能都是模塊化的,你可以根據需求來開啟或者關閉。
修改之后我們可以安照上篇博客中介紹的快速部署Swift的SAIO環境 一文中的方法執行腳本setup_saio.sh安裝了。
這里我們簡單注釋一下setup_saio.sh腳本(是根據swift官網文檔編寫的)
#!/bin/sh
#install indepency
sudo apt-get install -y python-software-properties
# If you want install swift use deb package,enable it
#sudo add-apt-repository ppa:swift-core/release
sudo apt-get update
sudo apt-get install -y curl
sudo apt-get install -y gcc
sudo apt-get install -y git-core
sudo apt-get install -y memcached
sudo apt-get install -y python-configobj
sudo apt-get install -y python-coverage
sudo apt-get install -y python-dev
sudo apt-get install -y python-nose
sudo apt-get install -y python-setuptools
sudo apt-get install -y python-simplejson
sudo apt-get install -y python-xattr
sudo apt-get install -y sqlite3
sudo apt-get install -y xfsprogs
sudo apt-get install -y python-webob
sudo apt-get install -y python-eventlet
sudo apt-get install -y python-greenlet
sudo apt-get install -y python-pastedeploy
sudo apt-get install -y python-netifaces
sudo apt-get install -y memcached
#Add swift user
sudo useradd -d /home/swift -s /bin/bash -U swift
sudo password swift
#setup xfs for disk
sudo apt-get -y install vsftpd
#創建一個回環設備來做存儲點
sudo dd if=/dev/zero of=:w
sudo dd if=/dev/zero of=/srv/swift-disk bs=1024 count=0 seek=1000000
sudo mkfs.xfs -i size=1024 /srv/swift-disk
sudo echo "/srv/swift-disk /mnt/sdb1 xfs loop,noatime,nodiratime,nobarrier,logbufs=8 0 0" >> /etc/fstab
sudo mkdir /mnt/sdb1
sudo mount /mnt/sdb1
sudo mkdir /mnt/sdb1/1 /mnt/sdb1/2 /mnt/sdb1/3 /mnt/sdb1/4 /mnt/sdb1/test
sudo chown -R swift:swift /mnt/sdb1/*
if ! [ -e /srv ]
then
sudo mkdir /srv
fi
for x in 1 2 3 4
do
sudo ln -s /mnt/sdb1/$x /srv/$X
done
sudo mkdir -p /etc/swift/object-server /etc/swift/container-server /etc/swift/account-server
for x in 1 2 3 4
do
sudo mkdir -p /srv/$x/node/sdb$x
done
sudo mkdir -p /var/run/swift
sudo chown -R swift:swift /etc/swift /srv/1/ /srv/2/ /srv/3/ /srv/4/ /var/run/swift
sudo echo -e "mkdir /var/run/swift \\n chown swift:swift /var/run/swift" >> /etc/rc.local
if [ -e /home/swift/.bashrc ]
then
sudo echo -e "export SWIFT_TEST_CONFIG_FILE=/etc/swift/test.conf \\n export PATH=${PATH}:~/bin" >> /home/swift/.bashrc
sudo source ~/.bashrc
fi
#configure swift and related conf
if ! [ -e /etc/swift ]
then
sudo mkdir /etc/swift
fi
chown swift:swift /etc/swift
# copy conf files
cp --recursive conf/* /etc/swift/
cp rsyncd.conf /etc
sudo service rsync restart
#some system need easy_install and pip
apt-get -y install python-setuptools
easy_install pip
#Install swift
git clone git://github.com/openstack/swift.git
cd swift ; pip install -r tools/pip-requires
sudo python setup.py install; cd ..
#Install swiftclient
git clone https://github.com/openstack/python-swiftclient.git
cd python-swiftclient; pip install -r tools/pip-requires
sudo python setup.py install;cd ..
#make ring and start server
cd bin;
#創建ring
sh remakerings
#啟動服務
swift-init main start
此時我們搭建的環境是一個默認的三副本配置。
【基本操作】
1.生成builder文件和builder相關配置
你可以在終端中直接輸入swift-ring-builder,查看swift-ring-builder命令相關的解釋:
swift-ring-builder <builder_file>
Shows information about the ring and the devices within.
顯示ring以及ring中設備的信息,swift-1.8.0中對device新增了一個region屬性swift-ring-builder <builder_file> add
z<zone>-<ip>:<port>/<device_name>_<meta> <weight>
[z<zone>-<ip>:<port>/<device_name>_<meta> <weight>] ...
Adds devices to the ring with the given information. No partitions will be
assigned to the new device until after running 'rebalance'. This is so you
can make multiple device changes and rebalance them all just once.
使用給出的信息添加新的設備到ring上。add操作不會分配partitions到新的設備上,只有運行了'rebalance'命令后才會進行分區的分配。
因此,這種機制可以允許你一次添加多個設備,并只執行一次rebalance實現對這些設備的分區分配。
swift-ring-builder <builder_file> create <part_power> <replicas> <min_part_hours>
Creates <builder_file> with 2^<part_power> partitions and <replicas>.
<min_part_hours> is number of hours to restrict moving a partition more
than once.
使用2的<part_power>次方個分區和<replicas>副本數創建<builder_file>.<min_part_hour>是一個分區被連續移動兩次之間的最小時間間隔swift-ring-builder <builder_file> list_parts <search-value> [<search-value>] ..
Returns a 2 column list of all the partitions that are assigned to any of
the devices matching the search values given. The first column is the
assigned partition number and the second column is the number of device
matches for that partition. The list is ordered from most number of matches
to least. If there are a lot of devices to match against, this command
could take a while to run.
返回一個兩列的列表,包含與搜索值相匹配的所有設備的所有分區。
第一列是關聯的分區編號
第二列是與分區匹配的設備編號
列表按匹配的編號大小從大到小排序,如果有很多設備與搜索符合,則這個命令需要多運行一會兒
swift-ring-builder <builder_file> rebalance
Attempts to rebalance the ring by reassigning partitions that haven't been
recently reassigned.
rebalance命令嘗試重新平衡環,通過重新分配分區最近沒有被重新分配的分區。
swift-ring-builder <builder_file> remove <search-value> [search-value ...]
Removes the device(s) from the ring. This should normally just be used for
a device that has failed. For a device you wish to decommission, it's best
to set its weight to 0, wait for it to drain all its data, then use this
remove command. This will not take effect until after running 'rebalance'.
This is so you can make multiple device changes and rebalance them all just
once.
remove命令將設備從ring中移除。一般情況下,這個命令應該僅用在那些失敗的設備上。
如果你想將一個設備退役掉,那么最好的方式是將它的weight設置為0,待它將其上所有的數據都移走之后,再使用這個命令移除設備。
remove操作不會重新分配partitions,只有運行了'rebalance'命令后才會進行分區的分配。因此,這種機制可以允許你一次添加刪除個設備,并只執行一次rebalance實現對這些設備的分區分配。
swift-ring-builder <builder_file> search <search-value>
Shows information about matching devices.
顯示匹配的設備的信息swift-ring-builder <builder_file> set_info <search-value> <ip>:<port>/<device_name>_<meta>
[<search-value> <ip>:<port>/<device_name>_<meta>] ...
For each search-value, resets the matched device's information.
This information isn't used to assign partitions, so you can use
'write_ring' afterward to rewrite the current ring with the newer
device information. Any of the parts are optional in the final
<ip>:<port>/<device_name>_<meta> parameter; just give what you
want to change. For instance set_info d74 _"snet: 5.6.7.8" would
just update the meta data for device id 74.
set_info命令會重新設置每一個與<search-value>相匹配的設備信息。這個信息不會用來重新分配分區,因此你可以使用'write_ring'來直接重寫當前的ring。
<ip>:<port>/<device_name>_<meta>參數的任意一個部分都是可選的,你只需要給出你需要更改的部分。
比如,set_info d74 _"snet: 5.6.7.8"就僅僅會把id為74的設備的元數據更新為"snet: 5.6.7.8"
swift-ring-builder <builder_file> set_min_part_hours <hours>
Changes the <min_part_hours> to the given <hours>. This should be set to
however long a full replication/update cycle takes. We're working on a way
to determine this more easily than scanning logs.
set_min_part_hours命令將<min_part_hours>設置為參數給定的<hours>.
這個時間應該被設置的至少滿足一個完整的replication/update周期。我們正在努力找到一個方法可以比看日志更簡單的決定這個時間
swift-ring-builder <builder_file> set_weight <search-value> <weight>
[<search-value> <weight] ...
Resets the devices' weights. No partitions will be reassigned to or from
the device until after running 'rebalance'. This is so you can make
multiple device changes and rebalance them all just once.
重新設置設備的weight。set_weight操作后,設備上的partition不會重新分配,只有運行了'rebalance'命令后才會進行分區的分配。
因此,這種機制可以允許你一次添加多個設備,并只執行一次rebalance實現對這些設備的分區分配。
swift-ring-builder <builder_file> set_replicas <replicas>
Changes the replica count to the given <replicas>. <replicas> may
be a floating-point value, in which case some partitions will have
floor(<replicas>) replicas and some will have ceiling(<replicas>)
in the correct proportions.A rebalance is needed to make the change take effect.
set_replicas命令用于使用參數中的<replicas>來設置副本數。
<replicas>可以是一個浮點數,因此在一些場景中一些分區的副本數可能是floor(<replicas>),也可能是(<replicas>),這取決于正確的比例。
需要執行一個rebalance命令來使副本設置生效。該命令是swift-1.8.0新增的。
swift-ring-builder <builder_file> validate
Just runs the validation routines on the ring. 僅運行builder的validate方法,使ring生效
swift-ring-builder <builder_file> write_ring
Just rewrites the distributable ring file. This is done automatically after
a successful rebalance, so really this is only useful after one or more
'set_info' calls when no rebalance is needed but you want to send out the
new device information.
write_ring命令僅是用來重寫分部環境下的ring文件。這個命令會在成功執行一個rebalance操作后唄自動執行。
因此,它僅在你執行了一次或多次'set_info'命令,不想rebalance卻想保留新信息時使用。
2.使用curl工具訪問swift
創建賬戶:
#curl -X PUT -I http://localhost:8080/v1.0/myaccount01
返回201 Created,表示正常。
創建容器
#curl -X PUT -I http://localhost:8080/v1.0/myaccount01/mycontainer01/
返回 201 Created,表示正常。
上傳文件
#curl -X PUT -I -T yourfile http://localhost:8080/v1.0/myaccount01/mycontainer01/
返回 201 Created,表示正常。
查看容器中的文件:
#curl -D- http://localhost:8080/v1.0/myaccount01/mycontainer01
或
#curl -s http://localhost:8080/v1.0/myaccount01/mycontainer01/ -X GET
刪除容器中的對象:
curl -s http://localhost:8080/v1.0/myaccount01/mycontainer01 -X DELETE
查看自己的文件存放的物理位置:
#swift-get-nodes /etc/swift/object.ring.gz myaccount01 mycontainer01 yourfile
根據返回結果,可以自己去驗證文件的最終物理存儲位置。
獲取帳戶的所有容器
curl -D- http://localhost:8080/v1.0/myaccount01
------------------------------------------全文完------------------------------------------
PS:
關注個人 微博:愛數三國奶糕
微信公眾賬號:比特科技
二維碼:
可以及時獲得趣味文章、熱門技術、行業熱點等相關文章的推送。
關注比特科技,構建IT生態圈。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。